提交 ad73c981 编写于 作者: N Nicolas Garcia Belmonte 提交者: Mr.doob

* Enable Scene light object management

 * Add lighting code in the scene rendering loop
上级 d342239c
......@@ -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 );
......
......@@ -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 ) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册