CameraNode.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import Object3DNode from './Object3DNode.js';
  2. import { addNodeClass } from '../core/Node.js';
  3. import { nodeImmutable } from '../shadernode/ShaderNode.js';
  4. class CameraNode extends Object3DNode {
  5. constructor( scope = CameraNode.POSITION ) {
  6. super( scope );
  7. }
  8. getNodeType( builder ) {
  9. const scope = this.scope;
  10. if ( scope === CameraNode.PROJECTION_MATRIX ) {
  11. return 'mat4';
  12. } else if ( scope === CameraNode.NEAR || scope === CameraNode.FAR ) {
  13. return 'float';
  14. }
  15. return super.getNodeType( builder );
  16. }
  17. update( frame ) {
  18. const camera = frame.camera;
  19. const uniformNode = this._uniformNode;
  20. const scope = this.scope;
  21. if ( scope === CameraNode.VIEW_MATRIX ) {
  22. uniformNode.value = camera.matrixWorldInverse;
  23. } else if ( scope === CameraNode.PROJECTION_MATRIX ) {
  24. uniformNode.value = camera.projectionMatrix;
  25. } else if ( scope === CameraNode.NEAR ) {
  26. uniformNode.value = camera.near;
  27. } else if ( scope === CameraNode.FAR ) {
  28. uniformNode.value = camera.far;
  29. } else {
  30. this.object3d = camera;
  31. super.update( frame );
  32. }
  33. }
  34. generate( builder ) {
  35. const scope = this.scope;
  36. if ( scope === CameraNode.PROJECTION_MATRIX ) {
  37. this._uniformNode.nodeType = 'mat4';
  38. } else if ( scope === CameraNode.NEAR || scope === CameraNode.FAR ) {
  39. this._uniformNode.nodeType = 'float';
  40. }
  41. return super.generate( builder );
  42. }
  43. }
  44. CameraNode.PROJECTION_MATRIX = 'projectionMatrix';
  45. CameraNode.NEAR = 'near';
  46. CameraNode.FAR = 'far';
  47. export default CameraNode;
  48. export const cameraProjectionMatrix = nodeImmutable( CameraNode, CameraNode.PROJECTION_MATRIX );
  49. export const cameraNear = nodeImmutable( CameraNode, CameraNode.NEAR );
  50. export const cameraFar = nodeImmutable( CameraNode, CameraNode.FAR );
  51. export const cameraViewMatrix = nodeImmutable( CameraNode, CameraNode.VIEW_MATRIX );
  52. export const cameraNormalMatrix = nodeImmutable( CameraNode, CameraNode.NORMAL_MATRIX );
  53. export const cameraWorldMatrix = nodeImmutable( CameraNode, CameraNode.WORLD_MATRIX );
  54. export const cameraPosition = nodeImmutable( CameraNode, CameraNode.POSITION );
  55. addNodeClass( CameraNode );