CameraNode.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.CameraNode = function ( scope, camera ) {
  5. THREE.TempNode.call( this, 'v3' );
  6. this.setScope( scope || THREE.CameraNode.POSITION );
  7. this.setCamera( camera );
  8. };
  9. THREE.CameraNode.fDepthColor = new THREE.FunctionNode( [
  10. "float depthColor( float mNear, float mFar ) {",
  11. " #ifdef USE_LOGDEPTHBUF_EXT",
  12. " float depth = gl_FragDepthEXT / gl_FragCoord.w;",
  13. " #else",
  14. " float depth = gl_FragCoord.z / gl_FragCoord.w;",
  15. " #endif",
  16. " return 1.0 - smoothstep( mNear, mFar, depth );",
  17. "}"
  18. ].join( "\n" ) );
  19. THREE.CameraNode.POSITION = 'position';
  20. THREE.CameraNode.DEPTH = 'depth';
  21. THREE.CameraNode.TO_VERTEX = 'toVertex';
  22. THREE.CameraNode.prototype = Object.create( THREE.TempNode.prototype );
  23. THREE.CameraNode.prototype.constructor = THREE.CameraNode;
  24. THREE.CameraNode.prototype.setCamera = function ( camera ) {
  25. this.camera = camera;
  26. this.requestUpdate = camera !== undefined;
  27. };
  28. THREE.CameraNode.prototype.setScope = function ( scope ) {
  29. switch ( this.scope ) {
  30. case THREE.CameraNode.DEPTH:
  31. delete this.near;
  32. delete this.far;
  33. break;
  34. }
  35. this.scope = scope;
  36. switch ( scope ) {
  37. case THREE.CameraNode.DEPTH:
  38. var camera = this.camera;
  39. this.near = new THREE.FloatNode( camera ? camera.near : 1 );
  40. this.far = new THREE.FloatNode( camera ? camera.far : 1200 );
  41. break;
  42. }
  43. };
  44. THREE.CameraNode.prototype.getType = function ( builder ) {
  45. switch ( this.scope ) {
  46. case THREE.CameraNode.DEPTH:
  47. return 'fv1';
  48. }
  49. return this.type;
  50. };
  51. THREE.CameraNode.prototype.isUnique = function ( builder ) {
  52. switch ( this.scope ) {
  53. case THREE.CameraNode.DEPTH:
  54. case THREE.CameraNode.TO_VERTEX:
  55. return true;
  56. }
  57. return false;
  58. };
  59. THREE.CameraNode.prototype.isShared = function ( builder ) {
  60. switch ( this.scope ) {
  61. case THREE.CameraNode.POSITION:
  62. return false;
  63. }
  64. return true;
  65. };
  66. THREE.CameraNode.prototype.generate = function ( builder, output ) {
  67. var material = builder.material;
  68. var result;
  69. switch ( this.scope ) {
  70. case THREE.CameraNode.POSITION:
  71. result = 'cameraPosition';
  72. break;
  73. case THREE.CameraNode.DEPTH:
  74. var func = THREE.CameraNode.fDepthColor;
  75. builder.include( func );
  76. result = func.name + '(' + this.near.build( builder, 'fv1' ) + ',' + this.far.build( builder, 'fv1' ) + ')';
  77. break;
  78. case THREE.CameraNode.TO_VERTEX:
  79. result = 'normalize( ' + new THREE.PositionNode( THREE.PositionNode.WORLD ).build( builder, 'v3' ) + ' - cameraPosition )';
  80. break;
  81. }
  82. return builder.format( result, this.getType( builder ), output );
  83. };
  84. THREE.CameraNode.prototype.updateFrame = function ( delta ) {
  85. switch ( this.scope ) {
  86. case THREE.CameraNode.DEPTH:
  87. var camera = this.camera;
  88. this.near.number = camera.near;
  89. this.far.number = camera.far;
  90. break;
  91. }
  92. };