Example #1
0
        /// <summary>
        /// Converts g3.SimpleMesh to UnityEngine.Mesh
        /// </summary>
        /// <param name="simpleMesh">SimpleMesh</param>
        /// <returns>UnityEngine.Mesh</returns>
        public static Mesh ToMesh(this SimpleMesh simpleMesh)
        {
            Mesh unityMesh = new Mesh();

            MeshTransforms.ConvertZUpToYUp(simpleMesh);
            Vector3[]     vertices = new Vector3[simpleMesh.VertexCount];
            Color[]       colors   = new Color[simpleMesh.VertexCount];
            Vector2[]     uvs      = new Vector2[simpleMesh.VertexCount];
            Vector3[]     normals  = new Vector3[simpleMesh.VertexCount];
            NewVertexInfo data;

            for (int i = 0; i < simpleMesh.VertexCount; i++)
            {
                data        = simpleMesh.GetVertexAll(i);
                vertices[i] = (Vector3)data.v;
                if (data.bHaveC)
                {
                    colors[i] = (Color)data.c;
                }
                if (data.bHaveUV)
                {
                    uvs[i] = (Vector2)data.uv;
                }
                if (data.bHaveN)
                {
                    normals[i] = (Vector3)data.n;
                }
            }
            unityMesh.vertices = vertices;
            if (simpleMesh.HasVertexColors)
            {
                unityMesh.colors = colors;
            }
            if (simpleMesh.HasVertexUVs)
            {
                unityMesh.uv = uvs;
            }
            if (simpleMesh.HasVertexNormals)
            {
                unityMesh.normals = normals;
            }
            int[] triangles = new int[simpleMesh.TriangleCount * 3];
            int   j         = 0;

            foreach (Index3i tri in simpleMesh.TrianglesItr())
            {
                triangles[j * 3]     = tri.a;
                triangles[j * 3 + 1] = tri.b;
                triangles[j * 3 + 2] = tri.c;
                j++;
            }
            unityMesh.triangles = triangles;
            return(unityMesh);
        }
Example #2
0
        public static Mesh ToStrideMesh(GraphicsDevice graphicsDevice, SimpleMesh g3Mesh, Vector3 offset, float scaling = 1f)
        {
            if (g3Mesh is null || g3Mesh.VertexCount == 0)
            {
                return(null);
            }

            var vertexDeclaration = GetVertexDeclaration(g3Mesh);

            var            vertices    = new byte[g3Mesh.VertexCount * vertexDeclaration.VertexStride];
            var            boundingBox = BoundingBox.Empty;
            BoundingSphere boundingSphere;

            unsafe
            {
                fixed(byte *ptr = vertices)
                {
                    byte *current = ptr;

                    for (int i = 0; i < g3Mesh.VertexCount; i++)
                    {
                        var vi = g3Mesh.GetVertexAll(i);
                        var p  = (new Vector3((float)vi.v.x, (float)vi.v.y, (float)vi.v.z) + offset) * scaling;
                        BoundingBox.Merge(ref boundingBox, ref p, out boundingBox);
                        Unsafe.Write(current, p);

                        current += sizeof(Vector3);
                        if (vi.bHaveN)
                        {
                            Unsafe.Write(current, vi.n);
                            current += sizeof(Vector3);
                        }
                        if (vi.bHaveUV)
                        {
                            Unsafe.Write(current, vi.uv);
                            current += sizeof(Vector2);
                        }
                        if (vi.bHaveC)
                        {
                            Unsafe.Write(current, new Color(vi.c.x, vi.c.y, vi.c.z));
                            current += sizeof(Color);
                        }
                    }

                    BoundingSphere.FromPoints((IntPtr)ptr, 0, g3Mesh.VertexCount, vertexDeclaration.VertexStride, out boundingSphere);
                }
            }

            var vertexBuffer = Buffer.New(graphicsDevice, vertices, vertexDeclaration.VertexStride, BufferFlags.VertexBuffer);
            var indexBuffer  = Buffer.Index.New(graphicsDevice, g3Mesh.Triangles.Reverse().ToArray());

            return(new Mesh()
            {
                Draw = new MeshDraw()
                {
                    VertexBuffers = new VertexBufferBinding[]
                    {
                        new VertexBufferBinding(vertexBuffer, vertexDeclaration, g3Mesh.VertexCount)
                    },
                    IndexBuffer = new IndexBufferBinding(indexBuffer, is32Bit: true, g3Mesh.Triangles.Length),
                    DrawCount = g3Mesh.Triangles.Length,
                    PrimitiveType = PrimitiveType.TriangleList
                },
                BoundingBox = boundingBox,
                BoundingSphere = boundingSphere
            });
        }