Ejemplo n.º 1
0
		public override void Import (Stream input, Stream output, string filename) {
			using (var importer = new AssimpContext()) {
				var scene = importer.ImportFileFromStream(input,
					PostProcessSteps.Triangulate | PostProcessSteps.SortByPrimitiveType | PostProcessSteps.GenerateNormals,
					Path.GetExtension(filename));
				using (var tw = new TarWriter(output)) {
					var textures = new Dictionary<string, string>();
					using (var ms = new MemoryStream()) {
						using (var bw = new BinaryWriter(ms)) {
							this.WriteModel(scene, bw, textures);
							bw.Flush();
							//this.PrintNode(scene, scene.RootNode, 0);
							ms.Position = 0;
							tw.Write(ms, ms.Length, "model.bin");
						}
					}
					this.WriteTextures(scene, tw, textures);
				}
			}
		}
Ejemplo n.º 2
0
        public static MeshModel Load(Stream stream, string formatHint)
        {
            using (AssimpContext importer = new AssimpContext())
            {
                var scene = importer.ImportFileFromStream(
                    stream,
                    PostProcessSteps.Triangulate |
                    // PostProcessSteps.PreTransformVertices |
                    PostProcessSteps.GenerateUVCoords |
                    PostProcessSteps.GenerateNormals,
                    formatHint);

                List<Mesh> meshes = new List<Mesh>();
                for (int i = 0; i < scene.MeshCount; i++)
                {
                    var mesh = scene.Meshes[i];
                    var indices = mesh.GetIndices();

                    int texture = 6;

                    BlockVertex[] vertices = new BlockVertex[mesh.VertexCount];
                    var src = mesh.Vertices;
                    var normals = mesh.HasNormals ? mesh.Normals : null;
                    var uvs = mesh.HasTextureCoords(0) ? mesh.TextureCoordinateChannels[0] : null;
                    for (int v = 0; v < vertices.Length; v++)
                    {
                        vertices[v].position = src[v].TK();
                        vertices[v].color = Vector3.One;
                        if (normals != null)
                            vertices[v].normal = normals[v].TK();
                        if (uvs != null)
                            vertices[v].uv = uvs[v].TK();
                        else
                            vertices[v].uv = 0.5f * Vector3.One;

                        // Set texture ID of the model
                        vertices[v].uv.Z = texture;
                    }

                    meshes.Add(new Mesh(indices, vertices, texture));
                }

                return new MeshModel(meshes);
            }
        }