CameraNode.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. const scope = this.scope;
  17. if ( scope === CameraNode.PROJECTION || scope === CameraNode.VIEW ) {
  18. return 'mat4';
  19. }
  20. return 'vec3';
  21. }
  22. update( frame ) {
  23. const camera = frame.camera;
  24. const inputNode = this._inputNode;
  25. const scope = this.scope;
  26. if ( scope === CameraNode.PROJECTION ) {
  27. inputNode.value = camera.projectionMatrix;
  28. } else if ( scope === CameraNode.VIEW ) {
  29. inputNode.value = camera.matrixWorldInverse;
  30. } else if ( scope === CameraNode.POSITION ) {
  31. camera.getWorldPosition( inputNode.value );
  32. }
  33. }
  34. generate( builder, output ) {
  35. const nodeData = builder.getDataFromNode( this );
  36. let inputNode = this._inputNode;
  37. if ( nodeData.inputNode === undefined ) {
  38. const scope = this.scope;
  39. if ( scope === CameraNode.PROJECTION || scope === CameraNode.VIEW ) {
  40. if ( inputNode === null || inputNode.isMatrix4Node !== true ) {
  41. inputNode = new Matrix4Node( null );
  42. }
  43. } else if ( scope === CameraNode.POSITION ) {
  44. if ( inputNode === null || inputNode.isVector3Node !== true ) {
  45. inputNode = new Vector3Node();
  46. }
  47. }
  48. this._inputNode = inputNode;
  49. nodeData.inputNode = inputNode;
  50. }
  51. return inputNode.build( builder, output );
  52. }
  53. }
  54. export default CameraNode;