2
0

FunctionNode.js 5.0 KB

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