How to displace particles using shader

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By DamonR

I have followed Bastiaan Olij’s tutorial for generating grass using particles and shader (https://m.youtube.com/watch?v=uMB3-g8v1B0&feature=youtu.be). It has mostly worked, and grass is generated and follows where the transmitter is placed. However, the displacement along the y axis is always off. I have tried using different shaded height maps, and testing different variables, but have not been able to recreate the effect of following the height map properly.

I have not heavily modified the code, which can be found from his tutorial. Maybe something has changed in the shader code since 2018, I have only been experimenting with shaders for a few months. Any help is appreciated.

Thank you for your time.

Damon.

Sorting the comments by new reveals this suggestion: “An excellent tutorial, as usual, Bastiaan. I’ve found that in Godot 3.2 I have to set the heightmap texture’s “Detect 3D” flag to “true”, enable “Repeat” and reimport it in order for the shader to read the “.r” value as a height.”
Does that solve your issue?

DDoop | 2020-07-05 15:24

Hi DDoop,

I did see that comment when i first tried to get the heightmap to work.
Doing what was said in that comment is needed to get the texture to work as a heightmap, but it has been after getting that fixed that it still doesn’t align properly along the y axis.

Thank you for your time.

Damon.

DamonR | 2020-07-05 15:34

:bust_in_silhouette: Reply From: albinaask

The shader syntax has been added to since 2018, but the code written in 2018 will to the extent of my knowledge work fine today, Since your code is not identical to the one in the tutorial(in which case it would do the exact same thing), you may only be of by a minus sign, which will throw your math of, it has happened to me many times :). That fact in combination with the fact that mathematical algorithms are horrible to debug makes shading one of the most difficult parts of game development. I will leave you with some of my techniques that I commonly use to debug shaders:

Try to analyse mathematical patterns in your errors, i.e if the grass is constantly appearing 1 meter above the ground then a one maybe should be a zero, or vice versa. If it follows some other mathematical formula, I’d look for that in my shader. I’d try to render them in one solid colour, in order to check whether it’s the vertex coordinates that are off or the UV’s of the particle. Another thing I’d try is to check whether the generated UV’s from where you sample the height map is correct by simply colouring each fragment in the samples colour, that way you know that that scale is correct. Another thing to check whether the amplitude of the terrain is equal to the one generated in the shader, aka, the terrain and grass may line up at some height but not at another.

Those would be the first checks I’d do, they will help you narrow down your search to something manageable and will hopefully you get a good understanding of the problem you’re trying to solve with your debug. as always working with shaders, you really can’t get any printouts in the console, ensuring you’ve done right, but are left to visual debugging, which takes some time to get used to, but don’t give up, you will solve the problem sooner or later!! If you run these tests, come back with your code and what the problem is I probably can help you better, but as usual there is no fit-all solution to shader debugging, but I hope I have given you some advise on how to wrap your head around the problem. You are quite welcome to come back to hopefully get more substantial advise once you figure out in what way they differ from the terrain, but given the info you have given, there are simply a million things that can throw those strands of yours of course!!

Hi Albinaask.

Thanks for the reply. I have started testing with different patterns and contrasts on the heightmaps. It seems it is my heightmap that is the problem. It worked fine in blender, but doesn’t behave the same way in Godot. I played about with the amplitude and it does allow me to line it up on some levels, but not on others.

I think you’re right and it will be lots of tweaking and seeing. I was hoping there was a simpler way to fix it, maybe something I had overlooked. I’ve created a new project to test different heightmaps and settings, so hopefully I can narrow down exactly what I need to change. I’ve only been playing with shaders for a short time, they are useful and quite powerful (I also use splatmap shaders, very useful), but debugging can be slow.

Thank you for the suggestions, I will work through them once I have the heightmap specific project fully built. I’ll link the project as well in case I missed something simple in my codes as well.

Thank you for your time.

Damon.

DamonR | 2020-07-05 19:13

Hi.

I’ve managed to get a version working, following your advice , going through the model and image files, I found my generated heightmaps had different values than expected. Remaking the heightmaps in an external program and using edited versions to black out some areas has helped align the grass.

Setting the amplitude to the highest point on the land also helped, rather than trying to adjust it to the ground level I was working at.

Still a long way til it looks good, as it goes over some ledges, but it’s a start. Thanks for the advice. And yes, being able to print to console would be great.

Thank you for your time.

Damon

DamonR | 2020-07-09 14:05