BuilderNode.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.BuilderNode = function( material ) {
  5. this.material = material;
  6. this.caches = [];
  7. this.isVerify = false;
  8. this.addCache();
  9. };
  10. THREE.BuilderNode.prototype = {
  11. constructor: THREE.BuilderNode,
  12. addCache : function( name, requires ) {
  13. this.caches.push( {
  14. name : name || '',
  15. requires : requires || {}
  16. } );
  17. return this.updateCache();
  18. },
  19. removeCache : function() {
  20. this.caches.pop();
  21. return this.updateCache();
  22. },
  23. updateCache : function() {
  24. var cache = this.caches[ this.caches.length - 1 ];
  25. this.cache = cache.name;
  26. this.requires = cache.requires;
  27. return this;
  28. },
  29. require : function( name, node ) {
  30. this.requires[ name ] = node;
  31. return this;
  32. },
  33. include : function( func ) {
  34. this.material.include( this.shader, func );
  35. return this;
  36. },
  37. getFormatConstructor : function( len ) {
  38. return THREE.BuilderNode.constructors[ len - 1 ];
  39. },
  40. getFormat : function( format ) {
  41. return format.replace( 'c', 'v3' ).replace( /fv1|iv1/, 'v1' );
  42. },
  43. getFormatLength : function( format ) {
  44. return parseInt( this.getFormat( format ).substr( 1 ) );
  45. },
  46. getFormatByLength : function( len ) {
  47. if ( len == 1 ) return 'fv1';
  48. return 'v' + len;
  49. },
  50. format : function( code, from, to ) {
  51. var format = this.getFormat( from + '=' + to );
  52. switch ( format ) {
  53. case 'v1=v2': return 'vec2(' + code + ')';
  54. case 'v1=v3': return 'vec3(' + code + ')';
  55. case 'v1=v4': return 'vec4(' + code + ')';
  56. case 'v2=v1': return code + '.x';
  57. case 'v2=v3': return 'vec3(' + code + ',0.0)';
  58. case 'v2=v4': return 'vec4(' + code + ',0.0,0.0)';
  59. case 'v3=v1': return code + '.x';
  60. case 'v3=v2': return code + '.xy';
  61. case 'v3=v4': return 'vec4(' + code + ',0.0)';
  62. case 'v4=v1': return code + '.x';
  63. case 'v4=v2': return code + '.xy';
  64. case 'v4=v3': return code + '.xyz';
  65. }
  66. return code;
  67. },
  68. getType : function( format ) {
  69. return THREE.BuilderNode.type[ format ];
  70. },
  71. getUuid : function( uuid, useCache ) {
  72. useCache = useCache !== undefined ? useCache : true;
  73. if ( useCache && this.cache ) uuid = this.cache + '-' + uuid;
  74. return uuid;
  75. },
  76. getElementByIndex : function( index ) {
  77. return THREE.BuilderNode.elements[ index ];
  78. },
  79. getElementIndex : function( elm ) {
  80. return THREE.BuilderNode.elements.indexOf( elm );
  81. },
  82. isShader : function( shader ) {
  83. return this.shader == shader || this.isVerify;
  84. },
  85. setShader : function( shader ) {
  86. this.shader = shader;
  87. return this;
  88. }
  89. };
  90. THREE.BuilderNode.type = {
  91. 'float' : 'fv1',
  92. vec2 : 'v2',
  93. vec3 : 'v3',
  94. vec4 : 'v4'
  95. };
  96. THREE.BuilderNode.constructors = [
  97. '',
  98. 'vec2',
  99. 'vec3',
  100. 'vec4'
  101. ];
  102. THREE.BuilderNode.elements = [
  103. 'x',
  104. 'y',
  105. 'z',
  106. 'w'
  107. ];