public static ModelData Load(Stream stream) { ModelData modelData = new ModelData(); Collada.ColladaRoot colladaRoot = new Collada.ColladaRoot(XElement.Load(stream)); if (colladaRoot.Scene.VisualScene.Nodes.Count > 1) { ModelBoneData modelBoneData = new ModelBoneData(); modelData.Bones.Add(modelBoneData); modelBoneData.ParentBoneIndex = -1; modelBoneData.Name = string.Empty; modelBoneData.Transform = Matrix.Identity; using (List <Collada.ColladaNode> .Enumerator enumerator = colladaRoot.Scene.VisualScene.Nodes.GetEnumerator()) { while (enumerator.MoveNext()) { Collada.ColladaNode node = enumerator.Current; Collada.LoadNode(modelData, modelBoneData, node, Matrix.CreateScale(colladaRoot.Asset.Meter)); } goto IL_104; } } foreach (Collada.ColladaNode node2 in colladaRoot.Scene.VisualScene.Nodes) { Collada.LoadNode(modelData, null, node2, Matrix.CreateScale(colladaRoot.Asset.Meter)); } IL_104: foreach (ModelBuffersData modelBuffersData in modelData.Buffers) { Collada.IndexVertices(modelBuffersData.VertexDeclaration.VertexStride, modelBuffersData.Vertices, out modelBuffersData.Vertices, out modelBuffersData.Indices); } return(modelData); }
private static ModelMeshData LoadGeometry(ModelData data, ModelBoneData parentBoneData, Collada.ColladaGeometry geometry) { ModelMeshData modelMeshData = new ModelMeshData(); data.Meshes.Add(modelMeshData); modelMeshData.Name = parentBoneData.Name; modelMeshData.ParentBoneIndex = data.Bones.IndexOf(parentBoneData); bool flag = false; foreach (Collada.ColladaPolygons polygons in geometry.Mesh.Polygons) { ModelMeshPartData modelMeshPartData = Collada.LoadPolygons(data, polygons); modelMeshData.MeshParts.Add(modelMeshPartData); modelMeshData.BoundingBox = (flag ? BoundingBox.Union(modelMeshData.BoundingBox, modelMeshPartData.BoundingBox) : modelMeshPartData.BoundingBox); flag = true; } return(modelMeshData); }
private static ModelBoneData LoadNode(ModelData data, ModelBoneData parentBoneData, Collada.ColladaNode node, Matrix transform) { ModelBoneData modelBoneData = new ModelBoneData(); data.Bones.Add(modelBoneData); modelBoneData.ParentBoneIndex = ((parentBoneData != null) ? data.Bones.IndexOf(parentBoneData) : -1); modelBoneData.Name = node.Name; modelBoneData.Transform = node.Transform * transform; foreach (Collada.ColladaNode node2 in node.Nodes) { Collada.LoadNode(data, modelBoneData, node2, Matrix.Identity); } foreach (Collada.ColladaGeometry geometry in node.Geometries) { Collada.LoadGeometry(data, modelBoneData, geometry); } return(modelBoneData); }
private static void SCModelWriter(MemoryStream memoryStream, FileStream fileStream) { ModelData modelData = Collada.Load(fileStream); ModelContentWriter.Write(memoryStream, modelData, Vector3.One); }
private static ModelMeshPartData LoadPolygons(ModelData data, Collada.ColladaPolygons polygons) { ModelMeshPartData modelMeshPartData = new ModelMeshPartData(); int num = 0; Dictionary <VertexElement, Collada.ColladaInput> dictionary = new Dictionary <VertexElement, Collada.ColladaInput>(); foreach (Collada.ColladaInput colladaInput in polygons.Inputs) { string str = (colladaInput.Set == 0) ? string.Empty : colladaInput.Set.ToString(CultureInfo.InvariantCulture); if (colladaInput.Semantic == "POSITION") { dictionary[new VertexElement(num, VertexElementFormat.Vector3, "POSITION" + str)] = colladaInput; num += 12; } else if (colladaInput.Semantic == "NORMAL") { dictionary[new VertexElement(num, VertexElementFormat.Vector3, "NORMAL" + str)] = colladaInput; num += 12; } else if (colladaInput.Semantic == "TEXCOORD") { dictionary[new VertexElement(num, VertexElementFormat.Vector2, "TEXCOORD" + str)] = colladaInput; num += 8; } else if (colladaInput.Semantic == "COLOR") { dictionary[new VertexElement(num, VertexElementFormat.NormalizedByte4, "COLOR" + str)] = colladaInput; num += 4; } } VertexDeclaration vertexDeclaration = new VertexDeclaration(Enumerable.ToArray <VertexElement>(dictionary.Keys)); ModelBuffersData modelBuffersData = Enumerable.FirstOrDefault <ModelBuffersData>(data.Buffers, (ModelBuffersData vd) => vd.VertexDeclaration == vertexDeclaration); if (modelBuffersData == null) { modelBuffersData = new ModelBuffersData(); data.Buffers.Add(modelBuffersData); modelBuffersData.VertexDeclaration = vertexDeclaration; } modelMeshPartData.BuffersDataIndex = data.Buffers.IndexOf(modelBuffersData); int num2 = polygons.P.Count / polygons.Inputs.Count; List <int> list = new List <int>(); if (polygons.VCount.Count == 0) { int num3 = 0; for (int i = 0; i < num2 / 3; i++) { list.Add(num3); list.Add(num3 + 2); list.Add(num3 + 1); num3 += 3; } } else { int num4 = 0; foreach (int num5 in polygons.VCount) { if (num5 == 3) { list.Add(num4); list.Add(num4 + 2); list.Add(num4 + 1); num4 += 3; } else { if (num5 != 4) { throw new NotSupportedException("Collada polygons with less than 3 or more than 4 vertices are not supported."); } list.Add(num4); list.Add(num4 + 2); list.Add(num4 + 1); list.Add(num4 + 2); list.Add(num4); list.Add(num4 + 3); num4 += 4; } } } int vertexStride = modelBuffersData.VertexDeclaration.VertexStride; int num6 = modelBuffersData.Vertices.Length; modelBuffersData.Vertices = Collada.ExtendArray <byte>(modelBuffersData.Vertices, list.Count * vertexStride); using (BinaryWriter binaryWriter = new BinaryWriter(new MemoryStream(modelBuffersData.Vertices, num6, list.Count * vertexStride))) { bool flag = false; foreach (KeyValuePair <VertexElement, Collada.ColladaInput> keyValuePair in dictionary) { VertexElement key = keyValuePair.Key; Collada.ColladaInput value = keyValuePair.Value; if (key.Semantic.StartsWith("POSITION")) { for (int j = 0; j < list.Count; j++) { float[] array = value.Source.Accessor.Source.Array; int offset = value.Source.Accessor.Offset; int stride = value.Source.Accessor.Stride; int num7 = polygons.P[list[j] * polygons.Inputs.Count + value.Offset]; binaryWriter.BaseStream.Position = (long)(j * vertexStride + key.Offset); float num8 = array[offset + stride * num7]; float num9 = array[offset + stride * num7 + 1]; float num10 = array[offset + stride * num7 + 2]; modelMeshPartData.BoundingBox = (flag ? BoundingBox.Union(modelMeshPartData.BoundingBox, new Vector3(num8, num9, num10)) : new BoundingBox(num8, num9, num10, num8, num9, num10)); flag = true; binaryWriter.Write(num8); binaryWriter.Write(num9); binaryWriter.Write(num10); } } else if (key.Semantic.StartsWith("NORMAL")) { for (int k = 0; k < list.Count; k++) { float[] array2 = value.Source.Accessor.Source.Array; int offset2 = value.Source.Accessor.Offset; int stride2 = value.Source.Accessor.Stride; int num11 = polygons.P[list[k] * polygons.Inputs.Count + value.Offset]; binaryWriter.BaseStream.Position = (long)(k * vertexStride + key.Offset); float num12 = array2[offset2 + stride2 * num11]; float num13 = array2[offset2 + stride2 * num11 + 1]; float num14 = array2[offset2 + stride2 * num11 + 2]; float num15 = 1f / (float)Math.Sqrt(num12 * num12 + num13 * num13 + num14 * num14); binaryWriter.Write(num15 * num12); binaryWriter.Write(num15 * num13); binaryWriter.Write(num15 * num14); } } else if (key.Semantic.StartsWith("TEXCOORD")) { for (int l = 0; l < list.Count; l++) { float[] array3 = value.Source.Accessor.Source.Array; int offset3 = value.Source.Accessor.Offset; int stride3 = value.Source.Accessor.Stride; int num16 = polygons.P[list[l] * polygons.Inputs.Count + value.Offset]; binaryWriter.BaseStream.Position = (long)(l * vertexStride + key.Offset); binaryWriter.Write(array3[offset3 + stride3 * num16]); binaryWriter.Write(1f - array3[offset3 + stride3 * num16 + 1]); } } else { if (!key.Semantic.StartsWith("COLOR")) { throw new Exception(); } for (int m = 0; m < list.Count; m++) { float[] array4 = value.Source.Accessor.Source.Array; int offset4 = value.Source.Accessor.Offset; int stride4 = value.Source.Accessor.Stride; int num17 = polygons.P[list[m] * polygons.Inputs.Count + value.Offset]; binaryWriter.BaseStream.Position = (long)(m * vertexStride + key.Offset); Color color = new Color(array4[offset4 + stride4 * num17], array4[offset4 + stride4 * num17 + 1], array4[offset4 + stride4 * num17 + 2], array4[offset4 + stride4 * num17 + 3]); binaryWriter.Write(color.PackedValue); } } } } modelMeshPartData.StartIndex = num6 / vertexStride; modelMeshPartData.IndicesCount = list.Count; return(modelMeshPartData); }
public static void WriteModel(Stream mainStream, Stream daeStream) { BinaryWriter binaryWriter = new BinaryWriter(mainStream, Encoding.UTF8, true); binaryWriter.Write(true); Matrix m = Matrix.CreateScale(new Vector3(1f, 1f, 1f)); ModelData modelData = Collada.Load(daeStream); binaryWriter.Write(modelData.Bones.Count); foreach (ModelBoneData current in modelData.Bones) { Matrix matrix = (current.ParentBoneIndex < 0) ? (current.Transform * m) : current.Transform; binaryWriter.Write(current.ParentBoneIndex); binaryWriter.Write(current.Name); binaryWriter.Write(matrix.M11); binaryWriter.Write(matrix.M12); binaryWriter.Write(matrix.M13); binaryWriter.Write(matrix.M14); binaryWriter.Write(matrix.M21); binaryWriter.Write(matrix.M22); binaryWriter.Write(matrix.M23); binaryWriter.Write(matrix.M24); binaryWriter.Write(matrix.M31); binaryWriter.Write(matrix.M32); binaryWriter.Write(matrix.M33); binaryWriter.Write(matrix.M34); binaryWriter.Write(matrix.M41); binaryWriter.Write(matrix.M42); binaryWriter.Write(matrix.M43); binaryWriter.Write(matrix.M44); } binaryWriter.Write(modelData.Meshes.Count); foreach (ModelMeshData current2 in modelData.Meshes) { binaryWriter.Write(current2.ParentBoneIndex); binaryWriter.Write(current2.Name); binaryWriter.Write(current2.MeshParts.Count); binaryWriter.Write(current2.BoundingBox.Min.X); binaryWriter.Write(current2.BoundingBox.Min.Y); binaryWriter.Write(current2.BoundingBox.Min.Z); binaryWriter.Write(current2.BoundingBox.Max.X); binaryWriter.Write(current2.BoundingBox.Max.Y); binaryWriter.Write(current2.BoundingBox.Max.Z); foreach (ModelMeshPartData current3 in current2.MeshParts) { binaryWriter.Write(current3.BuffersDataIndex); binaryWriter.Write(current3.StartIndex); binaryWriter.Write(current3.IndicesCount); binaryWriter.Write(current3.BoundingBox.Min.X); binaryWriter.Write(current3.BoundingBox.Min.Y); binaryWriter.Write(current3.BoundingBox.Min.Z); binaryWriter.Write(current3.BoundingBox.Max.X); binaryWriter.Write(current3.BoundingBox.Max.Y); binaryWriter.Write(current3.BoundingBox.Max.Z); } } binaryWriter.Write(modelData.Buffers.Count); foreach (ModelBuffersData current4 in modelData.Buffers) { binaryWriter.Write(current4.VertexDeclaration.VertexElements.Length); foreach (VertexElement current5 in current4.VertexDeclaration.VertexElements) { binaryWriter.Write(current5.Offset); binaryWriter.Write((int)current5.Format); binaryWriter.Write(current5.Semantic); } binaryWriter.Write(current4.Vertices.Length); binaryWriter.Write(current4.Vertices); binaryWriter.Write(current4.Indices.Length); binaryWriter.Write(current4.Indices); } }