Browse Source

* Enable Scene light object management
* Add lighting code in the scene rendering loop

Nicolas Garcia Belmonte 15 years ago
parent
commit
ad73c981ee
2 changed files with 53 additions and 5 deletions
  1. 32 3
      src/renderers/WebGLRenderer.js
  2. 21 2
      src/scenes/Scene.js

+ 32 - 3
src/renderers/WebGLRenderer.js

@@ -30,9 +30,10 @@ THREE.WebGLRenderer = function () {
 
 	this.render = function ( scene, camera ) {
 
-		var face, faceColor, object, material, normal,
+		var face, faceColor, object, material, normal, lightColor, lightDirection, light,
 		vertexArray, faceArray, colorArray, normalArray, vertexIndex,
-		o, ol, f, fl, m, ml, i, v1, v2, v3, v4;
+		o, ol, f, fl, m, ml, i, v1, v2, v3, v4,
+		l, ll;
 
 		if ( this.autoClear ) {
 
@@ -40,7 +41,30 @@ THREE.WebGLRenderer = function () {
 
 		}
 
-		for ( o = 0, ol = scene.objects.length; o < ol; o++ ) {
+		//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++ ) {
 
 			object = scene.objects[ o ];
 
@@ -291,6 +315,11 @@ 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.color = _gl.getAttribLocation( _program, "color" );
 		_gl.enableVertexAttribArray( _program.color );
 

+ 21 - 2
src/scenes/Scene.js

@@ -5,7 +5,7 @@
 THREE.Scene = function () {
 
 	this.objects = [];
-	// this.lights = [];
+	this.lights = [];
 
 	this.addObject = function ( object ) {
 
@@ -24,7 +24,26 @@ THREE.Scene = function () {
 
 			}
 		}
-	}
+	};
+
+  this.addLight = function ( light ) {
+
+    this.lights.push(light);
+
+  };
+
+  this.removeLight = function ( light ) {
+
+    for ( var i = 0, l = this.lights.length; i < l; i++ ) {
+
+      if ( light == this.lights[ i ] ) {
+
+        this.lights.splice( i, 1 );
+        return;
+
+      }
+    }
+  };
 
 	// Deprecated
 	this.add = function ( object ) {