CameraNode.js 3.6 KB

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