Example #1
0
        private static IMeshBuilder <MaterialBuilder> BuildMapGeometryMeshStatic(MapGeometryModel model)
        {
            var meshBuilder = VERTEX.CreateCompatibleMesh();

            foreach (MapGeometrySubmesh submesh in model.Submeshes)
            {
                List <MapGeometryVertex> vertices = submesh.GetVertices();
                List <ushort>            indices  = submesh.GetIndices();

                MaterialBuilder material  = new MaterialBuilder(submesh.Material).WithUnlitShader();
                var             primitive = meshBuilder.UsePrimitive(material);

                List <VERTEX> gltfVertices = new List <VERTEX>();
                foreach (MapGeometryVertex vertex in vertices)
                {
                    gltfVertices.Add(CreateVertex(vertex));
                }

                for (int i = 0; i < indices.Count; i += 3)
                {
                    VERTEX v1 = gltfVertices[indices[i + 0]];
                    VERTEX v2 = gltfVertices[indices[i + 1]];
                    VERTEX v3 = gltfVertices[indices[i + 2]];

                    primitive.AddTriangle(v1, v2, v3);
                }
            }

            return(meshBuilder);
        }
Example #2
0
        static void Main(string[] args)
        {
            var material = new MaterialBuilder("material1").WithUnlitShader();

            var mesh = VERTEX.CreateCompatibleMesh("points");

            // create a point cloud primitive
            var pointCloud = mesh.UsePrimitive(material, 1);

            var galaxy = new Galaxy();

            galaxy.scaleOverPlane = 0.05f;
            galaxy.randomJitter   = 0.01f;
            foreach (var startPoint in galaxy.CreateStarts(50000))
            {
                pointCloud.AddPoint((startPoint, Vector4.One));
            }

            galaxy.scaleOverPlane = 0.15f;
            galaxy.randomJitter   = 0.02f;
            foreach (var startPoint in galaxy.CreateStarts(50000))
            {
                pointCloud.AddPoint((startPoint, new Vector4(0.4f, 0.8f, 0.7f, 1)));
            }

            galaxy.randomJitter = 0.07f;
            foreach (var startPoint in galaxy.CreateStarts(10000))
            {
                pointCloud.AddPoint((startPoint, new Vector4(0.2f, 0.6f, 0.5f, 1)));
            }

            // create a new gltf model
            var model = ModelRoot.CreateModel();

            // add all meshes (just one in this case) to the model
            model.CreateMeshes(mesh);

            // create a scene, a node, and assign the first mesh (the terrain)
            model.UseScene("Default")
            .CreateNode().WithMesh(model.LogicalMeshes[0]);

            // save the model as GLB
            model.SaveGLB("Galaxy.glb");
        }
        public static ModelRoot ToGltf(this SimpleSkin skn, Dictionary <string, MagickImage> materialTextues = null)
        {
            SceneBuilder sceneBuilder = new SceneBuilder("model");
            var          meshBuilder  = VERTEX.CreateCompatibleMesh();

            foreach (SimpleSkinSubmesh submesh in skn.Submeshes)
            {
                MaterialBuilder material         = new MaterialBuilder(submesh.Name).WithUnlitShader();
                var             submeshPrimitive = meshBuilder.UsePrimitive(material);

                // Assign submesh Image
                if (materialTextues is not null && materialTextues.ContainsKey(submesh.Name))
                {
                    MagickImage submeshImage = materialTextues[submesh.Name];
                    AssignMaterialTexture(material, submeshImage);
                }

                // Build vertices
                var vertices = new List <VERTEX>(submesh.Vertices.Count);
                foreach (SimpleSkinVertex vertex in submesh.Vertices)
                {
                    VertexPositionNormal positionNormal = new VertexPositionNormal(vertex.Position, vertex.Normal);
                    VertexTexture1       uv             = new VertexTexture1(vertex.UV);

                    vertices.Add(new VERTEX(positionNormal, uv));
                }

                // Add vertices to primitive
                for (int i = 0; i < submesh.Indices.Count; i += 3)
                {
                    VERTEX v1 = vertices[submesh.Indices[i + 0]];
                    VERTEX v2 = vertices[submesh.Indices[i + 1]];
                    VERTEX v3 = vertices[submesh.Indices[i + 2]];

                    submeshPrimitive.AddTriangle(v1, v2, v3);
                }
            }

            sceneBuilder.AddRigidMesh(meshBuilder, new AffineTransform(new Vector3(-1, 1, 1), Quaternion.Identity, Vector3.Zero).Matrix);

            return(sceneBuilder.ToGltf2());
        }
Example #4
0
        public static ModelRoot ToGltf(this StaticObject staticObject)
        {
            ModelRoot root  = ModelRoot.CreateModel();
            Scene     scene = root.UseScene("default");

            var mesh = VERTEX.CreateCompatibleMesh();

            foreach (StaticObjectSubmesh submesh in staticObject.Submeshes)
            {
                MaterialBuilder material  = new MaterialBuilder(submesh.Name);
                var             primitive = mesh.UsePrimitive(material);

                List <VERTEX> vertices = new List <VERTEX>();
                foreach (StaticObjectVertex vertex in submesh.Vertices)
                {
                    vertices.Add(new VERTEX()
                                 .WithGeometry(vertex.Position)
                                 .WithMaterial(vertex.UV));
                }

                for (int i = 0; i < submesh.Indices.Count; i += 3)
                {
                    VERTEX v1 = vertices[(int)submesh.Indices[i + 0]];
                    VERTEX v2 = vertices[(int)submesh.Indices[i + 1]];
                    VERTEX v3 = vertices[(int)submesh.Indices[i + 2]];

                    primitive.AddTriangle(v1, v2, v3);
                }
            }

            scene
            .CreateNode()
            .WithMesh(root.CreateMesh(mesh));

            return(root);
        }
        public static void DoIt()
        {
            // todo: do something with laz file...
            var material = new MaterialBuilder("material1").WithUnlitShader();

            var mesh = VERTEX.CreateCompatibleMesh("points");

            // create a point cloud primitive
            var pointCloud = mesh.UsePrimitive(material, 1);


            var points = LasReader.Readlas("utrecht.laz");

            foreach (var p in points)
            {
                pointCloud.AddPoint((p, new Vector4(0.4f, 0.8f, 0.7f, 1)));
            }

            // create a new gltf model
            var model = ModelRoot.CreateModel();

            // add all meshes (just one in this case) to the model
            model.CreateMeshes(mesh);

            // create a scene, a node, and assign the first mesh (the terrain)
            model.UseScene("Default")
            .CreateNode().WithMesh(model.LogicalMeshes[0]);

            // save the model as GLB
            model.SaveGLB("Galaxy.glb");

            MessageBox.Show("Galaxy.glb is created");

            /**
             *
             * var galaxy = new Galaxy();
             *
             * galaxy.scaleOverPlane = 0.05f;
             * galaxy.randomJitter = 0.01f;
             * foreach (var startPoint in galaxy.CreateStarts(50000))
             * {
             *  pointCloud.AddPoint((startPoint, Vector4.One));
             * }
             *
             * galaxy.scaleOverPlane = 0.15f;
             * galaxy.randomJitter = 0.02f;
             * foreach (var startPoint in galaxy.CreateStarts(50000))
             * {
             *  pointCloud.AddPoint((startPoint, new Vector4(0.4f, 0.8f, 0.7f, 1)));
             * }
             *
             * galaxy.randomJitter = 0.07f;
             * foreach (var startPoint in galaxy.CreateStarts(10000))
             * {
             *  pointCloud.AddPoint((startPoint, new Vector4(0.2f, 0.6f, 0.5f, 1)));
             * }
             *
             * // create a new gltf model
             * var model = ModelRoot.CreateModel();
             *
             * // add all meshes (just one in this case) to the model
             * model.CreateMeshes(mesh);
             *
             * // create a scene, a node, and assign the first mesh (the terrain)
             * model.UseScene("Default")
             *  .CreateNode().WithMesh(model.LogicalMeshes[0]);
             *
             * // save the model as GLB
             * model.SaveGLB("Galaxy.glb");
             *
             * MessageBox.Show("Galaxy.glb is created");
             */
        }
Example #6
0
        private static IMeshBuilder <MaterialBuilder> BuildMesh(PapaMeshBinding meshBinding)
        {
            var meshBuilder = VERTEX.CreateCompatibleMesh();

            foreach (PapaMaterialGroup materialGroup in meshBinding.Mesh.MaterialGroups)
            {
                // Skip empty materials
                if (materialGroup.PrimitiveCount == 0)
                {
                    continue;
                }

                MaterialBuilder materialBuiler    = new MaterialBuilder(materialGroup.Name).WithSpecularGlossinessShader();
                var             materialPrimitive = meshBuilder.UsePrimitive(materialBuiler);

                // Check for DiffuseColor material parameter
                if (materialGroup.Material.VectorParameters.Any(x => x.Name == "DiffuseColor"))
                {
                    PapaVectorParameter diffuseColor = materialGroup.Material.VectorParameters.FirstOrDefault(x => x.Name == "DiffuseColor");

                    materialBuiler.UseChannel(KnownChannel.Diffuse).Parameter = diffuseColor.Value;
                }

                int minVertex = int.MaxValue;
                int maxVertex = int.MinValue;
                for (uint i = materialGroup.FirstIndex; i < materialGroup.PrimitiveCount * 3; i++)
                {
                    uint index = meshBinding.Mesh.Indices[(int)i];

                    if (index < minVertex)
                    {
                        minVertex = (int)index;
                    }
                    if (index > maxVertex)
                    {
                        maxVertex = (int)index;
                    }
                }

                int           vertexCount = maxVertex - minVertex + 1;
                List <VERTEX> vertices    = new(vertexCount);
                for (int i = minVertex; i < maxVertex + 1; i++)
                {
                    PapaVertex papaVertex = meshBinding.Mesh.Vertices[i];
                    VERTEX     vertex     = new VERTEX();

                    vertex.Geometry = new VertexPositionNormal()
                    {
                        Position = papaVertex.Position.Value,
                        Normal   = papaVertex.Normal.HasValue ? papaVertex.Normal.Value : new Vector3()
                    };

                    vertex.Material = new VertexColor2Texture2()
                    {
                        Color0    = papaVertex.Color1.HasValue ? papaVertex.Color1.Value : new Color(),
                        Color1    = papaVertex.Color2.HasValue ? papaVertex.Color2.Value : new Color(),
                        TexCoord0 = papaVertex.TexCoord1.HasValue ? papaVertex.TexCoord1.Value : new Vector2(),
                        TexCoord1 = papaVertex.TexCoord2.HasValue ? papaVertex.TexCoord2.Value : new Vector2(),
                    };

                    vertices.Add(vertex);
                }

                for (uint i = materialGroup.FirstIndex; i < materialGroup.PrimitiveCount * 3; i += 3)
                {
                    int index0 = (int)meshBinding.Mesh.Indices[(int)i + 0] - minVertex;
                    int index1 = (int)meshBinding.Mesh.Indices[(int)i + 1] - minVertex;
                    int index2 = (int)meshBinding.Mesh.Indices[(int)i + 2] - minVertex;

                    VERTEX vertex0 = vertices[index0];
                    VERTEX vertex1 = vertices[index1];
                    VERTEX vertex2 = vertices[index2];

                    materialPrimitive.AddTriangle(vertex0, vertex1, vertex2);
                }
            }

            return(meshBuilder);
        }