示例#1
0
        public static PlotlyScene ToPlotly(this Schema2.Mesh mesh)
        {
            var dstScene = new PlotlyScene();

            dstScene.AppendTriangles(mesh.EvaluateTriangles(), GetMaterialColor);
            return(dstScene);
        }
示例#2
0
        private static IEnumerable <SRCPRIM> _GetValidPrimitives(SRCMESH srcMesh)
        {
            foreach (var srcPrim in srcMesh.Primitives)
            {
                var ppp = srcPrim.GetVertexAccessor("POSITION");
                if (ppp.Count < 3)
                {
                    continue;
                }

                if (srcPrim.DrawPrimitiveType == Schema2.PrimitiveType.POINTS)
                {
                    continue;
                }
                if (srcPrim.DrawPrimitiveType == Schema2.PrimitiveType.LINES)
                {
                    continue;
                }
                if (srcPrim.DrawPrimitiveType == Schema2.PrimitiveType.LINE_LOOP)
                {
                    continue;
                }
                if (srcPrim.DrawPrimitiveType == Schema2.PrimitiveType.LINE_STRIP)
                {
                    continue;
                }

                yield return(srcPrim);
            }
        }
示例#3
0
        internal void _WriteMesh(SRCMESH srcMesh)
        {
            if (_Device == null)
            {
                throw new InvalidOperationException();
            }

            var srcPrims = _GetValidPrimitives(srcMesh)
                           .ToDictionary(item => item, item => new MeshPrimitiveReader(item, item.Material?.DoubleSided ?? false));

            VertexNormalsFactory.CalculateSmoothNormals(srcPrims.Values.ToList());
            VertexTangentsFactory.CalculateTangents(srcPrims.Values.ToList());

            foreach (var srcPrim in srcPrims)
            {
                _CurrentMeshIndex = srcMesh.LogicalIndex;

                _WriteMeshPrimitive(srcPrim.Value, srcPrim.Key.Material);
            }
        }
示例#4
0
            public static MeshData FromGltfMesh(GLTF.Mesh mesh)
            {
                var vcount = mesh.Primitives.Select(prim => prim.VertexAccessors["POSITION"].Count).Sum();

                if (vcount >= ushort.MaxValue)
                {
                    throw new Exception($"Too many vertices ({vcount}) in mesh {mesh.Name}. Limit is 65535");
                }

                var attribsUsed = mesh.Primitives.First().VertexAccessors.Select(i => i.Key).OrderBy(i => i);
                var ok          = mesh.Primitives.Select(i => i.VertexAccessors.Keys.OrderBy(j => j)).Aggregate(true, (acc, curr) => acc && curr.SequenceEqual(attribsUsed));

                if (!ok)
                {
                    throw new Exception("Vertex attributes not consistent between Primitives. Diesel cannot represent this.");
                }

                var ms = new MeshData();

                ms.materials = mesh.Primitives.Select(i => i.Material?.Name ?? "Material: Default Material").Distinct().ToList();

                uint currentBaseVertex = 0;
                uint currentBaseIndex  = 0;

                foreach (var prim in mesh.Primitives)
                {
                    var vertices  = GetVerticesFromPrimitive(prim).ToList();
                    var primFaces = prim.GetTriangleIndices().ToList();
                    var matname   = prim.Material?.Name ?? "Material: Default Material";

                    var ra = new DM.RenderAtom
                    {
                        BaseIndex     = currentBaseIndex,
                        BaseVertex    = currentBaseVertex,
                        MaterialId    = (uint)ms.materials.IndexOf(matname),
                        TriangleCount = (uint)primFaces.Count
                    };

                    var vertexIds = new Dictionary <Vertex, int>();
                    foreach (var(A, B, C) in primFaces)
                    {
                        var vtxA = vertices[A];
                        var vtxB = vertices[B];
                        var vtxC = vertices[C];

                        if (!vertexIds.ContainsKey(vtxA))
                        {
                            vertexIds[vtxA] = ms.AppendVertex(vtxA);
                        }
                        if (!vertexIds.ContainsKey(vtxB))
                        {
                            vertexIds[vtxB] = ms.AppendVertex(vtxB);
                        }
                        if (!vertexIds.ContainsKey(vtxC))
                        {
                            vertexIds[vtxC] = ms.AppendVertex(vtxC);
                        }

                        var df = new DM.Face(
                            (ushort)vertexIds[vtxA],
                            (ushort)vertexIds[vtxB],
                            (ushort)vertexIds[vtxC]
                            );

                        ms.faces.Add(df);
                    }

                    ra.GeometrySliceLength = (uint)vertexIds.Count;
                    ms.renderAtoms.Add(ra);

                    currentBaseIndex  += ra.TriangleCount * 3;
                    currentBaseVertex += ra.GeometrySliceLength;
                }
                return(ms);
            }