CameraNode.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import Object3DNode from './Object3DNode.js';
  2. import { addNodeClass } from '../core/Node.js';
  3. import { label } from '../core/ContextNode.js';
  4. import { NodeUpdateType } from '../core/constants.js';
  5. //import { sharedUniformGroup } from '../core/UniformGroupNode.js';
  6. import { nodeImmutable } from '../shadernode/ShaderNode.js';
  7. //const cameraGroup = sharedUniformGroup( 'camera' );
  8. class CameraNode extends Object3DNode {
  9. constructor( scope = CameraNode.POSITION ) {
  10. super( scope );
  11. this.updateType = NodeUpdateType.RENDER;
  12. //this._uniformNode.groupNode = cameraGroup;
  13. }
  14. getNodeType( builder ) {
  15. const scope = this.scope;
  16. if ( scope === CameraNode.PROJECTION_MATRIX ) {
  17. return 'mat4';
  18. } else if ( scope === CameraNode.NEAR || scope === CameraNode.FAR || scope === CameraNode.LOG_DEPTH ) {
  19. return 'float';
  20. }
  21. return super.getNodeType( builder );
  22. }
  23. update( frame ) {
  24. const camera = frame.camera;
  25. const uniformNode = this._uniformNode;
  26. const scope = this.scope;
  27. //cameraGroup.needsUpdate = true;
  28. if ( scope === CameraNode.VIEW_MATRIX ) {
  29. uniformNode.value = camera.matrixWorldInverse;
  30. } else if ( scope === CameraNode.PROJECTION_MATRIX ) {
  31. uniformNode.value = camera.projectionMatrix;
  32. } else if ( scope === CameraNode.PROJECTION_MATRIX_INVERSE ) {
  33. uniformNode.value = camera.projectionMatrixInverse;
  34. } else if ( scope === CameraNode.NEAR ) {
  35. uniformNode.value = camera.near;
  36. } else if ( scope === CameraNode.FAR ) {
  37. uniformNode.value = camera.far;
  38. } else if ( scope === CameraNode.LOG_DEPTH ) {
  39. uniformNode.value = 2.0 / ( Math.log( camera.far + 1.0 ) / Math.LN2 );
  40. } else {
  41. this.object3d = camera;
  42. super.update( frame );
  43. }
  44. }
  45. generate( builder ) {
  46. const scope = this.scope;
  47. if ( scope === CameraNode.PROJECTION_MATRIX ) {
  48. this._uniformNode.nodeType = 'mat4';
  49. } else if ( scope === CameraNode.NEAR || scope === CameraNode.FAR || scope === CameraNode.LOG_DEPTH ) {
  50. this._uniformNode.nodeType = 'float';
  51. }
  52. return super.generate( builder );
  53. }
  54. }
  55. CameraNode.PROJECTION_MATRIX = 'projectionMatrix';
  56. CameraNode.PROJECTION_MATRIX_INVERSE = 'projectionMatrixInverse';
  57. CameraNode.NEAR = 'near';
  58. CameraNode.FAR = 'far';
  59. CameraNode.LOG_DEPTH = 'logDepth';
  60. export default CameraNode;
  61. export const cameraProjectionMatrix = label( nodeImmutable( CameraNode, CameraNode.PROJECTION_MATRIX ), 'projectionMatrix' );
  62. export const cameraProjectionMatrixInverse = label( nodeImmutable( CameraNode, CameraNode.PROJECTION_MATRIX_INVERSE ), 'projectionMatrixInverse' );
  63. export const cameraNear = nodeImmutable( CameraNode, CameraNode.NEAR );
  64. export const cameraFar = nodeImmutable( CameraNode, CameraNode.FAR );
  65. export const cameraLogDepth = nodeImmutable( CameraNode, CameraNode.LOG_DEPTH );
  66. export const cameraViewMatrix = nodeImmutable( CameraNode, CameraNode.VIEW_MATRIX );
  67. export const cameraNormalMatrix = nodeImmutable( CameraNode, CameraNode.NORMAL_MATRIX );
  68. export const cameraWorldMatrix = nodeImmutable( CameraNode, CameraNode.WORLD_MATRIX );
  69. export const cameraPosition = nodeImmutable( CameraNode, CameraNode.POSITION );
  70. addNodeClass( 'CameraNode', CameraNode );