RectAreaLightNode.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 } from '../../lights/RectAreaLight.js';
  8. import { Matrix4 } from '../../math/Matrix4.js';
  9. import { Vector3 } from '../../math/Vector3.js';
  10. const _matrix41 = /*@__PURE__*/ new Matrix4();
  11. const _matrix42 = /*@__PURE__*/ new Matrix4();
  12. let ltcLib = null;
  13. class RectAreaLightNode extends AnalyticLightNode {
  14. constructor( light = null ) {
  15. super( light );
  16. this.halfHeight = uniform( new Vector3() );
  17. this.halfWidth = uniform( new Vector3() );
  18. }
  19. update( frame ) {
  20. super.update( frame );
  21. const { light } = this;
  22. const viewMatrix = frame.camera.matrixWorldInverse;
  23. _matrix42.identity();
  24. _matrix41.copy( light.matrixWorld );
  25. _matrix41.premultiply( viewMatrix );
  26. _matrix42.extractRotation( _matrix41 );
  27. this.halfWidth.value.set( light.width * 0.5, 0.0, 0.0 );
  28. this.halfHeight.value.set( 0.0, light.height * 0.5, 0.0 );
  29. this.halfWidth.value.applyMatrix4( _matrix42 );
  30. this.halfHeight.value.applyMatrix4( _matrix42 );
  31. }
  32. setup( builder ) {
  33. super.setup( builder );
  34. let ltc_1, ltc_2;
  35. if ( builder.isAvailable( 'float32Filterable' ) ) {
  36. ltc_1 = texture( ltcLib.LTC_FLOAT_1 );
  37. ltc_2 = texture( ltcLib.LTC_FLOAT_2 );
  38. } else {
  39. ltc_1 = texture( ltcLib.LTC_HALF_1 );
  40. ltc_2 = texture( ltcLib.LTC_HALF_2 );
  41. }
  42. const { colorNode, light } = this;
  43. const lightingModel = builder.context.lightingModel;
  44. const lightPosition = objectViewPosition( light );
  45. const reflectedLight = builder.context.reflectedLight;
  46. lightingModel.directRectArea( {
  47. lightColor: colorNode,
  48. lightPosition,
  49. halfWidth: this.halfWidth,
  50. halfHeight: this.halfHeight,
  51. reflectedLight,
  52. ltc_1,
  53. ltc_2
  54. }, builder.stack, builder );
  55. }
  56. static setLTC( ltc ) {
  57. ltcLib = ltc;
  58. }
  59. }
  60. export default RectAreaLightNode;
  61. addNodeClass( 'RectAreaLightNode', RectAreaLightNode );
  62. addLightNode( RectAreaLight, RectAreaLightNode );