HemisphereLightHelper.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. function HemisphereLightHelper( light, size, color ) {
  15. Object3D.call( this );
  16. this.light = light;
  17. this.light.updateMatrixWorld();
  18. this.matrix = light.matrixWorld;
  19. this.matrixAutoUpdate = false;
  20. this.color = color;
  21. var geometry = new OctahedronBufferGeometry( size );
  22. geometry.rotateY( Math.PI * 0.5 );
  23. this.material = new MeshBasicMaterial( { wireframe: true, fog: false } );
  24. if ( this.color === undefined ) this.material.vertexColors = VertexColors;
  25. var position = geometry.getAttribute( 'position' );
  26. var colors = new Float32Array( position.count * 3 );
  27. geometry.addAttribute( 'color', new BufferAttribute( colors, 3 ) );
  28. this.add( new Mesh( geometry, this.material ) );
  29. this.update();
  30. }
  31. HemisphereLightHelper.prototype = Object.create( Object3D.prototype );
  32. HemisphereLightHelper.prototype.constructor = HemisphereLightHelper;
  33. HemisphereLightHelper.prototype.dispose = function () {
  34. this.children[ 0 ].geometry.dispose();
  35. this.children[ 0 ].material.dispose();
  36. };
  37. HemisphereLightHelper.prototype.update = function () {
  38. var vector = new Vector3();
  39. var color1 = new Color();
  40. var color2 = new Color();
  41. return function update() {
  42. var mesh = this.children[ 0 ];
  43. if ( this.color !== undefined ) {
  44. this.material.color.set( this.color );
  45. } else {
  46. var colors = mesh.geometry.getAttribute( 'color' );
  47. color1.copy( this.light.color );
  48. color2.copy( this.light.groundColor );
  49. for ( var i = 0, l = colors.count; i < l; i ++ ) {
  50. var color = ( i < ( l / 2 ) ) ? color1 : color2;
  51. colors.setXYZ( i, color.r, color.g, color.b );
  52. }
  53. colors.needsUpdate = true;
  54. }
  55. mesh.lookAt( vector.setFromMatrixPosition( this.light.matrixWorld ).negate() );
  56. };
  57. }();
  58. export { HemisphereLightHelper };
粤ICP备19079148号