CameraNode.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. this.near = new THREE.FloatNode( camera ? camera.near : 1 );
  39. this.far = new THREE.FloatNode( camera ? camera.far : 1200 );
  40. break;
  41. }
  42. };
  43. THREE.CameraNode.prototype.getType = function( builder ) {
  44. switch ( this.scope ) {
  45. case THREE.CameraNode.DEPTH:
  46. return 'fv1';
  47. }
  48. return this.type;
  49. };
  50. THREE.CameraNode.prototype.isUnique = function( builder ) {
  51. switch ( this.scope ) {
  52. case THREE.CameraNode.DEPTH:
  53. case THREE.CameraNode.TO_VERTEX:
  54. return true;
  55. }
  56. return false;
  57. };
  58. THREE.CameraNode.prototype.isShared = function( builder ) {
  59. switch ( this.scope ) {
  60. case THREE.CameraNode.POSITION:
  61. return false;
  62. }
  63. return true;
  64. };
  65. THREE.CameraNode.prototype.generate = function( builder, output ) {
  66. var material = builder.material;
  67. var result;
  68. switch ( this.scope ) {
  69. case THREE.CameraNode.POSITION:
  70. result = 'cameraPosition';
  71. break;
  72. case THREE.CameraNode.DEPTH:
  73. var func = THREE.CameraNode.fDepthColor;
  74. builder.include( func );
  75. result = func.name + '(' + this.near.build( builder, 'fv1' ) + ',' + this.far.build( builder, 'fv1' ) + ')';
  76. break;
  77. case THREE.CameraNode.TO_VERTEX:
  78. result = 'normalize( ' + new THREE.PositionNode( THREE.PositionNode.WORLD ).build( builder, 'v3' ) + ' - cameraPosition )';
  79. break;
  80. }
  81. return builder.format( result, this.getType( builder ), output );
  82. };
  83. THREE.CameraNode.prototype.updateFrame = function( delta ) {
  84. switch ( this.scope ) {
  85. case THREE.CameraNode.DEPTH:
  86. this.near.number = camera.near;
  87. this.far.number = camera.far;
  88. break;
  89. }
  90. };