NodeFunction.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. * @thanks bhouston / https://clara.io/
  4. */
  5. THREE.NodeFunction = function( src, includes, extensions ) {
  6. THREE.NodeGL.call( this );
  7. this.parse( src || '', includes, extensions );
  8. };
  9. THREE.NodeFunction.prototype = Object.create( THREE.NodeGL.prototype );
  10. THREE.NodeFunction.prototype.constructor = THREE.NodeFunction;
  11. THREE.NodeFunction.prototype.parseReference = function( name ) {
  12. switch(name) {
  13. case 'uv': return new THREE.NodeUV().name;
  14. case 'uv2': return new THREE.NodeUV(1).name;
  15. case 'position': return new THREE.NodePosition().name;
  16. case 'worldPosition': return new THREE.NodePosition( THREE.NodePosition.WORLD ).name;
  17. case 'normal': return new THREE.NodeNormal().name;
  18. case 'normalPosition': return new THREE.NodeNormal( THREE.NodeNormal.WORLD ).name;
  19. case 'viewPosition': return new THREE.NodePosition( THREE.NodeNormal.VIEW ).name;
  20. case 'viewNormal': return new THREE.NodeNormal( THREE.NodeNormal.VIEW ).name;
  21. }
  22. return name;
  23. };
  24. THREE.NodeFunction.prototype.getNodeType = function( builder, type ) {
  25. return builder.getType( type ) || type;
  26. };
  27. THREE.NodeFunction.prototype.getInputByName = function( name ) {
  28. var i = this.input.length;
  29. while(i--) {
  30. if (this.input[i].name === name)
  31. return this.input[i];
  32. }
  33. };
  34. THREE.NodeFunction.prototype.getType = function( builder ) {
  35. return this.getNodeType( builder, this.type );
  36. };
  37. THREE.NodeFunction.prototype.getInclude = function( name ) {
  38. var i = this.includes.length;
  39. while(i--) {
  40. if (this.includes[i].name === name)
  41. return this.includes[i];
  42. }
  43. return undefined;
  44. };
  45. THREE.NodeFunction.prototype.parse = function( src, includes, extensions ) {
  46. var rDeclaration = /^([a-z_0-9]+)\s([a-z_0-9]+)\s?\((.*?)\)/i;
  47. var rProperties = /[a-z_0-9]+/ig;
  48. this.includes = includes || [];
  49. this.extensions = extensions || {};
  50. var match = src.match( rDeclaration );
  51. this.input = [];
  52. if (match && match.length == 4) {
  53. this.type = match[1];
  54. this.name = match[2];
  55. var inputs = match[3].match( rProperties );
  56. if (inputs) {
  57. var i = 0;
  58. while(i < inputs.length) {
  59. var qualifier = inputs[i++];
  60. var type, name;
  61. if (qualifier == 'in' || qualifier == 'out' || qualifier == 'inout') {
  62. type = inputs[i++];
  63. }
  64. else {
  65. type = qualifier;
  66. qualifier = '';
  67. }
  68. name = inputs[i++];
  69. this.input.push({
  70. name : name,
  71. type : type,
  72. qualifier : qualifier
  73. });
  74. }
  75. }
  76. var match, offset = 0;
  77. while (match = rProperties.exec(src)) {
  78. var prop = match[0];
  79. var reference = this.parseReference( prop );
  80. if (prop != reference) {
  81. src = src.substring( 0, match.index + offset ) + reference + src.substring( match.index + prop.length + offset );
  82. offset += reference.length - prop.length;
  83. }
  84. if (this.getInclude(reference) === undefined && THREE.NodeLib.contains(reference)) {
  85. this.includes.push( THREE.NodeLib.get(reference) );
  86. }
  87. }
  88. this.src = src;
  89. }
  90. else {
  91. this.type = '';
  92. this.name = '';
  93. }
  94. };