|
2 | 2 | * @author abelnation / http://github.com/abelnation |
3 | 3 | * @author Mugen87 / http://github.com/Mugen87 |
4 | 4 | * @author WestLangley / http://github.com/WestLangley |
| 5 | + * |
| 6 | + * This helper must be added as a child of the light |
5 | 7 | */ |
6 | 8 |
|
7 | | -import { Object3D } from '../core/Object3D.js'; |
8 | 9 | import { Line } from '../objects/Line.js'; |
| 10 | +import { Mesh } from '../objects/Mesh.js'; |
9 | 11 | import { LineBasicMaterial } from '../materials/LineBasicMaterial.js'; |
| 12 | +import { MeshBasicMaterial } from '../materials/MeshBasicMaterial.js'; |
| 13 | +import { Float32BufferAttribute } from '../core/BufferAttribute.js'; |
10 | 14 | import { BufferGeometry } from '../core/BufferGeometry.js'; |
11 | | -import { BufferAttribute } from '../core/BufferAttribute.js'; |
12 | 15 |
|
13 | 16 | function RectAreaLightHelper( light, color ) { |
14 | 17 |
|
15 | | - Object3D.call( this ); |
| 18 | + this.type = 'RectAreaLightHelper'; |
16 | 19 |
|
17 | 20 | this.light = light; |
18 | | - this.light.updateMatrixWorld(); |
19 | 21 |
|
20 | | - this.matrix = light.matrixWorld; |
21 | | - this.matrixAutoUpdate = false; |
| 22 | + this.color = color; // optional hardwired color for the helper |
22 | 23 |
|
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(); |
24 | 29 |
|
25 | 30 | var material = new LineBasicMaterial( { fog: false } ); |
26 | 31 |
|
27 | | - var geometry = new BufferGeometry(); |
| 32 | + Line.call( this, geometry, material ); |
28 | 33 |
|
29 | | - geometry.addAttribute( 'position', new BufferAttribute( new Float32Array( 5 * 3 ), 3 ) ); |
| 34 | + // |
30 | 35 |
|
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 ]; |
33 | 37 |
|
| 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 } ) ) ); |
34 | 43 |
|
35 | 44 | this.update(); |
36 | 45 |
|
37 | 46 | } |
38 | 47 |
|
39 | | -RectAreaLightHelper.prototype = Object.create( Object3D.prototype ); |
| 48 | +RectAreaLightHelper.prototype = Object.create( Line.prototype ); |
40 | 49 | RectAreaLightHelper.prototype.constructor = RectAreaLightHelper; |
41 | 50 |
|
42 | | -RectAreaLightHelper.prototype.dispose = function () { |
43 | | - |
44 | | - this.children[ 0 ].geometry.dispose(); |
45 | | - this.children[ 0 ].material.dispose(); |
46 | | - |
47 | | -}; |
48 | | - |
49 | 51 | RectAreaLightHelper.prototype.update = function () { |
50 | 52 |
|
51 | | - // calculate new dimensions of the helper |
| 53 | + this.scale.set( 0.5 * this.light.width, 0.5 * this.light.height, 1 ); |
52 | 54 |
|
53 | | - var hx = this.light.width * 0.5; |
54 | | - var hy = this.light.height * 0.5; |
| 55 | + if ( this.color !== undefined ) { |
55 | 56 |
|
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 ); |
58 | 59 |
|
59 | | - // update vertices |
| 60 | + } else { |
60 | 61 |
|
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 ); |
66 | 63 |
|
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 ); |
68 | 68 |
|
69 | | - if ( this.color !== undefined ) { |
| 69 | + this.children[ 0 ].material.color.copy( this.material.color ); |
70 | 70 |
|
71 | | - this.line.material.color.set( this.color ); |
| 71 | + } |
72 | 72 |
|
73 | | - } else { |
| 73 | +}; |
74 | 74 |
|
75 | | - this.line.material.color.copy( this.light.color ); |
| 75 | +RectAreaLightHelper.prototype.dispose = function () { |
76 | 76 |
|
77 | | - } |
| 77 | + this.geometry.dispose(); |
| 78 | + this.material.dispose(); |
| 79 | + this.children[ 0 ].geometry.dispose(); |
| 80 | + this.children[ 0 ].material.dispose(); |
78 | 81 |
|
79 | 82 | }; |
80 | 83 |
|
|
0 commit comments