aboutsummaryrefslogtreecommitdiff
path: root/data/shaders/terrain.vert
blob: dd5b863f27af8c29d488b507df09e824a2184c55 (plain)
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
42
43
44
45
46
47
48
// Transformation matrices
uniform mat4 modelViewMatrix;
uniform mat4 normalMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;

// Vertex attributes
attribute vec3 position;
attribute vec3 normal;
attribute vec3 tangent;
attribute vec2 uv;

// Uniforms
uniform sampler2D tNormal;
uniform sampler2D tDisplacement;
uniform float uDisplacementScale;
uniform float uDisplacementBias;

// Varyings
varying vec3 vTangent;
varying vec3 vBinormal;
varying vec3 vNormal;
varying vec2 vUv;
varying vec3 vViewPosition;

void main()
{
    vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
    vViewPosition = -mvPosition.xyz;
    vNormal = normalize(vec3(normalMatrix * vec4(normal, 1.0)));

    // tangent and binormal vectors
    vTangent = normalize(vec3(normalMatrix * vec4(tangent, 1.0)));
    vBinormal = cross( vNormal, vTangent );
    vBinormal = normalize( vBinormal );

    // texture coordinates
    vUv = uv;

    // displacement mapping
    vec3 dv = texture2D( tDisplacement, uv ).xyz;
    float df = uDisplacementScale * dv.x + uDisplacementBias;
    vec4 displacedPosition = vec4(vNormal.xyz * df, 0.0 ) + mvPosition;
    gl_Position = projectionMatrix * displacedPosition;

    vec3 normalTex = texture2D(tNormal, uv).xyz * 2.0 - 1.0;
    vNormal = vec3(normalMatrix * vec4(normalTex, 1.0));
}