|
@@ -43,28 +43,28 @@ THREE.WebGLRenderer = function () {
|
|
|
|
|
|
//lighting
|
|
|
_gl.uniform1i( _program.enableLighting, scene.lights.length );
|
|
|
-
|
|
|
- for ( l = 0, ll = scene.lights.length; l < ll; l++ ) {
|
|
|
-
|
|
|
- light = scene.lights[ l ];
|
|
|
-
|
|
|
- if ( light instanceof THREE.AmbientLight ) {
|
|
|
-
|
|
|
- lightColor = light.color;
|
|
|
- _gl.uniform3f( _program.ambientColor, lightColor.r, lightColor.g, lightColor.b );
|
|
|
-
|
|
|
- } else if( light instanceof THREE.DirectionalLight ) {
|
|
|
-
|
|
|
- lightColor = light.color;
|
|
|
- lightDirection = light.direction;
|
|
|
- _gl.uniform3f( _program.lightingDirection, lightDirection.x, lightDirection.y, lightDirection.z );
|
|
|
- _gl.uniform3f( _program.directionalColor, lightColor.r, lightColor.g, lightColor.b );
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- for ( o = 0, ol = scene.objects.length; o < ol; o++ ) {
|
|
|
+
|
|
|
+ for ( l = 0, ll = scene.lights.length; l < ll; l++ ) {
|
|
|
+
|
|
|
+ light = scene.lights[ l ];
|
|
|
+
|
|
|
+ if ( light instanceof THREE.AmbientLight ) {
|
|
|
+
|
|
|
+ lightColor = light.color;
|
|
|
+ _gl.uniform3f( _program.ambientColor, lightColor.r, lightColor.g, lightColor.b );
|
|
|
+
|
|
|
+ } else if( light instanceof THREE.DirectionalLight ) {
|
|
|
+
|
|
|
+ lightColor = light.color;
|
|
|
+ lightDirection = light.direction;
|
|
|
+ _gl.uniform3f( _program.lightingDirection, lightDirection.x, lightDirection.y, lightDirection.z );
|
|
|
+ _gl.uniform3f( _program.directionalColor, lightColor.r, lightColor.g, lightColor.b );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ for ( o = 0, ol = scene.objects.length; o < ol; o++ ) {
|
|
|
|
|
|
object = scene.objects[ o ];
|
|
|
|
|
@@ -94,9 +94,9 @@ THREE.WebGLRenderer = function () {
|
|
|
vertexArray.push( v2.x, v2.y, v2.z );
|
|
|
vertexArray.push( v3.x, v3.y, v3.z );
|
|
|
|
|
|
- normalArray.push( normal.x, normal.y, normal.z );
|
|
|
- normalArray.push( normal.x, normal.y, normal.z );
|
|
|
- normalArray.push( normal.x, normal.y, normal.z );
|
|
|
+ normalArray.push( normal.x, normal.y, normal.z );
|
|
|
+ normalArray.push( normal.x, normal.y, normal.z );
|
|
|
+ normalArray.push( normal.x, normal.y, normal.z );
|
|
|
|
|
|
colorArray.push( faceColor.r, faceColor.g, faceColor.b, faceColor.a );
|
|
|
colorArray.push( faceColor.r, faceColor.g, faceColor.b, faceColor.a );
|
|
@@ -118,10 +118,10 @@ THREE.WebGLRenderer = function () {
|
|
|
vertexArray.push( v3.x, v3.y, v3.z );
|
|
|
vertexArray.push( v4.x, v4.y, v4.z );
|
|
|
|
|
|
- normalArray.push( normal.x, normal.y, normal.z );
|
|
|
- normalArray.push( normal.x, normal.y, normal.z );
|
|
|
- normalArray.push( normal.x, normal.y, normal.z );
|
|
|
- normalArray.push( normal.x, normal.y, normal.z );
|
|
|
+ normalArray.push( normal.x, normal.y, normal.z );
|
|
|
+ normalArray.push( normal.x, normal.y, normal.z );
|
|
|
+ normalArray.push( normal.x, normal.y, normal.z );
|
|
|
+ normalArray.push( normal.x, normal.y, normal.z );
|
|
|
|
|
|
colorArray.push( faceColor.r, faceColor.g, faceColor.b, faceColor.a );
|
|
|
colorArray.push( faceColor.r, faceColor.g, faceColor.b, faceColor.a );
|
|
@@ -145,9 +145,9 @@ THREE.WebGLRenderer = function () {
|
|
|
_gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webGLVertexBuffer );
|
|
|
_gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( vertexArray ), _gl.STATIC_DRAW );
|
|
|
|
|
|
- object.__webGLNormalBuffer = _gl.createBuffer();
|
|
|
- _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webGLNormalBuffer );
|
|
|
- _gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( normalArray ), _gl.STATIC_DRAW );
|
|
|
+ object.__webGLNormalBuffer = _gl.createBuffer();
|
|
|
+ _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webGLNormalBuffer );
|
|
|
+ _gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( normalArray ), _gl.STATIC_DRAW );
|
|
|
|
|
|
object.__webGLColorBuffer = _gl.createBuffer();
|
|
|
_gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webGLColorBuffer );
|
|
@@ -167,9 +167,9 @@ THREE.WebGLRenderer = function () {
|
|
|
_program.viewMatrixArray = new Float32Array( viewMatrix.flatten() );
|
|
|
_program.projectionMatrixArray = new Float32Array( camera.projectionMatrix.flatten() );
|
|
|
|
|
|
- normalMatrix = THREE.Matrix4.makeInvert(viewMatrix).transpose();
|
|
|
- _program.normalMatrixArray = new Float32Array( normalMatrix.flatten() );
|
|
|
-
|
|
|
+ normalMatrix = THREE.Matrix4.makeInvert(viewMatrix).transpose();
|
|
|
+ _program.normalMatrixArray = new Float32Array( normalMatrix.flatten() );
|
|
|
+
|
|
|
_gl.uniformMatrix4fv( _program.viewMatrix, false, _program.viewMatrixArray );
|
|
|
_gl.uniformMatrix4fv( _program.projectionMatrix, false, _program.projectionMatrixArray );
|
|
|
_gl.uniformMatrix4fv( _program.normalMatrix, false, _program.normalMatrixArray );
|
|
@@ -177,8 +177,8 @@ THREE.WebGLRenderer = function () {
|
|
|
_gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webGLVertexBuffer );
|
|
|
_gl.vertexAttribPointer( _program.position, 3, _gl.FLOAT, false, 0, 0 );
|
|
|
|
|
|
- _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webGLNormalBuffer );
|
|
|
- _gl.vertexAttribPointer( _program.normal, 3, _gl.FLOAT, false, 0, 0 );
|
|
|
+ _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webGLNormalBuffer );
|
|
|
+ _gl.vertexAttribPointer( _program.normal, 3, _gl.FLOAT, false, 0, 0 );
|
|
|
|
|
|
for ( m = 0, ml = object.material.length; m < ml; m++ ) {
|
|
|
|
|
@@ -256,50 +256,50 @@ THREE.WebGLRenderer = function () {
|
|
|
_program = _gl.createProgram();
|
|
|
|
|
|
_gl.attachShader( _program, getShader( "fragment", [
|
|
|
- "#ifdef GL_ES",
|
|
|
- "precision highp float;",
|
|
|
- "#endif",
|
|
|
-
|
|
|
- "varying vec4 vcolor;",
|
|
|
- "varying vec3 lightWeighting;",
|
|
|
-
|
|
|
- "void main(){",
|
|
|
-
|
|
|
- "gl_FragColor = vec4(vcolor.rgb * lightWeighting, vcolor.a);",
|
|
|
-
|
|
|
- "}"
|
|
|
- ].join("\n") ) );
|
|
|
+ "#ifdef GL_ES",
|
|
|
+ "precision highp float;",
|
|
|
+ "#endif",
|
|
|
+
|
|
|
+ "varying vec4 vcolor;",
|
|
|
+ "varying vec3 lightWeighting;",
|
|
|
+
|
|
|
+ "void main(){",
|
|
|
+
|
|
|
+ "gl_FragColor = vec4(vcolor.rgb * lightWeighting, vcolor.a);",
|
|
|
+
|
|
|
+ "}"
|
|
|
+ ].join("\n") ) );
|
|
|
|
|
|
_gl.attachShader( _program, getShader( "vertex", [
|
|
|
- "attribute vec3 position;",
|
|
|
- "attribute vec3 normal;",
|
|
|
- "attribute vec4 color;",
|
|
|
-
|
|
|
- "uniform bool enableLighting;",
|
|
|
- "uniform vec3 ambientColor;",
|
|
|
- "uniform vec3 directionalColor;",
|
|
|
- "uniform vec3 lightingDirection;",
|
|
|
-
|
|
|
- "uniform mat4 viewMatrix;",
|
|
|
- "uniform mat4 projectionMatrix;",
|
|
|
- "uniform mat4 normalMatrix;",
|
|
|
- "varying vec4 vcolor;",
|
|
|
- "varying vec3 lightWeighting;",
|
|
|
-
|
|
|
- "void main(void) {",
|
|
|
-
|
|
|
- "if(!enableLighting) {",
|
|
|
- "lightWeighting = vec3(1.0, 1.0, 1.0);",
|
|
|
- "} else {",
|
|
|
- "vec4 transformedNormal = normalMatrix * vec4(normal, 1.0);",
|
|
|
- "float directionalLightWeighting = max(dot(transformedNormal.xyz, lightingDirection), 0.0);",
|
|
|
- "lightWeighting = ambientColor + directionalColor * directionalLightWeighting;",
|
|
|
- "}",
|
|
|
-
|
|
|
- "vcolor = color;",
|
|
|
- "gl_Position = projectionMatrix * viewMatrix * vec4( position, 1.0 );",
|
|
|
-
|
|
|
- "}"].join("\n") ) );
|
|
|
+ "attribute vec3 position;",
|
|
|
+ "attribute vec3 normal;",
|
|
|
+ "attribute vec4 color;",
|
|
|
+
|
|
|
+ "uniform bool enableLighting;",
|
|
|
+ "uniform vec3 ambientColor;",
|
|
|
+ "uniform vec3 directionalColor;",
|
|
|
+ "uniform vec3 lightingDirection;",
|
|
|
+
|
|
|
+ "uniform mat4 viewMatrix;",
|
|
|
+ "uniform mat4 projectionMatrix;",
|
|
|
+ "uniform mat4 normalMatrix;",
|
|
|
+ "varying vec4 vcolor;",
|
|
|
+ "varying vec3 lightWeighting;",
|
|
|
+
|
|
|
+ "void main(void) {",
|
|
|
+
|
|
|
+ "if(!enableLighting) {",
|
|
|
+ "lightWeighting = vec3(1.0, 1.0, 1.0);",
|
|
|
+ "} else {",
|
|
|
+ "vec4 transformedNormal = normalMatrix * vec4(normal, 1.0);",
|
|
|
+ "float directionalLightWeighting = max(dot(transformedNormal.xyz, lightingDirection), 0.0);",
|
|
|
+ "lightWeighting = ambientColor + directionalColor * directionalLightWeighting;",
|
|
|
+ "}",
|
|
|
+
|
|
|
+ "vcolor = color;",
|
|
|
+ "gl_Position = projectionMatrix * viewMatrix * vec4( position, 1.0 );",
|
|
|
+
|
|
|
+ "}"].join("\n") ) );
|
|
|
|
|
|
_gl.linkProgram( _program );
|
|
|
|
|
@@ -315,19 +315,19 @@ THREE.WebGLRenderer = function () {
|
|
|
_program.projectionMatrix = _gl.getUniformLocation( _program, "projectionMatrix" );
|
|
|
_program.normalMatrix = _gl.getUniformLocation( _program, "normalMatrix" );
|
|
|
|
|
|
- _program.enableLighting = _gl.getUniformLocation(_program, 'enableLighting');
|
|
|
- _program.ambientColor = _gl.getUniformLocation(_program, 'ambientColor');
|
|
|
- _program.directionalColor = _gl.getUniformLocation(_program, 'directionalColor');
|
|
|
- _program.lightingDirection = _gl.getUniformLocation(_program, 'lightingDirection');
|
|
|
-
|
|
|
+ _program.enableLighting = _gl.getUniformLocation(_program, 'enableLighting');
|
|
|
+ _program.ambientColor = _gl.getUniformLocation(_program, 'ambientColor');
|
|
|
+ _program.directionalColor = _gl.getUniformLocation(_program, 'directionalColor');
|
|
|
+ _program.lightingDirection = _gl.getUniformLocation(_program, 'lightingDirection');
|
|
|
+
|
|
|
_program.color = _gl.getAttribLocation( _program, "color" );
|
|
|
_gl.enableVertexAttribArray( _program.color );
|
|
|
|
|
|
_program.position = _gl.getAttribLocation( _program, "position" );
|
|
|
_gl.enableVertexAttribArray( _program.position );
|
|
|
|
|
|
- _program.normal = _gl.getAttribLocation( _program, "normal" );
|
|
|
- _gl.enableVertexAttribArray( _program.normal );
|
|
|
+ _program.normal = _gl.getAttribLocation( _program, "normal" );
|
|
|
+ _gl.enableVertexAttribArray( _program.normal );
|
|
|
|
|
|
_program.viewMatrixArray = new Float32Array(16);
|
|
|
_program.projectionMatrixArray = new Float32Array(16);
|