Pārlūkot izejas kodu

simple lighting from a static directional source + ambient.

https://developer.mozilla.org/en-US/docs/Web/WebGL/Lighting_in_WebGL
Lucas Stadler 12 gadi atpakaļ
vecāks
revīzija
458d8aaa69
2 mainītis faili ar 43 papildinājumiem un 6 dzēšanām
  1. 16 0
      js/pixl/public/geometry.js
  2. 27 6
      js/pixl/public/trixl.html

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

@ -51,3 +51,19 @@ geometry.cube = function() {
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,23 +27,38 @@
27 27
	<span id="debug"></span>
28 28
	<script type="gl/vertex-shader">
29 29
		attribute vec3 pos;
30
		attribute vec3 normal;
31
30 32
		varying vec3 world_pos;
33
		varying vec3 lighting;
31 34
32 35
		uniform mat4 transform;
33 36
34 37
		void main() {
35 38
			world_pos = pos;
36 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 51
	</script>
39 52
	<script type="gl/fragment-shader">
40 53
		precision mediump float;
54
41 55
		uniform vec4 color;
42 56
		varying vec3 world_pos;
57
		varying vec3 lighting;
43 58
44 59
		void main() {
45 60
			if (color.x >= 0.0 && color.x <= 1.0) {
46
				gl_FragColor = color;
61
				gl_FragColor = vec4(color.rgb * lighting, color.a);
47 62
			} else {
48 63
				gl_FragColor = vec4(world_pos.x, world_pos.y, world_pos.z, 1);
49 64
			}
@ -173,18 +188,24 @@
173 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 191
		var vs = document.querySelector("script[type='gl/vertex-shader']").textContent;
181 192
		var fs = document.querySelector("script[type='gl/fragment-shader']").textContent;
182 193
		var program = createProgram(vs, fs);
183 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 199
		program.vertexPosAttrib = gl.getAttribLocation(program, 'pos');
186 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 210
		program.color = gl.getUniformLocation(program, 'color');
190 211
		gl.uniform4f(program.color, -1, -1, -1, -1);