Ejemplo n.º 1
0
        public void Initialize()
        {
            var positions = new List <Vector3>();
            var indices   = new List <int>();

            _vd = VertexDescriptor.GetDescriptor <VertexP>(); // new VertexDescriptor(new VertexDeclaration(GGraphicDeviceFactory.Device, VertexP.Elements));

            _BuildGeoSphere(_subdivitions, 1, positions, indices);

            _vertices = new VertexP[positions.Count];
            for (int i = 0; i < positions.Count; i++)
            {
                _vertices[i] = new VertexP(positions[i]);
            }
            this._indices = new ushort[indices.Count];
            for (int i = 0; i < indices.Count; i++)
            {
                this._indices[i] = (ushort)indices[i];
            }

            if (_vb != null)
            {
                _vb.Dispose();
            }

            _vb = GraphicDeviceFactory.Device.CreateVertexBuffer(data: _vertices);

            if (_ib != null)
            {
                _ib.Dispose();
            }

            _ib = GraphicDeviceFactory.Device.CreateIndexBuffer(data: this._indices);
        }
        public static void CreateVertexBuffer(Mesh mesh, ref ComputeBuffer ret, ref int num_vertices, VertexFormat fmt = VertexFormat.Full)
        {
            int[] indices = mesh.GetIndices(0);

            switch (fmt)
            {
            case VertexFormat.P:
            {
                Vector3[] vertices = mesh.vertices;

                VertexP[] v = new VertexP[indices.Length];
                if (vertices != null)
                {
                    for (int i = 0; i < indices.Length; ++i)
                    {
                        v[i].vertex = vertices[indices[i]];
                    }
                }

                ret = new ComputeBuffer(indices.Length, VertexP.size);
                ret.SetData(v);
                num_vertices = v.Length;
            }
            break;

            case VertexFormat.PU:
            {
                Vector3[] vertices = mesh.vertices;
                Vector2[] uv       = mesh.uv;

                VertexPU[] v = new VertexPU[indices.Length];
                if (vertices != null)
                {
                    for (int i = 0; i < indices.Length; ++i)
                    {
                        v[i].vertex = vertices[indices[i]];
                    }
                }
                if (uv != null)
                {
                    for (int i = 0; i < indices.Length; ++i)
                    {
                        v[i].uv = uv[indices[i]];
                    }
                }

                ret = new ComputeBuffer(indices.Length, VertexPU.size);
                ret.SetData(v);
                num_vertices = v.Length;
            }
            break;

            case VertexFormat.PN:
            {
                Vector3[] vertices = mesh.vertices;
                Vector3[] normals  = mesh.normals;

                VertexPN[] v = new VertexPN[indices.Length];
                if (vertices != null)
                {
                    for (int i = 0; i < indices.Length; ++i)
                    {
                        v[i].vertex = vertices[indices[i]];
                    }
                }
                if (normals != null)
                {
                    for (int i = 0; i < indices.Length; ++i)
                    {
                        v[i].normal = normals[indices[i]];
                    }
                }

                ret = new ComputeBuffer(indices.Length, VertexPN.size);
                ret.SetData(v);
                num_vertices = v.Length;
            }
            break;

            case VertexFormat.Full:
            {
                Vector3[] vertices = mesh.vertices;
                Vector3[] normals  = mesh.normals;
                Vector4[] tangents = mesh.tangents;
                Vector2[] uv       = mesh.uv;

                VertexFull[] v = new VertexFull[indices.Length];
                if (vertices != null)
                {
                    for (int i = 0; i < indices.Length; ++i)
                    {
                        v[i].vertex = vertices[indices[i]];
                    }
                }
                if (normals != null)
                {
                    for (int i = 0; i < indices.Length; ++i)
                    {
                        v[i].normal = normals[indices[i]];
                    }
                }
                if (tangents != null)
                {
                    for (int i = 0; i < indices.Length; ++i)
                    {
                        v[i].tangent = tangents[indices[i]];
                    }
                }
                if (uv != null)
                {
                    for (int i = 0; i < indices.Length; ++i)
                    {
                        v[i].uv = uv[indices[i]];
                    }
                }

                ret = new ComputeBuffer(indices.Length, VertexFull.size);
                ret.SetData(v);
                num_vertices = v.Length;
            }
            break;
            }
        }