biswas08433 It should work with any mesh.
Infinite ground grid shader
posted this on godot shaders, reposting here in case it's missed.
experiencing a weird issue where the projection on the y axis is wrong if the camera is far from the origin. you can test by setting the camera’s z position in the demo project to something like 50 and you’ll see the grid appears much below y 0.
the camera's fade distance also seems to shorten the further the camera gets from the world origin. when far away, the grid will start fading out much sooner.
joshbromberg Fixed the shader code. Please test it. The fade distance was wrongly calculated from camera instead from origin. Thanks for spotting it.
xyz it works! thanks for fixing.
not sure if this is the intended use of it or not, but do you know how i could keep the fade consistent in screen space? when the camera is far away the perceived size of the grid shrinks. i think it would make sense to work this way out of the box since it's supposed to be an infinite grid.
joshbromberg i've done this myself just by updating the fadeEnd parameter based on the camera position's length (distance to origin)
joshbromberg Yeah, you can update shader parameters based on what the camera is doing.
- Edited
Excuse me for being a beginner, but how do I pull this into my project?
I see it says: "Usage: Assign a shader to a 3D sprite or a quad and put it directly in front of camera so that it covers the entire view." Still, I have no idea how.
Thank you, great stuff.
Yes I did, but I still don´t know how to bring this stuff into my existing project. For example, how do you assign a shader to a node? And where do you put in the code, as it is not a regular attached .gd script? I have not dealt with shaders before. Thx.
- Edited
vfung Assign a new shader material to the object, create a new shader for that material and with that shader selected paste the code into the shader editor. The official docs cover it in detail: https://docs.godotengine.org/en/stable/tutorials/shaders/shader_materials.html
Thanks, I managed to create the shader material and pasted the code into the shader editor. However I still don´t see the shader "screen" to place in front of the camera.
BTW, the shader code was saved into a .gdshader file, but in your project there is no such file, it looks I´m doing something different.
vfung Try to replicate the node setup from the demo scene. The geometry that has a shader must always be in front of the camera, obscuring the whole camera view. This may seem unintuitive at first but that's how the shader works. It needs full screen "coverage" to draw on.
In the demo project the shader resource is embedded in the scene file but this doesn't really matter. It can be saved in a separate file as well.
Thank you, I got it work.
Hello again. I would like to turn on/off grid depending on the zoom factor (camera distance). Is there a way to do it? Thx.
- Edited
vfung I would like to turn on/off grid depending on the zoom factor (camera distance). Is there a way to do it? Thx.
You can just toggle the visibility of the quad the grid is rendered on, depending on your criteria. Or alternatively, set the alpha component of gridColor
parameter to zero. With the latter approach you can even gradually fade it.
Thanks xyz, but can you please explain in more basic terms. How do I toggle visibility of the quad?
Got it, thank you!
Hey @xyz, big thanks for creating that shader. It's been a lifesaver. I've got a question though. I'm trying to get the grid to display on top of other objects. I tried putting it on a quad mesh, but the grid always ends up behind everything else, no matter what I do to move the quad mesh above other objects. Any tips on how to fix that? Thanks a ton!
- Edited
usual_efficiency_410 The shader outputs proper depth so it's always drawn "on the ground". If you want it to be drawn over everything just replace the line that assigns to DEPTH
with:
DEPTH = 0.0;
Note that you'll also need to keep your camera facing quad or sprite closer to the camera than any other object.
Btw don't assign the shader to a horizontal ground quad or plane because that's not how it's really intended to be used.