public static async Task AddModelToScene(RenderLoop targetRenderLoop, ImportedModelContainer importedModel) { foreach (var actImportedObject in importedModel.Objects) { actImportedObject.Tag1 = new ModelViewerSceneObjectMetadata(false); } await targetRenderLoop.Scene.ImportAsync(importedModel); }
/// <summary> /// Imports all objects from the given source. /// </summary> public async Task <IEnumerable <SceneObject> > ImportAsync(ImportedModelContainer modelContainer) { if (!modelContainer.IsFinished || !modelContainer.IsValid) { throw new SeeingSharpException($"Given {nameof(ImportedModelContainer)} is not finished or valid!"); } // Append all data to the scene var result = new List <SceneObject>(modelContainer.Objects.Count); await this.ManipulateSceneAsync(manipulator => { // AddObject all resources first foreach (var actResourceInfo in modelContainer.ImportedResources) { manipulator.AddResource( actResourceInfo.ResourceFactory, actResourceInfo.ResourceKey); } // AddObject all objects foreach (var actObject in modelContainer.Objects) { manipulator.AddObject(actObject); result.Add(actObject); } // Apply parent/child relationships foreach (var actDependencyInfo in modelContainer.ParentChildRelationships) { manipulator.AddChildObject( actDependencyInfo.Parent, actDependencyInfo.Child); } }); return(result); }
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); })); } }
/// <summary> /// Imports all objects from the given source. /// </summary> /// <param name="objSource">The source to load from.</param> /// <param name="importOptions">All options for import logic.</param> public async Task <IEnumerable <SceneObject> > ImportAsync(ResourceLink objSource, ImportOptions importOptions) { List <SceneObject> result = new List <SceneObject>(); // Import all data ImportedModelContainer modelContainer = await GraphicsCore.Current.ImportersAndExporters .ImportAsync(objSource, importOptions); // Append all data to the scene await this.ManipulateSceneAsync((manipulator) => { // Add all resources first foreach (var actResourceInfo in modelContainer.ImportedResources) { manipulator.AddResource( actResourceInfo.ResourceFactory, actResourceInfo.ResourceKey); } // Add all objects foreach (var actObject in modelContainer.Objects) { manipulator.Add(actObject); result.Add(actObject); } // Apply parent/child relationships foreach (var actDependencyInfo in modelContainer.ParentChildRelationships) { manipulator.AddChild( actDependencyInfo.Item1, actDependencyInfo.Item2); } }); return(result); }
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); }
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)); } }