HemisphereLightHelper.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { Vector3 } from '../math/Vector3';
  2. import { Color } from '../math/Color';
  3. import { Object3D } from '../core/Object3D';
  4. import { Mesh } from '../objects/Mesh';
  5. import { VertexColors } from '../constants';
  6. import { MeshBasicMaterial } from '../materials/MeshBasicMaterial';
  7. import { OctahedronBufferGeometry } from '../geometries/OctahedronBufferGeometry';
  8. import { BufferAttribute } from '../core/BufferAttribute';
  9. /**
  10. * @author alteredq / http://alteredqualia.com/
  11. * @author mrdoob / http://mrdoob.com/
  12. * @author Mugen87 / https://github.com/Mugen87
  13. */
  14. function HemisphereLightHelper( light, size ) {
  15. Object3D.call( this );
  16. this.light = light;
  17. this.light.updateMatrixWorld();
  18. this.matrix = light.matrixWorld;
  19. this.matrixAutoUpdate = false;
  20. var geometry = new OctahedronBufferGeometry( size );
  21. geometry.rotateY( Math.PI * 0.5 );
  22. var material = new MeshBasicMaterial( { vertexColors: VertexColors, wireframe: true } );
  23. var position = geometry.getAttribute( 'position' );
  24. var colors = new Float32Array( position.count * 3 );
  25. geometry.addAttribute( 'color', new BufferAttribute( colors, 3 ) );
  26. this.add( new Mesh( geometry, material ) );
  27. this.update();
  28. }
  29. HemisphereLightHelper.prototype = Object.create( Object3D.prototype );
  30. HemisphereLightHelper.prototype.constructor = HemisphereLightHelper;
  31. HemisphereLightHelper.prototype.dispose = function () {
  32. this.children[ 0 ].geometry.dispose();
  33. this.children[ 0 ].material.dispose();
  34. };
  35. HemisphereLightHelper.prototype.update = function () {
  36. var vector = new Vector3();
  37. var color1 = new Color();
  38. var color2 = new Color();
  39. return function update() {
  40. var mesh = this.children[ 0 ];
  41. var colors = mesh.geometry.getAttribute( 'color' );
  42. color1.copy( this.light.color ).multiplyScalar( this.light.intensity );
  43. color2.copy( this.light.groundColor ).multiplyScalar( this.light.intensity );
  44. for ( var i = 0, l = colors.count; i < l; i ++ ) {
  45. var color = ( i < ( l / 2 ) ) ? color1 : color2;
  46. colors.setXYZ( i, color.r, color.g, color.b );
  47. }
  48. mesh.lookAt( vector.setFromMatrixPosition( this.light.matrixWorld ).negate() );
  49. colors.needsUpdate = true;
  50. };
  51. }();
  52. export { HemisphereLightHelper };