protected override void CreateMesh() { this.streamMesh = new SeparateStreamsMesh(); this.meshStreams = new BspMeshStreams(); meshStreams.Positions = streamMesh.SetStream(Streams.Position, 0, new ListMeshStream<Float3>(StreamConverterFactory)); meshStreams.Normals = streamMesh.SetStream(Streams.Normal, 0, new ListMeshStream<Float3>(StreamConverterFactory)); meshStreams.Colors = streamMesh.SetStream(Streams.Color, 0, new ListMeshStream<Color>(StreamConverterFactory)); meshStreams.TexCoord0 = streamMesh.SetStream(Streams.TexCoord, 0, new ListMeshStream<Float2>(StreamConverterFactory)); meshStreams.TexCoord1 = streamMesh.SetStream(Streams.TexCoord, 1, new ListMeshStream<Float2>(StreamConverterFactory)); }
private void ParseMesh(Scene scene, XElement mesh, MeshSkinAndMaterials skinAndMaterials, SeparateStreamsMesh dstMesh) { Dictionary<string, StreamKey> knownStreams = new Dictionary<string, StreamKey>(); Dictionary<StreamKey, string> knownSemantics = new Dictionary<StreamKey, string>(); foreach (var element in mesh.Elements()) { foreach (var subElement in element.Elements().Where(x=>x.Name == this.schema.inputName)) { var semanticAttribute = subElement.Attribute(schema.semanticName); var semantic = (semanticAttribute == null)?null:semanticAttribute.Value; var setAttribute = subElement.Attribute(schema.setName); var set = (setAttribute==null)?0:int.Parse(setAttribute.Value, CultureInfo.InvariantCulture); var sourceAttribute = subElement.Attribute(schema.sourceAttributeName); var source = (sourceAttribute == null)?null:sourceAttribute.Value; var key = new StreamKey(semantic, set); StreamKey knownStreamKey; if (!knownStreams.TryGetValue(source, out knownStreamKey)) { knownStreams.Add(source,key); } else if (knownStreamKey != key) { throw new NotImplementedException(); } string knownSemanticsKey; if (!knownSemantics.TryGetValue(key, out knownSemanticsKey)) { knownSemantics.Add(key, source); } else if (knownSemanticsKey != source) { throw new NotImplementedException(); } } } foreach (var streamName in knownStreams) { var s = mesh.ElementByUrl(streamName.Key); if (s != null) { if (s.Name == schema.verticesName) { } else { var source = ParseSource(s, schema); dstMesh.SetStream(streamName.Value.Key, streamName.Value.Channel, CreateMeshStream(source, streamName.Value.Key)); } } } foreach (var element in mesh.Elements()) { if (element.Name == this.schema.polygonsName) { this.ParsePolygons(skinAndMaterials, dstMesh, element); } else if (element.Name == this.schema.polylistName) { this.ParsePolylist(mesh, skinAndMaterials, dstMesh, element); } else if (element.Name == this.schema.trianglesName) { this.ParseTriangles(mesh, skinAndMaterials, dstMesh, element); } else if (element.Name == this.schema.verticesName || element.Name == this.schema.sourceName) { } else { throw new DaeException(message: "Unknown mesh element " + element.Name.LocalName); } } }
public static IMesh BuildSoftEdgedBox(float x, float y, float z, IStreamConverterFactory converterFactory) { Float3[] p = new[] { new Float3(-x / 2.0f, -y / 2.0f, z / 2.0f), new Float3(x / 2.0f, -y / 2.0f, z / 2.0f), new Float3(-x / 2.0f, y / 2.0f, z / 2.0f), new Float3(x / 2.0f, y / 2.0f, z / 2.0f), new Float3(-x / 2.0f, y / 2.0f, -z / 2.0f), new Float3(x / 2.0f, y / 2.0f, -z / 2.0f), new Float3(-x / 2.0f, -y / 2.0f, -z / 2.0f), new Float3(x / 2.0f, -y / 2.0f, -z / 2.0f), }; Float3[] n = new[] { new Float3(-0.57735f, -0.57735f, -0.57735f), new Float3(-0.57735f, 0.57735f, -0.57735f), new Float3(0.57735f, -0.57735f, -0.57735f), new Float3(0.57735f, 0.57735f, -0.57735f), new Float3(-0.57735f, -0.57735f, 0.57735f), new Float3(-0.57735f, 0.57735f, 0.57735f), new Float3(0.57735f, -0.57735f, 0.57735f), new Float3(0.57735f, 0.57735f, 0.57735f), }; Float3[] uv = new[] { new Float3(0.0f, 0.0f, 0.0f), new Float3(1.0f, 0.0f, 0.0f), new Float3(1.0f, 1.0f, 0.0f), new Float3(0.0f, 1.0f, 0.0f), }; var res = new SeparateStreamsMesh(); res.SetStream(Streams.Position, 0, new ArrayMeshStream<Float3>(p, converterFactory)); res.SetStream(Streams.Normal, 0, new ArrayMeshStream<Float3>(n, converterFactory)); res.SetStream(Streams.TexCoord, 0, new ArrayMeshStream<Float3>(uv, converterFactory)); res.SetStream(Streams.TexCoord, 1, new ArrayMeshStream<Float3>(uv, converterFactory)); res.SetStream(Streams.Color, 0, new ArrayMeshStream<Color>(new Color[] { Color.FromArgb(255, 255, 255, 255) }, converterFactory)); var t2 = Float3.Normalize(p[7] - p[6]); var b2 = Float3.Cross(Float3.Normalize(n[0] + n[2] + n[3] + n[1]), t2); var t3 = Float3.Normalize(p[1] - p[0]); var b3 = Float3.Cross(Float3.Normalize(n[4] + n[6] + n[2] + n[0]), t3); var t4 = Float3.Normalize(p[5] - p[3]); var b4 = Float3.Cross(Float3.Normalize(n[7] + n[3] + n[2] + n[6]), t4); var t5 = Float3.Normalize(p[2] - p[4]); var b5 = Float3.Cross(Float3.Normalize(n[1] + n[5] + n[4] + n[0]), t5); res.SetStream(Streams.Tangent, 0, new ArrayMeshStream<Float3>(new Float3[] { new Float3(1, 0, 0), new Float3(1, 0, 0), t2, t3, t4, t5 }, converterFactory)); res.SetStream(Streams.Binormal, 0, new ArrayMeshStream<Float3>(new Float3[] { new Float3(0, 1, 0), new Float3(0, 0, -1), b2, b3, b4, b5 }, converterFactory)); var s = res.CreateSubmesh(); s.VertexSourceType = VertexSourceType.QuadList; var positionIndices = s.SetIndexStream(Streams.Position, 0, new ListMeshStream<int>(24, converterFactory)); var normalIndices = s.SetIndexStream(Streams.Normal, 0, new ListMeshStream<int>(24, converterFactory)); var uv0Indices = s.SetIndexStream(Streams.TexCoord, 0, new ListMeshStream<int>(24, converterFactory)); var uv1Indices = s.SetIndexStream(Streams.TexCoord, 1, new ListMeshStream<int>(24, converterFactory)); var colorIndices = s.SetIndexStream(Streams.Color, 0, new ListMeshStream<int>(24, converterFactory)); var tangentIndices = s.SetIndexStream(Streams.Tangent, 0, new ListMeshStream<int>(24, converterFactory)); var binormalIndices = s.SetIndexStream(Streams.Binormal, 0, new ListMeshStream<int>(24, converterFactory)); Vertex vertex; Float3 t; Float3 b; // Quad 0 TOP //t = new Float3(1, 0, 0); //b = new Float3(0, 1, 0); //Float3.Cross(new Float3(0,0,1),t) positionIndices.Add(0); normalIndices.Add(4); colorIndices.Add(0); uv0Indices.Add(3); uv1Indices.Add(3); tangentIndices.Add(0); binormalIndices.Add(0); positionIndices.Add(1); normalIndices.Add(6); colorIndices.Add(0); uv0Indices.Add(2); uv1Indices.Add(2); tangentIndices.Add(0); binormalIndices.Add(0); positionIndices.Add(3); normalIndices.Add(7); colorIndices.Add(0); uv0Indices.Add(1); uv1Indices.Add(1); tangentIndices.Add(0); binormalIndices.Add(0); positionIndices.Add(2); normalIndices.Add(5); colorIndices.Add(0); uv0Indices.Add(0); uv1Indices.Add(0); tangentIndices.Add(0); binormalIndices.Add(0); //vertex = new Vertex // { Position = p[0], Color = color, Normal = n[4], UV0 = uv[3], UV1 = uv[3], Tangent = t, Binormal = b }; //s.Add(ref vertex); //vertex = new Vertex // { Position = p[1], Color = color, Normal = n[6], UV0 = uv[2], UV1 = uv[2], Tangent = t, Binormal = b }; //s.Add(ref vertex); //vertex = new Vertex // { Position = p[3], Color = color, Normal = n[7], UV0 = uv[1], UV1 = uv[1], Tangent = t, Binormal = b }; //s.Add(ref vertex); //vertex = new Vertex // { Position = p[2], Color = color, Normal = n[5], UV0 = uv[0], UV1 = uv[0], Tangent = t, Binormal = b }; //s.Add(ref vertex); positionIndices.Add(2); normalIndices.Add(5); colorIndices.Add(0); uv0Indices.Add(3); uv1Indices.Add(3); tangentIndices.Add(1); binormalIndices.Add(1); positionIndices.Add(3); normalIndices.Add(7); colorIndices.Add(0); uv0Indices.Add(2); uv1Indices.Add(2); tangentIndices.Add(1); binormalIndices.Add(1); positionIndices.Add(5); normalIndices.Add(3); colorIndices.Add(0); uv0Indices.Add(1); uv1Indices.Add(1); tangentIndices.Add(1); binormalIndices.Add(1); positionIndices.Add(4); normalIndices.Add(1); colorIndices.Add(0); uv0Indices.Add(0); uv1Indices.Add(0); tangentIndices.Add(1); binormalIndices.Add(1); //// Quad 1 //t = new Float3(1, 0, 0); //b = new Float3(0, 0, -1); //vertex = new Vertex // { Position = p[2], Color = color, Normal = n[5], UV0 = uv[3], UV1 = uv[3], Tangent = t, Binormal = b }; //s.Add(ref vertex); //vertex = new Vertex // { Position = p[3], Color = color, Normal = n[7], UV0 = uv[2], UV1 = uv[2], Tangent = t, Binormal = b }; //s.Add(ref vertex); //vertex = new Vertex // { Position = p[5], Color = color, Normal = n[3], UV0 = uv[1], UV1 = uv[1], Tangent = t, Binormal = b }; //s.Add(ref vertex); //vertex = new Vertex // { Position = p[4], Color = color, Normal = n[1], UV0 = uv[0], UV1 = uv[0], Tangent = t, Binormal = b }; //s.Add(ref vertex); positionIndices.Add(4); normalIndices.Add(1); colorIndices.Add(0); uv0Indices.Add(3); uv1Indices.Add(3); tangentIndices.Add(2); binormalIndices.Add(2); positionIndices.Add(5); normalIndices.Add(3); colorIndices.Add(0); uv0Indices.Add(2); uv1Indices.Add(2); tangentIndices.Add(2); binormalIndices.Add(2); positionIndices.Add(7); normalIndices.Add(2); colorIndices.Add(0); uv0Indices.Add(1); uv1Indices.Add(1); tangentIndices.Add(2); binormalIndices.Add(2); positionIndices.Add(6); normalIndices.Add(0); colorIndices.Add(0); uv0Indices.Add(0); uv1Indices.Add(0); tangentIndices.Add(2); binormalIndices.Add(2); //// Quad 2 //t = Float3.Normalize(p[7] - p[6]); //b = Float3.Cross(Float3.Normalize(n[0] + n[2] + n[3] + n[1]), t); //vertex = new Vertex // { Position = p[4], Color = color, Normal = n[1], UV0 = uv[3], UV1 = uv[3], Tangent = t, Binormal = b }; //s.Add(ref vertex); //vertex = new Vertex // { Position = p[5], Color = color, Normal = n[3], UV0 = uv[2], UV1 = uv[2], Tangent = t, Binormal = b }; //s.Add(ref vertex); //vertex = new Vertex // { Position = p[7], Color = color, Normal = n[2], UV0 = uv[1], UV1 = uv[1], Tangent = t, Binormal = b }; //s.Add(ref vertex); //vertex = new Vertex // { Position = p[6], Color = color, Normal = n[0], UV0 = uv[0], UV1 = uv[0], Tangent = t, Binormal = b }; //s.Add(ref vertex); positionIndices.Add(6); normalIndices.Add(0); colorIndices.Add(0); uv0Indices.Add(3); uv1Indices.Add(3); tangentIndices.Add(3); binormalIndices.Add(3); positionIndices.Add(7); normalIndices.Add(2); colorIndices.Add(0); uv0Indices.Add(2); uv1Indices.Add(2); tangentIndices.Add(3); binormalIndices.Add(3); positionIndices.Add(1); normalIndices.Add(6); colorIndices.Add(0); uv0Indices.Add(1); uv1Indices.Add(1); tangentIndices.Add(3); binormalIndices.Add(3); positionIndices.Add(0); normalIndices.Add(4); colorIndices.Add(0); uv0Indices.Add(0); uv1Indices.Add(0); tangentIndices.Add(3); binormalIndices.Add(3); //// Quad 3 //t = Float3.Normalize(p[1] - p[0]); //b = Float3.Cross(Float3.Normalize(n[4] + n[6] + n[2] + n[0]), t); //vertex = new Vertex // { Position = p[6], Color = color, Normal = n[0], UV0 = uv[3], UV1 = uv[3], Tangent = t, Binormal = b }; //s.Add(ref vertex); //vertex = new Vertex // { Position = p[7], Color = color, Normal = n[2], UV0 = uv[2], UV1 = uv[2], Tangent = t, Binormal = b }; //s.Add(ref vertex); //vertex = new Vertex // { Position = p[1], Color = color, Normal = n[6], UV0 = uv[1], UV1 = uv[1], Tangent = t, Binormal = b }; //s.Add(ref vertex); //vertex = new Vertex // { Position = p[0], Color = color, Normal = n[4], UV0 = uv[0], UV1 = uv[0], Tangent = t, Binormal = b }; //s.Add(ref vertex); positionIndices.Add(1); normalIndices.Add(6); colorIndices.Add(0); uv0Indices.Add(3); uv1Indices.Add(3); tangentIndices.Add(4); binormalIndices.Add(4); positionIndices.Add(7); normalIndices.Add(2); colorIndices.Add(0); uv0Indices.Add(2); uv1Indices.Add(2); tangentIndices.Add(4); binormalIndices.Add(4); positionIndices.Add(5); normalIndices.Add(3); colorIndices.Add(0); uv0Indices.Add(1); uv1Indices.Add(1); tangentIndices.Add(4); binormalIndices.Add(4); positionIndices.Add(3); normalIndices.Add(7); colorIndices.Add(0); uv0Indices.Add(0); uv1Indices.Add(0); tangentIndices.Add(4); binormalIndices.Add(4); //// Quad 4 //t = Float3.Normalize(p[5] - p[3]); //b = Float3.Cross(Float3.Normalize(n[7] + n[3] + n[2] + n[6]), t); //vertex = new Vertex // { Position = p[1], Color = color, Normal = n[6], UV0 = uv[3], UV1 = uv[3], Tangent = t, Binormal = b }; //s.Add(ref vertex); //vertex = new Vertex // { Position = p[7], Color = color, Normal = n[2], UV0 = uv[2], UV1 = uv[2], Tangent = t, Binormal = b }; //s.Add(ref vertex); //vertex = new Vertex // { Position = p[5], Color = color, Normal = n[3], UV0 = uv[1], UV1 = uv[1], Tangent = t, Binormal = b }; //s.Add(ref vertex); //vertex = new Vertex // { Position = p[3], Color = color, Normal = n[7], UV0 = uv[0], UV1 = uv[0], Tangent = t, Binormal = b }; //s.Add(ref vertex); positionIndices.Add(6); normalIndices.Add(0); colorIndices.Add(0); uv0Indices.Add(3); uv1Indices.Add(3); tangentIndices.Add(5); binormalIndices.Add(5); positionIndices.Add(0); normalIndices.Add(4); colorIndices.Add(0); uv0Indices.Add(2); uv1Indices.Add(2); tangentIndices.Add(5); binormalIndices.Add(5); positionIndices.Add(2); normalIndices.Add(5); colorIndices.Add(0); uv0Indices.Add(1); uv1Indices.Add(1); tangentIndices.Add(5); binormalIndices.Add(5); positionIndices.Add(4); normalIndices.Add(1); colorIndices.Add(0); uv0Indices.Add(0); uv1Indices.Add(0); tangentIndices.Add(5); binormalIndices.Add(5); //// Quad 5 //t = Float3.Normalize(p[2] - p[4]); //b = Float3.Cross(Float3.Normalize(n[1] + n[5] + n[4] + n[0]), t); //vertex = new Vertex // { Position = p[6], Color = color, Normal = n[0], UV0 = uv[3], UV1 = uv[3], Tangent = t, Binormal = b }; //s.Add(ref vertex); //vertex = new Vertex // { Position = p[0], Color = color, Normal = n[4], UV0 = uv[2], UV1 = uv[2], Tangent = t, Binormal = b }; //s.Add(ref vertex); //vertex = new Vertex // { Position = p[2], Color = color, Normal = n[5], UV0 = uv[1], UV1 = uv[1], Tangent = t, Binormal = b }; //s.Add(ref vertex); //vertex = new Vertex // { Position = p[4], Color = color, Normal = n[1], UV0 = uv[0], UV1 = uv[0], Tangent = t, Binormal = b }; //s.Add(ref vertex); return res; }
private void ParsSubMesh(AseParser parser, Scene scene, Node node) { var mesh = new SeparateStreamsMesh(); var submesh = mesh.CreateSubmesh(); node.Mesh = mesh; scene.Geometries.Add(mesh); ArrayMeshStream<Float3> vertices = null; ListMeshStream<Float3> normalStream = null; FaceNormal[] normals = null; ArrayMeshStream<Float3> tvertices = null; ArrayMeshStream<Color> cols = null; AseFace[] faces = null; AseTFace[] tfaces = null; Tuple<int, int, int>[] colFaces = null; //TODO: submesh can have it's own vertex streams parser.Consume("{"); for (;;) { var attr = parser.Consume(); if (attr == null || attr == "}") { break; } if (0 == string.Compare(attr, "*TIMEVALUE", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeFloat(); continue; } if (0 == string.Compare(attr, "*MESH_NUMVERTEX", StringComparison.InvariantCultureIgnoreCase)) { vertices = new ArrayMeshStream<Float3>(parser.ConsumeInt(), converterFactory); mesh.SetStream(Streams.Position, 0, vertices); continue; } if (0 == string.Compare(attr, "*MESH_VERTEX_LIST", StringComparison.InvariantCultureIgnoreCase)) { this.ParseVertexList(parser, vertices); continue; } if (0 == string.Compare(attr, "*MESH_NUMTVERTEX", StringComparison.InvariantCultureIgnoreCase)) { tvertices = new ArrayMeshStream<Float3>(parser.ConsumeInt(), converterFactory); mesh.SetStream(Streams.TexCoord, 0, tvertices); continue; } if (0 == string.Compare(attr, "*MESH_TVERTLIST", StringComparison.InvariantCultureIgnoreCase)) { this.ParseTVertList(parser, tvertices); continue; } if (0 == string.Compare(attr, "*MESH_NUMTVFACES", StringComparison.InvariantCultureIgnoreCase)) { tfaces = new AseTFace[parser.ConsumeInt()]; continue; } if (0 == string.Compare(attr, "*MESH_TFACELIST", StringComparison.InvariantCultureIgnoreCase)) { this.ParseTFaceList(parser, tfaces); continue; } if (0 == string.Compare(attr, "*MESH_NUMCVERTEX", StringComparison.InvariantCultureIgnoreCase)) { cols = new ArrayMeshStream<Color>(parser.ConsumeInt(), converterFactory); mesh.SetStream(Streams.Color, 0, cols); continue; } if (0 == string.Compare(attr, "*MESH_CVERTLIST", StringComparison.InvariantCultureIgnoreCase)) { this.ParseColList(parser, cols); continue; } if (0 == string.Compare(attr, "*MESH_NUMCVFACES", StringComparison.InvariantCultureIgnoreCase)) { colFaces = new Tuple<int, int, int>[parser.ConsumeInt()]; continue; } if (0 == string.Compare(attr, "*MESH_CFACELIST", StringComparison.InvariantCultureIgnoreCase)) { this.ParseColFaceList(parser, colFaces); continue; } if (0 == string.Compare(attr, "*MESH_NORMALS", StringComparison.InvariantCultureIgnoreCase)) { normals = new FaceNormal[faces.Length]; this.ParsNormalList(parser, normals); continue; } if (0 == string.Compare(attr, "*MESH_NUMFACES", StringComparison.InvariantCultureIgnoreCase)) { faces = new AseFace[parser.ConsumeInt()]; continue; } if (0 == string.Compare(attr, "*MESH_FACE_LIST", StringComparison.InvariantCultureIgnoreCase)) { this.ParseFaceList(parser, faces); continue; } parser.UnknownLexemError(); } ListMeshStream<int> positionIndices = null; if (vertices != null) { positionIndices = new ListMeshStream<int>(faces.Length * 3,converterFactory); submesh.SetIndexStream(Streams.Position, 0, positionIndices); } ListMeshStream<int> normalIndices = null; if (normals != null) { normalStream = new ListMeshStream<Float3>(faces.Length * 3, converterFactory); mesh.SetStream(Streams.Normal, 0, normalStream); normalIndices = new ListMeshStream<int>(faces.Length * 3, converterFactory); submesh.SetIndexStream(Streams.Normal, 0, normalIndices); } ListMeshStream<int> colorIndices = null; if (cols != null) { colorIndices = new ListMeshStream<int>(faces.Length * 3, converterFactory); submesh.SetIndexStream(Streams.Color, 0, colorIndices); } ListMeshStream<int> texCoordIndices = null; if (tvertices != null) { texCoordIndices = new ListMeshStream<int>(faces.Length * 3, converterFactory); submesh.SetIndexStream(Streams.TexCoord, 0, texCoordIndices); } for (int i = 0; i < faces.Length; ++i) { // -------------------------------------------------- // if (positionIndices != null) { positionIndices.Add(faces[i].A); } if (normalIndices != null) { normalIndices.Add(normalStream.Count); normalStream.Add(normals[i].A.Normal); } if (colorIndices != null) { colorIndices.Add(colFaces[i].Item1); } if (texCoordIndices != null) { texCoordIndices.Add(tfaces[i].A); } // -------------------------------------------------- // if (positionIndices != null) { positionIndices.Add(faces[i].C); } if (normalIndices != null) { normalIndices.Add(normalStream.Count); normalStream.Add(normals[i].C.Normal); } if (colorIndices != null) { colorIndices.Add(colFaces[i].Item3); } if (texCoordIndices != null) { texCoordIndices.Add(tfaces[i].C); } // -------------------------------------------------- // if (positionIndices != null) { positionIndices.Add(faces[i].B); } if (normalIndices != null) { normalIndices.Add(normalStream.Count); normalStream.Add(normals[i].B.Normal); } if (colorIndices != null) { colorIndices.Add(colFaces[i].Item2); } if (texCoordIndices != null) { texCoordIndices.Add(tfaces[i].B); } // -------------------------------------------------- // } }