PositionNode.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.isShared = 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. builder.requires.position = true;
  37. result = builder.isShader( 'vertex' ) ? 'transformed' : 'vPosition';
  38. break;
  39. case PositionNode.WORLD:
  40. builder.requires.worldPosition = true;
  41. result = 'vWPosition';
  42. break;
  43. case PositionNode.VIEW:
  44. result = builder.isShader( 'vertex' ) ? '-mvPosition.xyz' : 'vViewPosition';
  45. break;
  46. case PositionNode.PROJECTION:
  47. result = builder.isShader( 'vertex' ) ? '( projectionMatrix * modelViewMatrix * vec4( position, 1.0 ) )' : 'vec4( 0.0 )';
  48. break;
  49. }
  50. return builder.format( result, this.getType( builder ), output );
  51. };
  52. PositionNode.prototype.copy = function ( source ) {
  53. TempNode.prototype.copy.call( this, source );
  54. this.scope = source.scope;
  55. };
  56. PositionNode.prototype.toJSON = function ( meta ) {
  57. var data = this.getJSONNode( meta );
  58. if ( ! data ) {
  59. data = this.createJSONNode( meta );
  60. data.scope = this.scope;
  61. }
  62. return data;
  63. };
  64. NodeLib.addKeyword( 'position', function () {
  65. return new PositionNode();
  66. } );
  67. NodeLib.addKeyword( 'worldPosition', function () {
  68. return new PositionNode( PositionNode.WORLD );
  69. } );
  70. NodeLib.addKeyword( 'viewPosition', function () {
  71. return new PositionNode( NormalNode.VIEW );
  72. } );
  73. export { PositionNode };