Search Unity

[RELEASED] DirectX 11 Grass Shader

Discussion in 'Assets and Asset Store' started by Nonakesh, Aug 17, 2015.

  1. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Yes, unfortunately using multiple blades of grass will cause some visible problems. I think they may be less visible when you enable the "random grass orientation mode". It's a little bit more performance costly, but the billboards no longer rotate with the camera, so it's probably more suited for large textures. I'd also try to create groups of grass. It may no longer be a full field of grass, but it's still somewhat natural looking for grass to grow in groups. It would probably loook even more convincing if you threw in a few bushes and other bigger plants as well.

    Yes, the density jumps up in levels, that's because the shader uses GPU tessellation at it's core. Tessellation has certain levels where it jumps up in density. A large part of the shader is trying to hide the switch of levels. You could try using the different smoothing modes (width, height, alpha) and see if some of them work better than others.

    Here's what I would try:
    - Add a MonoBehaviour to the floor, it's not supposed to do anything on it's own, but it will hold both the density texture of the grass material and the grass textures for each grass type.
    - Whenever you're cutting grass, cast a raycast below the player, take the current texture coordinates / UV and search for the data MonoBehaviour on the object below.
    - Check at the density value at the tex coord of your density texture, then create 4 particle effects and let them emit a value based on the current density. If the MonoBehaviour wasn't found don't cast anything.

    That's probably the simplest way to get accurate results.. if you don't care about it being perfectly accurate, just add a tag to your floor when it has grass, emit particles if the tag is there.

    You should try changing the intensity/priority parameter on the material (not sure what I called it exactly, it should be the first one in the list). It simply changes how multiple interactions are mixed together, the higher the value, the more the grass will be changed by that influencer, compared to others.

    No problem, I'm glad I can help!
     
    macish likes this.
  2. DGordon

    DGordon

    Joined:
    Dec 8, 2013
    Posts:
    649
    When Im in deferred mode, the grass ignores the ambient lighting. I'm using 2019.4.19f1. Forward mode doesnt have this problem, it looks great.

    Please fix this asap, or let me know what Im doing wrong, since I need to use deferred for this. Its a cell shaded game and this is absolutely critical for it, and forward is not a possibility for a variety of reasons.

    Thanks!

    [Edit: looking through the forum it looks like this is known. However ... there's no real work-around for this. I don't understand enough to know what the problem is with ambient light here in deferred, but I mean ... how hard can it be to just either grab that like _everything else in Unity I've seen can_, or give us a minimum threshold to set that on the material itself? There's no reason this should go completely black in deferred for any part not touched by a lightsource (ie: directional light). That just means it was built in a janky way for it ... project specs can't be defined by grass o_O]
     
    Last edited: Mar 14, 2021
  3. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    No, I think you misread some of the other posts. The grass definitely doesn't ignore ambient light in ambient mode, that's not even possible, because Unity handles all lighting itself in deferred. The problem is rather that Unity treats the grass like any other diffuse surface, so no subsurface scattering or anything, like with real grass. You could try to force your ambient light to have more light right at the horizon, or you could add a second directional light that fills out the shadows on the opposite side of the main light.
    You can also use the Forward Only grass shader, to force forward mode while Unity is in deferred mode. That way you can use the more stylized lighting modes (including camera oriented blades of grass) or you can use the subsurface scattering setting on the grass type.

    The only "real" solution to the problem would be changing the deferred renderer to include some sort of SSS. It might also be possible to hack something together with emission, make the grass glow, but I'm not sure if it's possible for that to look good.

    The easiest fix is definitely switching to the Forward Only shader version.
     
    DGordon likes this.
  4. PhoenixAdvanced

    PhoenixAdvanced

    Joined:
    Sep 30, 2016
    Posts:
    316
    Hi,

    I have just discovered a really odd bug with the grass in my project, and I'm fairly stumped as to what is causing it.

    I am not sure if it's a bug with the grass itself, or with unity, or something that I am doing.

    The bug seems to occur mostly in the build, as far as I am aware (I think I saw it a few times in the editor too, but I can't reproduce this).

    This image shows the bug:

    https://i.imgur.com/vI3fMl5.jpg

    As you can see, the grass on the right of the image is fine, the grass on the left has an odd dark shadow covering it, almost like a shadow cast by an object, but I have made doubly sure that there are no objects casting shadows on the grass.

    This image is taken from the editor, in the exact same spot:

    https://i.imgur.com/UqcKrrp.jpg

    The frost effect on the screen is just a UI element, but the grass is fine, and doesn't have the lighting glitch.

    I actually ran the game in the build and the editor at the same time, standing in the same place, so the conditions were precisely identical.

    This bug seems intermittent, and doesn't always occur.

    I have a time of day system in my game, so the direction of the sun object changes during the game, but I have tried various rotations and settings, etc, of this, and I can't reproduce this issue.

    It's very difficult to test when I can't reproduce it in the editor.

    Does anyone have any idea where to look to narrow this down?

    Could it be a shader bug? Which shader? Or could it be related to lighting, shadows, etc? I have tried pretty much everything I can think of, including removing all items in the scene that can cast a shadow, and it hasn't made a difference, there's still this random black seam along the grass, which is quite noticeable.

    I'm using unity 2020.2, and I don't recall seeing this on previous versions of unity, I think this is a relatively new bug, but I haven't done much build testing for some time.

    Thanks for any advice!
     
  5. PhoenixAdvanced

    PhoenixAdvanced

    Joined:
    Sep 30, 2016
    Posts:
    316
    Just a quick update, it *seems* that this issue doesn't occur on the first run of my game, it only occurs, as far as I can tell, when I save the game and then load it. When the game loads, this issue seems present.

    I am loading the game simply by storing the players position, and then moving them to that position in the world, so I don't see how this could be an issue.
     
  6. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    It looks like it might be a problem with specular lighting? Is it possible your cubemaps are being recalculated when you reload the game?

    Here are a few things I'd try:
    • There's a toggle in the top section of the shader called something like "ignore ambient specular light" or "ignore specular global illumination" (i can't remember the exact name). Enable the toggle and check if it helps.
    • Try out some of the cheaper lighting modes and see if they work for your art style, for example "unshaded"
    • Try using the "Surface normal" mode, if it fits your art style it might fix the problem
    • Just set glossiness to 0, or the specular color to black
    • If all of that doesn't help, maybe try the random orientation grass mode.
    I hope something here solves the problem, my guess would be that the "ignore lighting" toggle might already fix it.
     
  7. PhoenixAdvanced

    PhoenixAdvanced

    Joined:
    Sep 30, 2016
    Posts:
    316
    Hi, thanks for your reply.

    I am still really stumped by this!

    I managed to reproduce the issue in the editor, which made testing easier, and I have tried all of your suggestions, plus every single unity option and setting I can find.

    I have removed everything from my scene except for a camera, a directional light, and a plane with the grass material on it.

    I have also tried every grass setting.

    All of the grass variants have the same issue except for the "Simple" grass setting (all of the 1 texture, 2 texture, etc, options are affected), so I'm guessing it's a texture issue?

    I am going to try deleting and reimporting the grass next, maybe something got corrupted along the way?

    This is an image of the grass with the issue more easily noticeable:

    https://i.imgur.com/8Pv07jG.jpg

    This is with the stripped down scene, just camera and plane, and a directional light.

    It seems that there are black marks on the grass on one side, but not on the other side?

    Almost like the normals are flipped or something?

    But again, this only seems to occur on a second run of the game (after the player and the objects in the world are moved to their saved location).

    So, it is possible that I am moving the player while something is being loaded or generated in the background?
     
  8. PhoenixAdvanced

    PhoenixAdvanced

    Joined:
    Sep 30, 2016
    Posts:
    316
    Just a quick update:

    I have updated my version of the grass asset to the latest version on the asset store, and the issue still occurs, so it's not an out of date version...
     
  9. PhoenixAdvanced

    PhoenixAdvanced

    Joined:
    Sep 30, 2016
    Posts:
    316
    I believe I have solved this.

    The issue took some time to track down, but it seems to be related to another shader that I am using in my project for rendering snow on top of the terrain and grass during random snowstorms.

    It seems that I can fix this issue by applying the snow effect and them immediately removing it when the game loads.

    I'm not sure why this works, I assume that the issue is caused by the snow shader not being properly loaded before a snowstorm occurs in the game, and applying and removing the shader causes it to be fully loaded?
     
  10. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Sorry for not answering last week, I was very busy. But if it was caused by something external, I'm not sure I could have helped anyways. I'm glad you figured out a solution.

    Honestly, I'm a bit confused how another effect could have affected my shader, unless it's either in deferred mode or the other effect is a post processing effect. On the other hand, most of the lighting uses Unity's rendering functions, so if they somehow override those, it's possible.
     
  11. PhoenixAdvanced

    PhoenixAdvanced

    Joined:
    Sep 30, 2016
    Posts:
    316
    Hi, that's fine!

    I think the other shader may have been a post processing effect, could that have been it?

    I just noticed it completely by accident when a random snowstorm occurred as I was debugging, and I noticed that the shader issue was fixed.
     
  12. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Yes, if a post processing shader uses the current normals / depth and modifies the lighting based on that, I can imagine it causing problem with the grass.
     
  13. DGordon

    DGordon

    Joined:
    Dec 8, 2013
    Posts:
    649
    Assuming HDRP still a no-go? I was using this in my BiRP project (ended up finding the settings to make it work nicely!) but I've since upgraded to HDRP and lost my grass.

    I'm making a tile-based game, so this was perfect for putting grass into the tile prefabs that should have it.
     
    mikelortega likes this.
  14. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I mean you can try the experimental version I made some time ago, but when I made it, it was extremely broken. The project I'm working at right now also uses HDRP, so maybe I know it better now and it would be worth checking it out again. That said, I'm really busy at the moment, so no promises. Last time I tried porting it to HDRP, it was mostly a frustrating and pointless search through its internals.
     
  15. DGordon

    DGordon

    Joined:
    Dec 8, 2013
    Posts:
    649
    no problem. I owned this enough years I cant complain about no HDRP. Thanks for trying :)
     
    Nonakesh likes this.
  16. PhoenixAdvanced

    PhoenixAdvanced

    Joined:
    Sep 30, 2016
    Posts:
    316
    Hi,

    I may have missed something in the Dx 11 grass documentation, but is it possible to add "patches" of grass, so that it's not completely uniform?

    I can see that there is a "density texture" slot in the grass, but this seems to only work for simple grass, and it only works with a specific texture.

    What I am looking for is some way of "randomising" the density for a particular set of grass textures, so if I had four grass types displayed, I could have a different density for each one? I think this would look quite strong.

    Here is an image of my Dx11 grass in a sample scene in unity:

    https://imgur.com/3suKBYG

    I am using multiple textures, but they all have the same uniform density. It might look better if some areas had slightly more or one texture than another.

    In this image, from Far Cry 5, you can see what I mean:

    https://steamcommunity.com/sharedfiles/filedetails/?id=2474233034

    You can see that the reddish grass type is not completely uniform, there are patches with nothing. There are also patches of a white grass type in the foreground.

    I think this effects looks quite good.

    Would this be impossible to add in Dx 11 grass? I am about to spend a few days looking into it, to see if I can add it myself, but before I do, I'd like to know if it's totally impossible, or feasible.
     
  17. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I think the density texture is exactly what you are looking for. Make sure that your mesh has texture coordinates (UVs), then check if your material has texture density mode enabled, there's also a vertex color based mode. The density texture works like this: each color channel (RGBA) represents one grass type, so you can control all of them individually. If you use a black and white noise texture, all grass types will have the same random density.
     
  18. PhoenixAdvanced

    PhoenixAdvanced

    Joined:
    Sep 30, 2016
    Posts:
    316
    Ah, very clever! Would there be any way of randomising this, instead of using a static texture? Or is that even necessary?
     
  19. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Well, I think you should be able to recreate the grass in Far Cry with a texture, depending on the terrain size you'd either need a very large texture or split the terrain into smaller part if you want very small details. Even with procedural terrain, I think I would either try to use a texture or vertex colors, instead if trying to modify the shader.
    Just try noise textures with different resolutions, or use the grass painter to modify the density texture directly.
     
  20. PhoenixAdvanced

    PhoenixAdvanced

    Joined:
    Sep 30, 2016
    Posts:
    316
    I'll certainly do that.

    Is textured grass supposed to work with the density texture? For me, only the simple grass works with the density texture, is that something I've broken?
     
  21. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    It should work as expected. Are you sure there isn't something wrong with the material settings (like wrong density mode)?
     
  22. PhoenixAdvanced

    PhoenixAdvanced

    Joined:
    Sep 30, 2016
    Posts:
    316
    Hi, yeah, I figured out the issue.

    I have actually made changes to how the density works, and I am basically varying the density of the grass types by altitude, so that some grass types appear only in low lying regions, etc. This is all done procedurally.

    I actually implemented my own solution, to combine the existing altitude specific code with the density textures, but it really looks good:

    https://i.imgur.com/WdlOfgQm.jpg
    https://i.imgur.com/A24s8vrm.jpg
    https://i.imgur.com/ReSzroqm.jpg

    I still have some tweaking to do with the grass widths and heights, and the actual density values themselves, but it really works great.
     
  23. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Very nice, sounds like an interesting modification!
     
    PhoenixAdvanced likes this.
  24. nubdev

    nubdev

    Joined:
    Mar 30, 2013
    Posts:
    87
    Yes hello in deferred rendering all grass is transparent unless fully illuminated, is there an easy fix to change render queue or anything else? Thanks
     
  25. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Like, actually transparent? Can you send a screenshot of that? Also maybe a screenshot of your shader settings?
     
  26. purum5548

    purum5548

    Joined:
    Jan 9, 2019
    Posts:
    3
    I'm using URP and try to use Grass Fallback System

    when i tested in default unity 3d, it works well
    but with URP, fallback does not work

    i find the reason is, there is no material for URP Grass Fallback.
    In URP, Grass Fallback use material for Default
     
  27. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Yes, you're right, the Grass Fallback System doesn't have shaders that work with URP. I'm not even sure if the rest of the Fallback pipeline would work correctly when URP is used. I would recommend using the shader directly instead.
     
  28. purum5548

    purum5548

    Joined:
    Jan 9, 2019
    Posts:
    3
    Umm, I'm working on URP Mobile workload, So cannot use shader directly.
    any planning for support or can i use different shader for grass fallback instead?
     
  29. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    At the moment I don't plan to support it, unfortunately. As far as I remember, the shader wasn't too complicated, so you could try recreating it in Shader Graph.

    If not being able to use the Fallback system is a dealbreaker, you can send me your invoice number and I'll give you a refund.

    Sorry for not being of more help, but I've been focusing on a game of my own lately.
     
  30. khos

    khos

    Joined:
    May 10, 2016
    Posts:
    1,490
    Hi there,
    Question regarding Grass Painter and Unity Terrains if ok: I have assigned a material to my Unity Terrain that uses your Grass Shader, I can see the grass as normal, all good it seems. When I open the grass painter I always only see the message "No grass object selected....." in the painter window:

    upload_2021-8-2_14-40-20.png
    If I click on the terrain object it does not get selected...

    What other components must be added to the Unity Terrain to be grass paintable?
    I have checked the tutorials (works ok there but there is not terrain demo!) / docs but there does not seem to be anything specific for this situation.
     
  31. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    You have to add a GrassRenderer component to the terrain, then add your material there. That way the Grass Painter should recognize the terrain as well.

    You should take a look at the NatureMeshFilter (automatically added with the GrassRenderer) as well, you can change how the terrain is converted to mesh there, for additional control.
     
    khos likes this.
  32. khos

    khos

    Joined:
    May 10, 2016
    Posts:
    1,490
    Many thanks, I tried that but the behaviour is the same, I can describe as: When the Paint Dialog Window/View is open, the focus is lost on the Terrain to paint on, I cannot select any object in scene/hierarchy, if I go to Game view, then I can select eg the terrain, but as soon as going back to the scene view, focus is lost and I cannot select any object. A bit odd.

    Maybe I will copy the terrain to a sample scene see what it does there..
    Any info/advice would be appreciated.
     
  33. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    That sounds more or less how it should behave, when the painter is working. You select the terrain in the hierarchy, then it switches into the paint mode and you should be able to draw on the terrain. Are you sure you created the textures correctly?

    You could also watch the tutorial and read the documentation, maybe that helps: https://stixgames.com/grassshader/documentation/grass-painter
     
  34. khos

    khos

    Joined:
    May 10, 2016
    Posts:
    1,490
    Hi,
    Regarding "Are you sure you created the textures correctly?", maybe that could be an issue as on my painter view I do not see any option to create the textures, hmm, why not?
     
  35. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I think it's only visible when there is no density texture in the material.

    Oh and I completely forgot to ask if there are any error messages in the editor console?
     
  36. khos

    khos

    Joined:
    May 10, 2016
    Posts:
    1,490
    No errors in log :)
    Looks like I got it working on my one terrain, after I filled the material (with material that uses your shader) slot in the grass renderer script I added to the terrain. Then it seems I can now remove/add...paint! :)

    I have a 2nd terrain in the same scene, trying to copy/setup that, but seems that is not working et, I must be missing something obvious, but at least I can paint on the one, Cool!

    EDIT: I also had to enable the Terrain Collider for the painter to work :) I think that is part of it.
    Thanks for your help, I should be good now.
     
    Last edited: Aug 2, 2021
    Nonakesh likes this.
  37. khos

    khos

    Joined:
    May 10, 2016
    Posts:
    1,490
    Hi Nonakesh,
    I'd like to ask for some help if possible. I am not sure how to paint multiple textures on a terrain, I can paint one, the material is set to have two textures, if I open the grass painter I do not see any option to change texture, should that be possible? How do I vary the textures when painting or using the material?
     
  38. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Do you mean two grass types with different textures? Or something else?
    For the first one you have to paint the density texture, you can select the grass type in the painter.

    Either way, I would recommend reading the documentation for the grass painter, it also contains a tutorial video that should cover most features of the painter: https://stixgames.com/grassshader/documentation/grass-painter
     
  39. thiskidcalledtom

    thiskidcalledtom

    Joined:
    Nov 9, 2017
    Posts:
    35
    Hello! I am trying to put grass on my terrain. But the grass will only spawn on none-flat edges. My terrain is 99% mostly completely flat. Is there a way to get the shader to show up on flat terrain? Thank you.
     

    Attached Files:

  40. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Is it possible the grass is being rendered below the rest if the terrain for some reason?

    Are you using the GrassRenderer component, or have you added the material directly to a terrain object?
     
  41. MKayJay

    MKayJay

    Joined:
    Oct 8, 2013
    Posts:
    11
    Hey there. I'm hoping someone can point me in the right direction.
    I recently purchased this asset, and it's great and easy to use, for the most part. My need for it is more procedural, which I believe should be doable with this asset, but I keep getting into the problem shown on the screenshots below. The grass is either too short, bends too much, or simply doesn't show up in places. As a side note, I've encountered similar issues with other 'free' shaders, which was why I decided to go for a paid one, as this one definitely shows promise.


    If you look at these images, and immediately have a hunch of what could be wrong, please do let me know, so I know what to investigate.

    What I do is I generate the mesh for the terrain at runtime. At the same time, I generate another gameobject that uses the same mesh for the grass. Here I add a Meshrenderer and Mesh filter, along with the NatureMeshFilter and the GrassRenderer. All of this is added via code at runtime. Of course, the Grass material is set up, and also added at runtime.
    Now... If I just leave it like that, I actually get pretty decent results. The entire mesh gets very good-looking grass, but that is not what I'm aiming for. I want to only draw the grass, based on certain criteria. For the case of just getting it to work, I tried to simply base it on the heightmap; so what I do is I take the heightmap, and generate a displacement map based on that, which may be where I go wrong. Basically, I make a color map, where I only manipulate the alpha value (height of the grass), convert it to a texture, and use that as my displacement texture.
    Now I also tried with other displacement textures (By replacing them via the editor at runtime) and that produces similar issues, albeit it may look slightly better.
    I've also considered if it was due to a lack of UVs, but I tried generating them (also by code, at runtime) but seems to do nothing.
    I'm somewhat at a loss here, so any hints or input would be greatly appreciated - Thanks :)
     
  42. khos

    khos

    Joined:
    May 10, 2016
    Posts:
    1,490
    Hope it is ok to share, video of how I use your grass shader:
     
  43. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    I think you're pretty close actually. First of all, did I understand correctly that you are creating two meshes, one for the terrain and one for the grass? Because that's not necessary with the nature mesh filter, it already does that internally (copy the mesh and pre process it if necessary).
    You do need UV coordinates for your mesh if you want to use textures, that's necessary for all types of texture inputs.
    I think you are using the wrong texture property: the displacement texture is part of an older system and it's probably never the thing you want to use. Instead you should use either the "Color (RGB) Height (A)" texture, or the Density texture. You can check the shader file (Grass.shader) for the exact property name if you want to set it procedurally, I can't look it up right now.

    For procedural generation you could also use vertex colors, the shader has a "Vertex Color" density mode, it's basically the same as a density texture, each color channel represents a grass type, a color channel with 0 brighness represents no grass, 1 means full grass.

    I hope that helps!

    Nice use of the displacement! Thanks for sharing!
    Maybe you could try making the transition between "engine on" and "engine off" a bit longer? I feel like it's a bit too snappy, but otherwise it looks great.
    I'm always happy to see my shader in action! :D

    Edit: looking at it again, I don't think it's the animation, that looks quite good, but is it possible that the interaction renderers sometimes clip out of the displacement camera or something like that? Maybe changing the height and render distance of the displacement camera could help here?
     
    MKayJay and khos like this.
  44. khos

    khos

    Joined:
    May 10, 2016
    Posts:
    1,490
    Thanks, I'm not changing the size of the interaction object yet, but only moving it according to the rudder direction, literally on/off if hover /lift speed, I'll make smooth the object size etc to improve the effect :) I just like how the wind effect from props work on the grass , cool!

    Here is another vehicle with a similar effect:
     
  45. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Very nice! :)
     
  46. MKayJay

    MKayJay

    Joined:
    Oct 8, 2013
    Posts:
    11
    Hey, thanks a lot for the support. Took me a while to look at your feedback as I've been on vacation :p
    Now I'm back, and I've been struggling for a whole day now, still not entirely there. I took your advice to heart, and have a better understanding of how things work, but I still don't get the results I expect.
    For the following I'm about to show, I've done this both with Vertex Density Mode, and Texture Density Mode (And of course setting my 'density texture' to the right variables (_ColorMap and _DensityTexture respectively)
    The thing is. I do get a lot of nice-looking grass now, but not in the places I expect.
    So first off I generate my density texture. For testing purposes, I've created a very simple one, either White (with alpha 1), or black (with alpha 0):

    By comparing that to my terrain, it is (from how I expect this to work) quite obvious where the grass should be (Basically not in the water). The screenshot is taken at runtime from the editor (Note I cannot see grass in the Scene view, only in Game View)

    By walking around on the map, I encounter quite a lot of cases where the grass is definitely not in the right place, like here:

    Additionally, I encounter large areas with no grass, where I'm certain the grass is supposed to be.

    I've also ensured that every chunk has a 'density texture' that matches each one of them, (By extracting the texture and comparing it to the mesh terrain).

    First off: Am I misunderstanding something, or shouldn't my 'density texture' (Is that the right name?) not work as I described above?

    So what I've tried:
    I've tried flipping the texture, in various ways, but no success. Since I'm unable to see the grass from the scene view (And for performance reasons, the distance would be quite short anyways) It's hard to zoom out and get a good look at the generated grass pattern.
    • I've switched back and forth between texture density and vertex density, but I get similar results.
    • I also played around with the 'Object Space Mode' boolean on the shader, but no dice.
    • I've tried various ways of generating the texture, also with more smooth values, rather than just harsh 1 and 0 values, but I figured it was at least easier to test/debug with only 1 and 0 as the output color.
    • I've also ensured that it is not only on chunk borders that this occurs (In fact it seems unrelated to that and even with only one chunk, I still have the issue)
    So basically my questions are:
    - Am I doing something wrong, if what, please point me in the right direction?
    - If this is not related to how I use the Grass Shader, I'd still appreciate any thoughts on which direction I could look into, but seeing as my 'density texture' looks alright, compared to the generated mesh, I'm at a loss.I know there are a few minor imprecisions on the 'density texture' but those are acceptable for testing.

    Thanks in advance for any input :)
     
  47. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    That's honestly the first thing I thought of as well.

    I'm quite sure you have the right understanding of density texture, especially with the black and white map.

    That's completely unrelated, it just helps with creating moving platforms and the likes. By the way, as far as I remember I tried explaining all properties with tooltips, in case you missed that.

    I think what I would try is to understand how the grass is behaving strangely, have you tried scaling the whole terrain down so you can see the pattern at once? Here's one thing I could imagine, at least for vertex density it could be color bleeding: there's only one density value per vertex, so it might not be fine-grained enough for smaller detail. As for the density texture, I'm really not sure, but I'd still guess it's a problem with the UVs somehow.

    Here's another idea! Take one of your generated meshes and try painting on it with the grass painter, just get a rough estimate of what you'd like, then look at the texture. If you need help with the painter, I'd recommend watching the video tutorial I made for it.

    In case that still doesn't help, you could send me a scene with the generated terrain via private message and I could look at it, maybe I can find the problem.
     
    MKayJay likes this.
  48. MKayJay

    MKayJay

    Joined:
    Oct 8, 2013
    Posts:
    11
    Hey again.
    Thanks a lot for your input. I think I've come close to a proper solution thanks to your suggestions. I scaled down my world considerably so I could easier get an overview of how things looked.
    So... I had a total of three different issues, two of which I've now solved.

    First off, for reasons I don't understand, I need to both flip my generated textures vertically and then rotate it counterclockwise 90 degrees. The reason I don't understand this is that I generate my density map directly from my heightmap, meaning orientation and size and everything are exactly the same, (Even double checked by saving the images and comparing them in an image editor) but this is something I can live with for now.

    Secondly, I made a mistake in my code assigning the texture to the material. Since I assigned the material through the inspector, and then reused that for every new chunk of my terrain, I ended up with having the same texture assigned on all materials, as it seems that the material was shared (Although the inspector was showing each material as an instance.) I fixed this by simply constructing a new material for every terrain mesh.

    Now, everything looks as expected, except, if I do static batching. I guess this has something to do with the mesh being reconstructed. This issue isn't too apparent on larger chunks, but as I scaled down for testing, the grass started floating in mid-air, until I stopped doing any static batching (I do it for all the terrain meshes I generate).
    I'll continue to play around with it for a bit. I'm considering trying to add the GrassRenderer/NatureMesh, after the static batching has been done, but that takes a bit of rewrite of my code to try out.

    Anyways, for now, I'm very satisfied with the results- Thanks a lot for your support - I really appreciate it ;-)
     
  49. Nonakesh

    Nonakesh

    Joined:
    Aug 27, 2011
    Posts:
    576
    Yeah, that's possibly either my fault or the fault of Unity terrain. Coordinates can get really confusing, I'm sure you're aware, but most of all, I originally tried to match the UVs to Unity terrain. It's definitely possible that there's some strange stuff going on in my code. Glad you could it out though!

    Don't forget to also Destroy the material whenever you remove a part of your terrain. If you don't do that manually, I think it will cause a memory leak (although probably relatively small).

    You could also look at the NatureMeshFilter directly, it doesn't do anything special, just take a reference to your mesh and provide it to the grass renderer (unless you use any pre processing), maybe you can use that somehow.

    I'm glad I could help! :D
     
    MKayJay likes this.
  50. PhoenixAdvanced

    PhoenixAdvanced

    Joined:
    Sep 30, 2016
    Posts:
    316
    Hi,

    I've been using this resource for some time now, and I've just noticed what I think is an issue with the grass interaction system.

    I currently have grass excluders on my players, vehicles, etc, to prevent grass from appearing below them. I am using the "interaction object" with the "grass interaction/interaction" shader. This works fine under normal circumstances, however, my game allows players to land on realisitic planets, which means the world "up" vector can be arbitrary.

    I have noticed that at non y-up rotations, the grass displacement is not working correctly. Clearly, there is a normal somewhere, likely in the meshnormalrenderer.shader file, that is tied to world up.

    What I would like to do is pass in a worldnormal from unity (which can be arbitrary) to the shader to allow it to properly exclude the grass at any orientation.

    I'm certain that this is not expected behaviour, but unique to my use case, but can anyone provide any advice on how to fix this?

    Is it even possible?

    I can see a "worldNormal" line in the shader, but changing this myself doesn't seem to work.

    Thanks for any advice!