Saturday, December 2, 2023

Grass Geometry shader:

Grass Geometry shader:  Unity Version 2022.3

HLSL:
I’m a non-graphics programmer, learning graphics programming and I am attempting to prototype a scene with grass.  My first attempt to get something usable was a
a ShaderGraph Shader wrapped over some simple geometry Plaines then instanced in the scene 100’s of times.  It looked nice but the fps was horrible, something like 10fps - just for grass.


 

Geometry shader:
The internet told me I would need to use a geometry shader, so I used all my knowledge (Google), found some good examples, and started writing my own. Here is how it looked right at the start just getting a triangle to appear on each vertice of a mesh:



Pseudorandom Number Generation (PRNG):
The next thing is to increase the blade count per triangle.  For this I need a Pseudorandom Number Generation (PRNG), I’m using the following line of code for it: 

"float4 rando = float4(frac(sin(i * 43758.5453 * normal.x)), frac(sin(i * 143316.6325 * normal.y)), 0.0, 0.0);"

Note:  “i” is the count for each blade iteration on each triangle:

Just to explain for anyone new like me.  Frac, is the number after the decimal, and sin (Sin cos tan..) is great because it produces a number between 1 and -1, so this can be used to refactor large numbers easily to create a random number when normalised.  The diagram below shows how I get a point inside a triangle (A to B to C).

The function rando is used to get a random number between 0 & 1 based off the first vertices normal as a seed:

  • Blue = (1 / (A to B) ) * rando.
  • Red = ( (A to C) - Blue) * rando.

Then you can create a point by adding the vectors as shown above to get a new P1.


Known Bugs: 

  • My triangulation code is flawed because it assumes a right triangle, unintentionally, but it’s good enough for now and works at around 80% accuracy.
  • The randomisation is neutralised if the mesh plane is completely flat with uniform triangles because the UVs are all the same, so the number I use to start the randomisation is either 1 or 0:   Therefore, the multiplier is either producing 0 = nothing, or 1 the same number as previous.  I temporarily solved this by making sure the mesh’s vertices in Max are not equal to  float4(0.0, 0.0, 0,0, 0,0).
  • The starting blade foreach Triangle is stuck at the first vertice, I think this is a starting seed issue, easily fixed, but I’m going to leave it for now as I can add more blades and the randomness is almost ok.


Finaly:
Around 90fps give or take:

Colour options:


If you are trying to do this yourself here are things you will need to know;  How to get a random point in a triangle using multiplication. This is for the new point of each blade.  And how to create random numbers, using as little GPU power as possible.

 

Thanks.



Thursday, November 9, 2023

Depth Fog VFX:

Rendering Depth fog:

Unity Version 2022.3

Hi, Martin here.
For my first real post, I have an update to my Fog render feature I have been working on forever.  The reason - I am still getting used to HLSL and ShaderGraph, as I think this will help me produce the look and feel I want for my game.  Although I have learned a lot I still I’m nowhere near where I need to be.  This learning process is so I don’t have to use Unity’s out-of-the-box tools, such as depth fog, part of the fun for me is learning how to create such things and being able to fix things if they go wrong.

 

 

 

 

 

 

Fog:
The easiest way I found to get fog into my scene was to use a ShaderGraph material added to a custom render feature.  I found ShaderGraph a great tool to visualise the maths/interaction of the pixel and fragment shaders “parts”; It’s helped me understand things like Lerp, Depth, clamping etc.



I was able to get a first pass to work with some controls for; colour, start and end points and fog thickness.  It looks good.
 

I then switched to HLSL; this is an old code sample.  I eventually got it to work in HLSL, please don’t run a check on my Fragment shader, I’m still learning:



Both ShaderGraph and HLSL versions make use of the depth texture, this has exposed a problem.  When the camera angle changes, the fogs near and far planes change with it.  At this stage I’m not experienced enough to fix this, if it even is fixable - the only thing I could think of is locking the y camera camera plain?  Searching around I was advised to use distance fog, but it didn’t help with close-ups due to the curve effect from the camera position.  As the use case is for small rooms or close-ups etc I come up with a temporary alternative using the VFX Graph.

So here is my depth Fog using the VFX Graph.


A better term would be “World space Linear Layered Fog”, Allow me to explain:  Imagine the back of a room, with 30 sheets of huge tracing paper over it, that is the effect here.  I think it works well and it’s much more resilient to camera angle changes.  The number of layers can also be adjusted, improving the effect.  On the plus side, the framerate is around 100fps.  I’m not sure why as I thought the GPU was doing the heavy lifting here but my laptop fans go full power.
 


#indiegamesdaily #indiegames #madewithunity #videogame #gamedev #indiedev #indiegame #steam #indiegames #gamedevelopment #gameart #indiegamestudio #indiegamedev #indiedevelopment

Grass Geometry shader:

Grass Geometry shader:  Unity Version 2022.3 HLSL: I’m a non-graphics programmer, learning graphics programming and I am attempting to proto...