CameraNode.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import { TempNode } from '../core/TempNode.js';
  2. import { FunctionNode } from '../core/FunctionNode.js';
  3. import { FloatNode } from '../inputs/FloatNode.js';
  4. import { PositionNode } from '../accessors/PositionNode.js';
  5. class CameraNode extends TempNode {
  6. constructor( scope, camera ) {
  7. super( 'v3' );
  8. this.setScope( scope || CameraNode.POSITION );
  9. this.setCamera( camera );
  10. }
  11. setCamera( camera ) {
  12. this.camera = camera;
  13. this.updateFrame = camera !== undefined ? this.onUpdateFrame : undefined;
  14. }
  15. setScope( scope ) {
  16. switch ( this.scope ) {
  17. case CameraNode.DEPTH:
  18. delete this.near;
  19. delete this.far;
  20. break;
  21. }
  22. this.scope = scope;
  23. switch ( scope ) {
  24. case CameraNode.DEPTH:
  25. const camera = this.camera;
  26. this.near = new FloatNode( camera ? camera.near : 1 );
  27. this.far = new FloatNode( camera ? camera.far : 1200 );
  28. break;
  29. }
  30. }
  31. getType( /* builder */ ) {
  32. switch ( this.scope ) {
  33. case CameraNode.DEPTH:
  34. return 'f';
  35. }
  36. return this.type;
  37. }
  38. getUnique( /* builder */ ) {
  39. switch ( this.scope ) {
  40. case CameraNode.DEPTH:
  41. case CameraNode.TO_VERTEX:
  42. return true;
  43. }
  44. return false;
  45. }
  46. getShared( /* builder */ ) {
  47. switch ( this.scope ) {
  48. case CameraNode.POSITION:
  49. return false;
  50. }
  51. return true;
  52. }
  53. generate( builder, output ) {
  54. let result;
  55. switch ( this.scope ) {
  56. case CameraNode.POSITION:
  57. result = 'cameraPosition';
  58. break;
  59. case CameraNode.DEPTH:
  60. const depthColor = builder.include( CameraNode.Nodes.depthColor );
  61. result = depthColor + '( ' + this.near.build( builder, 'f' ) + ', ' + this.far.build( builder, 'f' ) + ' )';
  62. break;
  63. case CameraNode.TO_VERTEX:
  64. result = 'normalize( ' + new PositionNode( PositionNode.WORLD ).build( builder, 'v3' ) + ' - cameraPosition )';
  65. break;
  66. }
  67. return builder.format( result, this.getType( builder ), output );
  68. }
  69. onUpdateFrame( /* frame */ ) {
  70. switch ( this.scope ) {
  71. case CameraNode.DEPTH:
  72. const camera = this.camera;
  73. this.near.value = camera.near;
  74. this.far.value = camera.far;
  75. break;
  76. }
  77. }
  78. copy( source ) {
  79. super.copy( source );
  80. this.setScope( source.scope );
  81. if ( source.camera ) {
  82. this.setCamera( source.camera );
  83. }
  84. switch ( source.scope ) {
  85. case CameraNode.DEPTH:
  86. this.near.number = source.near;
  87. this.far.number = source.far;
  88. break;
  89. }
  90. return this;
  91. }
  92. toJSON( meta ) {
  93. let data = this.getJSONNode( meta );
  94. if ( ! data ) {
  95. data = this.createJSONNode( meta );
  96. data.scope = this.scope;
  97. if ( this.camera ) data.camera = this.camera.uuid;
  98. switch ( this.scope ) {
  99. case CameraNode.DEPTH:
  100. data.near = this.near.value;
  101. data.far = this.far.value;
  102. break;
  103. }
  104. }
  105. return data;
  106. }
  107. }
  108. CameraNode.Nodes = ( function () {
  109. const depthColor = new FunctionNode( /* glsl */`
  110. float depthColor( float mNear, float mFar ) {
  111. #ifdef USE_LOGDEPTHBUF_EXT
  112. float depth = gl_FragDepthEXT / gl_FragCoord.w;
  113. #else
  114. float depth = gl_FragCoord.z / gl_FragCoord.w;
  115. #endif
  116. return 1.0 - smoothstep( mNear, mFar, depth );
  117. }`
  118. );
  119. return {
  120. depthColor: depthColor
  121. };
  122. } )();
  123. CameraNode.POSITION = 'position';
  124. CameraNode.DEPTH = 'depth';
  125. CameraNode.TO_VERTEX = 'toVertex';
  126. CameraNode.prototype.nodeType = 'Camera';
  127. export { CameraNode };