HemisphereLightHelper.js 2.2 KB

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