- Home /
Level terrain for a given area with C# script?
Is there a way to level the terrain, similar to the paint height terrain editing tool? Specifically, I plan to have the user placing large objects (power-plants, houses, etc) and I want to flatten the terrain covered by the objects so that it looks nicer on uneven terrain. If there isn't a way to implement the paint height tool in script, is there any other way to do this that would be feasible and would produce the curves between the edges of the object and where the terrain is flat (so the terrain is still relatively smooth)?
On a side note, should I manually calculate the approximate slope of the terrain across the area using raycasts, or are there other potentially simpler ways of doing this?
Answer by MountDoomTeam · Mar 29, 2013 at 05:54 PM
Very easy, I am bad at writing code fast and writing pseudocode so sorry if explanation is vague-
determine area you want to flatten
terrain.vertices >0 and <10 for example make a loop: for all mesh.vertices in terrainobject.mesh.vertices{
If vertex position.x>0 && <10 If vertex position.z>0 && <10
determine the height you want to make the terrain, for example average them to the middle vertex which is vertex.z=5 and vertex.x=5...
Make all the vertex.y positions equals the height desired.
recalculate vertices,Recalculate normals, re calculate Collider
here is a vague script that does something similar:
function nzprl ( meshobject : GameObject){//this function takes mesh objects and changes their vertices in XY and Z into mountains
//Debug.Log( "object position " +meshobject.tra
yield WaitForSeconds (Random.Range (0, .3));
var mesh : Mesh = meshobject.GetComponent(MeshFilter).mesh;
var vtxArray : Vector3[]= mesh.vertices;
var mynormals: Vector3[] = mesh.normals;
//if (vtxArray == null)
var vertices = new Vector3[vtxArray.Length];
for (var i=0;i<vertices.Length;i++)
{
//Debug.Log( "vertex-position "+vtxArray[i] );
vtxArray[i] = meshobject.transform.TransformPoint(vtxArray[i]);
var vertex = vtxArray[i];
var cross1 = vtxArray[i] + Vector3(0, 0, 0.51);//invent 2 points very close to vertex to find normal slope of vertex
var cross2 = vtxArray[i] + Vector3(0.31, 0, -0.5);
vertex.y = equation1(vertex);//calculate height of geography
cross1.y = equation1(cross1);//calculate height of very close points to get normal slope
cross2.y = equation1(cross2);
mynormals[i] = Vector3.Normalize (Vector3.Cross ( vertex-cross1, vertex-cross2 ));
//Debug.Log( " mynormals[i] "+ mynormals[i] * 100000 );
// var x : int = transform.position.x / size.x * heightmap.width;
// var z : int = transform.position.z / size.z * heightmap.height;
//something
//vertex.y = htex[0].GetPixel(vtxArray[i].x/5, vtxArray[i].z/5).grayscale * 88;
vertices[i] = meshobject.transform.InverseTransformPoint(vertex);
}
mesh.vertices = vertices;
//yield WaitForSeconds (.05);
mesh.normals = mynormals;
yield WaitForSeconds (.1);
mesh.RecalculateBounds();
yield WaitForSeconds (.1);
//DestroyImmediate(meshobject.collider);
//meshobject.AddComponent("MeshCollider");
meshobject.GetComponent(MeshRenderer).enabled = true;
//meshobject.transform.position += Vector3(0, 50, 0);
}
// function equation1 ( vertex: Vector3 ): float//Perlin Mountains
// {
// var result =
// (Mathf.PerlinNoise(vertex.x*.0173+9999,35.456)
// *Mathf.PerlinNoise(vertex.z*.0201+9999,77.513)
// *22-1) * 8;
// return result;
// }
Answer by MaximilianPs · Apr 04, 2016 at 04:49 PM
I've converted the script to C#, applied the script to the terrain. I've made a Prefab with Cube, onMouseClick the cube it's istantiated on the terrain, but that code looks it doesn't work at all.
using UnityEngine;
using System.Collections;
//Once the user have choosen what a kind of object to place on terrain, it will be placed there.
// if the Terrain isn't at height 0 it will be deformed to sit the object on the right position. (by Smoothing terrain)
public class PlaceOjbect : MonoBehaviour {
GameObject building;
bool placed = false; // after the click the object will be placed and stop follow mouse
// Used on start.
void Awake()
{
building = (GameObject)Instantiate(Resources.Load("Prefabs/Building"), transform.position, transform.rotation);
building.GetComponent<Renderer>().material.color = Color.magenta;
//remove collider so sphere doesn'treact to physics Destroy(aimPoint.collider);
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update()
{
if (placed)
{
//Make object follow mouse
building.transform.position = new Vector3(Input.mousePosition.x,
Input.mousePosition.y,
building.transform.position.z - Camera.main.transform.position.z);
}
if (Input.GetMouseButton(0))
{
//create a line from the mouse position into the screen
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.name == "Terrain")
{
Vector3 placePos = hit.point;
placePos.y += .5f;
placePos.x = Mathf.Round(placePos.x);
placePos.z = Mathf.Round(placePos.z);
building.transform.position = placePos;
building.GetComponent<Renderer>().material.color = Color.blue;
placed = true;
Instantiate(building, placePos, Quaternion.identity);
}
}
}
}
IEnumerator nzprl(GameObject meshobject)
{
//this function takes mesh objects and changes their vertices in XY and Z into mountains
//Debug.Log( "object position " +meshobject.tra
yield return new WaitForSeconds(Random.Range(0, .3f));
Mesh mesh = meshobject.GetComponent<MeshFilter>().mesh;
Vector3[] vtxArray = mesh.vertices;
Vector3[] mynormals = mesh.normals;
Vector3[] vertices = new Vector3[vtxArray.Length];
for (var i = 0; i < vertices.Length; i++)
{
//Debug.Log( "vertex-position "+vtxArray[i] );
vtxArray[i] = meshobject.transform.TransformPoint(vtxArray[i]);
Vector3 vertex = vtxArray[i];
//invent 2 points very close to vertex to find normal slope of vertex
Vector3 cross1 = vtxArray[i] + new Vector3(0, 0, 0.51f);
Vector3 cross2 = vtxArray[i] + new Vector3(0.31f, 0, -0.5f);
vertex.y = equation1(vertex); //calculate height of geography
cross1.y = equation1(cross1); //calculate height of very close points to get normal slope
cross2.y = equation1(cross2);
mynormals[i] = Vector3.Normalize(Vector3.Cross(vertex - cross1, vertex - cross2));
//Debug.Log( " mynormals[i] "+ mynormals[i] * 100000 );
// var x : int = transform.position.x / size.x * heightmap.width;
// var z : int = transform.position.z / size.z * heightmap.height;
//something
//vertex.y = htex[0].GetPixel(vtxArray[i].x/5, vtxArray[i].z/5).grayscale * 88;
vertices[i] = meshobject.transform.InverseTransformPoint(vertex);
}
mesh.vertices = vertices;
//yield WaitForSeconds (.05);
mesh.normals = mynormals;
yield return new WaitForSeconds(.1f);
mesh.RecalculateBounds();
yield return new WaitForSeconds(.1f);
//DestroyImmediate(meshobject.collider);
//meshobject.AddComponent("MeshCollider");
meshobject.GetComponent<MeshRenderer>().enabled = true;
//meshobject.transform.position += Vector3(0, 50, 0);
}
float equation1(Vector3 vertex) //Perlin Mountains
{
var result =(Mathf.PerlinNoise(vertex.x * .0173f + 9999f, 35.456f)
* Mathf.PerlinNoise(vertex.z * .0201f + 9999f ,77.513f)*22-1) * 8;
return result;
}
}