CameraNode.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import Object3DNode from './Object3DNode.js';
  2. import Matrix4Node from '../inputs/Matrix4Node.js';
  3. class CameraNode extends Object3DNode {
  4. static PROJECTION_MATRIX = 'projectionMatrix';
  5. constructor( scope = CameraNode.POSITION ) {
  6. super( scope );
  7. }
  8. getType( builder ) {
  9. const scope = this.scope;
  10. if ( scope === CameraNode.PROJECTION_MATRIX ) {
  11. return 'mat4';
  12. }
  13. return super.getType( builder );
  14. }
  15. update( frame ) {
  16. const camera = frame.camera;
  17. const inputNode = this._inputNode;
  18. const scope = this.scope;
  19. if ( scope === CameraNode.PROJECTION_MATRIX ) {
  20. inputNode.value = camera.projectionMatrix;
  21. } else if ( scope === CameraNode.VIEW_MATRIX ) {
  22. inputNode.value = camera.matrixWorldInverse;
  23. } else {
  24. super.update( frame );
  25. }
  26. }
  27. generate( builder, output ) {
  28. const nodeData = builder.getDataFromNode( this );
  29. let inputNode = this._inputNode;
  30. if ( nodeData.inputNode === undefined ) {
  31. const scope = this.scope;
  32. if ( scope === CameraNode.PROJECTION_MATRIX ) {
  33. if ( inputNode === null || inputNode.isMatrix4Node !== true ) {
  34. inputNode = new Matrix4Node( null );
  35. }
  36. } else {
  37. return super.generate( builder, output );
  38. }
  39. this._inputNode = inputNode;
  40. nodeData.inputNode = inputNode;
  41. }
  42. return inputNode.build( builder, output );
  43. }
  44. }
  45. export default CameraNode;