Object3DNode.js 6.7 KB

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