Skip to content

Commit 32cd6bc

Browse files
authored
Merge pull request #14935 from WestLangley/dev-rect_light_orientation
Adopt glTF convention: lights face local neg-z direction
2 parents 6ecda44 + 3ae862b commit 32cd6bc

File tree

6 files changed

+47
-45
lines changed

6 files changed

+47
-45
lines changed

docs/api/en/helpers/RectAreaLightHelper.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ <h3>Example</h3>
2323

2424
var helper = new THREE.RectAreaLightHelper( light );
2525

26-
scene.add( helper );
26+
light.add( helper ); // helper must be added as a child of the light
2727
</code>
2828

2929

docs/api/en/lights/RectAreaLight.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ <h2>Examples</h2>
4141
scene.add( rectLight )
4242

4343
rectLightHelper = new THREE.RectAreaLightHelper( rectLight );
44-
scene.add( rectLightHelper );
44+
rectLight.add( rectLightHelper );
4545

4646
</code>
4747
</p>

examples/webgl_lights_rectarealight.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,12 @@
108108
rectLight.position.set( 5, 5, 0 );
109109
scene.add( rectLight );
110110

111-
var rectLightMesh = new THREE.Mesh( new THREE.PlaneBufferGeometry(), new THREE.MeshBasicMaterial() );
111+
var rectLightMesh = new THREE.Mesh( new THREE.PlaneBufferGeometry(), new THREE.MeshBasicMaterial( { side: THREE.BackSide } ) );
112112
rectLightMesh.scale.x = rectLight.width;
113113
rectLightMesh.scale.y = rectLight.height;
114114
rectLight.add( rectLightMesh );
115115

116116
var rectLightMeshBack = new THREE.Mesh( new THREE.PlaneBufferGeometry(), new THREE.MeshBasicMaterial( { color: 0x080808 } ) );
117-
rectLightMeshBack.rotation.y = Math.PI;
118117
rectLightMesh.add( rectLightMeshBack );
119118

120119
var geoFloor = new THREE.BoxBufferGeometry( 2000, 0.1, 2000 );

src/core/Object3D.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ Object3D.prototype = Object.assign( Object.create( EventDispatcher.prototype ),
332332

333333
position.setFromMatrixPosition( this.matrixWorld );
334334

335-
if ( this.isCamera ) {
335+
if ( this.isCamera || this.isLight ) {
336336

337337
m1.lookAt( position, target, this.up );
338338

src/helpers/RectAreaLightHelper.js

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,79 +2,82 @@
22
* @author abelnation / http://github.com/abelnation
33
* @author Mugen87 / http://github.com/Mugen87
44
* @author WestLangley / http://github.com/WestLangley
5+
*
6+
* This helper must be added as a child of the light
57
*/
68

7-
import { Object3D } from '../core/Object3D.js';
89
import { Line } from '../objects/Line.js';
10+
import { Mesh } from '../objects/Mesh.js';
911
import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
12+
import { MeshBasicMaterial } from '../materials/MeshBasicMaterial.js';
13+
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
1014
import { BufferGeometry } from '../core/BufferGeometry.js';
11-
import { BufferAttribute } from '../core/BufferAttribute.js';
1215

1316
function RectAreaLightHelper( light, color ) {
1417

15-
Object3D.call( this );
18+
this.type = 'RectAreaLightHelper';
1619

1720
this.light = light;
18-
this.light.updateMatrixWorld();
1921

20-
this.matrix = light.matrixWorld;
21-
this.matrixAutoUpdate = false;
22+
this.color = color; // optional hardwired color for the helper
2223

23-
this.color = color;
24+
var positions = [ 1, 1, 0, - 1, 1, 0, - 1, - 1, 0, 1, - 1, 0, 1, 1, 0 ];
25+
26+
var geometry = new BufferGeometry();
27+
geometry.addAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
28+
geometry.computeBoundingSphere();
2429

2530
var material = new LineBasicMaterial( { fog: false } );
2631

27-
var geometry = new BufferGeometry();
32+
Line.call( this, geometry, material );
2833

29-
geometry.addAttribute( 'position', new BufferAttribute( new Float32Array( 5 * 3 ), 3 ) );
34+
//
3035

31-
this.line = new Line( geometry, material );
32-
this.add( this.line );
36+
var positions2 = [ 1, 1, 0, - 1, 1, 0, - 1, - 1, 0, 1, 1, 0, - 1, - 1, 0, 1, - 1, 0 ];
3337

38+
var geometry2 = new BufferGeometry();
39+
geometry2.addAttribute( 'position', new Float32BufferAttribute( positions2, 3 ) );
40+
geometry2.computeBoundingSphere();
41+
42+
this.add( new Mesh( geometry2, new MeshBasicMaterial( { side: THREE.BackSide, fog: false } ) ) );
3443

3544
this.update();
3645

3746
}
3847

39-
RectAreaLightHelper.prototype = Object.create( Object3D.prototype );
48+
RectAreaLightHelper.prototype = Object.create( Line.prototype );
4049
RectAreaLightHelper.prototype.constructor = RectAreaLightHelper;
4150

42-
RectAreaLightHelper.prototype.dispose = function () {
43-
44-
this.children[ 0 ].geometry.dispose();
45-
this.children[ 0 ].material.dispose();
46-
47-
};
48-
4951
RectAreaLightHelper.prototype.update = function () {
5052

51-
// calculate new dimensions of the helper
53+
this.scale.set( 0.5 * this.light.width, 0.5 * this.light.height, 1 );
5254

53-
var hx = this.light.width * 0.5;
54-
var hy = this.light.height * 0.5;
55+
if ( this.color !== undefined ) {
5556

56-
var position = this.line.geometry.attributes.position;
57-
var array = position.array;
57+
this.material.color.set( this.color );
58+
this.children[ 0 ].material.color.set( this.color );
5859

59-
// update vertices
60+
} else {
6061

61-
array[ 0 ] = hx; array[ 1 ] = - hy; array[ 2 ] = 0;
62-
array[ 3 ] = hx; array[ 4 ] = hy; array[ 5 ] = 0;
63-
array[ 6 ] = - hx; array[ 7 ] = hy; array[ 8 ] = 0;
64-
array[ 9 ] = - hx; array[ 10 ] = - hy; array[ 11 ] = 0;
65-
array[ 12 ] = hx; array[ 13 ] = - hy; array[ 14 ] = 0;
62+
this.material.color.copy( this.light.color ).multiplyScalar( this.light.intensity );
6663

67-
position.needsUpdate = true;
64+
// prevent hue shift
65+
var c = this.material.color;
66+
var max = Math.max( c.r, c.g, c.b );
67+
if ( max > 1 ) c.multiplyScalar( 1 / max );
6868

69-
if ( this.color !== undefined ) {
69+
this.children[ 0 ].material.color.copy( this.material.color );
7070

71-
this.line.material.color.set( this.color );
71+
}
7272

73-
} else {
73+
};
7474

75-
this.line.material.color.copy( this.light.color );
75+
RectAreaLightHelper.prototype.dispose = function () {
7676

77-
}
77+
this.geometry.dispose();
78+
this.material.dispose();
79+
this.children[ 0 ].geometry.dispose();
80+
this.children[ 0 ].material.dispose();
7881

7982
};
8083

src/renderers/shaders/ShaderChunk/lights_physical_pars_fragment.glsl.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ float clearCoatDHRApprox( const in float roughness, const in float dotNL ) {
3636
float roughness = material.specularRoughness;
3737
3838
vec3 rectCoords[ 4 ];
39-
rectCoords[ 0 ] = lightPos - halfWidth - halfHeight; // counterclockwise
40-
rectCoords[ 1 ] = lightPos + halfWidth - halfHeight;
41-
rectCoords[ 2 ] = lightPos + halfWidth + halfHeight;
42-
rectCoords[ 3 ] = lightPos - halfWidth + halfHeight;
39+
rectCoords[ 0 ] = lightPos + halfWidth - halfHeight; // counterclockwise; light shines in local neg z direction
40+
rectCoords[ 1 ] = lightPos - halfWidth - halfHeight;
41+
rectCoords[ 2 ] = lightPos - halfWidth + halfHeight;
42+
rectCoords[ 3 ] = lightPos + halfWidth + halfHeight;
4343
4444
vec2 uv = LTC_Uv( normal, viewDir, roughness );
4545

0 commit comments

Comments
 (0)