NodeBuilder.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.NodeBuilder = function( material ) {
  5. this.material = material;
  6. this.caches = [];
  7. this.slots = [];
  8. this.keywords = {};
  9. this.parsing = false;
  10. this.optimize = true;
  11. this.update();
  12. };
  13. THREE.NodeBuilder.type = {
  14. float : 'fv1',
  15. vec2 : 'v2',
  16. vec3 : 'v3',
  17. vec4 : 'v4',
  18. mat4 : 'v4',
  19. int : 'iv1'
  20. };
  21. THREE.NodeBuilder.constructors = [
  22. 'float',
  23. 'vec2',
  24. 'vec3',
  25. 'vec4'
  26. ];
  27. THREE.NodeBuilder.elements = [
  28. 'x',
  29. 'y',
  30. 'z',
  31. 'w'
  32. ];
  33. THREE.NodeBuilder.prototype = {
  34. constructor: THREE.NodeBuilder,
  35. addCache : function( name, requires ) {
  36. this.caches.push( {
  37. name : name || '',
  38. requires : requires || {}
  39. } );
  40. return this.update();
  41. },
  42. removeCache : function() {
  43. this.caches.pop();
  44. return this.update();
  45. },
  46. addSlot : function( name ) {
  47. this.slots.push( {
  48. name : name || ''
  49. } );
  50. return this.update();
  51. },
  52. removeSlot : function() {
  53. this.slots.pop();
  54. return this.update();
  55. },
  56. isCache : function( name ) {
  57. var i = this.caches.length;
  58. while ( i -- ) {
  59. if ( this.caches[ i ].name == name ) return true;
  60. }
  61. return false;
  62. },
  63. isSlot : function( name ) {
  64. var i = this.slots.length;
  65. while ( i -- ) {
  66. if ( this.slots[ i ].name == name ) return true;
  67. }
  68. return false;
  69. },
  70. update : function() {
  71. var cache = this.caches[ this.caches.length - 1 ];
  72. var slot = this.slots[ this.slots.length - 1 ];
  73. this.slot = slot ? slot.name : '';
  74. this.cache = cache ? cache.name : '';
  75. this.requires = cache ? cache.requires : {};
  76. return this;
  77. },
  78. require : function( name, node ) {
  79. this.requires[ name ] = node;
  80. return this;
  81. },
  82. include : function( node, parent, source ) {
  83. this.material.include( this, node, parent, source );
  84. return this;
  85. },
  86. colorToVector : function( color ) {
  87. return color.replace( 'r', 'x' ).replace( 'g', 'y' ).replace( 'b', 'z' ).replace( 'a', 'w' );
  88. },
  89. getConstructorFromLength : function( len ) {
  90. return THREE.NodeBuilder.constructors[ len - 1 ];
  91. },
  92. getFormatName : function( format ) {
  93. return format.replace( /c/g, 'v3' ).replace( /fv1/g, 'v1' ).replace( /iv1/g, 'i' );
  94. },
  95. isFormatMatrix : function( format ) {
  96. return /^m/.test( format );
  97. },
  98. getFormatLength : function( format ) {
  99. return parseInt( this.getFormatName( format ).substr( 1 ) );
  100. },
  101. getFormatFromLength : function( len ) {
  102. if ( len == 1 ) return 'fv1';
  103. return 'v' + len;
  104. },
  105. format : function( code, from, to ) {
  106. var format = this.getFormatName( to + '=' + from );
  107. switch ( format ) {
  108. case 'v1=v2': return code + '.x';
  109. case 'v1=v3': return code + '.x';
  110. case 'v1=v4': return code + '.x';
  111. case 'v1=i': return 'float(' + code + ')';
  112. case 'v2=v1': return 'vec2(' + code + ')';
  113. case 'v2=v3': return code + '.xy';
  114. case 'v2=v4': return code + '.xy';
  115. case 'v2=i': return 'vec2(float(' + code + '))';
  116. case 'v3=v1': return 'vec3(' + code + ')';
  117. case 'v3=v2': return 'vec3(' + code + ',0.0)';
  118. case 'v3=v4': return code + '.xyz';
  119. case 'v3=i': return 'vec2(float(' + code + '))';
  120. case 'v4=v1': return 'vec4(' + code + ')';
  121. case 'v4=v2': return 'vec4(' + code + ',0.0,1.0)';
  122. case 'v4=v3': return 'vec4(' + code + ',1.0)';
  123. case 'v4=i': return 'vec4(float(' + code + '))';
  124. case 'i=v1': return 'int(' + code + ')';
  125. case 'i=v2': return 'int(' + code + '.x)';
  126. case 'i=v3': return 'int(' + code + '.x)';
  127. case 'i=v4': return 'int(' + code + '.x)';
  128. }
  129. return code;
  130. },
  131. getTypeByFormat : function( format ) {
  132. return THREE.NodeBuilder.type[ format ] || format;
  133. },
  134. getUuid : function( uuid, useCache ) {
  135. useCache = useCache !== undefined ? useCache : true;
  136. if ( useCache && this.cache ) uuid = this.cache + '-' + uuid;
  137. return uuid;
  138. },
  139. getElementByIndex : function( index ) {
  140. return THREE.NodeBuilder.elements[ index ];
  141. },
  142. getIndexByElement : function( elm ) {
  143. return THREE.NodeBuilder.elements.indexOf( elm );
  144. },
  145. isShader : function( shader ) {
  146. return this.shader == shader;
  147. },
  148. setShader : function( shader ) {
  149. this.shader = shader;
  150. return this;
  151. }
  152. };