CameraNode.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import Node from '../core/Node.js';
  2. import Vector3Node from '../inputs/Vector3Node.js';
  3. import Matrix4Node from '../inputs/Matrix4Node.js';
  4. import { NodeUpdateType } from '../core/constants.js';
  5. class CameraNode extends Node {
  6. static POSITION = 'position';
  7. static PROJECTION = 'projection';
  8. static VIEW = 'view';
  9. constructor( scope = CameraNode.POSITION ) {
  10. super();
  11. this.updateType = NodeUpdateType.Frame;
  12. this.scope = scope;
  13. this.inputNode = null;
  14. }
  15. getType() {
  16. if ( this.scope === CameraNode.PROJECTION || this.scope === CameraNode.VIEW ) {
  17. return 'mat4';
  18. }
  19. return 'vec3';
  20. }
  21. update( frame ) {
  22. const camera = frame.camera;
  23. const inputNode = this.inputNode;
  24. if ( this.scope === CameraNode.PROJECTION ) {
  25. inputNode.value = camera.projectionMatrix;
  26. } else if ( this.scope === CameraNode.VIEW ) {
  27. inputNode.value = camera.matrixWorldInverse;
  28. } else if ( this.scope === CameraNode.POSITION ) {
  29. camera.getWorldPosition( inputNode.value );
  30. }
  31. }
  32. generate( builder, output ) {
  33. const nodeData = builder.getDataFromNode( this );
  34. if ( this.initialized !== true ) {
  35. if ( this.scope === CameraNode.PROJECTION || this.scope === CameraNode.VIEW ) {
  36. this.inputNode = new Matrix4Node( null );
  37. } else {
  38. this.inputNode = new Vector3Node();
  39. }
  40. nodeData.initialized = true;
  41. }
  42. return this.inputNode.build( builder, output );
  43. }
  44. }
  45. export default CameraNode;