Loading...
 

Terrain changes

Table of Content

Need to experiment with terrain change, and procedural terrain creation, terrain loading/saving.

Code snippet to see how to change trees:

counter += Time.deltaTime;
            if (counter>0.1f)
            {
                counter = 0;
                TreeInstance[] trees = terrain.terrainData.treeInstances;
                if (trees [ 500 ] .position.y*terrain.terrainData.size.y>50f)
                {
                    trees [ 500 ] .position.y = 5f/ terrain.terrainData.size.y;
                }
                trees [ 500 ] .position+=Vector3.up * 0.001f;
                terrain.terrainData.treeInstances = trees;
                terrain.Flush();
                Debug.Log("Tree #500 is at "+Vector3.Scale(trees[500].position , terrain.terrainData.size));
            }


Working code snippet for lowering terrain. Does not take base terrain position and rotation offset into accunt:

TerrainData td = terrain.terrainData;
                int xRes = td.heightmapResolution;
                int yRes = td.heightmapResolution;
                Vector3 pointHit = hitInfo.point;
                testObject.transform.position = hitInfo.point;
                int xBase = (int)((hitInfo.point.x / td.size.x) * (float)td.heightmapResolution);
                int zBase = (int)((hitInfo.point.z / td.size.z) * (float)td.heightmapResolution);
                float[,] heights = terrain.terrainData.GetHeights(xBase, zBase, 1, 1);
                //Debug.Log("terrain data xres=" + xRes + ", yres=" + yRes);
                Debug.Log(String.Format("Terrain Size={2} Just hit Terrain at coordinates [{0:0.##},{1:0.##}] which is [{3},{4}] Y must be:{5}", hitInfo.point.x, hitInfo.point.z , td.size , xBase , zBase , heights[0,0]*td.size.y));
                float[,] newHeights = new float [1,1];
                newHeights[0,0] = ((heights[0, 0] * td.size.y) - 0.1f) / td.size.y;
                terrain.terrainData.SetHeights(xBase, zBase , newHeights);



Volume of a mesh:

public float SignedVolumeOfTriangle(Vector3 p1, Vector3 p2, Vector3 p3)
 {
     float v321 = p3.x * p2.y * p1.z;
     float v231 = p2.x * p3.y * p1.z;
     float v312 = p3.x * p1.y * p2.z;
     float v132 = p1.x * p3.y * p2.z;
     float v213 = p2.x * p1.y * p3.z;
     float v123 = p1.x * p2.y * p3.z;
     return (1.0f / 6.0f) * (-v321 + v231 + v312 - v132 - v213 + v123);
 }

 public float VolumeOfMesh(Mesh mesh)
 {
     float volume = 0;
     Vector3[] vertices = mesh.vertices;
     int[] triangles = mesh.triangles;
     for (int i = 0; i < mesh.triangles.Length; i += 3)
     {
         Vector3 p1 = vertices[triangles[i + 0]];
         Vector3 p2 = vertices[triangles[i + 1]];
         Vector3 p3 = vertices[triangles[i + 2]];
         volume += SignedVolumeOfTriangle(p1, p2, p3);
     }
     return Mathf.Abs(volume);
 }


and add this if you need to scale it:

volume *= meshFilter.gameObject.transform.localScale.x * meshFilter.gameObject.transform.localScale.y * meshFilter.gameObject.transform.localScale.z;