CameraNode.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.POSITION = 'position';
  10. THREE.CameraNode.DEPTH = 'depth';
  11. THREE.CameraNode.TO_VERTEX = 'toVertex';
  12. THREE.CameraNode.prototype = Object.create( THREE.TempNode.prototype );
  13. THREE.CameraNode.prototype.constructor = THREE.CameraNode;
  14. THREE.CameraNode.prototype.setCamera = function( camera ) {
  15. this.camera = camera;
  16. this.requestUpdate = camera !== undefined;
  17. };
  18. THREE.CameraNode.prototype.setScope = function( scope ) {
  19. switch ( this.scope ) {
  20. case THREE.CameraNode.DEPTH:
  21. delete this.near;
  22. delete this.far;
  23. break;
  24. }
  25. this.scope = scope;
  26. switch ( scope ) {
  27. case THREE.CameraNode.DEPTH:
  28. this.near = new THREE.FloatNode( camera ? camera.near : 1 );
  29. this.far = new THREE.FloatNode( camera ? camera.far : 1200 );
  30. break;
  31. }
  32. };
  33. THREE.CameraNode.prototype.getType = function( builder ) {
  34. switch ( this.scope ) {
  35. case THREE.CameraNode.DEPTH:
  36. return 'fv1';
  37. }
  38. return this.type;
  39. };
  40. THREE.CameraNode.prototype.isUnique = function( builder ) {
  41. switch ( this.scope ) {
  42. case THREE.CameraNode.DEPTH:
  43. case THREE.CameraNode.TO_VERTEX:
  44. return true;
  45. }
  46. return false;
  47. };
  48. THREE.CameraNode.prototype.isShared = function( builder ) {
  49. switch ( this.scope ) {
  50. case THREE.CameraNode.POSITION:
  51. return false;
  52. }
  53. return true;
  54. };
  55. THREE.CameraNode.prototype.generate = function( builder, output ) {
  56. var material = builder.material;
  57. var result;
  58. switch ( this.scope ) {
  59. case THREE.CameraNode.POSITION:
  60. result = 'cameraPosition';
  61. break;
  62. case THREE.CameraNode.DEPTH:
  63. builder.include( 'depthcolor' );
  64. result = 'depthcolor(' + this.near.build( builder, 'fv1' ) + ',' + this.far.build( builder, 'fv1' ) + ')';
  65. break;
  66. case THREE.CameraNode.TO_VERTEX:
  67. result = 'normalize( ' + new THREE.PositionNode( THREE.PositionNode.WORLD ).build( builder, 'v3' ) + ' - cameraPosition )';
  68. break;
  69. }
  70. return builder.format( result, this.getType( builder ), output );
  71. };
  72. THREE.CameraNode.prototype.updateAnimation = function( delta ) {
  73. switch ( this.scope ) {
  74. case THREE.CameraNode.DEPTH:
  75. this.near.number = camera.near;
  76. this.far.number = camera.far;
  77. break;
  78. }
  79. };