/
shader_examples.go
202 lines (195 loc) · 10.5 KB
/
shader_examples.go
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package webgl3d
import (
"github.com/go4orward/gowebgl/wcommon"
)
func NewShader_3DAxes(wctx *wcommon.WebGLContext) *wcommon.Shader {
var vertex_shader_code = `
precision mediump float;
uniform mat4 pvm; // Projection * View * Model matrix
attribute vec3 xyz; // XYZ coordinates
varying vec3 v_xyz; // (varying) XYZ coordinates
void main() {
gl_Position = pvm * vec4(xyz.x, xyz.y, xyz.z, 1.0);
v_xyz = xyz;
}`
var fragment_shader_code = `
precision mediump float;
varying vec3 v_xyz; // (varying) XYZ coordinates
void main() {
if (v_xyz.x != 0.0) gl_FragColor = vec4(1.0, 0.1, 0.1, 1.0);
else if (v_xyz.y != 0.0) gl_FragColor = vec4(0.1, 1.0, 0.1, 1.0);
else gl_FragColor = vec4(0.6, 0.6, 1.0, 1.0);
}`
shader, _ := wcommon.NewShader(wctx, vertex_shader_code, fragment_shader_code)
shader.SetBindingForUniform("pvm", "mat4", "renderer.pvm") // (Proj * View * Models) matrix
shader.SetBindingForAttribute("xyz", "vec3", "geometry.coords") // vertex coordinates
shader.CheckBindings() // check validity of the shader
return shader
}
func NewShader_ColorOnly(wctx *wcommon.WebGLContext) *wcommon.Shader {
// Shader for (XYZ + NORMAL) Geometry & (COLOR) Material & (DIRECTIONAL) Lighting
var vertex_shader_code = `
precision mediump float;
uniform mat4 pvm; // Projection * View * Model matrix
attribute vec3 xyz; // XYZ coordinates
void main() {
gl_Position = pvm * vec4(xyz, 1.0);
}`
var fragment_shader_code = `
precision mediump float;
uniform vec3 color; // single color
void main() {
gl_FragColor = vec4(color.rgb, 1.0);
}`
shader, _ := wcommon.NewShader(wctx, vertex_shader_code, fragment_shader_code)
shader.SetBindingForUniform("pvm", "mat4", "renderer.pvm") // (Proj * View * Models) matrix
shader.SetBindingForUniform("color", "vec3", "material.color") // material color
shader.SetBindingForAttribute("xyz", "vec3", "geometry.coords") // point XYZ coordinates
shader.CheckBindings() // check validity of the shader
return shader
}
func NewShader_NormalColor(wctx *wcommon.WebGLContext) *wcommon.Shader {
// Shader for (XYZ + NORMAL) Geometry & (COLOR) Material & (DIRECTIONAL) Lighting
var vertex_shader_code = `
precision mediump float;
uniform mat4 proj; // Projection matrix
uniform mat4 vwmd; // ModelView matrix
uniform mat3 light; // directional light ([0]:direction, [1]:color, [2]:ambient) COLUMN-MAJOR!
attribute vec3 xyz; // XYZ coordinates
attribute vec3 nor; // normal vector
varying vec3 v_light; // (varying) lighting intensity for the point
void main() {
gl_Position = proj * vwmd * vec4(xyz.x, xyz.y, xyz.z, 1.0);
float s = sqrt( vwmd[0][0]*vwmd[0][0] + vwmd[0][1]*vwmd[0][1] + vwmd[0][2]*vwmd[0][2]); // scaling
mat3 mvRot = mat3( vwmd[0][0]/s, vwmd[0][1]/s, vwmd[0][2]/s, vwmd[1][0]/s, vwmd[1][1]/s, vwmd[1][2]/s, vwmd[2][0]/s, vwmd[2][1]/s, vwmd[2][2]/s );
vec3 normal = mvRot * nor; // normal vector in camera space
float intensity = max(dot(normal, light[0]), 0.0); // light_intensity = dot(face_normal,light_direction)
v_light = intensity * light[1] + light[2]; // intensity * light_color + ambient_color
}`
var fragment_shader_code = `
precision mediump float;
uniform vec4 color; // material color
varying vec3 v_light; // (varying) lighting intensity
void main() {
gl_FragColor = vec4(color.rgb * v_light, color.a);
}`
shader, _ := wcommon.NewShader(wctx, vertex_shader_code, fragment_shader_code)
shader.SetBindingForUniform("proj", "mat4", "renderer.proj") // (Projection) matrix
shader.SetBindingForUniform("vwmd", "mat4", "renderer.vwmd") // (View * Models) matrix
shader.SetBindingForUniform("color", "vec4", "material.color") // material color
shader.SetBindingForUniform("light", "mat3", "lighting.dlight") // directional lighting
shader.SetBindingForAttribute("xyz", "vec3", "geometry.coords") // point XYZ coordinates
shader.SetBindingForAttribute("nor", "vec3", "geometry.normal") // point normal vectors
shader.CheckBindings() // check validity of the shader
return shader
}
func NewShader_TextureOnly(wctx *wcommon.WebGLContext) *wcommon.Shader {
// Shader for (XYZ + UV + NORMAL) Geometry & (TEXTURE) Material & (DIRECTIONAL) Lighting
var vertex_shader_code = `
precision mediump float;
uniform mat4 proj; // Projection matrix
uniform mat4 vwmd; // ModelView matrix
attribute vec3 xyz; // XYZ coordinates
attribute vec2 tuv; // texture coordinates
varying vec2 v_tuv; // (varying) texture coordinates
void main() {
gl_Position = proj * vwmd * vec4(xyz.x, xyz.y, xyz.z, 1.0);
v_tuv = tuv;
}`
var fragment_shader_code = `
precision mediump float;
uniform sampler2D text; // texture sampler (unit)
varying vec2 v_tuv; // (varying) texture coordinates
void main() {
gl_FragColor = texture2D(text, v_tuv);
}`
shader, _ := wcommon.NewShader(wctx, vertex_shader_code, fragment_shader_code)
shader.SetBindingForUniform("proj", "mat4", "renderer.proj") // (Projection) matrix
shader.SetBindingForUniform("vwmd", "mat4", "renderer.vwmd") // (View * Models) matrix
shader.SetBindingForUniform("text", "sampler2D", "material.texture") // texture sampler (unit:0)
shader.SetBindingForAttribute("xyz", "vec3", "geometry.coords") // point XYZ coordinates
shader.SetBindingForAttribute("tuv", "vec2", "geometry.textuv") // point UV coordinates (texture)
shader.CheckBindings() // check validity of the shader
return shader
}
func NewShader_NormalTexture(wctx *wcommon.WebGLContext) *wcommon.Shader {
// Shader for (XYZ + UV + NORMAL) Geometry & (TEXTURE) Material & (DIRECTIONAL) Lighting
var vertex_shader_code = `
precision mediump float;
uniform mat4 proj; // Projection matrix
uniform mat4 vwmd; // ModelView matrix
uniform mat3 light; // directional light ([0]:direction, [1]:color, [2]:ambient) COLUMN-MAJOR!
attribute vec3 xyz; // XYZ coordinates
attribute vec2 tuv; // texture coordinates
attribute vec3 nor; // normal vector
varying vec2 v_tuv; // (varying) texture coordinates
varying vec3 v_light; // (varying) lighting intensity for the point
void main() {
gl_Position = proj * vwmd * vec4(xyz.x, xyz.y, xyz.z, 1.0);
float s = sqrt( vwmd[0][0]*vwmd[0][0] + vwmd[0][1]*vwmd[0][1] + vwmd[0][2]*vwmd[0][2]); // scaling
mat3 mvRot = mat3( vwmd[0][0]/s, vwmd[0][1]/s, vwmd[0][2]/s, vwmd[1][0]/s, vwmd[1][1]/s, vwmd[1][2]/s, vwmd[2][0]/s, vwmd[2][1]/s, vwmd[2][2]/s );
vec3 normal = mvRot * nor; // normal vector in camera space
float intensity = max(dot(normal, light[0]), 0.0); // light_intensity = dot(face_normal,light_direction)
v_light = intensity * light[1] + light[2]; // intensity * light_color + ambient_color
v_tuv = tuv;
}`
var fragment_shader_code = `
precision mediump float;
uniform sampler2D text; // texture sampler (unit)
varying vec2 v_tuv; // (varying) texture coordinates
varying vec3 v_light; // (varying) lighting intensity
void main() {
vec4 color = texture2D(text, v_tuv);
gl_FragColor = vec4(color.rgb * v_light, color.a);
}`
shader, _ := wcommon.NewShader(wctx, vertex_shader_code, fragment_shader_code)
shader.SetBindingForUniform("proj", "mat4", "renderer.proj") // (Projection) matrix
shader.SetBindingForUniform("vwmd", "mat4", "renderer.vwmd") // (View * Models) matrix
shader.SetBindingForUniform("light", "mat3", "lighting.dlight") // directional lighting
shader.SetBindingForUniform("text", "sampler2D", "material.texture") // texture sampler (unit:0)
shader.SetBindingForAttribute("xyz", "vec3", "geometry.coords") // point XYZ coordinates
shader.SetBindingForAttribute("tuv", "vec2", "geometry.textuv") // point UV coordinates (texture)
shader.SetBindingForAttribute("nor", "vec3", "geometry.normal") // point normal vector
shader.CheckBindings() // check validity of the shader
return shader
}
func NewShader_InstancePoseColor(wctx *wcommon.WebGLContext) *wcommon.Shader {
// Shader for (XYZ + NORMAL) Geometry & (COLOR) Material & (DIRECTIONAL) Lighting
var vertex_shader_code = `
precision mediump float;
uniform mat4 proj; // Projection matrix
uniform mat4 vwmd; // ModelView matrix
attribute vec3 xyz; // XYZ coordinates
attribute vec3 nor; // normal vector
attribute vec3 ixyz; // instance pose : XYZ translation
attribute vec3 icolor; // instance pose : color
uniform mat3 light; // [0]: direction, [1]: color, [2]: ambient_color (column-major)
varying vec3 v_color; // (varying) instance color
varying vec3 v_light; // (varying) lighting intensity
void main() {
gl_Position = proj * vwmd * vec4(xyz.x + ixyz[0], xyz.y + ixyz[1], xyz.z + ixyz[2], 1.0);
float s = sqrt( vwmd[0][0]*vwmd[0][0] + vwmd[0][1]*vwmd[0][1] + vwmd[0][2]*vwmd[0][2]); // scaling
mat3 mvRot = mat3( vwmd[0][0]/s, vwmd[0][1]/s, vwmd[0][2]/s, vwmd[1][0]/s, vwmd[1][1]/s, vwmd[1][2]/s, vwmd[2][0]/s, vwmd[2][1]/s, vwmd[2][2]/s );
vec3 normal = mvRot * nor; // normal vector in camera space
float intensity = max(dot(normal, light[0]), 0.0); // light_intensity = dot(face_normal,light_direction)
v_light = intensity * light[1] + light[2]; // intensity * light_color + ambient_color
v_color = icolor;
}`
var fragment_shader_code = `
precision mediump float;
varying vec3 v_color; // (varying) instance color
varying vec3 v_light; // (varying) lighting intensity
void main() {
gl_FragColor = vec4(v_color * v_light, 1.0);
}`
shader, _ := wcommon.NewShader(wctx, vertex_shader_code, fragment_shader_code)
shader.SetBindingForUniform("proj", "mat4", "renderer.proj") // (Projection) matrix
shader.SetBindingForUniform("vwmd", "mat4", "renderer.vwmd") // (View * Models) matrix
shader.SetBindingForUniform("light", "mat3", "lighting.dlight") // directional lighting
shader.SetBindingForAttribute("xyz", "vec3", "geometry.coords") // point XYZ coordinates
shader.SetBindingForAttribute("nor", "vec3", "geometry.normal") // point normal vectors
shader.SetBindingForAttribute("ixyz", "vec3", "instance.pose:6:0") // instance position
shader.SetBindingForAttribute("icolor", "vec3", "instance.pose:6:3") // instance color
shader.CheckBindings() // check validity of the shader
return shader
}