Search Unity

Assets [RELEASED - FREE] Surreal PBR Grass Shader

Discussion in 'Works In Progress - Archive' started by TchPowDog85, May 11, 2018.

  1. TchPowDog85

    TchPowDog85

    Joined:
    Dec 29, 2015
    Posts:
    124
    ***************************************************************
    Beta Release - FREE

    So I've decided to give back to the wonderful Unity community and make this a free asset for you guys. If any of you make custom updates, give it to me and I will update the source code.

    Git:
    https://github.com/tchpowdog/SurrealGrassShader/tree/master/SurrealGrassShader

    Setup Tutorial (no audio):


    If anyone has questions, please feel free to ask. I'm not a good tutorial giver, so... But it's pretty easy to setup, I think.

    NOTE 1: If you use a density map, make sure to make it read/writable in the import settings.
    NOTE 2: This does work on meshes, just make sure your Target GameObject has a collider. This uses raycast, so whatever your collider(s) looks like along the Y axis will be the footprint of the grass.
    NOTE 3: The tutorial video is choppy when I pressed play, that's due to the recorder, you can see in the stats screen that I get good framerates.
    NOTE 4: I'm in the process of figuring out the LOD/Distance culling for this shader. I'm not a shader wizard so if anyone wants to help tackle that part, be my guest. Occlusion culling works out of the box, just set the "*_SGRenderer" object to "Occludee Static" and include all children. However, this is the type asset that will (should) be sprinkled throughout your scene. So make sure to use the Density map feature. Density map works on a grayscale, so if the color hit is Gray to Black, it will draw grass.

    Cheers, Everyone!
    ***************************************************************


    So I am currently working on a Grass Shader, rendering each blade of grass. I am using a point cloud technique to get the positions of each blade of grass. This has turned out to be a very efficient way of rendering grass. This is a PBR geometry shader which uses Albedo, Metallic, Roughness, AO, and Emission.

    Different lighting scenarios. All images are 240,000 blades of grass with Post Processing Stack AA (Quality). This is about a 20m square area of grass. I think a final product could handle maybe a 40m (which would be about a million blades of grass) square area before falloff (or LOD). That would be adjustable of course.

    Ambient Wet




    Ambient Dry



    Dawn Wet


    Dawn Dry


    Mid-day Wet


    Mid-day Dry


    Point lights Wet


    Point lights Dry


    Spot light Wet


    Spot Light Dry


    Notice the grass spawns on top of the terrain, conforming to the topology. This also works on meshes, however, it's not a "fur". It cannot spawn around a solid mesh. For example if you applied it to a sphere, only the top hemisphere will spawn grass. It works like rainfall. So anything point on the mesh that can be raycasted vertically in the -Y direction will spawn grass.

    As you can see in the images and video that the performance on this is EXCELLENT. In both the image and the video, I have the Post Processing Stack applied with AA set to "Quality", Bloom, and Vignette.

    Current settings for the shader include:

    Grass Blade 1
    Albedo Map
    Normal Map
    Roughness Map
    Distribution Weight
    Tint

    Grass Blade 2
    Albedo Map
    Normal Map
    Roughness Map
    Distribution Weight
    Tint

    Grass Blade 3
    Albedo Map
    Normal Map
    Roughness Map
    Distribution Weight
    Tint

    Root Settings
    Tint
    Tint Start Height
    Tint Spread

    PBR Settings
    Metallic
    Ambient Occlusion Map
    AO Strength

    Advanced Settings
    Alpha Cutoff
    Grass Height
    Grass Width
    Wind Speed
    Wind Strength
    Randomness Noise Texture

    The whole reason why I started this is because I needed a backyard lawn grass for my game and I got tired of looking for one, so that's what this is meant for. Obviously, it can be expanded to accommodate other grass types. However, theoretically, you could give it whatever grass texture you wanted currently. In the shader settings, you can set the width and height of the grass.

    Here are the current shader settings (this is not final):



    Setup Utility:


    I plan to provide 3 different grass blade textures with their PBR maps in the final product.

    I might provide the Shader in two forms - Unlit and PBR. Unlit actually looks decent and would probably run a little smoother in an end product. This is a DX11 shader and uses Deferred rendering only.

    The stats screen in the images and video above was rendering on my PC:
    i7-2600K (old... very old)
    24GB RAM (why didn't I just get 32??)
    GTX 1060 (I'm told it's comparable if not slightly under an XBOX One X.. ?? Please correct me if I'm wrong)

    I must say, developing this shader and the point cloud technique was inspired by Sam Wronski. He has a great channel on YouTube (World of Zero) that I highly recommend - it's about everything programming:
    https://www.youtube.com/channel/UCJKLCjeujQj-d3JjsbVtkJw
     
    Last edited: May 13, 2018
  2. SpeedySpike

    SpeedySpike

    Joined:
    Jun 27, 2016
    Posts:
    5
    This looks super cool! How does it look in VR?
     
    soleron likes this.
  3. TchPowDog85

    TchPowDog85

    Joined:
    Dec 29, 2015
    Posts:
    124
    Lol I have no idea. I don't even own a VR thingy. Do the VR thingys even support DirectX 11?? This is DirectX11 only. Also, this is PBR materials with high polycount. This will certainly be desktop and console only.
     
  4. TchPowDog85

    TchPowDog85

    Joined:
    Dec 29, 2015
    Posts:
    124
    Released, see OP for details.
     
    Mauri likes this.
  5. TchPowDog85

    TchPowDog85

    Joined:
    Dec 29, 2015
    Posts:
    124
    UPDATE - due to density map, grass objects that have no mesh will be deleted instead of remained empty.
     
  6. gl33mer

    gl33mer

    Joined:
    May 24, 2010
    Posts:
    281
    Thanks for the free Beta release. I have a great use case for this type of shader (especially if I can make it efficient on mobile,etc.)

    Getting these errors on startup:
    Code (CSharp):
    1. Assets/SurrealGrassShader/SurrealGrassCreator.cs(100,41): error CS1502: The best overloaded method match for `UnityEditor.EditorGUILayout.Popup(UnityEngine.GUIContent, int, UnityEngine.GUIContent[], params UnityEngine.GUILayoutOption[])' has some invalid arguments
    2.  
    3. Assets/SurrealGrassShader/SurrealGrassCreator.cs(100,96): error CS1503: Argument #3' cannot convertstring[]' expression to type `UnityEngine.GUIContent[]'
    Thanks.
     
  7. TchPowDog85

    TchPowDog85

    Joined:
    Dec 29, 2015
    Posts:
    124
    I'm not too sure you would want to do this on mobile because of hardware requirements (if you even can). This shader requires DirectX11 for the geometry piece.

    As far as your error:
    I had a moment last night to take a look at this and could not find a solution. I'll have more time tonight and will take a look. It really seems like a Unity bug. The "EditorGUILayout.Popup" class has not changed from 2017 to 2018, it's literally no different as far as I can tell and it works fine for me on 2018. it appears 2017 is not detecting the correct overload. Maybe I can find a way around the issue.
     
    Last edited: May 15, 2018
  8. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    Tried this in unity 5.6 had the same error just // out the line and it worked ...well at least spawned the grass but its all white and doesn't react to the wind. The shader doesn't throw any error messages so either its a unity version issue or the edited out line is the cause? or my crappy computer (32 bit) and old graphics card are at issue.Im in gamma space and direct x11

    Very curious how do u get the grass working like you have. There's very few batches(under 100) and no saved by batching at all yet 10 of thousands of blades of grass?
     
  9. TchPowDog85

    TchPowDog85

    Joined:
    Dec 29, 2015
    Posts:
    124
    As for it being white, make sure your camera is set to "Deferred" rendering and HDR is ON. For the wind, I think the default mat has wind speed set to 0. Try playing with the material settings for that.

    Someone correct me if I'm totally wrong on this, but as for the batching, your CPU is not involved with the rendering of this grass at all. All it's sending to the GPU is a point cloud with minimal information (points and normals). All this point cloud is used for is to tell the GPU where to create the blade of grass. Everything else happens on the GPU, the mesh is created on the GPU. Typically, your model's mesh has to be sent to the GPU with a lot of extra info, this is where batching comes into play. It's CPU dependent. With this grass, the mesh is created by the GPU, therefore, not requiring batching.

    This is different from GPU instancing, where your CPU copies a mesh and sends it to the GPU instead of instantiating a new mesh (this is what I understand of it anyway). Instancing is still CPU dependent.

    GPU geometry shaders are awesome! They are pretty hard to create, but the payoff is huge. In my tutorial, when I add the grass to the terrain (without the density map), that is rendering more than 6 million blades of grass! And you can see my framerate is jumping between ~120 and ~200 (with PBR lighting!) and batching stays around 100.

    It wouldn't be practical to create complex meshes on the GPU, but I figured it would be perfect for grass. The grass in this shader is only 6 triangles per blade of grass.
     
  10. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    Hi LSUT you were right on all counts hdr deferred and wind str at 0 so working now nice thx.Really BOSS of you to share this with the community..I have a entry lvl Gforce 610 GC so its not going to perform anything like yours at 4 mil tri's i think that would be 600k+grass blades my fps was at 2.2 lol.so i think i need to get a better GC when able well and windows 64 and extra ram and just a new computer really.Think a huge benefit with this is that instancing is constantly having to run in update so even the things you don't see are using up processor power whereas here i dont think thats the case when i looked away from the large area of grass my fps jumped above 200.Very nice work you have done here
     
  11. TchPowDog85

    TchPowDog85

    Joined:
    Dec 29, 2015
    Posts:
    124
    The community has a done a lot for me over the past year or so, so I owe it to you guys. Thank you very much. Glad you like it.

    Yeah I would advise to get a new PC if you're wanting to do game development. I would say keep that one though, it's good for doing lower-end testing.

    You could also help the rendering a ton by using Occlusion culling and Camera.layerCullDistances.

    If you look in the "Scripts" folder in my project, you will see a script for the camera. This script allows you to set cull distances for every layer. So if you set your grass objects to a layer by themselves, and set the cull distance to like 20-30, it will only render the grass objects within 20-30 meters of the camera. That combined with occlusion culling will help your frame rate tremendously.
     
  12. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    Ah didn't make the texture read/write just went over the original post to see if i missed something.I really want to make my game run on 32 bit as well as 64 machines but unity uses so much memory that u'd have to squeeze everything into a 800mb total size and im aiming for at least 2.5 gigs so yea 64 bit will have to come at some point.I think this grass would be especially great in small tuffs like around wells or houses.At present im using GO's for my grass.Created them in 60k vert batches works ok for large areas but not so much in a town.
     
  13. TchPowDog85

    TchPowDog85

    Joined:
    Dec 29, 2015
    Posts:
    124
    Yeah, the whole reason why I made this shader was because I needed a lawn grass for a neighborhood. But everything I found was either too tall of a grass texture or just didn't work well at high density. This works well for me though.
     
  14. TchPowDog85

    TchPowDog85

    Joined:
    Dec 29, 2015
    Posts:
    124
    UPDATE - I made a change to address the following error:
    Assets/SurrealGrassShader/SurrealGrassCreator.cs(100,41): error CS1502: The best overloaded method match for `UnityEditor.EditorGUILayout.Popup(UnityEngine.GUIContent, int, UnityEngine.GUIContent[], params UnityEngine.GUILayoutOption[])' has some invalid arguments

    Can you guys try this out. The change only effected the SurrealGrassCreator.cs file.
     
  15. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    Error is gone in 5.6 .well done!. What did that line do i just commented it out when it had the error but I don't see any difference with it working?
     
  16. TchPowDog85

    TchPowDog85

    Joined:
    Dec 29, 2015
    Posts:
    124
    That's where you select the "working" layer for the Creator tool. The target object has to be on a layer by itself in order for it to work correctly. The creator works like rainfall, so if it ran with multiple objects on the layer, you would have grass spawn on other objects along with the target object. The dropdown to select the layer in the Creator tool only contains a list of all your empty layers. Once the tool runs, it moves your Target Object back to its original layer and also puts your grass objects on the same layer as the target object.
     
  17. FreeGameDev

    FreeGameDev

    Joined:
    Dec 1, 2015
    Posts:
    67
    Great Job! Any idea how to port this to the new HD RenderPipeline?
     
  18. TchPowDog85

    TchPowDog85

    Joined:
    Dec 29, 2015
    Posts:
    124
    Not currently. When they release documentation for that, I'll give it a shot. It's still a DirectX11 shader, however, in the shader I use some of Unity's builtin shader functions for the lighting. I would think those could change a bit for HDRP.
     
  19. Tenebris_Lab

    Tenebris_Lab

    Joined:
    May 23, 2017
    Posts:
    35
    Made some updates for you.

    - Added forward version and added shadow support.

    Pull request on the git.
     
    FreeGameDev and Mark_01 like this.
  20. TchPowDog85

    TchPowDog85

    Joined:
    Dec 29, 2015
    Posts:
    124
    Awesome! Thanks for contributing.
     
    Tenebris_Lab and Mark_01 like this.
  21. FreeGameDev

    FreeGameDev

    Joined:
    Dec 1, 2015
    Posts:
    67
    You rock! Checking it out now. ;)

    Quick question. What settings do I need? Right now it is rendering completely transparent.


    Found it! I had forward rendering on. Need differed! Great Job!
     
    Last edited: May 29, 2018
  22. Marco-I

    Marco-I

    Joined:
    May 5, 2015
    Posts:
    39
    Hi everyone,
    first of all, thank you so much to @LSUTiger85 for this wonderful shader he created for us just for free!

    Second, well, unfortunately I got a problem with this.
    I'm trying to create a football pitch. For this, I created two 3d models, with a mesh collider attached to each of the 3d model. This works great! Apart from the fact that the tool doesn't cover the entire model.



    Initially I thought that the model was too big, so I tried to split the model in multiple parts, but same effect (to each single part). I tried to extrude the models (as you can see in the pic) but nothing, then maybe I thought that the mesh collider wasn't working correctly in Unity, so I placed those two cubes in the pic, and I realized that wasn't that one the problem.

    So why the tool isn't able to cover all my plane? :(

    EDIT : Even if I attach a box collider, the effect is the same.
     
    Last edited: May 31, 2018
  23. TchPowDog85

    TchPowDog85

    Joined:
    Dec 29, 2015
    Posts:
    124
    You tried a box collider that covers the entire field?
     
  24. TchPowDog85

    TchPowDog85

    Joined:
    Dec 29, 2015
    Posts:
    124
    The white and green are each two separate models?
     
  25. TchPowDog85

    TchPowDog85

    Joined:
    Dec 29, 2015
    Posts:
    124
    I think this is totally doable, what you'll want to do is create a plane in Unity or your modelling tool that is the exact size of your field. This plane will serve as the Target GameObject for rendering the green grass AND the white painted grass. Create two density masks (black and white images) - one representing the white part, and one representing the green part. Run the grass creator tool twice on the newly created plane, each time using a different density map. Then delete the plane.
     
  26. Marco-I

    Marco-I

    Joined:
    May 5, 2015
    Posts:
    39
    Hi,

    1) Yes, I tried a box collider, and doesn't solve the issue.

    2) Yes, there are two models that are part of the entire pitch (the white part, and the green part). Both have a very basic geometry, nothing special.

    3) From what I saw, anything that is not a simple rectangular plane (or a primitive geometry) doesn't work with the tool, because I even tried with this SUPERSIMPLE mesh:



    ..and as you can see, same effect (ignore the colours). I created the model just extruding a simple plane made in 3DS MAX. The file is an .FBX like the previous ones.

    I spent hours to try to understand why the tool works flawlessly on a simple plane made in unity, and not on something similar as well, made in 3DS MAX, but I wasn't able to find the reason. :(

    Anyway I'll try with the density masks on a simple plane made in Unity, hoping this will at least give me a similar results of what I've in mind.

    Thanks!!
     
  27. Marco-I

    Marco-I

    Joined:
    May 5, 2015
    Posts:
    39
    EDIT :

    I tried with the Grass Density Map, but the results isn't really accurate in some part like with the mesh collider... so I wasn't able to do anything... so sad. :(
     
    Last edited: Jun 1, 2018
  28. TchPowDog85

    TchPowDog85

    Joined:
    Dec 29, 2015
    Posts:
    124
    Mesh colliders are weird, I would certainly stick to box colliders. Especially if it is a flat plane. Thats a general Unity rule of thumb.

    If you don't mind, send me your scene and I'll take a look at it.
     
  29. Marco-I

    Marco-I

    Joined:
    May 5, 2015
    Posts:
    39
    Yeah, but it happens in the same way with box colliders. :( I mean, I had the same feeling, so I created a box with MAX, and using a box collider, but same result. The problem is in the FBX generated by 3DS MAX for sure.

    The scene is basically empty, so nothing to send to you, really. Try with the model in the last picture I sent to you. Try to generate the grass for the entire surface.. and let me know if you can please.

    Thank you.
     

    Attached Files:

    • ABC.7z
      File size:
      3.4 KB
      Views:
      561
  30. TchPowDog85

    TchPowDog85

    Joined:
    Dec 29, 2015
    Posts:
    124
    I just thought of something. Make sure that all of your objects are in positive world space. In other words, make sure your objects are not overlapping the X or Z planes. Once the grass objects have been created, you can set there parents to be your actual green and white objects and then move those into place wherever you want them.

    Also, in 3DS Max, set your object's origin to be the vertex of one of the corners

    BAD


    BAD


    BAD


    GOOD


    If this is the problem, it's a bug obviously, that I need to fix.
     
  31. TchPowDog85

    TchPowDog85

    Joined:
    Dec 29, 2015
    Posts:
    124
    The problem isn't with the FBX. The Creator tool only uses the TargetGameObject's collider, not it's mesh. I am currently at work, but I will take a look at this when I get home. Try the previous post I just posted.
     
  32. Marco-I

    Marco-I

    Joined:
    May 5, 2015
    Posts:
    39
    So, I did try with your method, but I just got the same identical result. It doesn't matter where I place the model in max (or Unity), the tool paint the grass always in the same area.

    So, all right. If you can, take the fbx I sent you, and try to put the grass on the entire surface, and let's see what happens. I'll try again on another PC (with different max and Unity) and I'll verify that there's nothing wrong with these two.

    Thank you so much for your time, and don't worry, let me know when it's convenient for you. :)
     
  33. TchPowDog85

    TchPowDog85

    Joined:
    Dec 29, 2015
    Posts:
    124
    So I've been playing with this and I'm having a hard time figuring out what the issue is. I think it has to do with the complexity of the plane (having cutouts and stuff). Try splitting up your plane into rectangular pieces and put them all together in Unity. You'll have to run the grass creator for each separate object, but I think this would work better. You don't have to use those separate objects in your scene, you can use the big plane, just delete the ones you used for creating the grass.
     
  34. Marco-I

    Marco-I

    Joined:
    May 5, 2015
    Posts:
    39
    Well, look again at the last picture i sent to you about the .FBX you tested.
    In the upper part, the tool just cover half of that portion of the plane, so it doesn't matter how you create the geometry, it seems that there is something that is preventing the tool to cover the entire surface of the .FBX model.

    So, I still believe that the tool has problems when working with external models. :( I knew it was too good to be true.. damn it.

    But okay, I'll try to do the same with the Unity primitives.. Thank you!

    EDIT : I also noticed that (in the same model) the entire shape is composed by 5 rectangles. The tool covers 2.5 of those. Coincidence? :D
     
    Last edited: Jun 5, 2018
  35. TchPowDog85

    TchPowDog85

    Joined:
    Dec 29, 2015
    Posts:
    124
    I'll take a look at it when I get a chance. It's definitely not too good to be true. I wrote that tool in 3 hours time. I made it work with Unity Terrains and Unity plane primitive just to get it out to you guys. I'm sure it wouldn't take much to make it work on external models.

    Even still, if you played with the tool for a bit in it's current state, you can achieve what you're trying to do just by creating a primitive plane the size of your soccer field and adding a density mask to the creator. Once you've created your grass objects, just delete the primitive plane and use your external models as you normally would. And place the grass objects where you want them.
     
  36. Marco-I

    Marco-I

    Joined:
    May 5, 2015
    Posts:
    39
    Like I said, I already tried to use the density mask, it works fine, but the result isn't really accurate like I'd want from it. I think it's a really good way to do things, just not the best option I have right now for my specific model (but I'll try to play more with it).

    I hope it won't take too long for you to add this extra feature, I think it would be a great addiction to your plugin and you could have our extreme gratitude.
     
  37. TchPowDog85

    TchPowDog85

    Joined:
    Dec 29, 2015
    Posts:
    124
    There's another option here. All my creator tool does is creates point cloud objects and assigns the grass material to those objects. You should be able to create your own point clouds (only points, no triangles) in 3ds Max and import that into unity and assign the grass material to it - everywhere you have a point, it will spawn grass. You would want to make sure you do the following though:

    1. Randomly position the points in X and Z (not in vertical Y). If you do not do this, the grass will spawn in a structured grid-like pattern, which will not look authentic. How I would do this is take your current green object(s) of the soccer field and subdivide it until you get a point density that you like, delete all edges, then apply a noise displacement in the X and Z direction to randomize the points' positions. Repeat this method for the white object(s). This is one method, I'm sure there are others.

    2. You do not want 1 point cloud object with millions of points. Cut this up into multiple objects that have no more than 60,000 points. (60,000 is a Unity limit on the point array that it can send to the GPU - however, I believe in version 2018, this was increased to somewhere above 100,000 points. You can play around with this if you'd like).
     
  38. Marco-I

    Marco-I

    Joined:
    May 5, 2015
    Posts:
    39
    Hi LSUTiger85,
    here just to tell you that your script doesn't work in builds, probably because you forgot to put tags that let your script to run only in editor mode.
    Don't do this for me, since I'm looking for other solutions right now, I just wanted to inform you. ;)

    Cheers.
     
  39. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    Man, this is legit. Thanks for making it free! I've been dying to find an acceptable replacement for Unity's abysmal default terrain grass, even going so far as to attempt this very thing myself (with much less success, unfortunately, as I have very little experience with geometry shaders :().

    After trying it out, I'm actually looking into speeding up the point cloud generation as performance on my FX-8150 CPU is, well... a bit slow when trying to cover a 4096x4096 terrain with high density grass. :p After some quick variable rearranging to reduce the number of memory allocations, I believe the bulk of the time is spent performing the raytraces for each grass blade (though I don't have any hard numbers on this, so correct me if I'm wrong). I noticed that RaycastCommand is now a thing with the new Job System which seems perfect for this kind of bulk raycasting so I'm gonna look into that in a while to see if it makes a difference.

    If that ends up helping a bunch, I might submit my changes in a pull request.
     
  40. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    Well, I attempted to use RaycastCommands to speed things up, but I failed to improve performance at all (and ended up just making things slower by a second or so :(). However, I still think that it may have potential as I am very unfamiliar with RaycastCommands and I bet there's some way to batch them that would actually help things. I just don't know how apparently. If someone who is knowledgeable with RaycastCommands (or other raycasting optimizations) wants to have a crack at it, that would be great.

    Of course, I realize my use of this asset is probably an extreme case (for smaller areas it works just great).
     
  41. Rensoburro_Taki

    Rensoburro_Taki

    Joined:
    Sep 2, 2011
    Posts:
    274
    O_O

    .....You are a god! Please continue, realease it, support it and we pay for it! Honestly!
     
  42. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    Hi Tiger
    So I noticed when i add the material direct to a mesh it renders the mesh invisible and based on the amount of vertices the mesh has presents stalks of grass.A higher vertice count on the mesh equals more grass..Is there any way to increase the amount of grass without increasing the vertices on the mesh without using the editor but just altering the shader direct?
     
  43. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    I can answer this cuz I tried investigating this myself. The answer is yes... but only technically (so probably not how you'd like it).

    The geometry shader has a section which specifies the number of output vertices for every input vertex which I think was around 12 in this particular case. In theory you could increase this number and modify the way it generates the triangles to effectively have more grass blades per vertex. However in practice, there's some pretty tight limits as to how much data can be output per vertex.

    Now I'm not an expert in this subject so I'm just going to attempt to summarize some of the google results I found, but the gist of it is that there's only a specific amount of memory reserved for the data the geometry shader outputs which effectively limits you to around 30-40 output vertices per input vertex in this shader (due to the number of components it returns). That means that generating more than 3 blades of grass per vertex is out of the question without some heavy modification. I don't think there's any real way around this unfortunately, but if someone more knowledgeable with geometry shaders thinks there's another way I'd love to hear it.

    Hopefully that helps a bit.
     
  44. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    Code (CSharp):
    1. [maxvertexcount(12)]    <-------------
    2.         void geom(point v2g IN[1], inout TriangleStream<Varyings> triStream)
    3.         {
    4.             float3 lightPosition = _WorldSpaceLightPos0;
    5.  
    6.             float3 v0Norm = IN[0].norm;
    7.             float3 noise = IN[0].noise;
    8.             float3 pAngle = float3(1, 0, 0);
    9.             pAngle = rotateVector(pAngle, ((noise.x + noise.z) * 360) * (3.14159 / 180));
    10.             float3 faceNormal = cross(pAngle, v0Norm);
    11.  
    12.        
    Hey Bro
    Thx for the speedy response.
    Is this the section of code you are referring to?Tried changing the 12 to higher values but didn't make any difference unless I went over 30 where it pink shadered me or under 12 where it just spazzed out my graphic card..Really no nothing about shaders tbh.
     
  45. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    Yep, thats the one I was referring to. The reason increasing it didn't change anything is because the actual geometry is being created a bit lower with the tristream. It's essentially a list of triangles to render. The maxvertexcount is just like it says: a maximum.
     
  46. TchPowDog85

    TchPowDog85

    Joined:
    Dec 29, 2015
    Posts:
    124
    Hey guys, sorry it took so long to respond. Unity doesn't notify me when some comments on my thread :(

    You probably could get more blades by modifying the shader itself, like GuitarBro was saying. I wouldn't though, for all practical purposes, you should be able to get a dense enough grass with what I've provided. Also, for visuals, if you increase the height of the grass, it appears more dense (like considerably more) - just a side note.
     
  47. TchPowDog85

    TchPowDog85

    Joined:
    Dec 29, 2015
    Posts:
    124
    Lol thanks. But definitely not a shader god... That was my very first shader to write. (probably hence the bad code)

    One thing that's funny - the new COD:Black Ops 4 Blackout mode uses this same exact technique. Obviously its probably more optimized, but they're definitely using a point cloud to spawn their blades of grass.
     
  48. ju_my

    ju_my

    Joined:
    Mar 7, 2017
    Posts:
    23
    Very nice work.
    Does someone knows if unity plans to implement the possibility of creating that kind of gemometry shader in unity shader graph? And it is possible at this point? Thank you
     
  49. TchPowDog85

    TchPowDog85

    Joined:
    Dec 29, 2015
    Posts:
    124
    I'm not entirely sure, but I think the Shader Graph is only for surface shaders. I don't think it supports geometry shaders. I could be wrong though... it's been a while since I looked at it.
     
  50. mehrankan

    mehrankan

    Joined:
    Apr 12, 2015
    Posts:
    61
    hey man, any chance this can work on the forward path?