Search Unity

[FREE] Vegetation Spawner, procedural tool for the terrain system

Discussion in 'Assets and Asset Store' started by StaggartCreations, Aug 20, 2020.

  1. Marked

    Marked

    Joined:
    Feb 24, 2015
    Posts:
    32
    Great Asset! Love it to place trees with it but I am having trouble getting the maximum amount of detail meshes out of it.
    Is there a hidden limit that I can increase ?
     
  2. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,263
    Glad you like it! The terrain uses a texture to denote where there's a detail mesh placed or not, essentially it represents a grid. If you have a 1024x1024 terrain, and the detail texture resolution is 512x512, a single pixel/texel is 2x2 units large. Meaning, grass can be placed at a minimum of 2 units apart.

    You can clearly tell the difference here between a 256px detail texture, and 512px (same size as terrain). Even though the spawn chance for the grass is 100%
    upload_2021-3-31_11-16-6.png

    You can configure the resolution of this texture under the Settings tab. It should be at least as large as a terrain tile itself, in order to achieve good coverage with a spawn chance of 100.

    upload_2021-3-31_11-10-17.png

    There's a warning displayed if it's lower than half the size of a terrain.
     
  3. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,263
    Submitted v1.0.6

    Changed:
    - Improved spawning speed of trees by only recalculating spawn points when needed.
    - Adding a new tree and closing the prefab picker will now simply create an empty tree (in case you want to drag in a specific prefab)

    Fixed:
    - Not being able to drag terrains into the terrain list
    - Seed for tree spawn chances had them spawn in rows
    - Undo/redo on tree parameters having no effect if "Auto respawn trees" was disabled in the settings
     
    HIBIKI_entertainment likes this.
  4. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,263
    @HIBIKI_entertainment I've submitted an update for the terrain painting tool, once this is available, you can use something like this to trigger trees to respawn every time the terrain is repainted:

    Code (CSharp):
    1. using System;
    2. using sc.terrain.proceduralpainter;
    3. using sc.terrain.vegetationspawner;
    4. using UnityEngine;
    5.  
    6. namespace DEV
    7. {
    8.     [ExecuteInEditMode]
    9.     public class RespawnOnRepaint : MonoBehaviour
    10.     {
    11.         [Min(0.1f)]
    12.         public float minSecondsBetween = 0.5f;
    13.  
    14.         private DateTime lastSpawnTime;
    15.      
    16.         private void OnEnable()
    17.         {
    18.             TerrainPainter.onTerrainRepaint += RespawnMaskedTrees;
    19.         }
    20.  
    21.         private void RespawnMaskedTrees(Terrain terrain)
    22.         {
    23.             if (VegetationSpawner.Current == null) return;
    24.  
    25.             //Only respawn periodically
    26.             if (DateTime.Now.Subtract(lastSpawnTime).TotalMilliseconds < (minSecondsBetween  * 1000f))
    27.             {
    28.                 return;
    29.             }
    30.             lastSpawnTime = DateTime.Now;
    31.          
    32.             //Ensure the GPU textures are copied to the CPU data
    33.             for (int i = 0; i < terrain.terrainData.alphamapLayers; i++)
    34.             {
    35.                 terrain.terrainData.SyncTexture(TerrainData.AlphamapTextureName);
    36.             }
    37.          
    38.             VegetationSpawner.Current.InitializeSeed();
    39.             VegetationSpawner.Current.RefreshTreePrefabs();
    40.          
    41.             foreach (SpawnerBase.TreeType item in VegetationSpawner.Current.treeTypes)
    42.             {
    43.                 //Only respawn trees using a layer masks
    44.                 if(item.layerMasks.Count == 0) continue;
    45.              
    46.                 VegetationSpawner.Current.SpawnTree(item, terrain);
    47.             }
    48.         }
    49.     }
    50. }
     
    HIBIKI_entertainment likes this.
  5. HIBIKI_entertainment

    HIBIKI_entertainment

    Joined:
    Dec 4, 2018
    Posts:
    595

    Ooo you absolute star!

    I had emailed you today as well about integration errors, but my head was being dopey and so was collab, so you can ignore it, if you see it.

    TX
     
  6. Exeneva

    Exeneva

    Joined:
    Dec 7, 2013
    Posts:
    432
    Can this be used to spawn masses of grass as gameobjects (for rendering using GPU Instancer)?
     
  7. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,263
    It adds items directly to the vegetation system, so no GameObjects are involved. Though GPUI works with the built-in vegetation system as well, so is able to grab all the assigned vegetation prefabs and pipe it into its rendering loop (Tree Manager and Detail Manager components).

    EDIT: Looks like some integration is needed for the combination to work better. GPUI expects vegetation items to be deleted through its own components. But there is no API to mirror this behavior. Worst case scenario is having to set up vegetation first, then GPUI.
     
    Last edited: May 2, 2021
  8. mick129

    mick129

    Joined:
    Jun 19, 2013
    Posts:
    228
    How did you managed to make it work with Nature Renderer?
    When I set the Nature Renderer script on any terrain, it will set the grass distance to 0 so I can't spawn any grass on the terrain.
     
  9. mick129

    mick129

    Joined:
    Jun 19, 2013
    Posts:
    228
    This is the error I get.
    Code (CSharp):
    1. Terrain has zero detail resolution
    2. UnityEngine.TerrainData:SetDetailLayer (int,int,int,int[,])
    3. sc.terrain.vegetationspawner.VegetationSpawner:SpawnGrassOnTerrain (UnityEngine.Terrain,sc.terrain.vegetationspawner.SpawnerBase/GrassPrefab) (at Assets/VegetationSpawner/Runtime/VegetationSpawner.Grass.cs:227)
    4. sc.terrain.vegetationspawner.VegetationSpawner:SpawnGrass (sc.terrain.vegetationspawner.SpawnerBase/GrassPrefab,UnityEngine.Terrain) (at Assets/VegetationSpawner/Runtime/VegetationSpawner.Grass.cs:87)
    5. sc.terrain.vegetationspawner.VegetationSpawnerInspector:DrawGrass () (at Assets/VegetationSpawner/Editor/VegetationSpawnerInspector.cs:719)
    6. sc.terrain.vegetationspawner.VegetationSpawnerInspector:OnInspectorGUI () (at Assets/VegetationSpawner/Editor/VegetationSpawnerInspector.cs:174)
    7. UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
     
  10. Dumad123

    Dumad123

    Joined:
    Aug 22, 2013
    Posts:
    16
    Would this package work for real-time procedural generation? If I were to randomly generate the terrain and place objects, then modify the package to generate grass/trees after that?
     
  11. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,263
    If you already have a Vegetation Spawner component set up, with all the vegetation added and spawn rules configured, you can call a function that spawns everything on a specific terrain: VegetationSpawner(Instance).RespawnTerrain(Terrain terrain, bool grass = true, bool trees = true)
     
    Dumad123 likes this.
  12. Incredible201

    Incredible201

    Joined:
    Feb 22, 2020
    Posts:
    2
    Hey! Great asset from what I've seen from yt, but it's not working really at all. I went into the demo and randomized the seed for the trees and the trees moved, but as soon as I put in my own prefab; they all just disappeared. I expected this but when I press respawn nothing happens. The same happens to my grass. I've tried everything I can think of and I'm really confused. When I first installed the package I got an error, messed with some values and it went away but it continued to not work. I've tried reinstalling, nothing. In the Vegetation Spawner Script, it says I'm on 1.0.6; if you could help me that would be wonderful because I'm really looking forward to using your asset :)
     
  13. CogumeloSoft

    CogumeloSoft

    Joined:
    Dec 28, 2012
    Posts:
    88
    Great asset! Has been a huge time saver for me
     
    StaggartCreations likes this.
  14. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,263
    What you can try is to add the prefab manually to the terrain, then paint it using the brushes. If it doesn't show up, it means it's not properly set up for use with the terrain (could be wrong pivot orientation, or exported with a scale of x0.01). The tool uses the same functionality under the hood, so if it doesn't work with manual painting, it won't work with procedural placement either.
     
  15. Incredible201

    Incredible201

    Joined:
    Feb 22, 2020
    Posts:
    2
    Okay, I got it working. I noticed that the trees didn't paint on the terrain with the normal paintbrush but kinda forgot about it. The reason the generator wasn't working was because the mesh filter and mesh renderer was a child to the actual game object. I did this because there is a bug where if you change the layer other than default on the gameObject that has the mesh renderer, it goes invisible. Thank you for the help.

    Edit: I'm not sure if that was the reason, but when I moved the renderer to the parent it worked.
     
    Last edited: Jul 13, 2021
  16. nicholasboshoff

    nicholasboshoff

    Joined:
    Oct 24, 2019
    Posts:
    3
    Hi there, any docs on how to execute it during runtime?
     
  17. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,263
    Not exactly, but you can browse through the available functions in your IDE, they should be pretty self explanatory (but granted, could use more summary descriptions). VegetationSpawner(Instance).Respawn(bool grass = true, bool trees = true) is the broadest, which will trigger all vegetation to respawn on the assigned terrains.
     
    nicholasboshoff likes this.
  18. RistoPaasivirta

    RistoPaasivirta

    Joined:
    Aug 25, 2017
    Posts:
    20
    I like the tool.

    Just a suggestion, allow negative height ranges for the trees/grass.
    When working with real world location data some areas are indeed below the median water level.

    Was pretty easy to fix by myself (just increased the slider ranges to -4000 to +4000).
     
  19. MaximilianPs

    MaximilianPs

    Joined:
    Nov 7, 2011
    Posts:
    322
    @StaggartCreations First of all, Amazing asset congratulation :)

    Wold be nice to have an option to copy the settings to past it on other objects,
    for example for trees.
    Or maybe a sort of group, so instead of choosing one tree per time, we could select 5 trees, and then set a set of options for all the 5 trees, maybe just by selecting the objects from the inspector interface with "shift+click"? :D

    I love your stuff, so keep updating it ;)
     
    StaggartCreations likes this.
  20. daedal

    daedal

    Joined:
    Dec 19, 2013
    Posts:
    31
    Hey @StaggartCreations,

    I tried using your tool with GPUI and at first everything seemed to work well. But then I realized that GPUI wasn't batching terrain details together across multiple terrains. So my performance isn't good enough. Is this a limitation of GPUI or could I have set things up incorrectly?

    I added the GPUI detail managers for the terrains first, then I added items to your spawner one by one. GPUI spits out errors at first with each new item, but once you add the prefabs it sorts itself out. However the draw calls are all duplicated for each terrain.

    Would another instancing solution work better?

    Thanks!
     
  21. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,263
    The minimum range should be based on the lowest point of the terrain as a whole. Though I also noticed in a specific project this wasn't picked up properly, so that's due for a fixerup!

    I had previously attempted to integrate GPUI, but the way it works is that it creates a copy of all the vegetation on the terrain into its Detail Manager components, which is in turn used for rendering. There unfortunately is no way for other scripts to update them with new information, the related functionality is tucked away directly into GPUI's UI code.

    This forces the uncomfortable workflow of having to spawn vegetation first, then set up the Detail Manager components afterwards. And removing them again when you want to modify the vegetation distribution.

    I'm personally more satisfied with Nature Renderer, since automatically updates to reflect vegetation changes. Its a matter of adding a component to the terrain and generally forgetting about it. Though, at the moment it does not render trees, which GPUI does do.
     
  22. MaximilianPs

    MaximilianPs

    Joined:
    Nov 7, 2011
    Posts:
    322
    Just now I understand that I can set a bunch of trees under a single "set"... still a good idea the option to copy/paste/duplicate the entire set... 'cause now I have to set it up from scratch the same array for the woods ;)

    upload_2021-10-18_22-1-52.png
     
  23. daedal

    daedal

    Joined:
    Dec 19, 2013
    Posts:
    31
    Thanks for the info! I just tried the Nature Renderer trial and it's rendering my details at 100x the size they should be, and they are also strangely following the camera with a very low fps... So I might have to live with a crappy GPUI workflow if I can't get that working.

    Edit: Nevermind! Got it working by adjusting the TVE shader settings. NR is running with much better performance than GPUI so that should work just fine for me.
     
    Last edited: Oct 18, 2021
  24. MaximilianPs

    MaximilianPs

    Joined:
    Nov 7, 2011
    Posts:
    322
    Hem.... that's with Sink at 1!
    And with the prefab position at -0.01
    upload_2021-10-23_20-42-38.png

    upload_2021-10-23_20-41-51.png

    Maybe I could child the tree inside a gameObject and positioning even more down on Y to fix it...
     
  25. Dr-Wh0

    Dr-Wh0

    Joined:
    Oct 19, 2020
    Posts:
    1
    Height is estimated relatively to your heightmap resolution, so if you set a low resolution in your terrain properties, guessed height will be very imprecise.

    I've read there that heightmap doesn't influence performances so you can increase it to max value (you can build your terrain topology with low resolution to get a nice looking lowpoly effect then after that increase it).

    I've solved this issue like this.

    Also, editing terrain properties after playing the spawn script will pull up all objects (trees&grass) to the surface... I don't know how to prevent Unity to recalculate position of the trees... It's annoying.
     
    MaximilianPs likes this.
  26. Homeship

    Homeship

    Joined:
    Mar 4, 2016
    Posts:
    9
    Installed VegSpawner. Trying with some grass prefabs. All was OK. Then I changed some grass settings, some VegSpawner settings, added some trees, DIDN't change a terrain. And in result I've got an error:

    IndexOutOfRangeException: Index was outside the bounds of the array.
    sc.terrain.vegetationspawner.VegetationSpawner.InsideOccupiedCell (UnityEngine.Terrain terrain, UnityEngine.Vector3 worldPos, UnityEngine.Vector2 normalizedPos) (at Assets/VegetationSpawner/Runtime/VegetationSpawner.Collision.cs:144)
    sc.terrain.vegetationspawner.VegetationSpawner.SpawnGrassOnTerrain (UnityEngine.Terrain terrain, sc.terrain.vegetationspawner.SpawnerBase+GrassPrefab item) (at Assets/VegetationSpawner/Runtime/VegetationSpawner.Grass.cs:141)
    sc.terrain.vegetationspawner.VegetationSpawner.SpawnGrass (sc.terrain.vegetationspawner.SpawnerBase+GrassPrefab item, UnityEngine.Terrain targetTerrain) (at Assets/VegetationSpawner/Runtime/VegetationSpawner.Grass.cs:87)
    sc.terrain.vegetationspawner.VegetationSpawner.SpawnAllGrass (UnityEngine.Terrain targetTerrain) (at Assets/VegetationSpawner/Runtime/VegetationSpawner.Grass.cs:38)
    sc.terrain.vegetationspawner.VegetationSpawner.Respawn (System.Boolean grass, System.Boolean trees) (at Assets/VegetationSpawner/Runtime/VegetationSpawner.cs:66)
    sc.terrain.vegetationspawner.VegetationSpawnerInspector.DrawSettings () (at Assets/VegetationSpawner/Editor/VegetationSpawnerInspector.cs:808)
    sc.terrain.vegetationspawner.VegetationSpawnerInspector.OnInspectorGUI () (at Assets/VegetationSpawner/Editor/VegetationSpawnerInspector.cs:177)
    UnityEditor.UIElements.InspectorElement+<>c__DisplayClass58_0.<CreateIMGUIInspectorFromEditor>b__0 () (at <fdb4e7a229ae4ef791f6716e71e93744>:0)
    UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)


    I tried to remove all trees, trying to respawn only grass - worked from the start... But error still persist and VegSpawner doesn't spawn anything.

    Screenshot_242.png Screenshot_241.png
     
  27. HIBIKI_entertainment

    HIBIKI_entertainment

    Joined:
    Dec 4, 2018
    Posts:
    595

    Did you make any terrain component changes?
    It looks like it's lost the reference to the terrain.
    you can try clicking off the game object, clicking back on and re-add the terrain in the Vegetation spawners terrain tab and finally respawn al in the settings tab ( _Warning_ will overite anything that's spawned
     
  28. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,263
    I looked over it and I suspect I know why this may have happened. I can't reproduce the error, but possibly found a fix. What you can do for now is to ensure the Cell Size is an even division of your terrain size. For example, for a 1000x1000 terrain, a value of 10 will do, rather than 10. Yet for 1024x1024, any power of two value works best.

    This is to avoid cells bleeding over the terrain like so:
    upload_2021-11-5_14-32-49.png
     
    HIBIKI_entertainment likes this.
  29. Homeship

    Homeship

    Joined:
    Mar 4, 2016
    Posts:
    9
    Screenshot_243.png
     
  30. Homeship

    Homeship

    Joined:
    Mar 4, 2016
    Posts:
    9
    I setted a minimal values for the cells - nothing changes. Error still persists.
    I repeat - first time I added a grass prefabs with default VegSpawner settings (form install) - grass spawned as expected. After I tried to turn some VegSpawner settings - error happened.

    Screenshot_244.png

    I turned off all checkboxes "Collision check" at all grass prefabs. Error dissappeared. But grass meshs weren't spawned.
    Screenshot_245.png Screenshot_246.png Screenshot_247.png
     
    Last edited: Nov 6, 2021
  31. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,263
    If possible, you could send a minimal repo project to my DM's, so I can take a look an pinpoint where the issue lies exactly.
     
  32. Homeship

    Homeship

    Joined:
    Mar 4, 2016
    Posts:
    9
    OK. I'll try to do a minimal project.
     
  33. Homeship

    Homeship

    Joined:
    Mar 4, 2016
    Posts:
    9
  34. coldfire1500

    coldfire1500

    Joined:
    Apr 28, 2018
    Posts:
    11
    Hi, Im using your asset to spawn my trees, the all have logic and extra components attached to it, they appear in the inspector view but after spawning they disappear, any idea why?
     
  35. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,263
    Sorry, completely missed your reply! Probably no longer relevant, but thanks for the repo project, I can indeed reproduce the same error. It's apparently because the terrain is nested under 2 other scaled transform, resulting in a terrain transform that's rotated and scaled, which is highly unconventional. This ends up messing with raycasting, making them think the terrain collider is rotated.

    A terrain doesn't spawn trees as GameObjects, hence any added components aren't going to be kept. It merely grabs the meshes and a collider from the prefab, and uses that for rendering.
     
  36. benmroz

    benmroz

    Joined:
    Feb 6, 2022
    Posts:
    3
    Hi, I'm using your Vegetation Spawner asset and I noticed that when spawning grass with no shading, the placement tool automatically adds a harsh black gradient to the base of the grass material. I've tried most everything to fix this, to no avail. Any ideas? Grass shading issue.png
     
  37. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,263
    For grass the terrain uses the built-in grass shader, which shades the object black, based on the vertex colors of the mesh. So to remove this, the vertex colors would have to be removed from the mesh. This tool merely spawns the vegetation, but the rendering itself is controlled by the terrain system.
     
  38. amit-chai

    amit-chai

    Joined:
    Jul 2, 2012
    Posts:
    80
    Hi! your asset looks amazing! Is there a way to do collision with grass at runtime? like when a vehicle passes over the grass, will be great to be able to hide grass under the vehicle. thanks!
     
  39. HIBIKI_entertainment

    HIBIKI_entertainment

    Joined:
    Dec 4, 2018
    Posts:
    595
    Some 3rd party shaders like The Vegetation Engine and rendering optimisations like Nature Renderer support interaction.

    And can compliment terrain spawners like staggarts here.

    I recommend doing lots of research and contacting developers before making large system purchases like that mind.
     
    mick129 likes this.
  40. HIBIKI_entertainment

    HIBIKI_entertainment

    Joined:
    Dec 4, 2018
    Posts:
    595
    Hey @StaggartCreations,

    We have found something that might be interesting with regard to terrains and transforms.

    This set-up uses terrain tools and has transforms on the parent.
    when changing seed ( or spawning anything)
    you can see it snaps to the edge only.

    The built-in "mass tree" spawn does work in that sense.

    I hope this helps some.
     

    Attached Files:

  41. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,263
    Good catch!

    Traditionally terrains aren't rotated, but I see now that this may inadvertently happen due to the transform hierarchy.

    A quick fix is to change line 42 in the TerrainSampler.cs file from
    Code (CSharp):
    1. Vector3 localPos = terrain.transform.InverseTransformPoint(worldPosition);
    to
    Code (CSharp):
    1. Vector3 localPos = worldPosition - terrain.GetPosition();
    Perhaps about time I submit a new update, there's a couple of changes I've been sitting on for a long while :p
     
    HIBIKI_entertainment likes this.
  42. HIBIKI_entertainment

    HIBIKI_entertainment

    Joined:
    Dec 4, 2018
    Posts:
    595
    You have been busy! but TLC updates are always a pleasure!

    Thank you kindly...yet again! :D
     
  43. Vmaster2000

    Vmaster2000

    Joined:
    Jun 23, 2020
    Posts:
    1
    How do I get it to dynamically generate with my procedural terrain? Currently I can add it but it only gets added to one terrain chunk when I want to make it generate every time a new terrain is created.
     
  44. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,263
    Version 1.0.7 is now available, with some minor fixes and changes

    Fixed:
    - Spawning of trees breaking if a terrain has a rotated transform
    - Collision cache baking also taking triggers into account.
    - Height range minimum value not accepting values lower than 0. It is now based on the lowest point of the terrain(s).
    - Minor UI corrections for the Personal/Light skin.
    - Collision cells not being properly centered

    Changed:
    - New grass items now use a white color by default.
     
    Bwacky and HIBIKI_entertainment like this.
  45. Bwacky

    Bwacky

    Joined:
    Nov 2, 2016
    Posts:
    208
    Ah yes, an update!
    Have you considered making a Vegetation Spawner Pro with Biome Support for $69,99 and take over the asset store as the go-to solution for spawning, like a certain asset once did in the years past, which kinda went into oblivion?
    :oops::oops::oops:
     
  46. HIBIKI_entertainment

    HIBIKI_entertainment

    Joined:
    Dec 4, 2018
    Posts:
    595
    Some well earned coverage for your Lake Project, I thoroughly enjoy talking about Staggart projects and tools.

    :D
     
  47. SuperFranTV

    SuperFranTV

    Joined:
    Oct 18, 2015
    Posts:
    140
    Hello,
    first of all thanks for the great tool.
    I have the following problem, my grass has LOD groups and must therefore be placed as trees because the terrain does not allow LOD for grass. Then when I place the grass (as trees) it is in the air on overhangs, how do I fix this?
     
  48. HIBIKI_entertainment

    HIBIKI_entertainment

    Joined:
    Dec 4, 2018
    Posts:
    595


    A quick observation here, but ensure your prefab default transforms are set to 0,0,0, if they're not this tool likely reads from Y0 but your grass could be say; Y16 for example.
    It's not an uncommon occurence so always worth checking
     
  49. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,263
    Glad you like it! Unity's terrain system doesn't support rotations on the X & Z axis, not for grass nor trees. Ideally, each mesh would align to the terrain surface normal, but everything's really outdated.
     
  50. SuperFranTV

    SuperFranTV

    Joined:
    Oct 18, 2015
    Posts:
    140
    Allready checked it out, on flat Terrain its align perfect.