|
| 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 | + |
| 6 | +class CameraNode extends Node { |
| 7 | + |
| 8 | + static POSITION = 'position'; |
| 9 | + static PROJECTION = 'projection'; |
| 10 | + static VIEW = 'view'; |
| 11 | + |
| 12 | + constructor( scope = CameraNode.POSITION ) { |
| 13 | + |
| 14 | + super(); |
| 15 | + |
| 16 | + this.updateType = NodeUpdateType.Frame; |
| 17 | + |
| 18 | + this.scope = scope; |
| 19 | + |
| 20 | + this._inputNode = null; |
| 21 | + |
| 22 | + } |
| 23 | + |
| 24 | + getType() { |
| 25 | + |
| 26 | + const scope = this.scope; |
| 27 | + |
| 28 | + if ( scope === CameraNode.PROJECTION || scope === CameraNode.VIEW ) { |
| 29 | + |
| 30 | + return 'mat4'; |
| 31 | + |
| 32 | + } |
| 33 | + |
| 34 | + return 'vec3'; |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + update( frame ) { |
| 39 | + |
| 40 | + const camera = frame.camera; |
| 41 | + const inputNode = this._inputNode; |
| 42 | + const scope = this.scope; |
| 43 | + |
| 44 | + if ( scope === CameraNode.PROJECTION ) { |
| 45 | + |
| 46 | + inputNode.value = camera.projectionMatrix; |
| 47 | + |
| 48 | + } else if ( scope === CameraNode.VIEW ) { |
| 49 | + |
| 50 | + inputNode.value = camera.matrixWorldInverse; |
| 51 | + |
| 52 | + } else if ( scope === CameraNode.POSITION ) { |
| 53 | + |
| 54 | + camera.getWorldPosition( inputNode.value ); |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + generate( builder, output ) { |
| 61 | + |
| 62 | + const nodeData = builder.getDataFromNode( this ); |
| 63 | + |
| 64 | + let inputNode = this._inputNode; |
| 65 | + |
| 66 | + if ( nodeData.inputNode === undefined ) { |
| 67 | + |
| 68 | + const scope = this.scope; |
| 69 | + |
| 70 | + if ( scope === CameraNode.PROJECTION || scope === CameraNode.VIEW ) { |
| 71 | + |
| 72 | + if ( inputNode === null || inputNode.isMatrix4Node !== true ) { |
| 73 | + |
| 74 | + inputNode = new Matrix4Node( null ); |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + } else if ( inputNode === null || inputNode.isVector3Node !== true ) { |
| 79 | + |
| 80 | + inputNode = new Vector3Node(); |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + this._inputNode = inputNode; |
| 85 | + |
| 86 | + nodeData.inputNode = inputNode; |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + return inputNode.build( builder, output ); |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | +} |
| 95 | + |
| 96 | +export default CameraNode; |
0 commit comments