Пример #1
0
        private static void ProcessMaterials(ImportedModelContainer modelContainer, Scene scene)
        {
            var materialCount = scene.MaterialCount;

            for (var materialIndex = 0; materialIndex < materialCount; materialIndex++)
            {
                var actMaterial = scene.Materials[materialIndex];

                modelContainer.AddResource(new ImportedResourceInfo(
                                               modelContainer.GetResourceKey("Material", materialIndex.ToString()),
                                               _ =>
                {
                    var materialResource = new StandardMaterialResource();
                    if (actMaterial.HasColorDiffuse)
                    {
                        materialResource.UseVertexColors      = false;
                        materialResource.MaterialDiffuseColor =
                            AssimpHelper.Color4FromAssimp(actMaterial.ColorDiffuse);
                    }
                    return(materialResource);
                }));
            }
        }
Пример #2
0
        private static void ProcessNode(
            ImportedModelContainer modelContainer,
            Scene scene, Node actNode, SceneObject?actParent,
            ObjectTreeBoundingBoxCalculator boundingBoxCalc)
        {
            SceneObject?nextParent = null;

            if (actNode.HasMeshes)
            {
                var actTransform = Matrix4x4.Transpose(AssimpHelper.MatrixFromAssimp(actNode.Transform));
                boundingBoxCalc.PushTransform(ref actTransform);

                // Count vertices
                var fullVertexCount = 0;
                foreach (var actMeshId in actNode.MeshIndices)
                {
                    var actMesh = scene.Meshes[actMeshId];
                    fullVertexCount += actMesh.VertexCount;
                }

                // This one has true geometry
                var meshCount    = actNode.MeshCount;
                var newGeometry  = new Geometry(fullVertexCount);
                var materialKeys = new NamedOrGenericKey[meshCount];
                for (var meshIndex = 0; meshIndex < meshCount; meshIndex++)
                {
                    var actMeshId     = actNode.MeshIndices[meshIndex];
                    var actBaseVertex = newGeometry.CountVertices;
                    var actMesh       = scene.Meshes[actMeshId];

                    List <Color4D>?vertexColors = null;
                    if (actMesh.HasVertexColors(0))
                    {
                        vertexColors = actMesh.VertexColorChannels[0];
                    }

                    List <Vector3D>?textureCoords1 = null;
                    if (actMesh.TextureCoordinateChannelCount > 0)
                    {
                        textureCoords1 = actMesh.TextureCoordinateChannels[0];
                    }

                    // Create all vertices
                    var vertexCount = actMesh.VertexCount;
                    for (var actVertexId = 0; actVertexId < vertexCount; actVertexId++)
                    {
                        var     vertexIndex = newGeometry.AddVertex();
                        ref var newVertex   = ref newGeometry.GetVertexBasicRef(vertexIndex);

                        newVertex.Position = AssimpHelper.Vector3FromAssimp(actMesh.Vertices[actVertexId]);
                        if (actMesh.HasNormals)
                        {
                            newVertex.Normal = AssimpHelper.Vector3FromAssimp(actMesh.Normals[actVertexId]);
                        }
                        if (vertexColors != null)
                        {
                            newVertex.Color = AssimpHelper.Color4FromAssimp(vertexColors[actVertexId]);
                        }
                        if (textureCoords1 != null)
                        {
                            newVertex.TexCoord1 = AssimpHelper.Vector2FromAssimp(textureCoords1[actVertexId]);
                        }

                        boundingBoxCalc.AddCoordinate(ref newVertex.Position);
                    }

                    // Create all faces
                    var newSurface = newGeometry.CreateSurface(actMesh.FaceCount * 3);
                    foreach (var actFace in actMesh.Faces)
                    {
                        if (actFace.IndexCount != 3)
                        {
                            continue;
                        }

                        newSurface.AddTriangle(
                            actBaseVertex + actFace.Indices[0],
                            actBaseVertex + actFace.Indices[1],
                            actBaseVertex + actFace.Indices[2]);
                    }

                    materialKeys[meshIndex] = modelContainer.GetResourceKey("Material", actMesh.MaterialIndex.ToString());
                }

                var geometryKey = modelContainer.GetResourceKey("Geometry", actNode.Name);
                modelContainer.AddResource(new ImportedResourceInfo(
                                               modelContainer.GetResourceKey("Geometry", actNode.Name),
                                               _ => new GeometryResource(newGeometry)));

                var newMesh = new Mesh(geometryKey, materialKeys);
                newMesh.CustomTransform    = actTransform;
                newMesh.TransformationType = SpacialTransformationType.CustomTransform;
                newMesh.Name = actNode.Name;
                modelContainer.AddObject(newMesh);
                nextParent = newMesh;

                if (actParent != null)
                {
                    modelContainer.AddParentChildRelationship(new ParentChildRelationship(actParent, newMesh));
                }
            }