Object3DNode.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { Vector3 } from 'three';
  2. import Node from '../core/Node.js';
  3. import UniformNode from '../core/UniformNode.js';
  4. import { NodeUpdateType } from '../core/constants.js';
  5. class Object3DNode extends Node {
  6. static VIEW_MATRIX = 'viewMatrix';
  7. static NORMAL_MATRIX = 'normalMatrix';
  8. static WORLD_MATRIX = 'worldMatrix';
  9. static POSITION = 'position';
  10. static VIEW_POSITION = 'viewPosition';
  11. constructor( scope = Object3DNode.VIEW_MATRIX, object3d = null ) {
  12. super();
  13. this.scope = scope;
  14. this.object3d = object3d;
  15. this.updateType = NodeUpdateType.Object;
  16. this._uniformNode = new UniformNode( null );
  17. }
  18. getNodeType() {
  19. const scope = this.scope;
  20. if ( scope === Object3DNode.WORLD_MATRIX || scope === Object3DNode.VIEW_MATRIX ) {
  21. return 'mat4';
  22. } else if ( scope === Object3DNode.NORMAL_MATRIX ) {
  23. return 'mat3';
  24. } else if ( scope === Object3DNode.POSITION || scope === Object3DNode.VIEW_POSITION ) {
  25. return 'vec3';
  26. }
  27. }
  28. update( frame ) {
  29. const object = this.object3d;
  30. const uniformNode = this._uniformNode;
  31. const scope = this.scope;
  32. if ( scope === Object3DNode.VIEW_MATRIX ) {
  33. uniformNode.value = object.modelViewMatrix;
  34. } else if ( scope === Object3DNode.NORMAL_MATRIX ) {
  35. uniformNode.value = object.normalMatrix;
  36. } else if ( scope === Object3DNode.WORLD_MATRIX ) {
  37. uniformNode.value = object.matrixWorld;
  38. } else if ( scope === Object3DNode.POSITION ) {
  39. uniformNode.value.setFromMatrixPosition( object.matrixWorld );
  40. } else if ( scope === Object3DNode.VIEW_POSITION ) {
  41. const camera = frame.camera;
  42. uniformNode.value = uniformNode.value || new Vector3();
  43. uniformNode.value.setFromMatrixPosition( object.matrixWorld );
  44. uniformNode.value.applyMatrix4( camera.matrixWorldInverse );
  45. }
  46. }
  47. generate( builder ) {
  48. const scope = this.scope;
  49. if ( scope === Object3DNode.WORLD_MATRIX || scope === Object3DNode.VIEW_MATRIX ) {
  50. this._uniformNode.nodeType = 'mat4';
  51. } else if ( scope === Object3DNode.NORMAL_MATRIX ) {
  52. this._uniformNode.nodeType = 'mat3';
  53. } else if ( scope === Object3DNode.POSITION || scope === Object3DNode.VIEW_POSITION ) {
  54. this._uniformNode.nodeType = 'vec3';
  55. }
  56. return this._uniformNode.build( builder );
  57. }
  58. serialize( data ) {
  59. super.serialize( data );
  60. data.scope = this.scope;
  61. }
  62. deserialize( data ) {
  63. super.deserialize( data );
  64. this.scope = data.scope;
  65. }
  66. }
  67. export default Object3DNode;