NodeBuilder.js 4.1 KB

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