CameraNode.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.nodeType = "Camera";
  25. THREE.CameraNode.prototype.setCamera = function ( camera ) {
  26. this.camera = camera;
  27. this.updateFrame = camera !== undefined ? this.onUpdateFrame : undefined;
  28. };
  29. THREE.CameraNode.prototype.setScope = function ( scope ) {
  30. switch ( this.scope ) {
  31. case THREE.CameraNode.DEPTH:
  32. delete this.near;
  33. delete this.far;
  34. break;
  35. }
  36. this.scope = scope;
  37. switch ( scope ) {
  38. case THREE.CameraNode.DEPTH:
  39. var camera = this.camera;
  40. this.near = new THREE.FloatNode( camera ? camera.near : 1 );
  41. this.far = new THREE.FloatNode( camera ? camera.far : 1200 );
  42. break;
  43. }
  44. };
  45. THREE.CameraNode.prototype.getType = function ( builder ) {
  46. switch ( this.scope ) {
  47. case THREE.CameraNode.DEPTH:
  48. return 'fv1';
  49. }
  50. return this.type;
  51. };
  52. THREE.CameraNode.prototype.isUnique = function ( builder ) {
  53. switch ( this.scope ) {
  54. case THREE.CameraNode.DEPTH:
  55. case THREE.CameraNode.TO_VERTEX:
  56. return true;
  57. }
  58. return false;
  59. };
  60. THREE.CameraNode.prototype.isShared = function ( builder ) {
  61. switch ( this.scope ) {
  62. case THREE.CameraNode.POSITION:
  63. return false;
  64. }
  65. return true;
  66. };
  67. THREE.CameraNode.prototype.generate = function ( builder, output ) {
  68. var material = builder.material;
  69. var result;
  70. switch ( this.scope ) {
  71. case THREE.CameraNode.POSITION:
  72. result = 'cameraPosition';
  73. break;
  74. case THREE.CameraNode.DEPTH:
  75. var func = THREE.CameraNode.fDepthColor;
  76. builder.include( func );
  77. result = func.name + '(' + this.near.build( builder, 'fv1' ) + ',' + this.far.build( builder, 'fv1' ) + ')';
  78. break;
  79. case THREE.CameraNode.TO_VERTEX:
  80. result = 'normalize( ' + new THREE.PositionNode( THREE.PositionNode.WORLD ).build( builder, 'v3' ) + ' - cameraPosition )';
  81. break;
  82. }
  83. return builder.format( result, this.getType( builder ), output );
  84. };
  85. THREE.CameraNode.prototype.onUpdateFrame = function ( frame ) {
  86. switch ( this.scope ) {
  87. case THREE.CameraNode.DEPTH:
  88. var camera = this.camera;
  89. this.near.number = camera.near;
  90. this.far.number = camera.far;
  91. break;
  92. }
  93. };
  94. THREE.CameraNode.prototype.toJSON = function ( meta ) {
  95. var data = this.getJSONNode( meta );
  96. if ( ! data ) {
  97. data = this.createJSONNode( meta );
  98. data.scope = this.scope;
  99. if ( this.camera ) data.camera = this.camera.uuid;
  100. switch ( this.scope ) {
  101. case THREE.CameraNode.DEPTH:
  102. data.near = this.near.number;
  103. data.far = this.far.number;
  104. break;
  105. }
  106. }
  107. return data;
  108. };