public override void InitWithGeometry(Geometry geometry) { VertexUnit unit1 = geometry.VertexUnit(edge.id1); VertexUnit unit2 = geometry.VertexUnit(edge.id2); VertexLine unit3 = new VertexLine(unit1, unit2, ratio); unit3.preferredSign = sign; geometry.VertexUnitSetId(unit3, 0); units = new VertexUnit[] { unit3 }; List <GeoElement> elementList = new List <GeoElement>(); GeoVertex geoVertex = new GeoVertex(unit3); elementList.Add(geoVertex); // if (!geometry.IsEdge(id, id1)) // elementList.Add(new GeoEdge(id, id1)); // if (!geometry.IsEdge(id, id2)) // elementList.Add(new GeoEdge(id, id2)); elements = elementList.ToArray(); dependencies.Add(unit1); dependencies.Add(unit2); }
public virtual VertexLine[] ToLines() { VertexLine[] lines = new VertexLine[Vertices.Count]; for (int i = 0; i < Vertices.Count - 1; ++i) { lines[i] = new VertexLine(Vertices[i].HardCopy(), Vertices[i + 1].HardCopy()); } lines[Vertices.Count - 1] = new VertexLine(Vertices[Vertices.Count - 1].HardCopy(), Vertices[0].HardCopy()); return(lines); }
public static void DecodePrimitives( Geometry geo, Matrix4 bindMatrix, InfluenceDef[] infList, out VertexShaderDesc info, out List <VertexPrimitive> lines, out List <VertexPolygon> faces) { info = VertexShaderDesc.JustPositions(); lines = new List <VertexPrimitive>(); faces = new List <VertexPolygon>(); Source src; int boneCount = 0; if (infList != null) { HashSet <string> bones = new HashSet <string>(); foreach (InfluenceDef inf in infList) { for (int i = 0; i < inf.WeightCount; ++i) { bones.Add(inf.Weights[i].Bone); } } boneCount = bones.Count; } info.BoneCount = boneCount; var m = geo.MeshElement; if (m == null) { return; } Vertices vertsElem; foreach (var prim in m.PrimitiveElements) { Dictionary <ESemantic, int> semanticCounts = new Dictionary <ESemantic, int>(); Dictionary <ESemantic, Dictionary <int, Source> > inputSources = new Dictionary <ESemantic, Dictionary <int, Source> >(); Dictionary <ESemantic, Source> vertexInputSources = new Dictionary <ESemantic, Source>(); foreach (InputShared inp in prim.InputElements) { if (inp.CommonSemanticType == ESemantic.VERTEX) { vertsElem = inp.Source.GetElement <Vertices>(inp.Root); foreach (InputUnshared input in vertsElem.InputElements) { ESemantic semantic = input.CommonSemanticType; if (semanticCounts.ContainsKey(semantic)) { ++semanticCounts[semantic]; } else { semanticCounts.Add(semantic, 1); } src = input.Source.GetElement <Source>(vertsElem.Root); vertexInputSources[input.CommonSemanticType] = src; } continue; } else { ESemantic semantic = inp.CommonSemanticType; if (semanticCounts.ContainsKey(semantic)) { ++semanticCounts[semantic]; } else { semanticCounts.Add(semantic, 1); } src = inp.Source.GetElement <Source>(inp.Root); if (src != null) { if (!inputSources.ContainsKey(semantic)) { inputSources.Add(semantic, new Dictionary <int, Source>()); } int set = (int)inp.Set; if (!inputSources[semantic].ContainsKey(set)) { inputSources[semantic].Add(set, src); } else { inputSources[semantic][set] = src; } } } } info.MorphCount = 0; //Morphs are stored in separate geometry entries, so they need to be combined later info.HasNormals = semanticCounts.ContainsKey(ESemantic.NORMAL) && semanticCounts[ESemantic.NORMAL] > 0; bool hasTexBinormal = semanticCounts.ContainsKey(ESemantic.TEXBINORMAL) && semanticCounts[ESemantic.TEXBINORMAL] > 0; bool hasBinormal = semanticCounts.ContainsKey(ESemantic.BINORMAL) && semanticCounts[ESemantic.BINORMAL] > 0; info.HasBinormals = hasTexBinormal || hasBinormal; bool hasTexTangent = semanticCounts.ContainsKey(ESemantic.TEXTANGENT) && semanticCounts[ESemantic.TEXTANGENT] > 0; bool hasTangent = semanticCounts.ContainsKey(ESemantic.TANGENT) && semanticCounts[ESemantic.TANGENT] > 0; info.HasTangents = hasTexTangent || hasTangent; info.ColorCount = semanticCounts.ContainsKey(ESemantic.COLOR) ? semanticCounts[ESemantic.COLOR] : 0; info.TexcoordCount = semanticCounts.ContainsKey(ESemantic.TEXCOORD) ? semanticCounts[ESemantic.TEXCOORD] : 0; int maxSets = Math.Max(info.MorphCount + 1, Math.Max(info.ColorCount, info.TexcoordCount)); Vertex[][] vertices = new Vertex[prim.PointCount][]; int[] indices = prim?.IndicesElement?.StringContent?.Values; if (indices == null) { WriteLine("Mesh has no face indices. Mesh will be empty."); return; } Matrix4 invTranspBindMatrix = bindMatrix; if (info.HasNormals || info.HasBinormals || info.HasTangents) { invTranspBindMatrix.Invert(); invTranspBindMatrix.Transpose(); } foreach (var inp in prim.InputElements) { int set = (int)inp.Set; int offset = (int)inp.Offset; if (inp.CommonSemanticType == ESemantic.VERTEX) { foreach (ESemantic s in vertexInputSources.Keys) { src = vertexInputSources[s]; DecodeSource(src, s, offset, set, maxSets, prim, indices, vertices, infList, bindMatrix, invTranspBindMatrix); } } else { src = inputSources[inp.CommonSemanticType][set]; DecodeSource(src, inp.CommonSemanticType, offset, set, maxSets, prim, indices, vertices, infList, bindMatrix, invTranspBindMatrix); } } int setIndex = 0; switch (prim.Type) { case EColladaPrimitiveType.Lines: VertexLine[] linesTemp = new VertexLine[vertices.Length / 2]; for (int i = 0, x = 0; i < vertices.Length; i += 2, ++x) { linesTemp[x] = new VertexLine(vertices[i][setIndex], vertices[i + 1][setIndex]); } lines.AddRange(linesTemp); break; case EColladaPrimitiveType.Linestrips: lines.Add(new VertexLineStrip(false, vertices.Select(x => x[setIndex]).ToArray())); break; case EColladaPrimitiveType.Triangles: VertexTriangle[] tris = new VertexTriangle[vertices.Length / 3]; for (int i = 0, x = 0; i < vertices.Length; i += 3, ++x) { tris[x] = new VertexTriangle( vertices[i][setIndex], vertices[i + 1][setIndex], vertices[i + 2][setIndex]); } faces.AddRange(tris); break; case EColladaPrimitiveType.Trifans: faces.Add(new VertexTriangleFan(vertices.Select(x => x[setIndex]).ToArray())); break; case EColladaPrimitiveType.Tristrips: faces.Add(new VertexTriangleStrip(vertices.Select(x => x[setIndex]).ToArray())); break; case EColladaPrimitiveType.Polylist: Polylist polyListPrim = (Polylist)prim; PolyCounts countsElem = polyListPrim.PolyCountsElement; int[] counts = countsElem.StringContent.Values; VertexPolygon[] polys = new VertexPolygon[counts.Length]; for (int vtxIndex = 0, polyIndex = 0; polyIndex < counts.Length; ++polyIndex) { int count = counts[polyIndex]; Vertex[] verts = new Vertex[count]; for (int polyVtxIndex = 0; polyVtxIndex < count; ++polyVtxIndex, ++vtxIndex) { verts[polyVtxIndex] = vertices[vtxIndex][setIndex]; } polys[polyIndex] = new VertexPolygon(verts); } faces.AddRange(polys); break; default: case EColladaPrimitiveType.Polygons: WriteLine($"Primitive type '{prim.Type.ToString()}' not supported. Mesh will be empty."); break; } } }