TempNode.js 3.2 KB

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