FunctionCallNode.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.FunctionCallNode = function ( func, inputs ) {
  5. THREE.TempNode.call( this );
  6. this.setFunction( func, inputs );
  7. };
  8. THREE.FunctionCallNode.prototype = Object.create( THREE.TempNode.prototype );
  9. THREE.FunctionCallNode.prototype.constructor = THREE.FunctionCallNode;
  10. THREE.FunctionCallNode.prototype.nodeType = "FunctionCall";
  11. THREE.FunctionCallNode.prototype.setFunction = function ( func, inputs ) {
  12. this.value = func;
  13. this.inputs = inputs || [];
  14. };
  15. THREE.FunctionCallNode.prototype.getFunction = function () {
  16. return this.value;
  17. };
  18. THREE.FunctionCallNode.prototype.getType = function ( builder ) {
  19. return this.value.getType( builder );
  20. };
  21. THREE.FunctionCallNode.prototype.generate = function ( builder, output ) {
  22. var material = builder.material;
  23. var type = this.getType( builder );
  24. var func = this.value;
  25. var code = func.build( builder, output ) + '( ';
  26. var params = [];
  27. for ( var i = 0; i < func.inputs.length; i ++ ) {
  28. var inpt = func.inputs[ i ];
  29. var param = this.inputs[ i ] || this.inputs[ inpt.name ];
  30. params.push( param.build( builder, builder.getTypeByFormat( inpt.type ) ) );
  31. }
  32. code += params.join( ', ' ) + ' )';
  33. return builder.format( code, type, output );
  34. };
  35. THREE.FunctionCallNode.prototype.copy = function ( source ) {
  36. THREE.GLNode.prototype.copy.call( this, source );
  37. for ( var prop in source.inputs ) {
  38. this.inputs[ prop ] = source.inputs[ prop ];
  39. }
  40. this.value = source.value;
  41. };
  42. THREE.FunctionCallNode.prototype.toJSON = function ( meta ) {
  43. var data = this.getJSONNode( meta );
  44. if ( ! data ) {
  45. var func = this.value;
  46. data = this.createJSONNode( meta );
  47. data.value = this.value.toJSON( meta ).uuid;
  48. if ( func.inputs.length ) {
  49. data.inputs = {};
  50. for ( var i = 0; i < func.inputs.length; i ++ ) {
  51. var inpt = func.inputs[ i ];
  52. var node = this.inputs[ i ] || this.inputs[ inpt.name ];
  53. data.inputs[ inpt.name ] = node.toJSON( meta ).uuid;
  54. }
  55. }
  56. }
  57. return data;
  58. };