Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by MakakWasTaken · Sep 26, 2015 at 04:09 PM · terrainmeshproceduralheightmapnoise

Mesh based on noise

I have been trying to generate a mesh based on some noise, and I want to say if the noise is 0 don't generate a vertices. But the problem is that the mesh will end up look really weird because of the way I generate my triangles.

Code:

     private void CreateMesh()
     {
         var m = new RidgeNoise(Random.seed);
         m.OctaveCount = 2;
         m.Frequency = 0.01f;

         int hw = Terrain.activeTerrain.terrainData.heightmapWidth;
         float[,] heightmap = Terrain.activeTerrain.terrainData.GetHeights(0, 0, hw, hw);
 
         int ChunkWidth = hw / 8;
         int ChunkCount = hw / ChunkWidth;
 
         float scale = Terrain.activeTerrain.terrainData.size.x / hw;
 
         Vector2 uvScale = new Vector2(1.0f / hw, 1.0f / hw);
 
         for (int cx = 0; cx < ChunkCount; cx++)
         {
             for (int cy = 0; cy < ChunkCount; cy++)
             {
                 int CW = hw / ChunkCount;
                 GameObject water = new GameObject();
                 Vector3 p = new Vector3(cx * ChunkWidth, 0f, cy * ChunkWidth);
 
                 water.transform.localScale = new Vector3(scale, 0.95f, scale);
                 water.transform.position = new Vector3(cx * (ChunkWidth * scale), 0f, cy * (ChunkWidth * scale));
                 water.name = "Water";
                 water.transform.parent = Terrain.activeTerrain.transform;
 
                 water.AddComponent<MeshRenderer>();
                 Mesh w = new Mesh();
                 MeshFilter mf = water.AddComponent<MeshFilter>();
 
                 Vector3[] tVertices = new Vector3[(CW + 1) * (CW + 1)];
                 Vector2[] tUV = new Vector2[tVertices.Length];
                 int[] triangles = new int[tVertices.Length * 6];
 
                 for (int y = 0; y < CW; y++)
                 {
                     for (int x = 0; x < CW; x++)
                     {
                         Vector2 curPos = new Vector2((p.x + x), (p.z + y));
                         float height = heightmap[(int)curPos.y, (int)curPos.x] * Terrain.activeTerrain.terrainData.size.y;
                         if (height < WaterLevel)
                         {
                             height = WaterLevel;
                         }
 
                         float v = Mathf.Max(0f, m.GetValue(curPos.x, curPos.y, 0f) - 0.875f) / 25.0f;
 
                         map.SetPixel((int)curPos.x, (int)curPos.y, Color.white * (v * 25));
                         if (v != 0)
                         {
                             tVertices[y * CW + x] = new Vector3(x, height, y);
                             tUV[y * CW + x] = Vector2.Scale(new Vector2(x, y), uvScale);
                             heightmap[(int)curPos.y, (int)curPos.x] -= v;
 
                         }
                     }
                 }
 
                 int index = 0;
                 for (int y = 0; y < CW - 1; y++)
                 {
                     for (int x = 0; x < CW - 1; x++)
                     {
                         triangles[index] = (y * CW) + x;
                         triangles[index + 1] = ((y + 1) * CW) + x;
                         triangles[index + 2] = ((y + 1) * CW) + x + 1;
 
                         triangles[index + 3] = (y * CW) + x;
                         triangles[index + 4] = ((y + 1) * CW) + x + 1;
                         triangles[index + 5] = (y * CW) + x + 1;
                         index += 6;
                     }
                 }
 
                 w.vertices = tVertices;
                 w.uv = tUV;
                 w.triangles = triangles;
 
                 w.RecalculateNormals();
                 w.Optimize();
 
                 mf.mesh = w;
             }
         }
     }

Noise: Noise Result: Result

result.png (9.4 kB)
noise.png (42.3 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Arycama · Sep 28, 2015 at 06:16 AM

If I understand correctly, you simply want to make terrain based on a texture? You can do this very easily by converting your image into a RAW file, and importing it by selecting "Import Raw...." towards the bottom of the settings tab after selecting the terrain GameObject.

You can use Photoshop to convert an image to RAW format, or use a free online converter.

Comment
Add comment · Show 1 · 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 MakakWasTaken · Sep 28, 2015 at 06:19 AM 0
Share

What I want to do is create a mesh which will only be there if the noise is over 0.0f

I know how to create the terrain but not the other part. I don't know how to create a mesh with holes in it.

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

31 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 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

The most efficient method of turning a voronoi graph into unity terrain object? 0 Answers

Rivers on procedurally terrain 0 Answers

Procedural Mountain Generation 2 Answers

Connecting Perlin Terrain Tiles 1 Answer

Terrain Clone 0 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