FunctionNode.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.nodeType = "Function";
  18. THREE.FunctionNode.prototype.isShared = function ( builder, output ) {
  19. return ! this.isMethod;
  20. };
  21. THREE.FunctionNode.prototype.getType = function ( builder ) {
  22. return builder.getTypeByFormat( this.type );
  23. };
  24. THREE.FunctionNode.prototype.getInputByName = function ( name ) {
  25. var i = this.inputs.length;
  26. while ( i -- ) {
  27. if ( this.inputs[ i ].name === name )
  28. return this.inputs[ i ];
  29. }
  30. };
  31. THREE.FunctionNode.prototype.getIncludeByName = function ( name ) {
  32. var i = this.includes.length;
  33. while ( i -- ) {
  34. if ( this.includes[ i ].name === name )
  35. return this.includes[ i ];
  36. }
  37. };
  38. THREE.FunctionNode.prototype.generate = function ( builder, output ) {
  39. var match, offset = 0, src = this.value;
  40. for ( var i = 0; i < this.includes.length; i ++ ) {
  41. builder.include( this.includes[ i ], this );
  42. }
  43. for ( var ext in this.extensions ) {
  44. builder.material.extensions[ ext ] = true;
  45. }
  46. while ( match = THREE.FunctionNode.rProperties.exec( this.value ) ) {
  47. var prop = match[ 0 ], isGlobal = this.isMethod ? ! this.getInputByName( prop ) : true;
  48. var reference = prop;
  49. if ( this.keywords[ prop ] || ( this.useKeywords && isGlobal && THREE.NodeLib.containsKeyword( prop ) ) ) {
  50. var node = this.keywords[ prop ];
  51. if ( ! node ) {
  52. var keyword = THREE.NodeLib.getKeywordData( prop );
  53. if ( keyword.cache ) node = builder.keywords[ prop ];
  54. node = node || THREE.NodeLib.getKeyword( prop, builder );
  55. if ( keyword.cache ) builder.keywords[ prop ] = node;
  56. }
  57. reference = node.build( builder );
  58. }
  59. if ( prop != reference ) {
  60. src = src.substring( 0, match.index + offset ) + reference + src.substring( match.index + prop.length + offset );
  61. offset += reference.length - prop.length;
  62. }
  63. if ( this.getIncludeByName( reference ) === undefined && THREE.NodeLib.contains( reference ) ) {
  64. builder.include( THREE.NodeLib.get( reference ) );
  65. }
  66. }
  67. if ( output === 'source' ) {
  68. return src;
  69. } else if ( this.isMethod ) {
  70. builder.include( this, false, src );
  71. return this.name;
  72. } else {
  73. return builder.format( "(" + src + ")", this.getType( builder ), output );
  74. }
  75. };
  76. THREE.FunctionNode.prototype.eval = function ( src, includes, extensions, keywords ) {
  77. src = ( src || '' ).trim();
  78. this.includes = includes || [];
  79. this.extensions = extensions || {};
  80. this.keywords = keywords || {};
  81. if ( this.isMethod ) {
  82. var match = src.match( THREE.FunctionNode.rDeclaration );
  83. this.inputs = [];
  84. if ( match && match.length == 4 ) {
  85. this.type = match[ 1 ];
  86. this.name = match[ 2 ];
  87. var inputs = match[ 3 ].match( THREE.FunctionNode.rProperties );
  88. if ( inputs ) {
  89. var i = 0;
  90. while ( i < inputs.length ) {
  91. var qualifier = inputs[ i ++ ];
  92. var type, name;
  93. if ( qualifier == 'in' || qualifier == 'out' || qualifier == 'inout' ) {
  94. type = inputs[ i ++ ];
  95. } else {
  96. type = qualifier;
  97. qualifier = '';
  98. }
  99. name = inputs[ i ++ ];
  100. this.inputs.push( {
  101. name: name,
  102. type: type,
  103. qualifier: qualifier
  104. } );
  105. }
  106. }
  107. } else {
  108. this.type = '';
  109. this.name = '';
  110. }
  111. }
  112. this.value = src;
  113. };
  114. THREE.FunctionNode.prototype.toJSON = function ( meta ) {
  115. var data = this.getJSONNode( meta );
  116. if ( ! data ) {
  117. data = this.createJSONNode( meta );
  118. data.src = this.value;
  119. data.isMethod = this.isMethod;
  120. data.useKeywords = this.useKeywords;
  121. if ( ! this.isMethod ) data.out = this.type;
  122. data.extensions = JSON.parse( JSON.stringify( this.extensions ) );
  123. data.keywords = {};
  124. for ( var keyword in this.keywords ) {
  125. data.keywords[ keyword ] = this.keywords[ keyword ].toJSON( meta ).uuid;
  126. }
  127. if ( this.includes.length ) {
  128. data.includes = [];
  129. for ( var i = 0; i < this.includes.length; i ++ ) {
  130. data.includes.push( this.includes[ i ].toJSON( meta ).uuid );
  131. }
  132. }
  133. }
  134. return data;
  135. };