-
Notifications
You must be signed in to change notification settings - Fork 0
/
shader.vert
91 lines (73 loc) · 1.93 KB
/
shader.vert
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"precision highp float;"
// Vertex positions, normals and uv coords for the fragment shader
"varying vec3 vp,vn;"
"varying vec2 vt;"
// Input vertex positions & normals and blend vertex positions & normals
"attribute vec3 p,n,p2,n2;"
// Input UV coords
"attribute vec2 t;"
// Camera position (x, y, z) and aspect ratio (w)
"uniform vec4 c;"
// Model position (x, y, z)
"uniform vec3 mp;"
// Model rotation (yaw, pitch)
"uniform vec2 mr;"
// Mouse rotation yaw (x), pitch (y)
"uniform vec2 m;"
// Blend factor between the two vertex positions
"uniform float f;"
// Flag to turn off lighting in the frag shadeer
"uniform int unlit;"
"varying float f_unlit;"
// Generate a rotation Matrix around the x,y,z axis;
// Used for model rotation and camera yaw
"mat4 rx(float r){"
"return mat4("
"1,0,0,0,"
"0,cos(r),sin(r),0,"
"0,-sin(r),cos(r),0,"
"0,0,0,1"
");"
"}"
"mat4 ry(float r){"
"return mat4("
"cos(r),0,-sin(r),0,"
"0,1,0,0,"
"sin(r),0,cos(r),0,"
"0,0,0,1"
");"
"}"
"mat4 rz(float r){"
"return mat4("
"cos(r),sin(r),0,0,"
"-sin(r),cos(r),0,0,"
"0,0,1,0,"
"0,0,0,1"
");"
"}"
"void main(void){"
"f_unlit = float(unlit);"
// Rotation Matrixes for model rotation
"mat4 "
"mry=ry(mr.x),"
"mrz=rz(mr.y);"
// Mix vertex positions, rotate and add the model position
"vp=(mry*mrz*vec4(mix(p,p2,f),1.)).xyz+mp;"
// Mix normals
"vn=(mry*mrz*vec4(mix(n,n2,f),1.)).xyz;"
// UV coords are handed over to the fragment shader as is
"vt=t;"
// Final vertex position is transformed by the projection matrix,
// rotated around mouse yaw/pitch and offset by the camera position
// We use a FOV of 90, so the matrix[0] and [5] are conveniently 1.
// (1 / Math.tan((90/180) * Math.PI / 2) === 1)
"gl_Position="
"mat4("
"1,0,0,0,"
"0,c.w,0,0,"
"0,0,1,1,"
"0,0,-2,0"
")*" // projection
"rx(-m.y)*ry(-m.x)*"
"vec4(vp-c.xyz,1.);"
"}"