// Token: 0x060027D2 RID: 10194 RVA: 0x001B4B24 File Offset: 0x001B2D24
        private void CollectTreeMeshes(Terrain terrain, List <RasterizationMesh> result)
        {
            TerrainData terrainData = terrain.terrainData;

            for (int i = 0; i < terrainData.treeInstances.Length; i++)
            {
                TreeInstance  treeInstance  = terrainData.treeInstances[i];
                TreePrototype treePrototype = terrainData.treePrototypes[treeInstance.prototypeIndex];
                if (!(treePrototype.prefab == null))
                {
                    Collider component = treePrototype.prefab.GetComponent <Collider>();
                    Vector3  pos       = terrain.transform.position + Vector3.Scale(treeInstance.position, terrainData.size);
                    if (component == null)
                    {
                        Bounds            bounds = new Bounds(terrain.transform.position + Vector3.Scale(treeInstance.position, terrainData.size), new Vector3(treeInstance.widthScale, treeInstance.heightScale, treeInstance.widthScale));
                        Matrix4x4         matrix = Matrix4x4.TRS(pos, Quaternion.identity, new Vector3(treeInstance.widthScale, treeInstance.heightScale, treeInstance.widthScale) * 0.5f);
                        RasterizationMesh item   = new RasterizationMesh(RecastMeshGatherer.BoxColliderVerts, RecastMeshGatherer.BoxColliderTris, bounds, matrix);
                        result.Add(item);
                    }
                    else
                    {
                        Vector3           s = new Vector3(treeInstance.widthScale, treeInstance.heightScale, treeInstance.widthScale);
                        RasterizationMesh rasterizationMesh = this.RasterizeCollider(component, Matrix4x4.TRS(pos, Quaternion.identity, s));
                        if (rasterizationMesh != null)
                        {
                            rasterizationMesh.RecalculateBounds();
                            result.Add(rasterizationMesh);
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        /** Generates a terrain chunk mesh */
        RasterizationMesh GenerateHeightmapChunk(float[, ] heights, Vector3 sampleSize, Vector3 offset, int x0, int z0, int width, int depth, int stride)
        {
            // Downsample to a smaller mesh (full resolution will take a long time to rasterize)
            // Round up the width to the nearest multiple of terrainSampleSize and then add 1
            // (off by one because there are vertices at the edge of the mesh)
            int resultWidth = CeilDivision(width, terrainSampleSize) + 1;
            int resultDepth = CeilDivision(depth, terrainSampleSize) + 1;

            var heightmapWidth = heights.GetLength(0);
            var heightmapDepth = heights.GetLength(1);

            // Create a mesh from the heightmap
            var terrainVertices = new Vector3[resultWidth * resultDepth];

            // Create lots of vertices
            for (int z = 0; z < resultDepth; z++)
            {
                for (int x = 0; x < resultWidth; x++)
                {
                    int sampleX = Math.Min(x0 + x * stride, heightmapWidth - 1);
                    int sampleZ = Math.Min(z0 + z * stride, heightmapDepth - 1);

                    terrainVertices[z * resultWidth + x] = new Vector3(sampleZ * sampleSize.x, heights[sampleX, sampleZ] * sampleSize.y, sampleX * sampleSize.z) + offset;
                }
            }

            // Create the mesh by creating triangles in a grid like pattern
            var tris          = new int[(resultWidth - 1) * (resultDepth - 1) * 2 * 3];
            int triangleIndex = 0;

            for (int z = 0; z < resultDepth - 1; z++)
            {
                for (int x = 0; x < resultWidth - 1; x++)
                {
                    tris[triangleIndex]     = z * resultWidth + x;
                    tris[triangleIndex + 1] = z * resultWidth + x + 1;
                    tris[triangleIndex + 2] = (z + 1) * resultWidth + x + 1;
                    triangleIndex          += 3;
                    tris[triangleIndex]     = z * resultWidth + x;
                    tris[triangleIndex + 1] = (z + 1) * resultWidth + x + 1;
                    tris[triangleIndex + 2] = (z + 1) * resultWidth + x;
                    triangleIndex          += 3;
                }
            }

#if ASTARDEBUG
            var color = AstarMath.IntToColor(x0 + 7 * z0, 0.7f);
            for (int i = 0; i < tris.Length; i += 3)
            {
                Debug.DrawLine(terrainVertices[tris[i]], terrainVertices[tris[i + 1]], color, 40);
                Debug.DrawLine(terrainVertices[tris[i + 1]], terrainVertices[tris[i + 2]], color, 40);
                Debug.DrawLine(terrainVertices[tris[i + 2]], terrainVertices[tris[i]], color, 40);
            }
#endif

            var mesh = new RasterizationMesh(terrainVertices, tris, new Bounds());
            // Could probably calculate these bounds in a faster way
            mesh.RecalculateBounds();
            return(mesh);
        }
Esempio n. 3
0
        void CollectTreeMeshes(Terrain terrain, List <RasterizationMesh> result)
        {
            TerrainData data = terrain.terrainData;

            for (int i = 0; i < data.treeInstances.Length; i++)
            {
                TreeInstance  instance = data.treeInstances[i];
                TreePrototype prot     = data.treePrototypes[instance.prototypeIndex];

                // Make sure that the tree prefab exists
                if (prot.prefab == null)
                {
                    continue;
                }

                var collider     = prot.prefab.GetComponent <Collider>();
                var treePosition = terrain.transform.position + Vector3.Scale(instance.position, data.size);
                var scale        = new Vector3(instance.widthScale, instance.heightScale, instance.widthScale);
                scale = Vector3.Scale(scale, prot.prefab.transform.localScale);

                if (collider == null)
                {
                    var instanceBounds = new Bounds(terrain.transform.position + Vector3.Scale(instance.position, data.size), new Vector3(instance.widthScale, instance.heightScale, instance.widthScale));

                    Matrix4x4 matrix = Matrix4x4.TRS(treePosition, Quaternion.identity, scale * 0.5f);

                    var mesh = new RasterizationMesh(BoxColliderVerts, BoxColliderTris, instanceBounds, matrix);

                    result.Add(mesh);
                }
                else
                {
                    // The prefab has a collider, use that instead

                    // Generate a mesh from the collider
                    RasterizationMesh mesh = RasterizeCollider(collider, Matrix4x4.TRS(treePosition, Quaternion.identity, scale));

                    // Make sure a valid mesh was generated
                    if (mesh != null)
                    {
                        // The bounds are incorrectly based on collider.bounds.
                        // It is incorrect because the collider is on the prefab, not on the tree instance
                        // so we need to recalculate the bounds based on the actual vertex positions
                        mesh.RecalculateBounds();
                        result.Add(mesh);
                    }
                }
            }
        }
        // Token: 0x060027D1 RID: 10193 RVA: 0x001B497C File Offset: 0x001B2B7C
        private RasterizationMesh GenerateHeightmapChunk(float[,] heights, Vector3 sampleSize, Vector3 offset, int x0, int z0, int width, int depth, int stride)
        {
            int num     = RecastMeshGatherer.CeilDivision(width, this.terrainSampleSize) + 1;
            int num2    = RecastMeshGatherer.CeilDivision(depth, this.terrainSampleSize) + 1;
            int length  = heights.GetLength(0);
            int length2 = heights.GetLength(1);
            int num3    = num * num2;

            Vector3[] array = ArrayPool <Vector3> .Claim(num3);

            for (int i = 0; i < num2; i++)
            {
                for (int j = 0; j < num; j++)
                {
                    int num4 = Math.Min(x0 + j * stride, length - 1);
                    int num5 = Math.Min(z0 + i * stride, length2 - 1);
                    array[i * num + j] = new Vector3((float)num5 * sampleSize.x, heights[num4, num5] * sampleSize.y, (float)num4 * sampleSize.z) + offset;
                }
            }
            int num6 = (num - 1) * (num2 - 1) * 2 * 3;

            int[] array2 = ArrayPool <int> .Claim(num6);

            int num7 = 0;

            for (int k = 0; k < num2 - 1; k++)
            {
                for (int l = 0; l < num - 1; l++)
                {
                    array2[num7]     = k * num + l;
                    array2[num7 + 1] = k * num + l + 1;
                    array2[num7 + 2] = (k + 1) * num + l + 1;
                    num7            += 3;
                    array2[num7]     = k * num + l;
                    array2[num7 + 1] = (k + 1) * num + l + 1;
                    array2[num7 + 2] = (k + 1) * num + l;
                    num7            += 3;
                }
            }
            RasterizationMesh rasterizationMesh = new RasterizationMesh(array, array2, default(Bounds));

            rasterizationMesh.numVertices  = num3;
            rasterizationMesh.numTriangles = num6;
            rasterizationMesh.pool         = true;
            rasterizationMesh.RecalculateBounds();
            return(rasterizationMesh);
        }
Esempio n. 5
0
        private RasterizationMesh GenerateHeightmapChunk(float[,] heights, Vector3 sampleSize, Vector3 offset, int x0, int z0, int width, int depth, int stride)
        {
            int num     = RecastMeshGatherer.CeilDivision(width, this.terrainSampleSize) + 1;
            int num2    = RecastMeshGatherer.CeilDivision(depth, this.terrainSampleSize) + 1;
            int length  = heights.GetLength(0);
            int length2 = heights.GetLength(1);

            Vector3[] array = new Vector3[num * num2];
            for (int i = 0; i < num2; i++)
            {
                for (int j = 0; j < num; j++)
                {
                    int num3 = Math.Min(x0 + j * stride, length - 1);
                    int num4 = Math.Min(z0 + i * stride, length2 - 1);
                    array[i * num + j] = new Vector3((float)num4 * sampleSize.x, heights[num3, num4] * sampleSize.y, (float)num3 * sampleSize.z) + offset;
                }
            }
            int[] array2 = new int[(num - 1) * (num2 - 1) * 2 * 3];
            int   num5   = 0;

            for (int k = 0; k < num2 - 1; k++)
            {
                for (int l = 0; l < num - 1; l++)
                {
                    array2[num5]     = k * num + l;
                    array2[num5 + 1] = k * num + l + 1;
                    array2[num5 + 2] = (k + 1) * num + l + 1;
                    num5            += 3;
                    array2[num5]     = k * num + l;
                    array2[num5 + 1] = (k + 1) * num + l + 1;
                    array2[num5 + 2] = (k + 1) * num + l;
                    num5            += 3;
                }
            }
            RasterizationMesh rasterizationMesh = new RasterizationMesh(array, array2, default(Bounds));

            rasterizationMesh.RecalculateBounds();
            return(rasterizationMesh);
        }
        private RasterizationMesh GenerateHeightmapChunk(float[,] heights, Vector3 sampleSize, Vector3 offset, int x0, int z0, int width, int depth, int stride)
        {
            int num    = CeilDivision(width, this.terrainSampleSize) + 1;
            int num2   = CeilDivision(depth, this.terrainSampleSize) + 1;
            int length = heights.GetLength(0);
            int num4   = heights.GetLength(1);

            Vector3[] vertices = new Vector3[num * num2];
            for (int i = 0; i < num2; i++)
            {
                for (int k = 0; k < num; k++)
                {
                    int num7 = Math.Min((int)(x0 + (k * stride)), (int)(length - 1));
                    int num8 = Math.Min((int)(z0 + (i * stride)), (int)(num4 - 1));
                    vertices[(i * num) + k] = new Vector3(num8 * sampleSize.x, heights[num7, num8] * sampleSize.y, num7 * sampleSize.z) + offset;
                }
            }
            int[] triangles = new int[(((num - 1) * (num2 - 1)) * 2) * 3];
            int   index     = 0;

            for (int j = 0; j < (num2 - 1); j++)
            {
                for (int m = 0; m < (num - 1); m++)
                {
                    triangles[index]     = (j * num) + m;
                    triangles[index + 1] = ((j * num) + m) + 1;
                    triangles[index + 2] = (((j + 1) * num) + m) + 1;
                    index               += 3;
                    triangles[index]     = (j * num) + m;
                    triangles[index + 1] = (((j + 1) * num) + m) + 1;
                    triangles[index + 2] = ((j + 1) * num) + m;
                    index               += 3;
                }
            }
            RasterizationMesh mesh = new RasterizationMesh(vertices, triangles, new Bounds());

            mesh.RecalculateBounds();
            return(mesh);
        }