RectAreaLightNode.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import AnalyticLightNode from './AnalyticLightNode.js';
  2. import { texture } from '../accessors/TextureNode.js';
  3. import { uniform } from '../core/UniformNode.js';
  4. import { lightViewPosition } from '../accessors/Lights.js';
  5. import { renderGroup } from '../core/UniformGroupNode.js';
  6. import { Matrix4 } from '../../math/Matrix4.js';
  7. import { Vector3 } from '../../math/Vector3.js';
  8. import { NodeUpdateType } from '../core/constants.js';
  9. const _matrix41 = /*@__PURE__*/ new Matrix4();
  10. const _matrix42 = /*@__PURE__*/ new Matrix4();
  11. let _ltcLib = null;
  12. /**
  13. * Module for representing rect area lights as nodes.
  14. *
  15. * @augments AnalyticLightNode
  16. */
  17. class RectAreaLightNode extends AnalyticLightNode {
  18. static get type() {
  19. return 'RectAreaLightNode';
  20. }
  21. /**
  22. * Constructs a new rect area light node.
  23. *
  24. * @param {?RectAreaLight} [light=null] - The rect area light source.
  25. */
  26. constructor( light = null ) {
  27. super( light );
  28. /**
  29. * Uniform node representing the half height of the are light.
  30. *
  31. * @type {UniformNode<vec3>}
  32. */
  33. this.halfHeight = uniform( new Vector3() ).setGroup( renderGroup );
  34. /**
  35. * Uniform node representing the half width of the are light.
  36. *
  37. * @type {UniformNode<vec3>}
  38. */
  39. this.halfWidth = uniform( new Vector3() ).setGroup( renderGroup );
  40. /**
  41. * The `updateType` is set to `NodeUpdateType.RENDER` since the light
  42. * relies on `viewMatrix` which might vary per render call.
  43. *
  44. * @type {string}
  45. * @default 'render'
  46. */
  47. this.updateType = NodeUpdateType.RENDER;
  48. }
  49. /**
  50. * Overwritten to updated rect area light specific uniforms.
  51. *
  52. * @param {NodeFrame} frame - A reference to the current node frame.
  53. */
  54. update( frame ) {
  55. super.update( frame );
  56. const { light } = this;
  57. const viewMatrix = frame.camera.matrixWorldInverse;
  58. _matrix42.identity();
  59. _matrix41.copy( light.matrixWorld );
  60. _matrix41.premultiply( viewMatrix );
  61. _matrix42.extractRotation( _matrix41 );
  62. this.halfWidth.value.set( light.width * 0.5, 0.0, 0.0 );
  63. this.halfHeight.value.set( 0.0, light.height * 0.5, 0.0 );
  64. this.halfWidth.value.applyMatrix4( _matrix42 );
  65. this.halfHeight.value.applyMatrix4( _matrix42 );
  66. }
  67. setupDirectRectArea( builder ) {
  68. let ltc_1, ltc_2;
  69. if ( builder.isAvailable( 'float32Filterable' ) ) {
  70. ltc_1 = texture( _ltcLib.LTC_FLOAT_1 );
  71. ltc_2 = texture( _ltcLib.LTC_FLOAT_2 );
  72. } else {
  73. ltc_1 = texture( _ltcLib.LTC_HALF_1 );
  74. ltc_2 = texture( _ltcLib.LTC_HALF_2 );
  75. }
  76. const { colorNode, light } = this;
  77. const lightPosition = lightViewPosition( light );
  78. return {
  79. lightColor: colorNode,
  80. lightPosition,
  81. halfWidth: this.halfWidth,
  82. halfHeight: this.halfHeight,
  83. ltc_1,
  84. ltc_2
  85. };
  86. }
  87. /**
  88. * Used to configure the internal BRDF approximation texture data.
  89. *
  90. * @param {RectAreaLightTexturesLib} ltc - The BRDF approximation texture data.
  91. */
  92. static setLTC( ltc ) {
  93. _ltcLib = ltc;
  94. }
  95. }
  96. export default RectAreaLightNode;
粤ICP备19079148号