PositionNode.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.PositionNode = function ( scope ) {
  5. THREE.TempNode.call( this, 'v3' );
  6. this.scope = scope || THREE.PositionNode.LOCAL;
  7. };
  8. THREE.PositionNode.LOCAL = 'local';
  9. THREE.PositionNode.WORLD = 'world';
  10. THREE.PositionNode.VIEW = 'view';
  11. THREE.PositionNode.PROJECTION = 'projection';
  12. THREE.PositionNode.prototype = Object.create( THREE.TempNode.prototype );
  13. THREE.PositionNode.prototype.constructor = THREE.PositionNode;
  14. THREE.PositionNode.prototype.nodeType = "Position";
  15. THREE.PositionNode.prototype.getType = function ( builder ) {
  16. switch ( this.scope ) {
  17. case THREE.PositionNode.PROJECTION:
  18. return 'v4';
  19. }
  20. return this.type;
  21. };
  22. THREE.PositionNode.prototype.isShared = function ( builder ) {
  23. switch ( this.scope ) {
  24. case THREE.PositionNode.LOCAL:
  25. case THREE.PositionNode.WORLD:
  26. return false;
  27. }
  28. return true;
  29. };
  30. THREE.PositionNode.prototype.generate = function ( builder, output ) {
  31. var material = builder.material;
  32. var result;
  33. switch ( this.scope ) {
  34. case THREE.PositionNode.LOCAL:
  35. material.requires.position = true;
  36. if ( builder.isShader( 'vertex' ) ) result = 'transformed';
  37. else result = 'vPosition';
  38. break;
  39. case THREE.PositionNode.WORLD:
  40. material.requires.worldPosition = true;
  41. if ( builder.isShader( 'vertex' ) ) result = 'vWPosition';
  42. else result = 'vWPosition';
  43. break;
  44. case THREE.PositionNode.VIEW:
  45. if ( builder.isShader( 'vertex' ) ) result = '-mvPosition.xyz';
  46. else result = 'vViewPosition';
  47. break;
  48. case THREE.PositionNode.PROJECTION:
  49. if ( builder.isShader( 'vertex' ) ) result = '(projectionMatrix * modelViewMatrix * vec4( position, 1.0 ))';
  50. else result = 'vec4( 0.0 )';
  51. break;
  52. }
  53. return builder.format( result, this.getType( builder ), output );
  54. };
  55. THREE.PositionNode.prototype.toJSON = function ( meta ) {
  56. var data = this.getJSONNode( meta );
  57. if ( ! data ) {
  58. data = this.createJSONNode( meta );
  59. data.scope = this.scope;
  60. }
  61. return data;
  62. };