Ejemplo n.º 1
0
        /// <summary>
        /// return unity mesh from this tree
        /// </summary>
        /// <returns>unity mesh</returns>
        public Mesh ToMesh()
        {
            var mesh = new Mesh();

            var polygons = root.AllPolygons();

            var triCount = polygons.Count * 3 * 2;

            var triangles0 = new List <int>(triCount);
            var triangles1 = new List <int>(triCount);
            var vertices   = new List <Vector3>(triCount);
            var normals    = new List <Vector3>(triCount);
            var uvs        = new List <Vector2>(triCount);

            var vertCache = new Dictionary <int, int>(triCount);
            var hash      = new VertexHash(0.001f, triCount * 3 * 2);

            var vertIdx = 0;

            foreach (var polygon in polygons)
            {
                var polyPoints = polygon.Vertices.Count;

                for (var i = 2; i < polyPoints; i++)
                {
                    var v0 = AddVertex(polygon.Vertices[0], vertices, normals, uvs, vertCache, hash);
                    var v1 = AddVertex(polygon.Vertices[i - 1], vertices, normals, uvs, vertCache, hash);
                    var v2 = AddVertex(polygon.Vertices[i], vertices, normals, uvs, vertCache, hash);

                    if (polygon.Id == 0)
                    {
                        triangles0.Add(v0);
                        triangles0.Add(v1);
                        triangles0.Add(v2);
                    }
                    else
                    {
                        triangles1.Add(v0);
                        triangles1.Add(v1);
                        triangles1.Add(v2);
                    }

                    vertIdx += 3;
                }
            }

            mesh.vertices     = vertices.ToArray();
            mesh.normals      = normals.ToArray();
            mesh.uv           = uvs.ToArray();
            mesh.subMeshCount = 2;
            mesh.SetTriangles(triangles0.ToArray(), 0);
            mesh.SetTriangles(triangles1.ToArray(), 1);

            mesh.RecalculateNormals();

            ;
            mesh.RecalculateBounds();

            return(mesh);
        }
Ejemplo n.º 2
0
        // Token: 0x06004170 RID: 16752 RVA: 0x0014A52C File Offset: 0x0014892C
        public Mesh ToMesh()
        {
            Mesh mesh = new Mesh();
            List <CSGPolygon> list      = this.root.AllPolygons();
            int                   num   = list.Count * 3 * 2;
            List <int>            list2 = new List <int>(num);
            List <int>            list3 = new List <int>(num);
            List <Vector3>        list4 = new List <Vector3>(num);
            List <Vector3>        list5 = new List <Vector3>(num);
            List <Vector2>        list6 = new List <Vector2>(num);
            Dictionary <int, int> cache = new Dictionary <int, int>(num);
            VertexHash            hash  = new VertexHash(0.001f, num * 3 * 2);
            int                   num2  = 0;

            foreach (CSGPolygon csgpolygon in list)
            {
                int count = csgpolygon.Vertices.Count;
                for (int i = 2; i < count; i++)
                {
                    int item  = this.AddVertex(csgpolygon.Vertices[0], list4, list5, list6, cache, hash);
                    int item2 = this.AddVertex(csgpolygon.Vertices[i - 1], list4, list5, list6, cache, hash);
                    int item3 = this.AddVertex(csgpolygon.Vertices[i], list4, list5, list6, cache, hash);
                    if (csgpolygon.Id == 0)
                    {
                        list2.Add(item);
                        list2.Add(item2);
                        list2.Add(item3);
                    }
                    else
                    {
                        list3.Add(item);
                        list3.Add(item2);
                        list3.Add(item3);
                    }
                    num2 += 3;
                }
            }
            mesh.vertices     = list4.ToArray();
            mesh.normals      = list5.ToArray();
            mesh.uv           = list6.ToArray();
            mesh.subMeshCount = 2;
            mesh.SetTriangles(list2.ToArray(), 0);
            mesh.SetTriangles(list3.ToArray(), 1);
            mesh.RecalculateNormals();
            mesh.RecalculateBounds();
            return(mesh);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// helper for caching duplicated vertices
        /// </summary>
        int AddVertex(CSGVertex v0, List<Vector3> vertices, List<Vector3> normals, List<Vector2> uvs, Dictionary<int, int> cache, VertexHash hash)
        {
            var h = hash.Hash(v0);

            int value;
            if (cache.TryGetValue(h, out value))
            {
                return value;
            }

            vertices.Add(v0.P);
            normals.Add(v0.N);
            uvs.Add(v0.UV);

            var id = vertices.Count - 1;

            cache.Add(h, id);

            return id;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// return unity mesh from this tree
        /// </summary>
        /// <returns>unity mesh</returns>
        public Mesh ToMesh()
        {
            var mesh = new Mesh();

            var polygons = root.AllPolygons();

            var triCount = polygons.Count*3*2;

            var triangles0 = new List<int>(triCount);
            var triangles1 = new List<int>(triCount);
            var vertices = new List<Vector3>(triCount);
            var normals = new List<Vector3>(triCount);
            var uvs = new List<Vector2>(triCount);

            var vertCache = new Dictionary<int, int>(triCount);
            var hash = new VertexHash(0.001f, triCount*3*2);

            var vertIdx = 0;

            foreach (var polygon in polygons)
            {
                var polyPoints = polygon.Vertices.Count;

                for (int i = 2; i < polyPoints; i++)
                {
                    var v0 = AddVertex(polygon.Vertices[0], vertices, normals, uvs, vertCache, hash);
                    var v1 = AddVertex(polygon.Vertices[i-1], vertices, normals, uvs, vertCache, hash);
                    var v2 = AddVertex(polygon.Vertices[i], vertices, normals, uvs, vertCache, hash);

                    if (polygon.Id == 0)
                    {
                        triangles0.Add(v0);
                        triangles0.Add(v1);
                        triangles0.Add(v2);
                    }
                    else
                    {
                        triangles1.Add(v0);
                        triangles1.Add(v1);
                        triangles1.Add(v2);
                    }

                    vertIdx += 3;
                }
            }

            mesh.vertices = vertices.ToArray();
            mesh.normals = normals.ToArray();
            mesh.uv = uvs.ToArray();
            mesh.subMeshCount = 2;
            mesh.SetTriangles(triangles0.ToArray(), 0);
            mesh.SetTriangles(triangles1.ToArray(), 1);

            mesh.RecalculateNormals();

            mesh.Optimize();
            mesh.RecalculateBounds();

            return mesh;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// helper for caching duplicated vertices
        /// </summary>
        private int AddVertex(CSGVertex v0, List <Vector3> vertices, List <Vector3> normals, List <Vector2> uvs, Dictionary <int, int> cache, VertexHash hash)
        {
            var h = hash.Hash(v0);

            int value;

            if (cache.TryGetValue(h, out value))
            {
                return(value);
            }

            vertices.Add(v0.P);
            normals.Add(v0.N);
            uvs.Add(v0.UV);

            var id = vertices.Count - 1;

            cache.Add(h, id);

            return(id);
        }
Ejemplo n.º 6
0
        // Token: 0x06004171 RID: 16753 RVA: 0x0014A704 File Offset: 0x00148B04
        private int AddVertex(CSGVertex v0, List <Vector3> vertices, List <Vector3> normals, List <Vector2> uvs, Dictionary <int, int> cache, VertexHash hash)
        {
            int key = hash.Hash(v0);
            int result;

            if (cache.TryGetValue(key, out result))
            {
                return(result);
            }
            vertices.Add(v0.P);
            normals.Add(v0.N);
            uvs.Add(v0.UV);
            int num = vertices.Count - 1;

            cache.Add(key, num);
            return(num);
        }