Ejemplo n.º 1
0
        public static DMesh3 ToDMesh3(this Mesh mesh)
        {
            var dMesh3 = new DMesh3();

            for (int i = 0; i < mesh.Vertices.Count; i++)
            {
                var vertex = mesh.Vertices[i];
                var normal = mesh.Normals[i];

                NewVertexInfo ni = new NewVertexInfo()
                {
                    v = new g3.Vector3d(vertex.X, vertex.Z, vertex.Y),
                    n = new g3.Vector3f(normal.X, normal.Z, normal.Y)
                };

                dMesh3.AppendVertex(ni);
            }

            foreach (var face in mesh.Faces)
            {
                dMesh3.AppendTriangle(face.A, face.B, face.C);
            }

            return(dMesh3);
        }
        public override bool BuildOnMesh(DMesh3Builder meshBuilder)
        {
            Vector3d v00 = Origin, v01 = Origin, v10 = Origin;

            v01.y += Height;
            v10   += AlongWidthDirection * Width;
            Vector3d v11 = v10;

            v11.y += Height;

            var vInfo = new NewVertexInfo
            {
                bHaveN = true,
                n      = new Vector3f(FrontNormal),
            };

            int i00 = AppentVertex(meshBuilder, vInfo, v00);
            int i01 = AppentVertex(meshBuilder, vInfo, v01);
            int i10 = AppentVertex(meshBuilder, vInfo, v10);
            int i11 = AppentVertex(meshBuilder, vInfo, v11);

            meshBuilder.AppendTriangle(i00, i11, i01);
            meshBuilder.AppendTriangle(i00, i10, i11);
            return(true);
        }
Ejemplo n.º 3
0
        public void Generate()
        {
            Append_mesh();

            AxisAlignedBox3i bounds = Voxels.GridBounds;

            bounds.Max -= Vector3i.One;

            int[] vertices = new int[4];

            foreach (Vector3i nz in Voxels.NonZeros())
            {
                Check_counts_or_append(6, 2);

                Box3d cube = Box3d.UnitZeroCentered;
                cube.Center = (Vector3D)nz;

                for (int fi = 0; fi < 6; ++fi)
                {
                    // checks dependent on neighbours
                    Index3i nbr = nz + gIndices.GridOffsets6[fi];
                    if (bounds.Contains(nbr))
                    {
                        if (SkipInteriorFaces && Voxels.Get(nbr))
                        {
                            continue;
                        }
                    }
                    else if (CapAtBoundary == false)
                    {
                        continue;
                    }


                    int           ni = gIndices.BoxFaceNormals[fi];
                    Vector3F      n  = (Vector3F)(Math.Sign(ni) * cube.Axis(Math.Abs(ni) - 1));
                    NewVertexInfo vi = new NewVertexInfo(Vector3D.Zero, n);
                    if (ColorSourceF != null)
                    {
                        vi.c      = ColorSourceF(nz);
                        vi.bHaveC = true;
                    }
                    for (int j = 0; j < 4; ++j)
                    {
                        vi.v        = cube.Corner(gIndices.BoxFaces[fi, j]);
                        vertices[j] = cur_mesh.AppendVertex(vi);
                    }

                    Index3i t0 = new Index3i(vertices[0], vertices[1], vertices[2], Clockwise);
                    Index3i t1 = new Index3i(vertices[0], vertices[2], vertices[3], Clockwise);
                    cur_mesh.AppendTriangle(t0);
                    cur_mesh.AppendTriangle(t1);
                }
            }
        }
Ejemplo n.º 4
0
        public static DMesh3 ToDMesh(this Mesh uMesh,
                                     bool bNorm = true,
                                     bool bUV   = false,
                                     bool bCol  = false)
        {
            int nV = uMesh.vertices.Length;

            bNorm &= (uMesh.normals != null && uMesh.normals.Length == nV);
            bUV   &= (uMesh.uv != null && uMesh.uv.Length == nV);
            bCol  &= (uMesh.colors != null && uMesh.colors.Length == nV);

            DMesh3 dMesh = new DMesh3(bNorm, bCol, bUV);

            for (int i = 0; i < nV; i++)
            {
                NewVertexInfo vi = new NewVertexInfo()
                {
                    v = uMesh.vertices[i]
                };
                if (bNorm)
                {
                    vi.bHaveN = true;
                    vi.n      = uMesh.normals[i];
                }
                if (bUV)
                {
                    vi.bHaveUV = true;
                    vi.uv      = uMesh.uv[i];
                }
                if (bUV)
                {
                    vi.bHaveC = true;
                    vi.c      = new Vector3f(uMesh.colors[i].r,
                                             uMesh.colors[i].g,
                                             uMesh.colors[i].b);
                }
                int vID = dMesh.AppendVertex(vi);
                Util.gDevAssert(vID == i);
            }

            int nT = uMesh.triangles.Length;

            for (int i = 0; i < nT; i += 3)
            {
                dMesh.AppendTriangle(uMesh.triangles[i],
                                     uMesh.triangles[i + 1],
                                     uMesh.triangles[i + 2]);
            }
            return(dMesh);
        }
Ejemplo n.º 5
0
        public void MakeMesh(NGonsCore.geometry3Sharp.mesh.DMesh3 m)
        {
            int nV = vertices.Count;

            for (int i = 0; i < nV; ++i)
            {
                NewVertexInfo ni = new NewVertexInfo()
                {
                    v = vertices[i]
                };
                if (WantNormals)
                {
                    ni.bHaveN = true;
                    ni.n      = normals[i];
                }
                if (WantUVs)
                {
                    ni.bHaveUV = true;
                    ni.uv      = uv[i];
                }
                int vID = m.AppendVertex(ni);
                Util.gDevAssert(vID == i);
            }
            int nT = triangles.Count;

            if (WantGroups && groups != null && groups.Length == nT)
            {
                for (int i = 0; i < nT; ++i)
                {
                    m.AppendTriangle(triangles[i], groups[i]);
                }
            }
            else
            {
                for (int i = 0; i < nT; ++i)
                {
                    m.AppendTriangle(triangles[i]);
                }
            }
        }
Ejemplo n.º 6
0
        int append_vertex(IMeshBuilder builder, Index3i vertIdx, bool bHaveNormals, bool bHaveColors, bool bHaveUVs)
        {
            int vi = 3 * vertIdx.a;

            if (vertIdx.a < 0 || vertIdx.a >= vPositions.Length / 3)
            {
                emit_warning("[OBJReader] append_vertex() referencing invalid vertex " + vertIdx.a.ToString());
                return(-1);
            }

            if (bHaveNormals == false && bHaveColors == false && bHaveUVs == false)
            {
                return(builder.AppendVertex(vPositions[vi], vPositions[vi + 1], vPositions[vi + 2]));
            }

            NewVertexInfo vinfo = new NewVertexInfo();

            vinfo.bHaveC = vinfo.bHaveN = vinfo.bHaveUV = false;
            vinfo.v      = new Vector3D(vPositions[vi], vPositions[vi + 1], vPositions[vi + 2]);
            if (bHaveNormals)
            {
                vinfo.bHaveN = true;
                int ni = 3 * vertIdx.b;
                vinfo.n = new Vector3F(vNormals[ni], vNormals[ni + 1], vNormals[ni + 2]);
            }
            if (bHaveColors)
            {
                vinfo.bHaveC = true;
                vinfo.c      = new Vector3F(vColors[vi], vColors[vi + 1], vColors[vi + 2]);
            }
            if (bHaveUVs)
            {
                vinfo.bHaveUV = true;
                int ui = 2 * vertIdx.c;
                vinfo.uv = new Vector2F(vUVs[ui], vUVs[ui + 1]);
            }

            return(builder.AppendVertex(vinfo));
        }
Ejemplo n.º 7
0
        public static UnityMesh ToUnityMesh(this DMesh3 dMesh,
                                            bool bNorm = true,
                                            bool bUV   = false,
                                            bool bCol  = false)
        {
            bNorm &= dMesh.HasVertexNormals;
            bUV   &= dMesh.HasVertexUVs;
            bCol  &= dMesh.HasVertexColors;

            int[] vertexMap   = new int[dMesh.VerticesBuffer.Length];
            int[] triangleMap = new int[dMesh.TrianglesBuffer.Length];
            int[] triangles   = new int[dMesh.TriangleCount * 3];

            List <Vector3d> vertices       = new List <Vector3d>();
            List <Vector3f> normals        = new List <Vector3f>();
            List <Vector2f> uv             = new List <Vector2f>();
            List <Colorf>   colors         = new List <Colorf>();
            List <int>      vertexUseCount = new List <int>();

            NewVertexInfo vInfo = new NewVertexInfo(new Vector3d(),
                                                    new Vector3f(),
                                                    new Vector3f(),
                                                    new Vector2f());

            IEnumerator e  = dMesh.TrianglesRefCounts.GetEnumerator();
            int         ti = 0;

            while (e.MoveNext())
            {
                int     iRef     = (int)e.Current;
                Index3i triangle = dMesh.GetTriangle(iRef);
                triangleMap[iRef] = ti;

                for (int i = 0; i < 3; i++)
                {
                    int vertIndex = triangle[i];
                    if (vertexMap[vertIndex] == 0)
                    {
                        vertexUseCount.Add(1);
                        dMesh.GetVertex(vertIndex, ref vInfo, bNorm, bCol, bUV);
                        vertices.Add(new Vector3f((float)vInfo.v.x,
                                                  (float)vInfo.v.y,
                                                  (float)vInfo.v.z));
                        vertexMap[vertIndex] = vertices.Count - 1;

                        if (bNorm)
                        {
                            normals.Add(vInfo.n);
                        }
                        if (bUV)
                        {
                            uv.Add(vInfo.uv);
                        }
                        if (bCol)
                        {
                            colors.Add(new Colorf(vInfo.c.x, vInfo.c.y, vInfo.c.z));
                        }
                    }
                    else
                    {
                        vertexUseCount[vertexMap[vertIndex]]++;
                    }

                    triangles[ti * 3 + i] = vertexMap[vertIndex];
                }
                ti++;
            }

            UnityMesh uMesh = new UnityMesh(vertexUseCount.ToArray(),
                                            triangles,
                                            vertices,
                                            normals,
                                            uv,
                                            colors);

            // Triangle normals and neighbors.
            e = dMesh.TrianglesRefCounts.GetEnumerator();
            while (e.MoveNext())
            {
                int   iRef      = (int)e.Current;
                int[] nb        = dMesh.GetTriNeighbourTris(iRef).array;
                int[] neighbors = new int[3];

                for (int i = 0; i < 3; i++)
                {
                    neighbors[i] = (nb[i] != -1) ? triangleMap[nb[i]] : -1;
                }
                uMesh.AddTriangleInfo(triangleMap[iRef],
                                      (Vector3f)dMesh.GetTriNormal(iRef),
                                      neighbors);
            }

            return(uMesh);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// If go has a MeshFilter, extract it and append to SimpleMesh.
        /// Returns false if no filter.
        /// </summary>
        bool AppendGOMesh(GameObject go, SimpleMesh m, int[] vertexMap, FScene scene, int gid)
        {
            MeshFilter filter = go.GetComponent <MeshFilter>();

            if (filter == null || filter.mesh == null)
            {
                return(false);
            }

            Mesh curMesh = filter.sharedMesh;

            Vector3[] vertices = curMesh.vertices;
            Vector3[] normals  = (WriteNormals) ? curMesh.normals : null;
            Color[]   colors   = (WriteVertexColors) ? curMesh.colors : null;
            Vector2[] uvs      = (WriteUVs) ? curMesh.uv : null;

            if (vertexMap.Length < curMesh.vertexCount)
            {
                vertexMap = new int[curMesh.vertexCount * 2];
            }

            for (int i = 0; i < curMesh.vertexCount; ++i)
            {
                NewVertexInfo vi = new NewVertexInfo();
                vi.bHaveN = WriteNormals; vi.bHaveC = WriteVertexColors; vi.bHaveUV = WriteUVs;

                Vector3f v = vertices[i];
                // local to world
                v = filter.gameObject.transform.TransformPoint(v);
                // world back to scene
                v    = scene.ToSceneP(v);
                vi.v = UnityUtil.SwapLeftRight(v);

                if (WriteNormals)
                {
                    Vector3 n = normals[i];
                    n    = filter.gameObject.transform.TransformDirection(n); // to world
                    n    = scene.ToSceneN(n);                                 // to scene
                    vi.n = UnityUtil.SwapLeftRight(n);
                }
                if (WriteVertexColors)
                {
                    vi.c = colors[i];
                }
                if (WriteUVs)
                {
                    vi.uv = uvs[i];
                }

                vertexMap[i] = m.AppendVertex(vi);
            }

            int[] triangles  = curMesh.triangles;
            int   nTriangles = triangles.Length / 3;

            for (int i = 0; i < nTriangles; ++i)
            {
                int a = vertexMap[triangles[3 * i]];
                int b = vertexMap[triangles[3 * i + 1]];
                int c = vertexMap[triangles[3 * i + 2]];
                m.AppendTriangle(a, c, b, gid);  // TRI ORIENTATION IS REVERSED HERE!!
            }

            return(true);
        }
Ejemplo n.º 9
0
        public static DMesh3 UnityMeshToDMesh(Mesh mesh, bool bSwapLeftright)
        {
            Vector3[] vertices = mesh.vertices;
            Vector3[] normals  = mesh.normals;
            Color32[] colors32 = mesh.colors32;
            Color[]   colors   = mesh.colors;
            Vector2[] uv       = mesh.uv;

            bool bNormals    = (normals.Length == mesh.vertexCount);
            bool bColors     = (colors.Length == mesh.vertexCount || colors32.Length == mesh.vertexCount);
            bool bByteColors = (colors32.Length == mesh.vertexCount);
            bool bUVs        = (uv.Length == mesh.vertexCount);

            DMesh3 dmesh = new DMesh3(bNormals, bColors, bUVs, false);

            for (int i = 0; i < mesh.vertexCount; ++i)
            {
                Vector3d v = vertices[i];
                if (bSwapLeftright)
                {
                    v.x = -v.x;
                    v.z = -v.z;
                }
                NewVertexInfo vInfo = new NewVertexInfo(v);
                if (bNormals)
                {
                    vInfo.bHaveN = true;
                    vInfo.n      = normals[i];
                    if (bSwapLeftright)
                    {
                        vInfo.n.x = -vInfo.n.x;
                        vInfo.n.z = -vInfo.n.z;
                    }
                }
                if (bColors)
                {
                    vInfo.bHaveC = true;
                    if (bByteColors)
                    {
                        vInfo.c = new Colorf(colors32[i].r, colors32[i].g, colors32[i].b, 255);
                    }
                    else
                    {
                        vInfo.c = colors[i];
                    }
                }
                if (bUVs)
                {
                    vInfo.bHaveUV = true;
                    vInfo.uv      = uv[i];
                }

                int vid = dmesh.AppendVertex(vInfo);
                if (vid != i)
                {
                    throw new InvalidOperationException("UnityUtil.UnityMeshToDMesh: indices weirdness...");
                }
            }

            int[] triangles = mesh.triangles;
            for (int i = 0; i < triangles.Length / 3; ++i)
            {
                dmesh.AppendTriangle(triangles[3 * i], triangles[3 * i + 1], triangles[3 * i + 2]);
            }

            return(dmesh);
        }
 public int AppentVertex(DMesh3Builder meshBuilder, NewVertexInfo vInfo, Vector3d vertex)
 {
     vInfo.v = vertex;
     return(meshBuilder.AppendVertex(vInfo));
 }
Ejemplo n.º 11
0
        public ExportStatus Export(FScene s, string filename)
        {
            List <WriteMesh> vMeshes = new List <WriteMesh>();

            if (WriteFaceGroups)
            {
                throw new Exception("SceneMeshExporter.Export: writing face groups has not yet been implemented!");
            }

            foreach (SceneObject so in s.SceneObjects)
            {
                if (so.IsTemporary)
                {
                    continue;
                }

                SimpleMesh m = new SimpleMesh();
                m.Initialize(WriteNormals, WriteVertexColors, WriteUVs, WriteFaceGroups);
                int groupCounter = 1;

                GameObject rootgo = so.RootGameObject;

                int[] vertexMap = new int[2048];
                foreach (GameObject childgo in rootgo.Children())
                {
                    MeshFilter filter = childgo.GetComponent <MeshFilter>();
                    if (filter == null || filter.mesh == null)
                    {
                        continue;
                    }
                    if (GOFilterF != null && GOFilterF(so, childgo) == false)
                    {
                        continue;
                    }

                    Mesh      curMesh  = filter.sharedMesh;
                    Vector3[] vertices = curMesh.vertices;
                    Vector3[] normals  = (WriteNormals) ? curMesh.normals : null;
                    Color[]   colors   = (WriteVertexColors) ? curMesh.colors : null;
                    Vector2[] uvs      = (WriteUVs) ? curMesh.uv : null;

                    if (vertexMap.Length < curMesh.vertexCount)
                    {
                        vertexMap = new int[curMesh.vertexCount * 2];
                    }

                    for (int i = 0; i < curMesh.vertexCount; ++i)
                    {
                        NewVertexInfo vi = new NewVertexInfo();
                        vi.bHaveN = WriteNormals; vi.bHaveC = WriteVertexColors; vi.bHaveUV = WriteUVs;

                        Vector3 v = vertices[i];
                        // local to world
                        v = filter.gameObject.transform.TransformPoint(v);
                        // world back to scene
                        vi.v = UnityUtil.SwapLeftRight(s.RootGameObject.transform.InverseTransformPoint(v));

                        if (WriteNormals)
                        {
                            Vector3 n = normals[i];
                            n    = filter.gameObject.transform.TransformDirection(n);
                            vi.n = UnityUtil.SwapLeftRight(s.RootGameObject.transform.InverseTransformDirection(n));
                        }
                        if (WriteVertexColors)
                        {
                            vi.c = colors[i];
                        }
                        if (WriteUVs)
                        {
                            vi.uv = uvs[i];
                        }

                        vertexMap[i] = m.AppendVertex(vi);
                    }

                    int[] triangles  = curMesh.triangles;
                    int   nTriangles = triangles.Length / 3;
                    for (int i = 0; i < nTriangles; ++i)
                    {
                        int a = vertexMap[triangles[3 * i]];
                        int b = vertexMap[triangles[3 * i + 1]];
                        int c = vertexMap[triangles[3 * i + 2]];
                        m.AppendTriangle(a, c, b, groupCounter);  // TRI ORIENTATION IS REVERSED HERE!!
                    }
                    groupCounter++;
                }

                vMeshes.Add(new WriteMesh(m, so.Name));
            }


            if (WriteInBackgroundThreads)
            {
                ExportStatus status = new ExportStatus()
                {
                    Exporter = this, IsComputing = true
                };
                WriteOptions useOptions = Options;
                useOptions.ProgressFunc = (cur, max) => {
                    status.Progress    = cur;
                    status.MaxProgress = max;
                };
                BackgroundWriteThread t = new BackgroundWriteThread()
                {
                    Meshes      = vMeshes, options = useOptions, Filename = filename,
                    CompletionF = (result) => {
                        LastWriteStatus         = result.code;
                        LastErrorMessage        = result.message;
                        status.LastErrorMessage = result.message;
                        status.Ok          = (result.code == IOCode.Ok);
                        status.IsComputing = false;
                    }
                };
                t.Start();
                return(status);
            }
            else
            {
                IOWriteResult result = StandardMeshWriter.WriteFile(filename, vMeshes, Options);
                LastWriteStatus  = result.code;
                LastErrorMessage = result.message;
                return(new ExportStatus()
                {
                    Exporter = this, IsComputing = false,
                    Ok = (result.code == IOCode.Ok),
                    LastErrorMessage = result.message
                });
            }
        }
Ejemplo n.º 12
0
        public virtual DMesh3 RestoreDMesh(TypedAttribSet attributes)
        {
            bool           is_compressed = false;
            TypedAttribSet meshAttr      = find_struct(attributes, IOStrings.BinaryDMeshStruct);

            if (meshAttr == null)
            {
                meshAttr      = find_struct(attributes, IOStrings.CompressedDMeshStruct);
                is_compressed = true;
            }
            if (meshAttr == null)
            {
                throw new Exception("SOFactory.RestoreDMesh: DMesh binary or compressed struct not found!");
            }

            VectorArray3d v = null;
            VectorArray3f n = null, c = null;
            VectorArray2f uv = null;

            VectorArray3i t = null;

            int[] g = null;

            IndexArray4i e = null;

            short[] e_ref = null;

            var storageMode = IOStrings.MeshStorageMode.EdgeRefCounts;

            if (meshAttr.ContainsKey(IOStrings.AMeshStorageMode))
            {
                storageMode = (IOStrings.MeshStorageMode)(int) meshAttr[IOStrings.AMeshStorageMode];
            }

            if (is_compressed)
            {
                if (check_key_or_debug_print(meshAttr, IOStrings.AMeshVertices3Compressed))
                {
                    v = meshAttr[IOStrings.AMeshVertices3Compressed] as VectorArray3d;
                }
                if (check_key_or_debug_print(meshAttr, IOStrings.AMeshNormals3Compressed))
                {
                    n = meshAttr[IOStrings.AMeshNormals3Compressed] as VectorArray3f;
                }
                if (check_key_or_debug_print(meshAttr, IOStrings.AMeshColors3Compressed))
                {
                    c = meshAttr[IOStrings.AMeshColors3Compressed] as VectorArray3f;
                }
                if (check_key_or_debug_print(meshAttr, IOStrings.AMeshUVs2Compressed))
                {
                    uv = meshAttr[IOStrings.AMeshUVs2Compressed] as VectorArray2f;
                }

                if (check_key_or_debug_print(meshAttr, IOStrings.AMeshTrianglesCompressed))
                {
                    t = meshAttr[IOStrings.AMeshTrianglesCompressed] as VectorArray3i;
                }
                if (check_key_or_debug_print(meshAttr, IOStrings.AMeshTriangleGroupsCompressed))
                {
                    g = meshAttr[IOStrings.AMeshTriangleGroupsCompressed] as int[];
                }

                if (check_key_or_debug_print(meshAttr, IOStrings.AMeshEdgesCompressed))
                {
                    e = meshAttr[IOStrings.AMeshEdgesCompressed] as IndexArray4i;
                }
                if (check_key_or_debug_print(meshAttr, IOStrings.AMeshEdgeRefCountsCompressed))
                {
                    e_ref = meshAttr[IOStrings.AMeshEdgeRefCountsCompressed] as short[];
                }
            }
            else
            {
                if (check_key_or_debug_print(meshAttr, IOStrings.AMeshVertices3Binary))
                {
                    v = meshAttr[IOStrings.AMeshVertices3Binary] as VectorArray3d;
                }
                if (check_key_or_debug_print(meshAttr, IOStrings.AMeshNormals3Binary))
                {
                    n = meshAttr[IOStrings.AMeshNormals3Binary] as VectorArray3f;
                }
                if (check_key_or_debug_print(meshAttr, IOStrings.AMeshColors3Binary))
                {
                    c = meshAttr[IOStrings.AMeshColors3Binary] as VectorArray3f;
                }
                if (check_key_or_debug_print(meshAttr, IOStrings.AMeshUVs2Binary))
                {
                    uv = meshAttr[IOStrings.AMeshUVs2Binary] as VectorArray2f;
                }

                if (check_key_or_debug_print(meshAttr, IOStrings.AMeshTrianglesBinary))
                {
                    t = meshAttr[IOStrings.AMeshTrianglesBinary] as VectorArray3i;
                }
                if (check_key_or_debug_print(meshAttr, IOStrings.AMeshTriangleGroupsBinary))
                {
                    g = meshAttr[IOStrings.AMeshTriangleGroupsBinary] as int[];
                }

                if (check_key_or_debug_print(meshAttr, IOStrings.AMeshEdgesBinary))
                {
                    e = meshAttr[IOStrings.AMeshEdgesBinary] as IndexArray4i;
                }
                if (check_key_or_debug_print(meshAttr, IOStrings.AMeshEdgeRefCountsBinary))
                {
                    e_ref = meshAttr[IOStrings.AMeshEdgeRefCountsBinary] as short[];
                }
            }

            DMesh3 m = new DMesh3();

            if (n != null)
            {
                m.EnableVertexNormals(Vector3f.Zero);
            }
            if (c != null)
            {
                m.EnableVertexColors(Vector3f.Zero);
            }
            if (uv != null)
            {
                m.EnableVertexUVs(Vector2f.Zero);
            }
            if (g != null)
            {
                m.EnableTriangleGroups(0);
            }

            if (storageMode == IOStrings.MeshStorageMode.EdgeRefCounts)
            {
                if (v == null || t == null || e == null || e_ref == null)
                {
                    return(null);
                }

                m.VerticesBuffer = new DVector <double>(v);
                if (n != null)
                {
                    m.NormalsBuffer = new DVector <float>(n);
                }
                if (c != null)
                {
                    m.ColorsBuffer = new DVector <float>(c);
                }
                if (uv != null)
                {
                    m.UVBuffer = new DVector <float>(uv);
                }
                m.TrianglesBuffer = new DVector <int>(t);
                if (g != null)
                {
                    m.GroupsBuffer = new DVector <int>(g);
                }

                m.EdgesBuffer    = new DVector <int>(e);
                m.EdgesRefCounts = new RefCountVector(e_ref);
                m.RebuildFromEdgeRefcounts();
            }
            else if (storageMode == IOStrings.MeshStorageMode.Minimal)
            {
                if (v == null || t == null)
                {
                    return(null);
                }

                int           NV    = v.Count;
                NewVertexInfo vinfo = new NewVertexInfo();
                for (int k = 0; k < NV; ++k)
                {
                    vinfo.v = v[k];
                    if (n != null)
                    {
                        vinfo.n = n[k];
                    }
                    if (c != null)
                    {
                        vinfo.c = c[k];
                    }
                    if (uv != null)
                    {
                        vinfo.uv = uv[k];
                    }
                    m.AppendVertex(ref vinfo);
                }

                int NT = t.Count;
                for (int k = 0; k < NT; ++k)
                {
                    Vector3i tri  = t[k];
                    int      setg = (g == null) ? -1 : g[k];
                    m.AppendTriangle(tri, setg);
                }
            }
            else
            {
                throw new Exception("SOFactory.RestoreDMesh: unsupported mesh storage mode");
            }

            return(m);
        }
Ejemplo n.º 13
0
        public static SimpleMesh UnityMeshToSimpleMesh(UnityEngine.Mesh mesh, bool bSwapLeftRight)
        {
            if (bSwapLeftRight)
            {
                throw new Exception("[RMSNOTE] I think this conversion is wrong, see MeshTransforms.SwapLeftRight. Just want to know if this code is ever hit.");
            }

            SimpleMesh smesh = new SimpleMesh();

            Vector3[] vertices = mesh.vertices;
            Vector3[] normals  = mesh.normals;
            Color32[] colors32 = mesh.colors32;
            Color[]   colors   = mesh.colors;
            Vector2[] uv       = mesh.uv;

            bool bNormals    = (normals.Length == mesh.vertexCount);
            bool bColors     = (colors.Length == mesh.vertexCount || colors32.Length == mesh.vertexCount);
            bool bByteColors = (colors32.Length == mesh.vertexCount);
            bool bUVs        = (uv.Length == mesh.vertexCount);

            smesh.Initialize(bNormals, bColors, bUVs, false);


            for (int i = 0; i < mesh.vertexCount; ++i)
            {
                Vector3d v = vertices[i];
                if (bSwapLeftRight)
                {
                    v.x = -v.x;
                    v.z = -v.z;
                }
                NewVertexInfo vInfo = new NewVertexInfo(v);
                if (bNormals)
                {
                    vInfo.bHaveN = true;
                    vInfo.n      = normals[i];
                    if (bSwapLeftRight)
                    {
                        vInfo.n.x = -vInfo.n.x;
                        vInfo.n.z = -vInfo.n.z;
                    }
                }
                if (bColors)
                {
                    vInfo.bHaveC = true;
                    if (bByteColors)
                    {
                        vInfo.c = new Colorf(colors32[i].r, colors32[i].g, colors32[i].b, 255);
                    }
                    else
                    {
                        vInfo.c = colors[i];
                    }
                }
                if (bUVs)
                {
                    vInfo.bHaveUV = true;
                    vInfo.uv      = uv[i];
                }

                int vid = smesh.AppendVertex(vInfo);
                if (vid != i)
                {
                    throw new InvalidOperationException("UnityUtil.UnityMeshToSimpleMesh: indices weirdness...");
                }
            }

            int[] triangles = mesh.triangles;
            for (int i = 0; i < triangles.Length / 3; ++i)
            {
                smesh.AppendTriangle(triangles[3 * i], triangles[3 * i + 1], triangles[3 * i + 2]);
            }

            return(smesh);
        }