RectAreaLightNode.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import AnalyticLightNode from './AnalyticLightNode.js';
  2. import { addLightNode } from './LightsNode.js';
  3. import { texture } from '../accessors/TextureNode.js';
  4. import { uniform } from '../core/UniformNode.js';
  5. import { objectViewPosition } from '../accessors/Object3DNode.js';
  6. import { addNodeClass } from '../core/Node.js';
  7. import { RectAreaLight, Matrix4, Vector3, UniformsLib } from 'three';
  8. const _matrix41 = new Matrix4();
  9. const _matrix42 = new Matrix4();
  10. let ltc_1, ltc_2;
  11. class RectAreaLightNode extends AnalyticLightNode {
  12. constructor( light = null ) {
  13. super( light );
  14. this.halfHeight = uniform( new Vector3() );
  15. this.halfWidth = uniform( new Vector3() );
  16. }
  17. update( frame ) {
  18. super.update( frame );
  19. const { light } = this;
  20. const viewMatrix = frame.camera.matrixWorldInverse;
  21. _matrix42.identity();
  22. _matrix41.copy( light.matrixWorld );
  23. _matrix41.premultiply( viewMatrix );
  24. _matrix42.extractRotation( _matrix41 );
  25. this.halfWidth.value.set( light.width * 0.5, 0.0, 0.0 );
  26. this.halfHeight.value.set( 0.0, light.height * 0.5, 0.0 );
  27. this.halfWidth.value.applyMatrix4( _matrix42 );
  28. this.halfHeight.value.applyMatrix4( _matrix42 );
  29. }
  30. setup( builder ) {
  31. super.setup( builder );
  32. if ( ltc_1 === undefined ) {
  33. if ( builder.isAvailable( 'float32Filterable' ) ) {
  34. ltc_1 = texture( UniformsLib.LTC_FLOAT_1 );
  35. ltc_2 = texture( UniformsLib.LTC_FLOAT_2 );
  36. } else {
  37. ltc_1 = texture( UniformsLib.LTC_HALF_1 );
  38. ltc_2 = texture( UniformsLib.LTC_HALF_2 );
  39. }
  40. }
  41. const { colorNode, light } = this;
  42. const lightingModel = builder.context.lightingModel;
  43. const lightPosition = objectViewPosition( light );
  44. const reflectedLight = builder.context.reflectedLight;
  45. lightingModel.directRectArea( {
  46. lightColor: colorNode,
  47. lightPosition,
  48. halfWidth: this.halfWidth,
  49. halfHeight: this.halfHeight,
  50. reflectedLight,
  51. ltc_1,
  52. ltc_2
  53. }, builder.stack, builder );
  54. }
  55. }
  56. export default RectAreaLightNode;
  57. addNodeClass( 'RectAreaLightNode', RectAreaLightNode );
  58. addLightNode( RectAreaLight, RectAreaLightNode );