PDA

View Full Version : Vertex displacement animation of a mesh



ibequa
08-31-2016, 11:56 AM
I want to achieve a ripple effect on a procedurally generated mesh: distort it's z coordinate so that the mesh kind of floats like a wave in a random manner.
To do that I'm generating a heightmap using a Perlin noise, feeding it into a vertex shader and smoothly (via time uniform parameter) interpolating to the sampled height. But when the interpolation is complete, I need to change a heightmap (to achieve randomness of a ripple), doing that flickers the mesh, that is, a transition is not smooth. How do I smooth it out? The mesh in VBO is just a plain triangulated surface with the constant z-coordinate.
My current vertex shader in glsl es:

attribute vec3 aVertexPosition; // vertex pos
attribute vec2 aUV; // uv coords of a vertex

uniform mat4 u_view;
uniform mat4 u_projection;
uniform mat4 u_model;

varying highp vec2 vUV;

uniform float u_time; // animation parameter
uniform sampler2D u_samplerHeight; // heightmap sampler

void main() {
float sample = texture2D(u_samplerHeight, aUV).r; // heightmap sample
float height = mix(aVertexPosition.z, sample, u_time); // interpolate

height *= 10.0; // scale it, so that changes are noticable

vec3 pos = vec3(aVertexPosition.xy, height);

gl_Position = u_projection * u_view * u_model * vec4(pos, 1.0);
vUV = aUV;
}

Silence
09-01-2016, 02:37 AM
One would normally won't do any animation with the Vertex Shader.

I will advise you to have a look at Geometry Shaders (https://www.opengl.org/wiki/Geometry_Shader). They are more meant to do change on the geometry. GS are however known not to be that fast. And people here and there tend to say they are not really useful anymore (but for one or two specific uses...).

So you can have a look at the Tesselation Shaders (https://www.opengl.org/wiki/Tessellation). They allow another way to do that, but a bit less simply. They also require a more recent hardware than GS.

ibequa
09-01-2016, 08:22 AM
One would normally won't do any animation with the Vertex Shader.

I will advise you to have a look at Geometry Shaders (https://www.opengl.org/wiki/Geometry_Shader). They are more meant to do change on the geometry. GS are however known not to be that fast. And people here and there tend to say they are not really useful anymore (but for one or two specific uses...).

So you can have a look at the Tesselation Shaders (https://www.opengl.org/wiki/Tessellation). They allow another way to do that, but a bit less simply. They also require a more recent hardware than GS.

I don't have any of that since I'm using WebGL 1.0

GClements
09-01-2016, 12:45 PM
One would normally won't do any animation with the Vertex Shader.
That's wrong. The vertex shader is where you'd normally do animation.



I will advise you to have a look at Geometry Shaders (https://www.opengl.org/wiki/Geometry_Shader).

I'd advise you to ignore geometry shaders. There's a fairly significant performance hit for using one, and they're only useful if you need to either change the type or number of primitives, or perform calculations which involve all of a primitive's vertices (a vertex shader sees each vertex in isolation).


So you can have a look at the Tesselation Shaders (https://www.opengl.org/wiki/Tessellation).

Tessellation shaders aren't useful here either.

GClements
09-01-2016, 12:48 PM
To do that I'm generating a heightmap using a Perlin noise, feeding it into a vertex shader and smoothly (via time uniform parameter) interpolating to the sampled height. But when the interpolation is complete, I need to change a heightmap

So interpolate between height maps. The mesh's vertices probably shouldn't even have a Z coordinate (i.e. the Z value at t=0 should come from the first height map). Between t=0 and t=1 you'd interpolate between the first and second height maps; between t=1 and t=2 you'd interpolate between the second and third height maps, etc.

Silence
09-02-2016, 03:12 AM
That's wrong. The vertex shader is where you'd normally do animation.


I'd advise you to ignore geometry shaders. There's a fairly significant performance hit for using one, and they're only useful if you need to either change the type or number of primitives, or perform calculations which involve all of a primitive's vertices (a vertex shader sees each vertex in isolation).


Tessellation shaders aren't useful here either.

So bad I was... :doh: