PositionNode.js 2.5 KB

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