Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by phazonninja · Mar 29, 2013 at 05:27 PM · terrainlevelheightflat

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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;
         
 // }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Adjust the terrain to a mesh 0 Answers

Problem with heightmap and terrain height maxing out at 10000, proportions *Solved* 0 Answers

Kinect issue with Character Movement while in flight 0 Answers

using height brush on plane? 2 Answers

Large, seamless environments 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges