Search Unity

[Released] Advanced Terrain Grass

Discussion in 'Assets and Asset Store' started by larsbertram1, Sep 11, 2017.

  1. SRGeodan

    SRGeodan

    Joined:
    Oct 16, 2014
    Posts:
    12
    This is how I generate grass.
    There is quite some unrelated code.
    But what else should I do?

    Code (CSharp):
    1.         public static void GenerateGrass(Terrain ter)
    2.         {
    3.             if (ter == null) return;
    4.             var td = ter.terrainData;
    5.             if (td == null) return;
    6.             TerrainBehaviour tb = ter.GetComponent<TerrainBehaviour>();
    7.             if (tb == null) return;
    8.             GrassManager gm = ter.gameObject.AddOrGetComponent<GrassManager>();
    9.             GrassSettings set = WorldManager.Instance.GrassSettings;
    10.             List<Texture2D> textures = Helpers.LoadAllAssets<Texture2D>(set.GrassLabel);
    11.             List<GameObject> prefabs = Helpers.LoadAllAssets<GameObject>(set.GrassLabel);
    12.             DetailPrototype[] dps = new DetailPrototype[textures.Count + prefabs.Count];
    13.             int k = 0;
    14.             foreach (var t in textures)
    15.             {
    16.                 dps[k] = new DetailPrototype();
    17.                 dps[k].prototypeTexture = t;
    18.                 dps[k].renderMode = DetailRenderMode.Grass;
    19.                 dps[k].minWidth = set.MinWidth;
    20.                 dps[k].maxWidth = set.MaxWidth;
    21.                 dps[k].minHeight = set.MinHeight;
    22.                 dps[k].maxHeight = set.MaxHeight;
    23.                 k++;
    24.             }
    25.             foreach (var pf in prefabs)
    26.             {
    27.                 dps[k] = new DetailPrototype();
    28.                 dps[k].prototype = pf;
    29.                 dps[k].usePrototypeMesh = true;
    30.                 dps[k].renderMode = DetailRenderMode.Grass;
    31.                 //dps[k].minWidth = set.MinWidth;
    32.                 //dps[k].maxWidth = set.MaxWidth;
    33.                 //dps[k].minHeight = set.MinHeight;
    34.                 //dps[k].maxHeight = set.MaxHeight;
    35.                 k++;
    36.             }
    37.             td.detailPrototypes = dps;
    38.             List<Color32> styles = new List<Color32>();
    39.             set.SpawnGrassOn.ToList().ForEach(style =>
    40.             {
    41.                 var pixels = StyleManager.Instance.StyleToPixels(style);
    42.                 if ( pixels != null && pixels.Count!=0)
    43.                     styles.AddRange(pixels);
    44.             });
    45.             styles = styles.Distinct().ToList();
    46.             float fx = WorldManager.Instance.TerrainSettings.MemoryResolution / WorldManager.Instance.TerrainSettings.DetailResolution;
    47.             for (k = 0; k < textures.Count; k++)
    48.             {
    49.                 var res = WorldManager.Instance.TerrainSettings.DetailResolution;
    50.                 int[,] map = new int[res, res];
    51.                 for (int y = 0; y < res; y++)
    52.                     for (int x = 0; x < res; x++)
    53.                     {
    54.                         Color32 style = GetStylePixel(tb, Mathf.RoundToInt( x * fx ), Mathf.RoundToInt( y * fx ));
    55.                         Color32 style2 = GetStylePixel(tb, Mathf.RoundToInt((x+1) * fx), Mathf.RoundToInt(y * fx));
    56.                   //      Color32 style3 = GetStylePixel(tb, Mathf.RoundToInt((x-1) * fx), Mathf.RoundToInt(y * fx));
    57.                         Color32 style4 = GetStylePixel(tb, Mathf.RoundToInt(x * fx), Mathf.RoundToInt((y+1) * fx));
    58.                  //       Color32 style5 = GetStylePixel(tb, Mathf.RoundToInt(x * fx), Mathf.RoundToInt((y-1) * fx));
    59.                         int d = 0;
    60.                         if (style.Equal(style2) && style2.Equal(style4) /*&& style3.Equal(style4) && style4.Equal(style5) && */ && styles.Any(c => c.Equal(style)))
    61.                         {
    62.                             d = set.Density;
    63.                         }
    64.                         map[y, x] = d;
    65.                     }
    66.                 td.SetDetailLayer(0, 0, k, map);
    67.             }
    68.  
    69.             gm.UpdateGrass();
    70.             for (int i = 0; i < gm.ShadowCastingMode.Length; i++)
    71.                 gm.ShadowCastingMode[i] = UnityEngine.Rendering.ShadowCastingMode.On;
    72.         }
     
  2. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    4,947
    the grassmanager expects a fully set up terrain with heightmap, detail density maps and grass prototypes.

    if you create the terrain from script i would start with removing the init function from the OnEnable().
    call it instead when your terrain is fully configured.

    you also have to take care about the arrays usually been filled by the inspector like v_mesh, v_mat, v_clip, InstanceRotation, WriteNormalBuffer etc.
     
  3. SRGeodan

    SRGeodan

    Joined:
    Oct 16, 2014
    Posts:
    12
    It is fully set up.
    Then I add the script either by hand or from script -> always results in the exception.
    I then do: GetPrototypes() from script or by hand, does not matter. Same errors.
    Tried Init, Tried UpdateGrass(), basically everything already.
    No luck :(
     
  4. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    4,947
    Let us have a look into this next week.
     
  5. oceanquigley

    oceanquigley

    Joined:
    Apr 20, 2018
    Posts:
    5
    Is it possible for the "Unlit" lighting mode to still receive shadows? I'm using baked lighting (which I'm saving as a texture and using as my grass base texture) for static objects, so the grass is beautifully integrated into the static scene - but the effect is ruined when a dynamic shadow goes over.
     
  6. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    4,947
    nope. it is unlit...
    but actually i would need some more information.
     
  7. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    4,947
    i read your answer in my emails - although you have deleted it here.
    you should be able to use a pigment map ( top-down screencapture of the terrain), get nice blending between the terrain and the grass and support real time shadows.
    you must not go unlit but fully lit and make sure that grass lighting matches terrain lighting.
     
    Last edited: Aug 14, 2019
  8. Gua

    Gua

    Joined:
    Oct 29, 2012
    Posts:
    328
    I'm having issues with Decal Master: Advanced Deferred Decals + Advanced Terrain Grass.



     
    Last edited: Aug 18, 2019
  9. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    4,947
    i do not know how decal master renders its deferred decals.
    the white border around the decal however seems to be smoothness getting smoothly blended from decal to gbuffer.
    atg however interpretes smoothness as specular mask. so this will fail.
    atg uses a custom gbuffer layout and material ids stored in the normalBuffer's alpha channel.
    you have to make sure that the decal shader will not affect this channel.
     
  10. unity_c1ndWE1Nn46iPg

    unity_c1ndWE1Nn46iPg

    Joined:
    Jan 13, 2019
    Posts:
    144
    Hey, I wonder if I use World Creator, will I still be able to use your shader?
     
  11. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    4,947
    yes, you are.
     
  12. Murkawski

    Murkawski

    Joined:
    Nov 30, 2014
    Posts:
    48
    Hey, I would appreciate some help. I have some 2 errors popping up. The last thing I did was remove 2 grass from the terrain details as I don't use them and don't need them. Do you know what the problem could be?
     

    Attached Files:

  13. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    4,947
    you have to rebuild the saved terrain data.
     
  14. Murkawski

    Murkawski

    Joined:
    Nov 30, 2014
    Posts:
    48
    Thanks. I'll try that
     
  15. Murkawski

    Murkawski

    Joined:
    Nov 30, 2014
    Posts:
    48
    Hello. I'm getting an error in the shader.
     

    Attached Files:

  16. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    4,947
    have you enabled "GPU instancing" in the material inspector? should be off.
    you may also edit the shader and comment #pragma multi_compile_instancing in the depth only and the shadow caster pass: we are using procedural instancing.

    please let me know if this fixes the error.
     
  17. Murkawski

    Murkawski

    Joined:
    Nov 30, 2014
    Posts:
    48
    Yes, I did enable GPU instancing. So I should turn it off right?

    And when I comment out what you mentioned than the error goes away. Ty
     
  18. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    4,947
    yes. we do not use unity's built in instancing.

    fine!
     
  19. unity_c1ndWE1Nn46iPg

    unity_c1ndWE1Nn46iPg

    Joined:
    Jan 13, 2019
    Posts:
    144
    I wonder if you will ever add touch bending feature and just increase the price? I do have VS but that asset prevent me from my performance technique so I don't use it anymore
     
    John-Lisenby likes this.
  20. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    4,947
    touch bending has been implemented by some other users already: please read this thread.
     
  21. unity_c1ndWE1Nn46iPg

    unity_c1ndWE1Nn46iPg

    Joined:
    Jan 13, 2019
    Posts:
    144
  22. unity_c1ndWE1Nn46iPg

    unity_c1ndWE1Nn46iPg

    Joined:
    Jan 13, 2019
    Posts:
    144
    I'm not a programmer
     
  23. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    4,947
    the conversation starts here:
    #post 177
    maybe @dongliang28 can share some more information or even code with you.
     
  24. John-Lisenby

    John-Lisenby

    Joined:
    Nov 8, 2013
    Posts:
    110
    Hello,

    I am getting shader error. Please see image. I not sure how or what needs to be updated to fix this error. FYI Im building for UWP / Xbox.

    Thanks,

    John
     

    Attached Files:

  25. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    4,947
    hmm, you may try to uncomment the paragma "nolightmap". it currently is commented because it causes trouble with baked shadow maps. i hope you do not use these...

    so line 59 of the shader should look like this:
    #pragma surface surf ATGSpecular vertex:vertgrass addshadow nodynlightmap nolppv nometa nolightmap
    doing so makes the warning go away in unity 5.6.3. on dx11. maybe it stops the shader compiler from erroring when compiling for xbox one.