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 Dragovv · Apr 01, 2017 at 10:40 AM · perlin noiseprocedural-terrain

Procedural terrain generation issues

Hi guys, i am trying to get along with procedural generation, i definitely not a pro in unity, however i was following this guide: click and got some results. For now i have 2 problems, i have some basic idea how to resolve them, however i would like to hear some other opinions.

First of all: gaps between chunks: Pisture1

they kinda disappear if i will zoom, however textures remain unfit and it is remarkable.

The second, as you can see i have a problem with red (i don't really know how to call it) marks on the ground. I ahve tried to use material from guide, but got same effect.

Besides, Perlin Noise ( i know that i can use Diamond square or simplex) is pseudo-random algorithm, so it will return the same values for the same input parametrs, so does it mean that my chunks will always be the same?

My code ( i am using LibNoise library from guide):

      void Awake()
     {
         var settings = new TerrainChunkSettings(129, 129, 100, 40, FlatTexture, SteepTexture, TerrainMaterial);
         var noiseProvider = new NoiseProvider();
         for (var i = 0; i < 4; i++)
             for (var j = 0; j < 4; j++)
                 new TerrainChunk(settings, noiseProvider, i, j).CreateTerrain();
     }
  public class TerrainChunkSettings
 {
     public int HeightmapResolution { get; private set; }

     public int AlphamapResolution { get; private set; }

     public int Length { get; private set; }

     public int Height { get; private set; }

     public Texture2D FlatTexture { get; private set; }

     public Texture2D SteepTexture { get; private set; }

     public Material TerrainMaterial { get; private set; }

     public TerrainChunkSettings(int heightmapResolution, int alphamapResolution, int length, int height, Texture2D flatTexture, Texture2D steepTexture, Material terrainMaterial)
     {
         HeightmapResolution = heightmapResolution;
         AlphamapResolution = alphamapResolution;
         Length = length;
         Height = height;
         FlatTexture = flatTexture;
         SteepTexture = steepTexture;
         TerrainMaterial = terrainMaterial;
     }
 }

  public class TerrainChunk
 {
     private Terrain Terrain { get; set; }

     private TerrainChunkSettings Settings { get; set; }

     private NoiseProvider NoiseProvider { get; set; }

     public int X { get; private set; }

     public int Z { get; private set; }

     private TerrainData Data { get; set; }

     private float[,] Heightmap { get; set; }
     public TerrainChunk(TerrainChunkSettings settings, NoiseProvider noiseProvider, int x, int z)
     {
         X = x;
         Z = z;
         Settings = settings;
         NoiseProvider = noiseProvider;
     }

     public void CreateTerrain()
     {
         var terrainData = new TerrainData();
         terrainData.heightmapResolution = Settings.HeightmapResolution;
         terrainData.alphamapResolution = Settings.AlphamapResolution;

         var heightmap = GetHeightmap();
         terrainData.SetHeights(0, 0, heightmap);
         ApplyTextures(terrainData);
         terrainData.size = new Vector3(Settings.Length, Settings.Height, Settings.Length);

         var newTerrainGameObject = Terrain.CreateTerrainGameObject(terrainData);
         newTerrainGameObject.transform.position = new Vector3(X * Settings.Length, 0, Z * Settings.Length);
         Terrain = newTerrainGameObject.GetComponent<Terrain>();
         Terrain.Flush();
     }

     private float[,] GetHeightmap()
     {
         var heightmap = new float[Settings.HeightmapResolution, Settings.HeightmapResolution];

         for (var zRes = 0; zRes < Settings.HeightmapResolution; zRes++)
         {
             for (var xRes = 0; xRes < Settings.HeightmapResolution; xRes++)
             {
                 var xCoordinate = X + (float)xRes / (Settings.HeightmapResolution - 1);
                 var zCoordinate = Z + (float)zRes / (Settings.HeightmapResolution - 1);

                 heightmap[zRes, xRes] = NoiseProvider.GetValue(xCoordinate, zCoordinate);
             }
         }

         return heightmap;
     }

     private void ApplyTextures(TerrainData terrainData)
     {
         var flatSplat = new SplatPrototype();
         var steepSplat = new SplatPrototype();

         flatSplat.texture = Settings.FlatTexture;
         steepSplat.texture = Settings.SteepTexture;

         terrainData.splatPrototypes = new SplatPrototype[]
         {
             flatSplat,
             steepSplat
         };

         terrainData.RefreshPrototypes();

         var splatMap = new float[terrainData.alphamapResolution, terrainData.alphamapResolution, 2];

         for (var zRes = 0; zRes < terrainData.alphamapHeight; zRes++)
         {
             for (var xRes = 0; xRes < terrainData.alphamapWidth; xRes++)
             {
                 var normalizedX = (float)xRes / (terrainData.alphamapWidth - 1);
                 var normalizedZ = (float)zRes / (terrainData.alphamapHeight - 1);

                 var steepness = terrainData.GetSteepness(normalizedX, normalizedZ);
                 var steepnessNormalized = Mathf.Clamp(steepness / 1.5f, 0, 1f);

                 splatMap[zRes, xRes, 0] = 1f - steepnessNormalized;
                 splatMap[zRes, xRes, 1] = steepnessNormalized;
             }
         }

         terrainData.SetAlphamaps(0, 0, splatMap);
     }
 }

  public interface INoiseProvider
 {
     float GetValue(float x, float z);
 }
 public class NoiseProvider : INoiseProvider
 {
     private Perlin PerlinNoiseGenerator;

     public NoiseProvider()
     {
         PerlinNoiseGenerator = new Perlin();
     }

     public float GetValue(float x, float z)
     {
         return (float)(PerlinNoiseGenerator.GetValue(x, 0, z) / 2f) + 0.5f;
     }
 }

untitled1.png (327.7 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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Perlin noise generates weird flat spots 1 Answer

Perlin Noise discrepancies 0 Answers

Procedural Tree Placement using perlin noise 1 Answer

Unexpected period length with Unity Mathematics pnoise method 0 Answers

Procedural level/terrain creation questions. 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