Пример #1
0
        public ModelRoot AddMesh(ModelRoot model, string nodeName, IndexedTriangulation indexedTriangulation, IEnumerable <Vector3> normals, PBRTexture textures, bool doubleSided = true)
        {
            // create a basic scene
            model = model ?? CreateNewModel();
            var rnode = model.LogicalScenes.First()?.FindNode(n => n.Name == nodeName);

            if (rnode == null)
            {
                rnode = model.LogicalScenes.First().CreateNode(nodeName);
            }

            var rmesh = rnode.Mesh = FindOrCreateMesh(model, string.Concat(rnode.Name, "Mesh"));


            var material = model.CreateMaterial(string.Concat(nodeName, "Material"))
                           .WithPBRMetallicRoughness(Vector4.One, textures?.BaseColorTexture?.FilePath, null, 0, 1)
                           .WithDoubleSide(doubleSided);

            if (textures != null && textures.NormalTexture != null)
            {
                material.WithChannelTexture("NORMAL", 0, textures.NormalTexture.FilePath);
            }

            // create mesh primitive
            MeshPrimitive primitive = rmesh.CreatePrimitive();


            if (indexedTriangulation.Colors != null && indexedTriangulation.Colors.Any())
            {
                primitive = primitive.WithVertexAccessor("POSITION", indexedTriangulation.Positions);
                primitive = primitive.WithVertexAccessor("COLOR_0", indexedTriangulation.Colors);
            }
            else
            {
                primitive = primitive.WithVertexAccessor("POSITION", indexedTriangulation.Positions);
            }

            if (normals != null)
            {
                primitive = primitive.WithVertexAccessor("NORMAL", normals.ToList());
            }
            primitive = primitive.WithIndicesAccessor(PrimitiveType.TRIANGLES, indexedTriangulation.Indices);

            if (textures != null && textures.TextureCoordSets == null)
            {
                (Vector3 Min, Vector3 Max)coordBounds = CalculateBounds(indexedTriangulation.Positions);

                textures.TextureCoordSets = indexedTriangulation.Positions.Select(pos => new Vector2(
                                                                                      MathHelper.Map(coordBounds.Min.X, coordBounds.Max.X, 0, 1, pos.X, true)
                                                                                      , MathHelper.Map(coordBounds.Min.Z, coordBounds.Max.Z, 0, 1, pos.Z, true)
                                                                                      ));

                primitive = primitive
                            .WithVertexAccessor("TEXCOORD_0", textures.TextureCoordSets.ToList());
            }

            primitive = primitive.WithMaterial(material);
            return(model);
        }
Пример #2
0
        public ModelRoot CloneWithMesh(ModelRoot inputModel, IndexedTriangulation indexedTriangulation)
        {
            var model = CreateNewModel();
            var rnode = model.LogicalScenes.First().CreateNode(TERRAIN_NODE_NAME);
            var rmesh = rnode.Mesh = FindOrCreateMesh(model, string.Concat(rnode.Name, "Mesh"));

            MaterialBuilder b = new MaterialBuilder();

            inputModel.LogicalMaterials.First().CopyTo(b);
            var material = Toolkit.CreateMaterial(model, b);

            // create mesh primitive
            MeshPrimitive primitive = rmesh.CreatePrimitive();

            if (indexedTriangulation.Colors != null && indexedTriangulation.Colors.Any())
            {
                primitive = primitive.WithVertexAccessor("POSITION", indexedTriangulation.Positions);
                primitive = primitive.WithVertexAccessor("COLOR_0", indexedTriangulation.Colors);
            }
            else
            {
                primitive = primitive.WithVertexAccessor("POSITION", indexedTriangulation.Positions);
            }
            var normals = _meshService.ComputeMeshNormals(indexedTriangulation.Positions, indexedTriangulation.Indices);

            primitive = primitive.WithVertexAccessor("NORMAL", normals.ToList());
            primitive = primitive.WithIndicesAccessor(PrimitiveType.TRIANGLES, indexedTriangulation.Indices);



            (Vector3 Min, Vector3 Max)coordBounds = CalculateBounds(indexedTriangulation.Positions);

            var coordSets = indexedTriangulation.Positions.Select(pos => new Vector2(
                                                                      MathHelper.Map(coordBounds.Min.X, coordBounds.Max.X, 0, 1, pos.X, true)
                                                                      , MathHelper.Map(coordBounds.Min.Z, coordBounds.Max.Z, 0, 1, pos.Z, true)
                                                                      ));

            primitive = primitive
                        .WithVertexAccessor("TEXCOORD_0", coordSets.ToList());


            primitive = primitive.WithMaterial(material);
            return(model);
        }