TempNode.js 2.7 KB

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