Просмотр исходного кода

simple lighting from a static directional source + ambient.

https://developer.mozilla.org/en-US/docs/Web/WebGL/Lighting_in_WebGL
Lucas Stadler лет назад: 12
Родитель
Сommit
458d8aaa69
2 измененных файлов с 43 добавлено и 6 удалено
  1. 16 0
      js/pixl/public/geometry.js
  2. 27 6
      js/pixl/public/trixl.html

+ 16 - 0
js/pixl/public/geometry.js

51
	];
51
	];
52
};
52
};
53
53
54
geometry.cube.normals = function() {
55
	var front = [0.0, 0.0, 1.0],
56
	    back = [0.0, 0.0, -1.0],
57
	    left = [-1.0, 0.0, 0.0],
58
	    right = [1.0, 0.0, 0.0],
59
	    top = [0.0, 1.0, 0.0],
60
	    bottom = [0.0, -1.0, 0.0];
61
62
	return [].concat(
63
		front, front, front, front, front, front,
64
		back, back, back, back, back, back,
65
		left, left, left, left, left, left,
66
		right, right, right, right, right, right,
67
		top, top, top, top, top, top,
68
		bottom, bottom, bottom, bottom, bottom, bottom);
69
}

+ 27 - 6
js/pixl/public/trixl.html

27
	<span id="debug"></span>
27
	<span id="debug"></span>
28
	<script type="gl/vertex-shader">
28
	<script type="gl/vertex-shader">
29
		attribute vec3 pos;
29
		attribute vec3 pos;
30
		attribute vec3 normal;
31
30
		varying vec3 world_pos;
32
		varying vec3 world_pos;
33
		varying vec3 lighting;
31
34
32
		uniform mat4 transform;
35
		uniform mat4 transform;
33
36
34
		void main() {
37
		void main() {
35
			world_pos = pos;
38
			world_pos = pos;
36
			gl_Position = vec4(transform * vec4(pos, 1));
39
			gl_Position = vec4(transform * vec4(pos, 1));
40
41
			highp vec3 ambientLight = vec3(0.6, 0.6, 0.6);
42
			highp vec3 directionalLightColor = vec3(0.5, 0.5, 0.75);
43
			highp vec3 directionalVector = vec3(0.85, 0.8, 0.75);
44
45
			// FIXME: transform should only be transform w/o view & perspective
46
			highp vec4 transformedNormal = transform * vec4(normal, 1.0);
47
48
			highp float directional = max(dot(normal, directionalVector), 0.0);
49
			lighting = ambientLight + (directionalLightColor * directional);
37
		}
50
		}
38
	</script>
51
	</script>
39
	<script type="gl/fragment-shader">
52
	<script type="gl/fragment-shader">
40
		precision mediump float;
53
		precision mediump float;
54
41
		uniform vec4 color;
55
		uniform vec4 color;
42
		varying vec3 world_pos;
56
		varying vec3 world_pos;
57
		varying vec3 lighting;
43
58
44
		void main() {
59
		void main() {
45
			if (color.x >= 0.0 && color.x <= 1.0) {
60
			if (color.x >= 0.0 && color.x <= 1.0) {
46
				gl_FragColor = color;
61
				gl_FragColor = vec4(color.rgb * lighting, color.a);
47
			} else {
62
			} else {
48
				gl_FragColor = vec4(world_pos.x, world_pos.y, world_pos.z, 1);
63
				gl_FragColor = vec4(world_pos.x, world_pos.y, world_pos.z, 1);
49
			}
64
			}
173
			vec3.add(trixl.camera.focus, trixl.camera.pos, front);
188
			vec3.add(trixl.camera.focus, trixl.camera.pos, front);
174
		}
189
		}
175
190
176
		var vertexPosBuffer = gl.createBuffer();
177
		gl.bindBuffer(gl.ARRAY_BUFFER, vertexPosBuffer);
178
		gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(geometry.cube()), gl.STATIC_DRAW);
179
180
		var vs = document.querySelector("script[type='gl/vertex-shader']").textContent;
191
		var vs = document.querySelector("script[type='gl/vertex-shader']").textContent;
181
		var fs = document.querySelector("script[type='gl/fragment-shader']").textContent;
192
		var fs = document.querySelector("script[type='gl/fragment-shader']").textContent;
182
		var program = createProgram(vs, fs);
193
		var program = createProgram(vs, fs);
183
		gl.useProgram(program);
194
		gl.useProgram(program);
184
195
196
		var vertexPosBuffer = gl.createBuffer();
197
		gl.bindBuffer(gl.ARRAY_BUFFER, vertexPosBuffer);
198
		gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(geometry.cube()), gl.STATIC_DRAW);
185
		program.vertexPosAttrib = gl.getAttribLocation(program, 'pos');
199
		program.vertexPosAttrib = gl.getAttribLocation(program, 'pos');
186
		gl.enableVertexAttribArray(program.vertexPosAttrib);
200
		gl.enableVertexAttribArray(program.vertexPosAttrib);
187
		gl.vertexAttribPointer(program.vertexPosBuffer, 3, gl.FLOAT, false, 0, 0);
201
		gl.vertexAttribPointer(program.vertexPosAttrib, 3, gl.FLOAT, false, 0, 0);
202
203
		var vertexNormalBuffer = gl.createBuffer();
204
		gl.bindBuffer(gl.ARRAY_BUFFER, vertexNormalBuffer);
205
		gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(geometry.cube.normals()), gl.STATIC_DRAW);
206
		program.vertexNormalAttrib = gl.getAttribLocation(program, 'normal');
207
		gl.enableVertexAttribArray(program.vertexNormalAttrib);
208
		gl.vertexAttribPointer(program.vertexNormalAttrib, 3, gl.FLOAT, false, 0, 0);
188
209
189
		program.color = gl.getUniformLocation(program, 'color');
210
		program.color = gl.getUniformLocation(program, 'color');
190
		gl.uniform4f(program.color, -1, -1, -1, -1);
211
		gl.uniform4f(program.color, -1, -1, -1, -1);