Esempio n. 1
0
        public void RebuildMesh()
        {
            if (_mesh == null)
            {
                Debug.LogError("Mesh asset is missing.");
                return;
            }

            _mesh.Clear();

            var builder = new Emgen.IcosphereBuilder();

            for (var i = 0; i < _subdivisionLevel; i++)
            {
                builder.Subdivide();
            }

            var vc = builder.vertexCache;

            if (_splitVertices)
            {
                _mesh.vertices = vc.MakeVertexArrayForFlatMesh();
                _mesh.SetIndices(vc.MakeIndexArrayForFlatMesh(), MeshTopology.Triangles, 0);
            }
            else
            {
                _mesh.vertices = vc.MakeVertexArrayForSmoothMesh();
                _mesh.SetIndices(vc.MakeIndexArrayForSmoothMesh(), MeshTopology.Triangles, 0);
            }
            _mesh.RecalculateNormals();
        }
Esempio n. 2
0
        public void RebuildMesh()
        {
            if (_mesh == null)
            {
                Debug.LogError("Mesh asset is missing.");
                return;
            }

            _mesh.Clear();

            var builder = new Emgen.IcosphereBuilder();
            for (var i = 0; i < _subdivisionLevel; i++) builder.Subdivide();

            var vc = builder.vertexCache;
            if (_splitVertices)
            {
                _mesh.vertices = vc.MakeVertexArrayForFlatMesh();
                _mesh.SetIndices(vc.MakeIndexArrayForFlatMesh(), MeshTopology.Triangles, 0);
            }
            else
            {
                _mesh.vertices = vc.MakeVertexArrayForSmoothMesh();
                _mesh.SetIndices(vc.MakeIndexArrayForSmoothMesh(), MeshTopology.Triangles, 0);
            }
            _mesh.RecalculateNormals();
        }
Esempio n. 3
0
        public void RebuildMesh()
        {
            if (_mesh == null)
            {
                Debug.LogError("Mesh asset is missing.");
                return;
            }

            _mesh.Clear();

            var builder = new Emgen.IcosphereBuilder();

            for (var i = 0; i < _subdivisionLevel; i++)
            {
                builder.Subdivide();
            }

            var vcache  = builder.vertexCache;
            var vcount  = 3 * vcache.triangles.Count;
            var varray1 = new List <Vector3>(vcount); // vertex itself
            var varray2 = new List <Vector3>(vcount); // consecutive vertex
            var varray3 = new List <Vector3>(vcount); // another consecutive vertex

            foreach (var t in vcache.triangles)
            {
                var v1 = vcache.vertices[t.i1];
                var v2 = vcache.vertices[t.i2];
                var v3 = vcache.vertices[t.i3];

                varray1.Add(v1);
                varray2.Add(v2);
                varray3.Add(v3);

                varray1.Add(v2);
                varray2.Add(v3);
                varray3.Add(v1);

                varray1.Add(v3);
                varray2.Add(v1);
                varray3.Add(v2);
            }

            var iarray = new int[vcount * 2]; // index array for lines

            for (var vi = 0; vi < vcount; vi += 3)
            {
                var i = vi * 2;

                iarray[i++] = vi;
                iarray[i++] = vi + 1;

                iarray[i++] = vi + 1;
                iarray[i++] = vi + 2;

                iarray[i++] = vi + 2;
                iarray[i++] = vi;
            }

            _mesh.SetVertices(varray1);
            _mesh.SetNormals(varray1);
            _mesh.SetUVs(0, varray2);
            _mesh.SetUVs(1, varray3);

            _mesh.subMeshCount = 2;
            _mesh.SetIndices(vcache.MakeIndexArrayForFlatMesh(), MeshTopology.Triangles, 0);
            _mesh.SetIndices(iarray, MeshTopology.Lines, 1);

            _mesh.bounds = new Bounds(Vector3.zero, Vector3.one * 10);

            _mesh.Optimize();
            _mesh.UploadMeshData(true);
        }
Esempio n. 4
0
        public void RebuildMesh()
        {
            if (_mesh == null)
            {
                Debug.LogError("Mesh asset is missing.");
                return;
            }

            _mesh.Clear();

            var builder = new Emgen.IcosphereBuilder();
            for (var i = 0; i < _subdivisionLevel; i++)
                builder.Subdivide();

            var vcache = builder.vertexCache;
            var vcount = 3 * vcache.triangles.Count;
            var varray1 = new List<Vector3>(vcount); // vertex itself
            var varray2 = new List<Vector3>(vcount); // consecutive vertex
            var varray3 = new List<Vector3>(vcount); // another consecutive vertex

            foreach (var t in vcache.triangles)
            {
                var v1 = vcache.vertices[t.i1];
                var v2 = vcache.vertices[t.i2];
                var v3 = vcache.vertices[t.i3];

                varray1.Add(v1);
                varray2.Add(v2);
                varray3.Add(v3);

                varray1.Add(v2);
                varray2.Add(v3);
                varray3.Add(v1);

                varray1.Add(v3);
                varray2.Add(v1);
                varray3.Add(v2);
            }

            var iarray = new int[vcount * 2]; // index array for lines

            for (var vi = 0; vi < vcount; vi += 3)
            {
                var i = vi * 2;

                iarray[i++] = vi;
                iarray[i++] = vi + 1;

                iarray[i++] = vi + 1;
                iarray[i++] = vi + 2;

                iarray[i++] = vi + 2;
                iarray[i++] = vi;
            }

            _mesh.SetVertices(varray1);
            _mesh.SetNormals(varray1);
            _mesh.SetUVs(0, varray2);
            _mesh.SetUVs(1, varray3);

            _mesh.subMeshCount = 2;
            _mesh.SetIndices(vcache.MakeIndexArrayForFlatMesh(), MeshTopology.Triangles, 0);
            _mesh.SetIndices(iarray, MeshTopology.Lines, 1);

            _mesh.bounds = new Bounds(Vector3.zero, Vector3.one * 10);

            _mesh.Optimize();
            _mesh.UploadMeshData(true);
        }
Esempio n. 5
0
        /// Rebuild mesh structure
        /// This function constructs a mesh in a really slow and
        /// memory-intensive way. Never call this at runtime.
        public void RebuildMesh()
        {
            if (_mesh == null)
            {
                Debug.LogError("Mesh asset is missing.");
                return;
            }

            _mesh.Clear();

            // Build an icosphere with the given subdivision level.
            var builder = new Emgen.IcosphereBuilder();

            for (var i = 0; i < _subdivisionLevel; i++)
            {
                builder.Subdivide();
            }

            // Make vertex arrays.
            var vc     = builder.vertexCache;
            var vcount = 3 * vc.triangles.Count;
            var va1    = new List <Vector3>(vcount); // vertex position
            var va2    = new List <Vector3>(vcount); // previous vertex position
            var va3    = new List <Vector3>(vcount); // next vertex position

            foreach (var t in vc.triangles)
            {
                var v1 = vc.vertices[t.i1];
                var v2 = vc.vertices[t.i2];
                var v3 = vc.vertices[t.i3];

                va1.Add(v1);
                va2.Add(v2);
                va3.Add(v3);

                va1.Add(v2);
                va2.Add(v3);
                va3.Add(v1);

                va1.Add(v3);
                va2.Add(v1);
                va3.Add(v2);
            }

            // Index array for lines
            var lines = new List <int>(2 * vcount);

            for (var i = 0; i < vcount; i += 3)
            {
                lines.Add(i);
                lines.Add(i + 1);
                lines.Add(i + 1);
                lines.Add(i + 2);
                lines.Add(i + 2);
                lines.Add(i);
            }

            // Build a mesh asset.
            _mesh.SetVertices(va1);
            _mesh.SetUVs(0, va2);
            _mesh.SetUVs(1, va3);

            _mesh.subMeshCount = 2;
            _mesh.SetIndices(vc.MakeIndexArrayForFlatMesh(), MeshTopology.Triangles, 0);
            _mesh.SetIndices(lines.ToArray(), MeshTopology.Lines, 1);

            // We have no idea about the bounds, so use a magic number.
            _mesh.bounds = new Bounds(Vector3.zero, Vector3.one * 5);

            _mesh.Optimize();
            _mesh.UploadMeshData(true);
        }