Object3DNode.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import Node from '../core/Node.js';
  2. import { NodeUpdateType } from '../core/constants.js';
  3. import UniformNode from '../core/UniformNode.js';
  4. import { nodeProxy } from '../tsl/TSLBase.js';
  5. import { Vector3 } from '../../math/Vector3.js';
  6. /**
  7. * This node can be used to access transformation related metrics of 3D objects.
  8. * Depending on the selected scope, a different metric is represented as a uniform
  9. * in the shader. The following scopes are supported:
  10. *
  11. * - `POSITION`: The object's position in world space.
  12. * - `VIEW_POSITION`: The object's position in view/camera space.
  13. * - `DIRECTION`: The object's direction in world space.
  14. * - `SCALE`: The object's scale in world space.
  15. * - `WORLD_MATRIX`: The object's matrix in world space.
  16. *
  17. * @augments Node
  18. */
  19. class Object3DNode extends Node {
  20. static get type() {
  21. return 'Object3DNode';
  22. }
  23. /**
  24. * Constructs a new object 3D node.
  25. *
  26. * @param {('position'|'viewPosition'|'direction'|'scale'|'worldMatrix')} scope - The node represents a different type of transformation depending on the scope.
  27. * @param {Object3D?} [object3d=null] - The 3D object.
  28. */
  29. constructor( scope, object3d = null ) {
  30. super();
  31. /**
  32. * The node reports a different type of transformation depending on the scope.
  33. *
  34. * @type {('position'|'viewPosition'|'direction'|'scale'|'worldMatrix')}
  35. */
  36. this.scope = scope;
  37. /**
  38. * The 3D object.
  39. *
  40. * @type {Object3D?}
  41. * @default null
  42. */
  43. this.object3d = object3d;
  44. /**
  45. * Overwritten since this type of node is updated per object.
  46. *
  47. * @type {String}
  48. * @default 'object'
  49. */
  50. this.updateType = NodeUpdateType.OBJECT;
  51. /**
  52. * Holds the value of the node as a uniform.
  53. *
  54. * @private
  55. * @type {UniformNode}
  56. */
  57. this._uniformNode = new UniformNode( null );
  58. }
  59. /**
  60. * Overwritten since the node type is inferred from the scope.
  61. *
  62. * @return {String} The node type.
  63. */
  64. getNodeType() {
  65. const scope = this.scope;
  66. if ( scope === Object3DNode.WORLD_MATRIX ) {
  67. return 'mat4';
  68. } else if ( scope === Object3DNode.POSITION || scope === Object3DNode.VIEW_POSITION || scope === Object3DNode.DIRECTION || scope === Object3DNode.SCALE ) {
  69. return 'vec3';
  70. }
  71. }
  72. /**
  73. * Updates the uniform value depending on the scope.
  74. *
  75. * @param {NodeFrame} frame - The current node frame.
  76. */
  77. update( frame ) {
  78. const object = this.object3d;
  79. const uniformNode = this._uniformNode;
  80. const scope = this.scope;
  81. if ( scope === Object3DNode.WORLD_MATRIX ) {
  82. uniformNode.value = object.matrixWorld;
  83. } else if ( scope === Object3DNode.POSITION ) {
  84. uniformNode.value = uniformNode.value || new Vector3();
  85. uniformNode.value.setFromMatrixPosition( object.matrixWorld );
  86. } else if ( scope === Object3DNode.SCALE ) {
  87. uniformNode.value = uniformNode.value || new Vector3();
  88. uniformNode.value.setFromMatrixScale( object.matrixWorld );
  89. } else if ( scope === Object3DNode.DIRECTION ) {
  90. uniformNode.value = uniformNode.value || new Vector3();
  91. object.getWorldDirection( uniformNode.value );
  92. } else if ( scope === Object3DNode.VIEW_POSITION ) {
  93. const camera = frame.camera;
  94. uniformNode.value = uniformNode.value || new Vector3();
  95. uniformNode.value.setFromMatrixPosition( object.matrixWorld );
  96. uniformNode.value.applyMatrix4( camera.matrixWorldInverse );
  97. }
  98. }
  99. /**
  100. * Generates the code snippet of the uniform node. The node type of the uniform
  101. * node also depends on the selected scope.
  102. *
  103. * @param {NodeBuilder} builder - The current node builder.
  104. * @return {String} The generated code snippet.
  105. */
  106. generate( builder ) {
  107. const scope = this.scope;
  108. if ( scope === Object3DNode.WORLD_MATRIX ) {
  109. this._uniformNode.nodeType = 'mat4';
  110. } else if ( scope === Object3DNode.POSITION || scope === Object3DNode.VIEW_POSITION || scope === Object3DNode.DIRECTION || scope === Object3DNode.SCALE ) {
  111. this._uniformNode.nodeType = 'vec3';
  112. }
  113. return this._uniformNode.build( builder );
  114. }
  115. serialize( data ) {
  116. super.serialize( data );
  117. data.scope = this.scope;
  118. }
  119. deserialize( data ) {
  120. super.deserialize( data );
  121. this.scope = data.scope;
  122. }
  123. }
  124. Object3DNode.WORLD_MATRIX = 'worldMatrix';
  125. Object3DNode.POSITION = 'position';
  126. Object3DNode.SCALE = 'scale';
  127. Object3DNode.VIEW_POSITION = 'viewPosition';
  128. Object3DNode.DIRECTION = 'direction';
  129. export default Object3DNode;
  130. /**
  131. * TSL function for creating an object 3D node that represents the object's direction in world space.
  132. *
  133. * @tsl
  134. * @function
  135. * @param {Object3D?} [object3d=null] - The 3D object.
  136. * @returns {Object3DNode<vec3>}
  137. */
  138. export const objectDirection = /*@__PURE__*/ nodeProxy( Object3DNode, Object3DNode.DIRECTION );
  139. /**
  140. * TSL function for creating an object 3D node that represents the object's world matrix.
  141. *
  142. * @tsl
  143. * @function
  144. * @param {Object3D?} [object3d=null] - The 3D object.
  145. * @returns {Object3DNode<mat4>}
  146. */
  147. export const objectWorldMatrix = /*@__PURE__*/ nodeProxy( Object3DNode, Object3DNode.WORLD_MATRIX );
  148. /**
  149. * TSL function for creating an object 3D node that represents the object's position in world space.
  150. *
  151. * @tsl
  152. * @function
  153. * @param {Object3D?} [object3d=null] - The 3D object.
  154. * @returns {Object3DNode<vec3>}
  155. */
  156. export const objectPosition = /*@__PURE__*/ nodeProxy( Object3DNode, Object3DNode.POSITION );
  157. /**
  158. * TSL function for creating an object 3D node that represents the object's scale in world space.
  159. *
  160. * @tsl
  161. * @function
  162. * @param {Object3D?} [object3d=null] - The 3D object.
  163. * @returns {Object3DNode<vec3>}
  164. */
  165. export const objectScale = /*@__PURE__*/ nodeProxy( Object3DNode, Object3DNode.SCALE );
  166. /**
  167. * TSL function for creating an object 3D node that represents the object's position in view/camera space.
  168. *
  169. * @tsl
  170. * @function
  171. * @param {Object3D?} [object3d=null] - The 3D object.
  172. * @returns {Object3DNode<vec3>}
  173. */
  174. export const objectViewPosition = /*@__PURE__*/ nodeProxy( Object3DNode, Object3DNode.VIEW_POSITION );
粤ICP备19079148号