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
0
Question by ExtremePowers · Feb 04, 2015 at 05:27 PM · terrainresolutionproceduralgrass

Procedural grass placement

I have managed to make a procedural grass placement system, but my problem is that the grass' density isn't heigh enough, but if I paint the grass onto the terrain it is perfect. How can it come that the grass is acting weird when procedurally placed?

My settings:

  1. Terrain Size: 10000x10000

  2. Detail Map: 2048x2048

  3. Detail resolution per patch: 128

  4. Detail Density: 1

Placement code:

 for(int x = 0; x < aw; x++) {
     for(int z = 0; z < aw; z++) {
         int worldPosX = (x+(hw-1))*(terrainWidth/hw);
         int worldPosZ  = (z+(hw-1))*(terrainWidth/hw);
 
            string Biome = GetBiome(worldPosX,worldPosZ);
         if (Biome == "Plain") {
             detailMap1[z,x] = 1;
         } else if (Biome == "Forest") {
             if (Random.Range(0,2) == 1) {
                 detailMap1[z,x] = 1;
             } else {
                 detailMap2[z,x] = 1;
             }
         }
     }
 }
 Chunk.terrainData.SetDetailLayer(0,0,0,detailMap1);
 Chunk.terrainData.SetDetailLayer(0,0,1,detailMap2);
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
1
Best Answer

Answer by AlucardJay · Feb 04, 2015 at 06:51 PM

The value range for each item in the detail array is an integer between 0 and 16. Try 16 and see what you get :)

Edit :

here is a little test. As you can see, any value above 8 is really overkill :

alt text

and here was the script I used :

 using UnityEngine;
 using System.Collections;
 
 public class TerrainDetailTest : MonoBehaviour 
 {
     public Terrain terrain;
 
     private TerrainData terrainData;
 
 
     void Start() 
     {
         if ( !terrain )
             Debug.LogError( gameObject.name + " has NO TERRAIN assigned in the Inspector" );
 
         terrainData = terrain.terrainData;
 
         GenerateTerrainDetail();
     }
     
     
     void GenerateTerrainDetail() 
     {
         int detailWidth = terrainData.detailWidth;
         int detailHeight = terrainData.detailHeight;
 
         int[,] details0 = new int[ detailWidth, detailHeight ];
         int[,] details1 = new int[ detailWidth, detailHeight ];
 
         int x, y, strength;
 
         for ( x = 0; x < detailWidth / 4; x ++ ) // divided by 4 just to show a test patch
         {
             for ( y = 0; y < detailHeight / 4; y ++ ) // test patch
             {
                 strength = ( x % 2 == 0 ? ( x / 2 ) % 17 : 0 ); // just to spread the grass out a bit to see the difference
 
                 if ( y % 4 == 0 ) // set detail layer 0 for every first row in 4
                     details0[ y, x ] = strength;
                 else if ( y % 4 == 2 ) // set detail layer 1 for every third row in 4
                     details1[ y, x ] = strength;
             }
         }
 
         terrainData.SetDetailLayer( 0, 0, 0, details0 );
         terrainData.SetDetailLayer( 0, 0, 1, details1 );
     }
 }



terraindetailtest.jpg (221.2 kB)
Comment
Add comment · Show 4 · 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 ExtremePowers · Feb 04, 2015 at 07:38 PM 0
Share

@alucardj Like this?

 detail$$anonymous$$ap1[z,x] = 16; //?

Because that seems to make the detail disapear completely.

avatar image AlucardJay · Feb 04, 2015 at 11:55 PM 0
Share

Strange, I shall check my terrain tools when I get home.

I found this information out by reading the detail layer from a terrain with details painted already, finding and returning the largest and smallest values found in the array.

avatar image AlucardJay · Feb 05, 2015 at 04:30 AM 0
Share

Works my end, have edited the answer to show my test script and result image.

avatar image ExtremePowers · Feb 06, 2015 at 07:39 AM 0
Share

Full Generation code:

     IEnumerator SpawnTheGrass() {
         Terrain t = Terrain.activeTerrain;
         int Dh = t.terrainData.detailWidth;

             /* From here */
     int CurDiff = $$anonymous$$athf.RoundToInt((8*$$anonymous$$athf.Pow (2, 8)));
     for (int i = 1; i < 9; i++) {
         int tmp = $$anonymous$$athf.RoundToInt((8*$$anonymous$$athf.Pow (2, i)));
         if ($$anonymous$$athf.Abs (terrainSize-tmp) < CurDiff) {
             CurDiff = $$anonymous$$athf.$$anonymous$$in (CurDiff,$$anonymous$$athf.Abs (terrainSize-tmp));
             Ah = tmp;
         }
     }

     detail$$anonymous$$ap1 = new int[Ah, Ah];
     detail$$anonymous$$ap2 = new int[Ah, Ah]; 
             /* To here is normaly in the Awake function */

         intendedProgress = 0.0f;
 
         for (int x = 0; x < Dh; x++) {
             for (int z = 0; z < Dh; z++) {
                 float normalPosX = ((x*1f)/Dh)*(terrainWidth*1f);
                 float normalPosZ = ((z*1f)/Dh)*(terrainWidth*1f);
                 int worldPosX = (x+(Dh-1))*(terrainWidth/Dh);
                 int worldPosZ  = (z+(Dh-1))*(terrainWidth/Dh);
 
                 float height = t.terrainData.GetInterpolatedHeight(normalPosX,normalPosZ);
                 string Biome = GetBiome (worldPosX,worldPosZ);
 
                 if (height > WaterLevel) {
                     if (Biome == "Plain") {
                         detail$$anonymous$$ap1[z,x] = 16;
                     } else if (Biome == "Forest") {
                         if (Random.Range (0,11) > 5) {
                             detail$$anonymous$$ap2[z,x] = 10;
                         } else {
                             detail$$anonymous$$ap1[z,x] = 10;
                         }
                     }
                 }
                 intendedProgress = (x*1f)/(Dh-1f);
                 if (z % (Dh-1f) == 0 && x % 100 == 0) {
                     yield return null;
                 }
             }
         }
         t.terrainData.SetDetailLayer(0,0,0,detail$$anonymous$$ap1);
         t.terrainData.SetDetailLayer(0,0,1,detail$$anonymous$$ap2);
         return true;
     }

EDIT: This didn't fix the issue.

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

20 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

Related Questions

HELP- my grass is all pixelated 1 Answer

Looking for Grass asset that can procedurally generate grass on a mesh at runtime 0 Answers

What is the difference with TerrainDetails? 0 Answers

Procedural grass placement 0 Answers

Overriding shaders of Terrain Grass in 2019.2 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