TempNode.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * Automatic node cache
  3. * @author sunag / http://www.sunag.com.br/
  4. */
  5. THREE.TempNode = function( type, params ) {
  6. THREE.GLNode.call( this, type );
  7. params = params || {};
  8. this.shared = params.shared !== undefined ? params.shared : true;
  9. this.unique = params.unique !== undefined ? params.unique : false;
  10. };
  11. THREE.TempNode.prototype = Object.create( THREE.GLNode.prototype );
  12. THREE.TempNode.prototype.constructor = THREE.TempNode;
  13. THREE.TempNode.prototype.build = function( builder, output, uuid, ns ) {
  14. output = output || this.getType( builder );
  15. var material = builder.material;
  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 = material.getDataNode( uuid );
  23. if ( builder.parsing ) {
  24. if ( data.deps || 0 > 0 ) {
  25. this.appendDepsNode( builder, data, output );
  26. return this.generate( builder, type, uuid );
  27. }
  28. return THREE.GLNode.prototype.build.call( this, builder, output, uuid );
  29. } else if ( isUnique ) {
  30. data.name = data.name || THREE.GLNode.prototype.build.call( this, builder, output, uuid );
  31. return data.name;
  32. } else if ( ! builder.optimize || data.deps == 1 ) {
  33. return THREE.GLNode.prototype.build.call( this, builder, output, uuid );
  34. }
  35. uuid = this.getUuid( false );
  36. var name = this.getTemp( builder, uuid );
  37. var type = data.output || this.getType( builder );
  38. if ( name ) {
  39. return builder.format( name, type, output );
  40. } else {
  41. name = THREE.TempNode.prototype.generate.call( this, builder, output, uuid, data.output, ns );
  42. var code = this.generate( builder, type, uuid );
  43. if ( builder.isShader( 'vertex' ) ) material.addVertexNode( name + '=' + code + ';' );
  44. else material.addFragmentNode( name + '=' + code + ';' );
  45. return builder.format( name, type, output );
  46. }
  47. }
  48. return THREE.GLNode.prototype.build.call( this, builder, output, uuid );
  49. };
  50. THREE.TempNode.prototype.isShared = function( builder, output ) {
  51. return output !== 'sampler2D' && output !== 'samplerCube' && this.shared;
  52. };
  53. THREE.TempNode.prototype.isUnique = function( builder, output ) {
  54. return this.unique;
  55. };
  56. THREE.TempNode.prototype.getUuid = function( unique ) {
  57. var uuid = unique || unique == undefined ? this.constructor.uuid || this.uuid : this.uuid;
  58. if ( typeof this.scope == "string" ) uuid = this.scope + '-' + uuid;
  59. return uuid;
  60. };
  61. THREE.TempNode.prototype.getTemp = function( builder, uuid ) {
  62. uuid = uuid || this.uuid;
  63. var material = builder.material;
  64. if ( builder.isShader( 'vertex' ) && material.vertexTemps[ uuid ] ) return material.vertexTemps[ uuid ].name;
  65. else if ( material.fragmentTemps[ uuid ] ) return material.fragmentTemps[ uuid ].name;
  66. };
  67. THREE.TempNode.prototype.generate = function( builder, output, uuid, type, ns ) {
  68. if ( ! this.isShared( builder, output ) ) console.error( "THREE.TempNode is not shared!" );
  69. uuid = uuid || this.uuid;
  70. if ( builder.isShader( 'vertex' ) ) return builder.material.getVertexTemp( uuid, type || this.getType( builder ), ns ).name;
  71. else return builder.material.getFragmentTemp( uuid, type || this.getType( builder ), ns ).name;
  72. };