PositionNode.js 2.1 KB

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