'how to apply displacement map to a vertex shader in Webgl?
Hello I have an image of displacement like this. I want to make the surface displace more when the part of image getting darker.

Here is my code in vertex shader. However, it doesn't work. There is no displacement :(. I'm not sure where is the problem
varying vec2 v_uv;
varying vec3 v_normal;
varying vec3 v_position;
uniform sampler2D tex;
uniform sampler2D dis;
void main() {
float height = texture2D(dis,uv).g;
vec3 pos = position + height*normal *10.0;
gl_Position = projectionMatrix * modelViewMatrix * vec4( pos, 1.0 );
vec4 pos_1 = (modelViewMatrix * vec4(position,1.0));
gl_Position = projectionMatrix * pos_1;
v_position = pos_1.xyz;
v_normal = normalMatrix * normal;
v_uv = uv;
}
//in fragment vector
varying vec2 v_uv;
uniform sampler2D tex;
uniform sampler2D dis;
varying vec3 v_normal;
const vec3 lightDirWorld = vec3(0,1,1);
void main()
{
vec3 nhat = normalize(v_normal);
vec3 lightDir = normalize(viewMatrix * vec4(lightDirWorld, 0)).xyz;
float light = dot(nhat, lightDir);
vec4 color =texture2D(tex, v_uv);
gl_FragColor = color*vec4(light,light,light,1);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
