PositionNode.js 2.3 KB

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