-
Notifications
You must be signed in to change notification settings - Fork 38
/
normal.shader
41 lines (36 loc) · 1.07 KB
/
normal.shader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
:copyright: 2011 by Florian Boesch <[email protected]>.
:license: GNU AGPL3, see LICENSE for more details.
*/
vertex:
attribute vec2 position;
void main(){
gl_Position = vec4(position, 0.0, 1.0);
}
fragment:
uniform vec2 viewport;
uniform sampler2D heights;
vec3 get(float x, float y){
vec2 uv = (gl_FragCoord.xy+vec2(x,y))/viewport;
float h = texture2D(heights, uv).x;
return vec3(uv.x, h, uv.y);
}
vec3 getn(vec3 pos, float x, float y){
vec3 v = get(x, y) - pos;
vec3 perp = cross(vec3(0.0, 1.0, 0.0), v);
return normalize(cross(v, perp));
}
void main(){
vec3 pos = get(0.0, 0.0);
vec3 normal = normalize((
getn(pos, -1.0, 1.0) +
getn(pos, 0.0, 1.0) +
getn(pos, 1.0, 1.0) +
getn(pos, -1.0, 0.0) +
getn(pos, 1.0, 0.0) +
getn(pos, -1.0, -1.0) +
getn(pos, 0.0, -1.0) +
getn(pos, 1.0, -1.0)
)/8.0);
gl_FragColor = vec4(normal, 1.0);
}