Object3DNode.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import Node from '../core/Node.js';
  2. import Matrix4Node from '../inputs/Matrix4Node.js';
  3. import Matrix3Node from '../inputs/Matrix3Node.js';
  4. import Vector3Node from '../inputs/Vector3Node.js';
  5. import { NodeUpdateType } from '../core/constants.js';
  6. class Object3DNode extends Node {
  7. static VIEW_MATRIX = 'viewMatrix';
  8. static NORMAL_MATRIX = 'normalMatrix';
  9. static WORLD_MATRIX = 'worldMatrix';
  10. static POSITION = 'position';
  11. static VIEW_POSITION = 'viewPosition';
  12. constructor( scope = Object3DNode.VIEW_MATRIX, object3d = null ) {
  13. super();
  14. this.scope = scope;
  15. this.object3d = object3d;
  16. this.updateType = NodeUpdateType.Object;
  17. this._inputNode = null;
  18. }
  19. getNodeType() {
  20. const scope = this.scope;
  21. if ( scope === Object3DNode.WORLD_MATRIX || scope === Object3DNode.VIEW_MATRIX ) {
  22. return 'mat4';
  23. } else if ( scope === Object3DNode.NORMAL_MATRIX ) {
  24. return 'mat3';
  25. } else if ( scope === Object3DNode.POSITION || scope === Object3DNode.VIEW_POSITION ) {
  26. return 'vec3';
  27. }
  28. }
  29. update( frame ) {
  30. const object = this.object3d !== null ? this.object3d : frame.object;
  31. const inputNode = this._inputNode;
  32. const camera = frame.camera;
  33. const scope = this.scope;
  34. if ( scope === Object3DNode.VIEW_MATRIX ) {
  35. inputNode.value = object.modelViewMatrix;
  36. } else if ( scope === Object3DNode.NORMAL_MATRIX ) {
  37. inputNode.value = object.normalMatrix;
  38. } else if ( scope === Object3DNode.WORLD_MATRIX ) {
  39. inputNode.value = object.matrixWorld;
  40. } else if ( scope === Object3DNode.POSITION ) {
  41. inputNode.value.setFromMatrixPosition( object.matrixWorld );
  42. } else if ( scope === Object3DNode.VIEW_POSITION ) {
  43. inputNode.value.setFromMatrixPosition( object.matrixWorld );
  44. inputNode.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._inputNode = new Matrix4Node( /*null*/ );
  51. } else if ( scope === Object3DNode.NORMAL_MATRIX ) {
  52. this._inputNode = new Matrix3Node( /*null*/ );
  53. } else if ( scope === Object3DNode.POSITION || scope === Object3DNode.VIEW_POSITION ) {
  54. this._inputNode = new Vector3Node();
  55. }
  56. return this._inputNode.build( builder );
  57. }
  58. }
  59. export default Object3DNode;