BuilderNode.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. isCache : function( name ) {
  24. var i = this.caches.length;
  25. while ( i -- ) {
  26. if ( this.caches[ i ].name == name ) return true;
  27. }
  28. return false;
  29. },
  30. updateCache : function() {
  31. var cache = this.caches[ this.caches.length - 1 ];
  32. this.cache = cache.name;
  33. this.requires = cache.requires;
  34. return this;
  35. },
  36. require : function( name, node ) {
  37. this.requires[ name ] = node;
  38. return this;
  39. },
  40. include : function( func ) {
  41. this.material.include( this.shader, func );
  42. return this;
  43. },
  44. colorToVector : function( color ) {
  45. return color.replace( 'r', 'x' ).replace( 'g', 'y' ).replace( 'b', 'z' ).replace( 'a', 'w' );
  46. },
  47. getFormatConstructor : function( len ) {
  48. return THREE.BuilderNode.constructors[ len - 1 ];
  49. },
  50. getFormatName : function( format ) {
  51. return format.replace( 'c', 'v3' ).replace( /fv1|iv1/, 'v1' );
  52. },
  53. getFormatLength : function( format ) {
  54. return parseInt( this.getFormatName( format ).substr( 1 ) );
  55. },
  56. getFormatByLength : function( len ) {
  57. if ( len == 1 ) return 'fv1';
  58. return 'v' + len;
  59. },
  60. format : function( code, from, to ) {
  61. var format = this.getFormatName( from + '=' + to );
  62. switch ( format ) {
  63. case 'v1=v2': return 'vec2(' + code + ')';
  64. case 'v1=v3': return 'vec3(' + code + ')';
  65. case 'v1=v4': return 'vec4(' + code + ')';
  66. case 'v2=v1': return code + '.x';
  67. case 'v2=v3': return 'vec3(' + code + ',0.0)';
  68. case 'v2=v4': return 'vec4(' + code + ',0.0,0.0)';
  69. case 'v3=v1': return code + '.x';
  70. case 'v3=v2': return code + '.xy';
  71. case 'v3=v4': return 'vec4(' + code + ',0.0)';
  72. case 'v4=v1': return code + '.x';
  73. case 'v4=v2': return code + '.xy';
  74. case 'v4=v3': return code + '.xyz';
  75. }
  76. return code;
  77. },
  78. getType : function( format ) {
  79. return THREE.BuilderNode.type[ format ];
  80. },
  81. getUuid : function( uuid, useCache ) {
  82. useCache = useCache !== undefined ? useCache : true;
  83. if ( useCache && this.cache ) uuid = this.cache + '-' + uuid;
  84. return uuid;
  85. },
  86. getElementByIndex : function( index ) {
  87. return THREE.BuilderNode.elements[ index ];
  88. },
  89. getIndexByElement : function( elm ) {
  90. return THREE.BuilderNode.elements.indexOf( elm );
  91. },
  92. isShader : function( shader ) {
  93. return this.shader == shader || this.isVerify;
  94. },
  95. setShader : function( shader ) {
  96. this.shader = shader;
  97. return this;
  98. }
  99. };
  100. THREE.BuilderNode.type = {
  101. 'float' : 'fv1',
  102. vec2 : 'v2',
  103. vec3 : 'v3',
  104. vec4 : 'v4'
  105. };
  106. THREE.BuilderNode.constructors = [
  107. '',
  108. 'vec2',
  109. 'vec3',
  110. 'vec4'
  111. ];
  112. THREE.BuilderNode.elements = [
  113. 'x',
  114. 'y',
  115. 'z',
  116. 'w'
  117. ];