RectAreaLightHelper.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * @author abelnation / http://github.com/abelnation
  3. */
  4. import { Object3D } from '../../core/Object3D';
  5. import { Vector3 } from '../../math/Vector3';
  6. import { Shape } from '../../extras/core/Shape';
  7. import { Mesh } from '../../objects/Mesh';
  8. import { MeshBasicMaterial } from '../../materials/MeshBasicMaterial';
  9. import { ShapeBufferGeometry } from '../../geometries/ShapeBufferGeometry';
  10. function RectAreaLightHelper( light ) {
  11. Object3D.call( this );
  12. this.light = light;
  13. this.light.updateMatrixWorld();
  14. this.lightMat = new MeshBasicMaterial( {
  15. color: light.color,
  16. fog: false
  17. } );
  18. this.lightWireMat = new MeshBasicMaterial( {
  19. color: light.color,
  20. fog: false,
  21. wireframe: true
  22. } );
  23. var hx = this.light.width / 2.0;
  24. var hy = this.light.height / 2.0;
  25. this.lightShape = new ShapeBufferGeometry( new Shape( [
  26. new Vector3( - hx, hy, 0 ),
  27. new Vector3( hx, hy, 0 ),
  28. new Vector3( hx, - hy, 0 ),
  29. new Vector3( - hx, - hy, 0 )
  30. ] ) );
  31. // shows the "front" of the light, e.g. where light comes from
  32. this.lightMesh = new Mesh( this.lightShape, this.lightMat );
  33. // shows the "back" of the light, which does not emit light
  34. this.lightWireMesh = new Mesh( this.lightShape, this.lightWireMat );
  35. this.add( this.lightMesh );
  36. this.add( this.lightWireMesh );
  37. this.update();
  38. }
  39. RectAreaLightHelper.prototype = Object.create( Object3D.prototype );
  40. RectAreaLightHelper.prototype.constructor = RectAreaLightHelper;
  41. RectAreaLightHelper.prototype.dispose = function () {
  42. this.lightMesh.geometry.dispose();
  43. this.lightMesh.material.dispose();
  44. this.lightWireMesh.geometry.dispose();
  45. this.lightWireMesh.material.dispose();
  46. };
  47. RectAreaLightHelper.prototype.update = function () {
  48. var vector = new Vector3();
  49. var vector2 = new Vector3();
  50. // TODO (abelnation) why not just make light helpers a child of the light object?
  51. if ( this.light.target ) {
  52. vector.setFromMatrixPosition( this.light.matrixWorld );
  53. vector2.setFromMatrixPosition( this.light.target.matrixWorld );
  54. var lookVec = vector2.clone().sub( vector );
  55. this.lightMesh.lookAt( lookVec );
  56. this.lightWireMesh.lookAt( lookVec );
  57. }
  58. this.lightMesh.material.color.copy( this.light.color ).multiplyScalar( this.light.intensity );
  59. this.lightWireMesh.material.color.copy( this.light.color ).multiplyScalar( this.light.intensity );
  60. var oldShape = this.lightShape;
  61. var hx = this.light.width / 2.0;
  62. var hy = this.light.height / 2.0;
  63. this.lightShape = new ShapeBufferGeometry( new Shape( [
  64. new Vector3( - hx, hy, 0 ),
  65. new Vector3( hx, hy, 0 ),
  66. new Vector3( hx, - hy, 0 ),
  67. new Vector3( - hx, - hy, 0 )
  68. ] ) );
  69. this.lightMesh.geometry = this.lightShape;
  70. this.lightWireMesh.geometry = this.lightShape;
  71. oldShape.dispose();
  72. };
  73. export { RectAreaLightHelper };
粤ICP备19079148号