TempNode.js 3.1 KB

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