//private static Float3 GetFloat3(int index, int stride, Input input, int[] poligonArray) //{ // return input.SourceData.GetFloat3(poligonArray[index * stride + input.Offset]); //} private void CopySkin(Scene scene, XElement instance, MeshSkinAndMaterials sam) { var bind = instance.Element(this.schema.bindMaterialName); if (bind != null) { var technique_common = bind.Element(this.schema.techniqueCommonName); if (technique_common != null) { var ms = new MaterialSet(); foreach (var instanceMaterial in technique_common.Elements(this.schema.instanceMaterialName)) { var symbol = instanceMaterial.AttributeValue(this.schema.symbolName); var target = instanceMaterial.AttributeValue(this.schema.targetName); var matId = this.collada.Element(this.schema.libraryMaterialsName).ElementByUrl(target).AttributeValue(this.schema.idName); ms.Add(symbol, (from m in scene.Materials where m.Id == matId select m).First()); } sam.Materials.Add(ms); } } }
private void ParseTriangles( XElement mesh, MeshSkinAndMaterials skinAndMaterials, SeparateStreamsMesh dstMesh, XElement element) { var meshInputs = this.ParseInputs(element); int[] p = new int[meshInputs.Count * meshInputs.Stride * 3]; this.ParseIntArray(element.ElementValue(this.schema.pName), p); var subMesh = dstMesh.CreateSubmesh(); subMesh.Material = skinAndMaterials.GetAnyMaterialFor(element.AttributeValue(this.schema.materialAttributeName)); subMesh.VertexSourceType = VertexSourceType.TriangleList; var streamList = StreamListOrderedByOffset(meshInputs, subMesh); for (int polygonIndex = 0; polygonIndex < meshInputs.Count; ++polygonIndex) { AddVertex(p, polygonIndex*3, streamList, meshInputs.Stride); AddVertex(p, polygonIndex * 3+1, streamList, meshInputs.Stride); AddVertex(p, polygonIndex * 3+2, streamList, meshInputs.Stride); } }
private void ParsePolylist( XElement mesh, MeshSkinAndMaterials skinAndMaterials, SeparateStreamsMesh dstMesh, XElement element) { var meshInputs = this.ParseInputs(element); int[] vcount = new int[meshInputs.Count]; this.ParseIntArray(element.ElementValue(this.schema.vcountName), vcount); var points = vcount.Sum(a => a); var firstSize = vcount[0]; bool isAllTheSame = vcount.All(i => i == firstSize); int[] p = new int[points * meshInputs.Stride]; this.ParseIntArray(element.ElementValue(this.schema.pName), p); var subMesh = dstMesh.CreateSubmesh(); subMesh.Material = skinAndMaterials.GetAnyMaterialFor(element.AttributeValue(this.schema.materialAttributeName)); var streamList = StreamListOrderedByOffset(meshInputs, subMesh); //if (isAllTheSame) //{ // if (firstSize == 3) // { // subMesh.VertexSourceType = VertexSourceType.TrianleList; // this.ParseElements(p, subMesh, meshInputs, points); // return; // } // if (firstSize == 4) // { // subMesh.VertexSourceType = VertexSourceType.QuadList; // this.ParseElements(p, subMesh, meshInputs, points); // return; // } //} subMesh.VertexSourceType = VertexSourceType.TriangleList; int startIndex = 0; for (int polygonIndex = 0; polygonIndex < vcount.Length; ++polygonIndex) { for (int i = 2; i < vcount[polygonIndex]; i++) { AddVertex(p, 0, streamList, meshInputs.Stride); AddVertex(p, i-1, streamList, meshInputs.Stride); AddVertex(p, i , streamList, meshInputs.Stride); } startIndex += vcount[polygonIndex]; } }
private void ParsePolygons( MeshSkinAndMaterials skinAndMaterials, SeparateStreamsMesh dstMesh, XElement element) { var meshInputs = this.ParseInputs(element); var subMesh = dstMesh.CreateSubmesh(); subMesh.Material = skinAndMaterials.GetAnyMaterialFor(element.AttributeValue(this.schema.materialAttributeName)); subMesh.VertexSourceType = VertexSourceType.TriangleList; var streamList = StreamListOrderedByOffset(meshInputs, subMesh); foreach (var polygon in element.Elements(this.schema.pName)) { var list = this.ParseIntList(polygon.Value); int numElements = list.Count / meshInputs.Stride; for (int i=1; i<numElements-1; ++i) { AddVertex(list, 0, streamList, meshInputs.Stride); AddVertex(list, i, streamList, meshInputs.Stride); AddVertex(list, i+1, streamList, meshInputs.Stride); } } }
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); } } }
private MeshSkinAndMaterials FindContext(Scene scene, XElement geometry) { var sam = new MeshSkinAndMaterials(); var geometryId = "#" + geometry.AttributeValue(this.schema.idName); var controllers = this.collada.Element(this.schema.libraryControllersName); if (controllers != null) { sam.SkinController = (from controller in controllers.Elements(this.schema.controllerName) let skin = controller.Element(this.schema.skinName) where skin != null && skin.AttributeValue(this.schema.sourceAttributeName) == geometryId select controller).FirstOrDefault(); } string controllerId = "#"; if (sam.SkinController != null) { controllerId = "#" + sam.SkinController.AttributeValue(this.schema.idName); } var libScenes = this.collada.Element(this.schema.libraryVisualScenesName); if (libScenes != null) { foreach (var visualScene in libScenes.Elements(this.schema.visualSceneName)) { foreach (var node in visualScene.Descendants(this.schema.nodeName)) { var instanceGeo = node.Element(this.schema.instanceGeometryName); if (instanceGeo != null && instanceGeo.AttributeValue(this.schema.urlName) == geometryId) { this.CopySkin(scene, instanceGeo, sam); } var instanceCtrl = node.Element(this.schema.instanceControllerName); if (instanceCtrl != null && instanceCtrl.AttributeValue(this.schema.urlName) == controllerId) { this.CopySkin(scene, instanceCtrl, sam); } } } } return sam; }