SceneNode.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { UVMapping } from '../../constants.js';
  2. import { Euler } from '../../math/Euler.js';
  3. import { Matrix4 } from '../../math/Matrix4.js';
  4. import Node from '../core/Node.js';
  5. import { renderGroup } from '../core/UniformGroupNode.js';
  6. import { nodeImmutable, uniform } from '../tsl/TSLBase.js';
  7. import { reference } from './ReferenceNode.js';
  8. import { error } from '../../utils.js';
  9. const _e1 = /*@__PURE__*/ new Euler();
  10. const _m1 = /*@__PURE__*/ new Matrix4();
  11. /**
  12. * This module allows access to a collection of scene properties. The following predefined TSL objects
  13. * are available for easier use:
  14. *
  15. * - `backgroundBlurriness`: A node that represents the scene's background blurriness.
  16. * - `backgroundIntensity`: A node that represents the scene's background intensity.
  17. * - `backgroundRotation`: A node that represents the scene's background rotation.
  18. *
  19. * @augments Node
  20. */
  21. class SceneNode extends Node {
  22. static get type() {
  23. return 'SceneNode';
  24. }
  25. /**
  26. * Constructs a new scene node.
  27. *
  28. * @param {('backgroundBlurriness'|'backgroundIntensity'|'backgroundRotation')} scope - The scope defines the type of scene property that is accessed.
  29. * @param {?Scene} [scene=null] - A reference to the scene.
  30. */
  31. constructor( scope = SceneNode.BACKGROUND_BLURRINESS, scene = null ) {
  32. super();
  33. /**
  34. * The scope defines the type of scene property that is accessed.
  35. *
  36. * @type {('backgroundBlurriness'|'backgroundIntensity'|'backgroundRotation')}
  37. */
  38. this.scope = scope;
  39. /**
  40. * A reference to the scene that is going to be accessed.
  41. *
  42. * @type {?Scene}
  43. * @default null
  44. */
  45. this.scene = scene;
  46. }
  47. /**
  48. * Depending on the scope, the method returns a different type of node that represents
  49. * the respective scene property.
  50. *
  51. * @param {NodeBuilder} builder - The current node builder.
  52. * @return {Node} The output node.
  53. */
  54. setup( builder ) {
  55. const scope = this.scope;
  56. const scene = this.scene !== null ? this.scene : builder.scene;
  57. let output;
  58. if ( scope === SceneNode.BACKGROUND_BLURRINESS ) {
  59. output = reference( 'backgroundBlurriness', 'float', scene );
  60. } else if ( scope === SceneNode.BACKGROUND_INTENSITY ) {
  61. output = reference( 'backgroundIntensity', 'float', scene );
  62. } else if ( scope === SceneNode.BACKGROUND_ROTATION ) {
  63. output = uniform( 'mat4' ).setName( 'backgroundRotation' ).setGroup( renderGroup ).onRenderUpdate( () => {
  64. const background = scene.background;
  65. if ( background !== null && background.isTexture && background.mapping !== UVMapping ) {
  66. _e1.copy( scene.backgroundRotation );
  67. // accommodate left-handed frame
  68. _e1.x *= - 1; _e1.y *= - 1; _e1.z *= - 1;
  69. _m1.makeRotationFromEuler( _e1 );
  70. } else {
  71. _m1.identity();
  72. }
  73. return _m1;
  74. } );
  75. } else {
  76. error( 'SceneNode: Unknown scope:', scope );
  77. }
  78. return output;
  79. }
  80. }
  81. SceneNode.BACKGROUND_BLURRINESS = 'backgroundBlurriness';
  82. SceneNode.BACKGROUND_INTENSITY = 'backgroundIntensity';
  83. SceneNode.BACKGROUND_ROTATION = 'backgroundRotation';
  84. export default SceneNode;
  85. /**
  86. * TSL object that represents the scene's background blurriness.
  87. *
  88. * @tsl
  89. * @type {SceneNode}
  90. */
  91. export const backgroundBlurriness = /*@__PURE__*/ nodeImmutable( SceneNode, SceneNode.BACKGROUND_BLURRINESS );
  92. /**
  93. * TSL object that represents the scene's background intensity.
  94. *
  95. * @tsl
  96. * @type {SceneNode}
  97. */
  98. export const backgroundIntensity = /*@__PURE__*/ nodeImmutable( SceneNode, SceneNode.BACKGROUND_INTENSITY );
  99. /**
  100. * TSL object that represents the scene's background rotation.
  101. *
  102. * @tsl
  103. * @type {SceneNode}
  104. */
  105. export const backgroundRotation = /*@__PURE__*/ nodeImmutable( SceneNode, SceneNode.BACKGROUND_ROTATION );
粤ICP备19079148号