2
0

BuilderNode.js 2.3 KB

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