예제 #1
0
        public void Test_SingleCoordinate()
        {
            var calculator = new ObjectTreeBoundingBoxCalculator();

            calculator.AddCoordinate(new Vector3(-1f, 0f, -1f));

            Assert.IsFalse(calculator.CanCreateBoundingBox);
        }
예제 #2
0
        public void Test_MoreEqualCoordinates_CreateBoxException()
        {
            var calculator = new ObjectTreeBoundingBoxCalculator();

            calculator.AddCoordinate(new Vector3(-1f, 0f, -1f));
            calculator.AddCoordinate(new Vector3(-1f, 0f, -1f));
            calculator.AddCoordinate(new Vector3(-1f, 0f, -1f));
            calculator.AddCoordinate(new Vector3(-1f, 0f, -1f));

            Assert.IsFalse(calculator.CanCreateBoundingBox);

            calculator.CreateBoundingBox();
        }
예제 #3
0
        public void Test_DefaultUsage()
        {
            var calculator = new ObjectTreeBoundingBoxCalculator();

            calculator.AddCoordinate(new Vector3(-1f, 0f, -1f));
            calculator.AddCoordinate(new Vector3(1f, 1f, 1f));

            Assert.IsTrue(calculator.CanCreateBoundingBox);

            var boundingBox = calculator.CreateBoundingBox();

            Assert.IsTrue(boundingBox.Minimum.Equals(new Vector3(-1f, 0f, -1f)));
            Assert.IsTrue(boundingBox.Maximum.Equals(new Vector3(1f, 1f, 1f)));
        }
예제 #4
0
        public ImportedModelContainer ImportModel(ResourceLink sourceFile, ImportOptions importOptions)
        {
            var modelContainer = new ImportedModelContainer(sourceFile, importOptions);

            // Load Assimp scene
            using var assimpContext = new AssimpContext();
            assimpContext.SetIOSystem(new AssimpIOSystem(sourceFile));
            var scene = assimpContext.ImportFile(
                sourceFile.FileNameWithExtension,
                PostProcessPreset.TargetRealTimeFast | PostProcessSteps.SplitLargeMeshes);

            // Load all materials
            ProcessMaterials(modelContainer, scene);

            // Load all scene objects
            var boundBoxCalculator = new ObjectTreeBoundingBoxCalculator();

            ProcessNode(modelContainer, scene, scene.RootNode, null, boundBoxCalculator);

            // Finish loading
            modelContainer.FinishLoading(boundBoxCalculator.CreateBoundingBox());

            return(modelContainer);
        }
예제 #5
0
        public void Test_Empty()
        {
            var calculator = new ObjectTreeBoundingBoxCalculator();

            Assert.IsFalse(calculator.CanCreateBoundingBox);
        }
예제 #6
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));
                }
            }