FunctionNode.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. * @thanks bhouston / https://clara.io/
  4. */
  5. THREE.FunctionNode = function( src, includesOrType, extensionsOrIncludes, keywordsOrExtensions ) {
  6. src = src || '';
  7. this.isMethod = typeof includesOrType !== "string";
  8. this.useKeywords = true;
  9. THREE.TempNode.call( this, this.isMethod ? null : includesOrType );
  10. if ( this.isMethod ) this.eval( src, includesOrType, extensionsOrIncludes, keywordsOrExtensions );
  11. else this.eval( src, extensionsOrIncludes, keywordsOrExtensions );
  12. };
  13. THREE.FunctionNode.rDeclaration = /^([a-z_0-9]+)\s([a-z_0-9]+)\s?\((.*?)\)/i;
  14. THREE.FunctionNode.rProperties = /[a-z_0-9]+/ig;
  15. THREE.FunctionNode.prototype = Object.create( THREE.TempNode.prototype );
  16. THREE.FunctionNode.prototype.constructor = THREE.FunctionNode;
  17. THREE.FunctionNode.prototype.isShared = function( builder, output ) {
  18. return ! this.isMethod;
  19. };
  20. THREE.FunctionNode.prototype.getType = function( builder ) {
  21. return builder.getTypeByFormat( this.type );
  22. };
  23. THREE.FunctionNode.prototype.getInputByName = function( name ) {
  24. var i = this.inputs.length;
  25. while ( i -- ) {
  26. if ( this.inputs[ i ].name === name )
  27. return this.inputs[ i ];
  28. }
  29. };
  30. THREE.FunctionNode.prototype.getIncludeByName = function( name ) {
  31. var i = this.includes.length;
  32. while ( i -- ) {
  33. if ( this.includes[ i ].name === name )
  34. return this.includes[ i ];
  35. }
  36. };
  37. THREE.FunctionNode.prototype.generate = function( builder, output ) {
  38. var match, offset = 0, src = this.value;
  39. for ( var i = 0; i < this.includes.length; i ++ ) {
  40. builder.include( this.includes[ i ], this );
  41. }
  42. for ( var ext in this.extensions ) {
  43. builder.material.extensions[ ext ] = true;
  44. }
  45. while ( match = THREE.FunctionNode.rProperties.exec( this.value ) ) {
  46. var prop = match[ 0 ], isGlobal = this.isMethod ? ! this.getInputByName( prop ) : true;
  47. var reference = prop;
  48. if ( this.keywords[ prop ] || ( this.useKeywords && isGlobal && THREE.NodeLib.containsKeyword( prop ) ) ) {
  49. var node = this.keywords[ prop ];
  50. if ( ! node ) {
  51. var keyword = THREE.NodeLib.getKeywordData( prop );
  52. if ( keyword.cache ) node = builder.keywords[ prop ];
  53. node = node || THREE.NodeLib.getKeyword( prop, builder );
  54. if ( keyword.cache ) builder.keywords[ prop ] = node;
  55. }
  56. reference = node.build( builder );
  57. }
  58. if ( prop != reference ) {
  59. src = src.substring( 0, match.index + offset ) + reference + src.substring( match.index + prop.length + offset );
  60. offset += reference.length - prop.length;
  61. }
  62. if ( this.getIncludeByName( reference ) === undefined && THREE.NodeLib.contains( reference ) ) {
  63. builder.include( THREE.NodeLib.get( reference ) );
  64. }
  65. }
  66. if ( output === 'source' ) {
  67. return src;
  68. } else if ( this.isMethod ) {
  69. builder.include( this, false, src );
  70. return this.name;
  71. } else {
  72. return builder.format( "(" + src + ")", this.getType( builder ), output );
  73. }
  74. };
  75. THREE.FunctionNode.prototype.eval = function( src, includes, extensions, keywords ) {
  76. src = ( src || '' ).trim();
  77. this.includes = includes || [];
  78. this.extensions = extensions || {};
  79. this.keywords = keywords || {};
  80. if ( this.isMethod ) {
  81. var match = src.match( THREE.FunctionNode.rDeclaration );
  82. this.inputs = [];
  83. if ( match && match.length == 4 ) {
  84. this.type = match[ 1 ];
  85. this.name = match[ 2 ];
  86. var inputs = match[ 3 ].match( THREE.FunctionNode.rProperties );
  87. if ( inputs ) {
  88. var i = 0;
  89. while ( i < inputs.length ) {
  90. var qualifier = inputs[ i ++ ];
  91. var type, name;
  92. if ( qualifier == 'in' || qualifier == 'out' || qualifier == 'inout' ) {
  93. type = inputs[ i ++ ];
  94. } else {
  95. type = qualifier;
  96. qualifier = '';
  97. }
  98. name = inputs[ i ++ ];
  99. this.inputs.push( {
  100. name : name,
  101. type : type,
  102. qualifier : qualifier
  103. } );
  104. }
  105. }
  106. } else {
  107. this.type = '';
  108. this.name = '';
  109. }
  110. }
  111. this.value = src;
  112. };