Displacement Vertex Shader

WavyGravyDisplaceBall.1.jpg
This demo shows how to use displacement mapping in a vertex shader, rather than using SOP vertex displacement, which is both much faster and allows for higher polygon density.
For an extra glitch effect, it allows you to drive displacement in a custom direction (instead of simply displacing along the vertex normal), using a texture. See description inside network for more details.

DisplaceGLSL.tox (10.2 KB)

2 Likes

Really nice, thanks for sharing!

looks amazing, is this supposed to work out of box? i get a plain blue screen!

playing with waviness to get colors, but still blue screen.
an tips to get this to work? thx
not master at glsl here but up for learning. if only I could see something happening!

Go inside the network and check cam1, the main rendering camera. You should be able to see something in the viewer, and also int eh Render TOP beside it. The blue is the background that is composited in after the render so it sounds like you are getting nothing from the GLSL shader to make the render show up.

Look for warnings or errors on any of the nodes inside the network.

Ben

It does work straight out of the box (for me at least. Just tested it again).
It could be a problem with your version of glsl. Straight blue means the sphere object is not rendering.
If you go into the main node (‘DisplaceGLSL’), and find the node (‘phongGLSL’), next to that is an info dat that shows error messages from the shader, which should throw a message if there is a problem compiling the shader.

To use this in your own networks, just copy the mat node (‘phong1GLSL’) into your own system (that you already know is working and viewable). The mat will throw errors, as it needs tops as input (look on the Sampler page on the GLSL parameter panel), as well as chops the Vectors1 page (for diplace amount).

The major drawback to this method is that the displacement happens through the material, which means that you’ll have to create a look that you want and integrate the vertext shader displacement. So to change the look of your material, you’ll need to create a new phong mat, dial the look that you want, then export it as a glsl mat using the ‘output’ button. Then you’ll need to edit the vertex shader of the new mat that you just created to contain the displacement part of the original glsl (copy and pasting might work, but you might have to work through some error messages).

One thing that is lacking here is the recalculation of normals.
Matthew Ragan has a lengthy tutorial that explains how to do this.
Check it out.
youtube.com/watch?v=JRSHoP0 … tu.be&t=91

thx for that, all works fine now, finally getting a bit of joy from GLSL.
very impressive your patch, I’ll probably never come up with something that complex, but good fun using it and helps global understanding.

I’m still on quest ; find out how TAS does that this kind of displacement if anyone has any clues.

youtube.com/watch?v=S893YS3Ix5I

I think what is quite confusing for GLSL rookies like me , in general is having GLSL multi(purple) and GLSL phong(yellow), a bit mixed up, when do you use this one or that one, you can kind of do the same things with both slightly.

One is more of material that can deform and the other one is more for composting techniques and geo shaders for what I get.

GLSL multi is identical to the GLSL TOP, except it allows for more than 3 inputs.
GLSL top is simply 2D image generation, shadertoy style.
GLSL Mat is the material, which is a bit different, as it works in the vertex > geometry > pixel pipeline, and basically constructs the material in the shader.

You can use the same code from a GLSL top in a GLSL material pixel shader, as they are similar. But better to keep them separate, and use the top as an input into your material.

TAS is crazy good. So, a big part of the trickery there is that the displacement is not going in just one direction. In most displacement setups, the vertex shader move its vertex in the direction of its normal, by the amount based on its color value.
In the vertex shader, only one vertex is processed at a time, as the shader only knows about one vertex (all the others are being processed simultaneously on another thread in the gpu).
Each vertex has these attributes, which are global variables (the are declared for you).
P - world position
N - normal (direction the vertex points toward, ie outward, used for rendering smooth surfaces).
UVs (vUV.st is how you reference this)
Color - the vertex color
So in displacement, the vertex is moved in the direction of the normal, by the amount of the color.

The trick TAS does to get those nice drops, is to alter the normal, so that displacement can occur in a direction other than just straight up. By altering the normal, you can displace in wacky directions, and get that droplet or mushroom curl displace effect.
My example does have a normal texture input that you can use to alter the direction of displacement. It’s kind of mind bending, but you are almost there.
If you follow along with MR’s tutorial, he explains how to get the surface to render properly so that the shading looks right. This has to do with calculating the vertex normal attribute AFTER displacement occurs. It’s a long tut, but totally worth understanding. Thank you MR!!

Also, if you want to dig deeper into understanding glsl tops, shadertoy style, I highly recommend the Art of Code series on youtube.
[url]https://www.youtube.com/channel/UCcAlTqd9zID6aNX3TzwxJXg[/url]
It really helps with the mental gymnastics of UV manipulation as painting.

1 Like