From ad73c981ee7c51d9bb224ab9e1b9263d03126fa1 Mon Sep 17 00:00:00 2001 From: Nicolas Garcia Belmonte Date: Thu, 9 Sep 2010 17:20:44 +0800 Subject: [PATCH] * Enable Scene light object management * Add lighting code in the scene rendering loop --- src/renderers/WebGLRenderer.js | 35 +++++++++++++++++++++++++++++++--- src/scenes/Scene.js | 23 ++++++++++++++++++++-- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/src/renderers/WebGLRenderer.js b/src/renderers/WebGLRenderer.js index f3a39870aa..277995a422 100644 --- a/src/renderers/WebGLRenderer.js +++ b/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 ); diff --git a/src/scenes/Scene.js b/src/scenes/Scene.js index c99c0fd424..02d319329e 100644 --- a/src/scenes/Scene.js +++ b/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 ) { -- GitLab