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 benk0913 · Jul 25, 2013 at 01:03 PM · terraingridgenerationhill

Terrain hill generating by code

Hello, friends.

I have a small problem which I couldnt find a solution for.

I am working on a terrain which randomly generates hills on it, I have made a method which makes hills, but makes them very pointy, more like round pyramids.

Thats the call for the method:

 CreateHill(Random.Range(0,513),Random.Range(0,513),Random.Range(0.005f,0.1f),Random.Range(1f,10f));



and thats the method itself:

 public void CreateHill(int x, int y, float height,float pointyness)
     {
         float point = 0;
         float distanceFromTop;
         
         terrainGRID[x,y]=height;
 
         for(int a=0;a<513;a++)
         {    
             for(int b=0;b<513;b++)
             {
                 distanceFromTop=Mathf.Sqrt(   Mathf.Pow((y-b),2)  +  Mathf.Pow((x-a),2)   );
                  point=((height-terrainGRID[a,b])*1000 - distanceFromTop*pointyness)/1000 ;
                 
                 if(point<0)
                 point=0;
                 
                 terrainGRID[a,b]+=point;
             }
         }
     }

what comes from it is:

![alt text][1]

What I'm trying to achive is a hilly hill, something more like ![alt text][2]

(The second one was made in the editor, not by code lol).

MANY thanks for anyone who helps me out! :) [1]: /storage/temp/13506-untitled.png [2]: /storage/temp/13507-untitled2.png

untitled2.png (79.9 kB)
untitled.png (61.8 kB)
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
4
Best Answer

Answer by tnetennba · Jul 25, 2013 at 01:26 PM

you need something like this to smooth your terrain points and round everything off a little. It's not the best example of terrain smoothing but it should give you an idea of what to do.

     private void Smooth()
     {
 
         float[,] height = terrain.terrainData.GetHeights(0, 0, terrain.terrainData.heightmapWidth,
                                           terrain.terrainData.heightmapHeight);
         float k = 0.5f;
         /* Rows, left to right */
         for (int x = 1; x < terrain.terrainData.heightmapWidth; x++)
             for (int z = 0; z < terrain.terrainData.heightmapHeight; z++)
                 height[x, z] = height[x - 1, z] * (1 - k) +
                           height[x, z] * k;
 
         /* Rows, right to left*/
         for (int x = terrain.terrainData.heightmapWidth - 2; x < -1; x--)
             for (int z = 0; z < terrain.terrainData.heightmapHeight; z++)
                 height[x, z] = height[x + 1, z] * (1 - k) +
                           height[x, z] * k;
 
         /* Columns, bottom to top */
         for (int x = 0; x < terrain.terrainData.heightmapWidth; x++)
             for (int z = 1; z < terrain.terrainData.heightmapHeight; z++)
                 height[x, z] = height[x, z - 1] * (1 - k) +
                           height[x, z] * k;
 
         /* Columns, top to bottom */
         for (int x = 0; x < terrain.terrainData.heightmapWidth; x++)
             for (int z = terrain.terrainData.heightmapHeight; z < -1; z--)
                 height[x, z] = height[x, z + 1] * (1 - k) +
                           height[x, z] * k;
 
         terrain.terrainData.SetHeights(0, 0, height);
     }
Comment
Add comment · Show 3 · 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 benk0913 · Jul 26, 2013 at 11:00 AM 0
Share

Looks good, I'll try it out and see if it works for this issue :)

avatar image benk0913 · Jul 26, 2013 at 11:06 AM 0
Share

It works great! $$anonymous$$any thanks, that is what I was looking for! :)

avatar image tnetennba · Jul 26, 2013 at 11:14 AM 0
Share

no problem.

avatar image
0

Answer by raygun · Sep 01, 2016 at 08:36 AM

OK, so I took your code that process a grid, using the distance from the centre to calculate the hill height.

Now, if you use a linear function you'll get a linear/sharp hill.

So, my version uses a bezier function which creates a nice sloping hill.

[ExecuteInEditMode] public class HillGenerator : MonoBehaviour { public int x; public int y; public int radius; public float height; }

 [CustomEditor (typeof(HillGenerator))]
 public class HillGeneratorEditor : Editor
 {
     public static Vector2 BezierCurve(float t, Vector2 pOne, Vector2 pTwo, Vector2 pThree, Vector2 pFour)
     {
         return Mathf.Pow (1 - t, 3) * pOne + 3 * Mathf.Pow (1 - t, 2) * t * pTwo + 3 * (1 - t) * t * t * pThree + Mathf.Pow (t, 3) * pFour;
     }
 
     public override void OnInspectorGUI()
     {
         DrawDefaultInspector ();
 
         var script = this.target as HillGenerator;
 
         if (GUILayout.Button ("Generate"))
         {
             this.CreateHill (script.x, script.y, script.height, script.radius);
         }
     }
 
     public void CreateHill(int x, int y, float height, int radius)
     {
         var diameter = radius * 2;
         var heightsCenteX = radius;
         var heightsCenteY = radius;
         var baseX = x - heightsCenteX;
         var baseY = y - heightsCenteY;
 
         var script = this.target as HillGenerator;
         var terrianData = script.GetComponent<Terrain> ().terrainData;
         var heights = terrianData.GetHeights (baseX, baseY, diameter, diameter);
 
         var controlPoint1 = new Vector2 (0.52f, 0.06f);
         var controlPoint2 = new Vector2 (0.42f, 0.95f);
 
         for (int a = 0; a < diameter; a++)
         {    
             for (int b = 0; b < diameter; b++)
             {
                 var distanceFromCenter = Mathf.Sqrt (Mathf.Pow ((heightsCenteY - b), 2) + Mathf.Pow ((heightsCenteX - a), 2));
                 var time = Math.Max (1 - (distanceFromCenter / radius), 0);
                 var bezierPoint = BezierCurve (time, new Vector2 (0, 0), controlPoint1, controlPoint2, new Vector2 (1, 1));
 
                 var pointHeight = bezierPoint.y * height;
 
                 if (pointHeight < 0)
                     pointHeight = 0;
 
                 heights [a, b] = pointHeight;
             }
         }
 
         terrianData.SetHeights (baseX, baseY, heights);
     }
 }

example: CreateHill(250, 250, 0.3, 100)

To visualise the curve you can use this site: http://www.css3beziercurve.net

controlPoint1 = site.point1 & 2

controlPoint2 = site.point3 & 4

Attach the script to a Terrain, set up the properties and hit Generate.

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

18 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Procedural terrain generation ? 0 Answers

How can I get a Low Poly terrain to work in Unity? 2 Answers

How do I create a relatively simple random 2d city grid? (includes image) 0 Answers

2D Procedural Terrain Generation + Pooling 2 Answers

Paint Grid Texture 1 Answer


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