PositionNode.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { TempNode } from '../core/TempNode.js';
  5. import { NodeLib } from '../core/NodeLib.js';
  6. function PositionNode( scope ) {
  7. TempNode.call( this, 'v3' );
  8. this.scope = scope || PositionNode.LOCAL;
  9. }
  10. PositionNode.LOCAL = 'local';
  11. PositionNode.WORLD = 'world';
  12. PositionNode.VIEW = 'view';
  13. PositionNode.PROJECTION = 'projection';
  14. PositionNode.prototype = Object.create( TempNode.prototype );
  15. PositionNode.prototype.constructor = PositionNode;
  16. PositionNode.prototype.nodeType = "Position";
  17. PositionNode.prototype.getType = function ( ) {
  18. switch ( this.scope ) {
  19. case PositionNode.PROJECTION:
  20. return 'v4';
  21. }
  22. return this.type;
  23. };
  24. PositionNode.prototype.getShared = function ( /* builder */ ) {
  25. switch ( this.scope ) {
  26. case PositionNode.LOCAL:
  27. case PositionNode.WORLD:
  28. return false;
  29. }
  30. return true;
  31. };
  32. PositionNode.prototype.generate = function ( builder, output ) {
  33. var result;
  34. switch ( this.scope ) {
  35. case PositionNode.LOCAL:
  36. if ( builder.isShader( 'vertex' ) ) {
  37. result = 'transformed';
  38. } else {
  39. builder.requires.position = true;
  40. result = 'vPosition';
  41. }
  42. break;
  43. case PositionNode.WORLD:
  44. if ( builder.isShader( 'vertex' ) ) {
  45. return '( modelMatrix * vec4( transformed, 1.0 ) ).xyz';
  46. } else {
  47. builder.requires.worldPosition = true;
  48. result = 'vWPosition';
  49. }
  50. break;
  51. case PositionNode.VIEW:
  52. result = builder.isShader( 'vertex' ) ? '-mvPosition.xyz' : 'vViewPosition';
  53. break;
  54. case PositionNode.PROJECTION:
  55. result = builder.isShader( 'vertex' ) ? '( projectionMatrix * modelViewMatrix * vec4( position, 1.0 ) )' : 'vec4( 0.0 )';
  56. break;
  57. }
  58. return builder.format( result, this.getType( builder ), output );
  59. };
  60. PositionNode.prototype.copy = function ( source ) {
  61. TempNode.prototype.copy.call( this, source );
  62. this.scope = source.scope;
  63. return this;
  64. };
  65. PositionNode.prototype.toJSON = function ( meta ) {
  66. var data = this.getJSONNode( meta );
  67. if ( ! data ) {
  68. data = this.createJSONNode( meta );
  69. data.scope = this.scope;
  70. }
  71. return data;
  72. };
  73. NodeLib.addKeyword( 'position', function () {
  74. return new PositionNode();
  75. } );
  76. NodeLib.addKeyword( 'worldPosition', function () {
  77. return new PositionNode( PositionNode.WORLD );
  78. } );
  79. NodeLib.addKeyword( 'viewPosition', function () {
  80. return new PositionNode( PositionNode.VIEW );
  81. } );
  82. export { PositionNode };