TempNode.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * Automatic node cache
  3. * @author sunag / http://www.sunag.com.br/
  4. */
  5. import { Node } from './Node.js';
  6. function TempNode( type, params ) {
  7. Node.call( this, type );
  8. params = params || {};
  9. this.shared = params.shared !== undefined ? params.shared : true;
  10. this.unique = params.unique !== undefined ? params.unique : false;
  11. };
  12. TempNode.prototype = Object.create( Node.prototype );
  13. TempNode.prototype.constructor = TempNode;
  14. TempNode.prototype.build = function ( builder, output, uuid, ns ) {
  15. output = output || this.getType( builder );
  16. if ( this.isShared( builder, output ) ) {
  17. var isUnique = this.isUnique( builder, output );
  18. if ( isUnique && this.constructor.uuid === undefined ) {
  19. this.constructor.uuid = THREE.Math.generateUUID();
  20. }
  21. uuid = builder.getUuid( uuid || this.getUuid(), ! isUnique );
  22. var data = builder.getNodeData( uuid ),
  23. type = data.output || this.getType( builder );
  24. if ( builder.parsing ) {
  25. if ( ( data.deps || 0 ) > 0 ) {
  26. this.appendDepsNode( builder, data, output );
  27. return this.generate( builder, output, uuid );
  28. }
  29. return Node.prototype.build.call( this, builder, output, uuid );
  30. } else if ( isUnique ) {
  31. data.name = data.name || Node.prototype.build.call( this, builder, output, uuid );
  32. return data.name;
  33. } else if ( !this.isShared( builder, type ) || ( ! builder.optimize || data.deps == 1 ) ) {
  34. return Node.prototype.build.call( this, builder, output, uuid );
  35. }
  36. uuid = this.getUuid( false );
  37. var name = this.getTemp( builder, uuid );
  38. if ( name ) {
  39. return builder.format( name, type, output );
  40. } else {
  41. name = TempNode.prototype.generate.call( this, builder, output, uuid, data.output, ns );
  42. var code = this.generate( builder, type, uuid );
  43. builder.addNodeCode( name + ' = ' + code + ';' );
  44. return builder.format( name, type, output );
  45. }
  46. }
  47. return Node.prototype.build.call( this, builder, output, uuid );
  48. };
  49. TempNode.prototype.isShared = function ( builder, output ) {
  50. return output !== 'sampler2D' && output !== 'samplerCube' && this.shared;
  51. };
  52. TempNode.prototype.isUnique = function ( builder, output ) {
  53. return this.unique;
  54. };
  55. TempNode.prototype.getUuid = function ( unique ) {
  56. var uuid = unique || unique == undefined ? this.constructor.uuid || this.uuid : this.uuid;
  57. if ( typeof this.scope == "string" ) uuid = this.scope + '-' + uuid;
  58. return uuid;
  59. };
  60. TempNode.prototype.getTemp = function ( builder, uuid ) {
  61. uuid = uuid || this.uuid;
  62. var tempVar = builder.getVars()[uuid]
  63. return tempVar ? tempVar.name : undefined;
  64. };
  65. TempNode.prototype.generate = function ( builder, output, uuid, type, ns ) {
  66. if ( ! this.isShared( builder, output ) ) console.error( "THREE.TempNode is not shared!" );
  67. uuid = uuid || this.uuid;
  68. return builder.getTempVar( uuid, type || this.getType( builder ), ns ).name;
  69. };
  70. export { TempNode };