BuilderNode.js 3.1 KB

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