HemisphereLightHelper.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { Vector3 } from '../math/Vector3.js';
  2. import { Color } from '../math/Color.js';
  3. import { Object3D } from '../core/Object3D.js';
  4. import { Mesh } from '../objects/Mesh.js';
  5. import { MeshBasicMaterial } from '../materials/MeshBasicMaterial.js';
  6. import { OctahedronBufferGeometry } from '../geometries/OctahedronGeometry.js';
  7. import { BufferAttribute } from '../core/BufferAttribute.js';
  8. const _vector = new Vector3();
  9. const _color1 = new Color();
  10. const _color2 = new Color();
  11. class HemisphereLightHelper extends Object3D {
  12. constructor( light, size, color ) {
  13. super();
  14. this.light = light;
  15. this.light.updateMatrixWorld();
  16. this.matrix = light.matrixWorld;
  17. this.matrixAutoUpdate = false;
  18. this.color = color;
  19. const geometry = new OctahedronBufferGeometry( size );
  20. geometry.rotateY( Math.PI * 0.5 );
  21. this.material = new MeshBasicMaterial( { wireframe: true, fog: false, toneMapped: false } );
  22. if ( this.color === undefined ) this.material.vertexColors = true;
  23. const position = geometry.getAttribute( 'position' );
  24. const colors = new Float32Array( position.count * 3 );
  25. geometry.setAttribute( 'color', new BufferAttribute( colors, 3 ) );
  26. this.add( new Mesh( geometry, this.material ) );
  27. this.update();
  28. }
  29. dispose() {
  30. this.children[ 0 ].geometry.dispose();
  31. this.children[ 0 ].material.dispose();
  32. }
  33. update() {
  34. const mesh = this.children[ 0 ];
  35. if ( this.color !== undefined ) {
  36. this.material.color.set( this.color );
  37. } else {
  38. const colors = mesh.geometry.getAttribute( 'color' );
  39. _color1.copy( this.light.color );
  40. _color2.copy( this.light.groundColor );
  41. for ( let i = 0, l = colors.count; i < l; i ++ ) {
  42. const color = ( i < ( l / 2 ) ) ? _color1 : _color2;
  43. colors.setXYZ( i, color.r, color.g, color.b );
  44. }
  45. colors.needsUpdate = true;
  46. }
  47. mesh.lookAt( _vector.setFromMatrixPosition( this.light.matrixWorld ).negate() );
  48. }
  49. }
  50. export { HemisphereLightHelper };