Пример #1
0
        public static Scene LddPartToAssimp(PartWrapper part, MeshExportOptions exportOptions = null)
        {
            if (exportOptions == null)
            {
                exportOptions = new MeshExportOptions();
            }

            var scene = new Scene()
            {
                RootNode = new Node("Root")
            };

            scene.Materials.Add(new Material()
            {
                Name = "BaseMaterial"
            });

            var meshNodes = new List <Node>();


            foreach (var surface in part.Surfaces)
            {
                string nodeName = "MainSurface";
                if (surface.SurfaceID > 0)
                {
                    nodeName = $"Decoration{surface.SurfaceID}";
                }

                var createdNodes = CreateSurfaceMeshNodes(surface, scene, nodeName, exportOptions);
                meshNodes.AddRange(createdNodes);
            }

            //meshNodes.AddRange(scene.GetNodeHierarchy().Where(x => x.MeshCount > 0));

            if (exportOptions.IncludeBones && part.IsFlexible)
            {
                var armatureNode = CreateArmatureNodeHierarchy(part, scene);

                foreach (var node in meshNodes)
                {
                    armatureNode.Children.Add(node);
                }

                scene.RootNode.Children.Add(armatureNode);
            }
            else if (meshNodes.Count > 1)
            {
                var groupNode = new Node()
                {
                    Name = "Part"
                };
                foreach (var node in meshNodes)
                {
                    groupNode.Children.Add(node);
                }
                scene.RootNode.Children.Add(groupNode);
            }
            else
            {
                scene.RootNode.Children.Add(meshNodes[0]);
            }

            if (exportOptions.IncludeConnections && part.Primitive.Connectors.Any())
            {
                var connectionsNode = new Node("Connections");
                scene.RootNode.Children.Add(connectionsNode);

                foreach (var connGroup in part.Primitive.Connectors.GroupBy(x => x.Type))
                {
                    int connectionIdx = 0;
                    foreach (var conn in connGroup)
                    {
                        var connNode = new Node($"{connGroup.Key.ToString()}{connectionIdx++}_Type_{conn.SubType}")
                        {
                            Transform = conn.Transform.ToMatrix4().ToAssimp()
                        };
                        //connNode.Metadata.Add("Type", new Assimp.Metadata.Entry(Assimp.MetaDataType.String, conn.Type.ToString()));
                        connectionsNode.Children.Add(connNode);
                    }
                }
            }

            if (exportOptions.IncludeCollisions && part.Primitive.Collisions.Any())
            {
                var collisionsNode = new Node("Collisions");
                scene.RootNode.Children.Add(collisionsNode);

                var sphereMesh = ResourceHelper.GetResourceModel("Models.Sphere.obj", "obj").Meshes[0];
                var boxMesh    = ResourceHelper.GetResourceModel("Models.Cube.obj", "obj").Meshes[0];

                foreach (var collGroup in part.Primitive.Collisions.GroupBy(x => x.CollisionType))
                {
                    int collisionIdx = 0;

                    foreach (var collision in collGroup)
                    {
                        var collNode      = new Node($"{collGroup.Key.ToString()}{collisionIdx++}");
                        var meshTransform =
                            Simple3D.Matrix4d.FromScale(collision.GetSize() * 2f) * collision.Transform.ToMatrix4d();

                        if (collision.CollisionType == LDD.Primitives.Collisions.CollisionType.Box)
                        {
                            collNode.MeshIndices.Add(scene.MeshCount);
                            scene.Meshes.Add(boxMesh.Clone());
                        }
                        else
                        {
                            collNode.MeshIndices.Add(scene.MeshCount);
                            scene.Meshes.Add(sphereMesh.Clone());
                        }
                        //connNode.Metadata.Add("Type", new Assimp.Metadata.Entry(Assimp.MetaDataType.String, conn.Type.ToString()));
                        collNode.Transform = ((Simple3D.Matrix4)meshTransform).ToAssimp();
                        collisionsNode.Children.Add(collNode);
                    }
                }
            }

            //if (exportOptions.IncludeRoundEdgeData)
            //{
            //    foreach (var mat in scene.Materials)
            //    {
            //    }
            //}

            return(scene);
        }
Пример #2
0
        private static Node CreateMeshNode(Scene scene, MeshGeometry geometry, string name, int materialIndex, MeshExportOptions exportOptions)
        {
            var meshNode = new Node()
            {
                Name = name
            };

            var aMesh = exportOptions.IncludeRoundEdgeData ?
                        LddRoundEdgeMeshToAssimp(geometry) : LddMeshToAssimp(geometry);

            aMesh.MaterialIndex = materialIndex;
            meshNode.MeshIndices.Add(scene.MeshCount);
            scene.Meshes.Add(aMesh);

            return(meshNode);
        }
Пример #3
0
        public static Scene PartProjectToAssimp(PartProject project, MeshExportOptions exportOptions = null)
        {
            var partWrapper = project.GenerateLddPart();

            return(LddPartToAssimp(partWrapper, exportOptions));
        }
Пример #4
0
        private static List <Node> CreateSurfaceMeshNodes(PartSurfaceMesh partSurface, Scene scene, string surfaceName, MeshExportOptions exportOptions)
        {
            var meshNodes = new List <Node>();

            int materialIndex = scene.MaterialCount;

            scene.Materials.Add(new Material()
            {
                Name = $"{surfaceName}_Material"
            });

            if (exportOptions.IndividualComponents)
            {
                int meshIndex = 0;

                foreach (var comp in partSurface.Mesh.Cullings)
                {
                    string nodeName = $"{surfaceName}_Mesh{meshIndex++}";

                    var compModel = partSurface.Mesh.GetCullingGeometry(comp);
                    var mNode     = CreateMeshNode(scene, compModel, nodeName, materialIndex, exportOptions);
                    meshNodes.Add(mNode);


                    if (comp.ReplacementMesh != null && exportOptions.IncludeAltMeshes)
                    {
                        mNode = CreateMeshNode(scene, comp.ReplacementMesh, nodeName + "Alt", materialIndex, exportOptions);
                        meshNodes.Add(mNode);
                    }
                }
            }
            else
            {
                var mNode = CreateMeshNode(scene, partSurface.Mesh.Geometry, surfaceName, materialIndex, exportOptions);
                meshNodes.Add(mNode);
            }

            return(meshNodes);
        }