TempNode.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. var material = builder.material;
  15. if ( this.isShared() ) {
  16. var isUnique = this.isUnique();
  17. if ( isUnique && this.constructor.uuid === undefined ) {
  18. this.constructor.uuid = THREE.Math.generateUUID();
  19. }
  20. uuid = builder.getUuid( uuid || this.getUuid(), ! isUnique );
  21. var data = material.getDataNode( uuid );
  22. if ( builder.parsing ) {
  23. if ( data.deps || 0 > 0 ) {
  24. this.appendDepsNode( builder, data, output );
  25. return this.generate( builder, type, uuid );
  26. }
  27. return THREE.GLNode.prototype.build.call( this, builder, output, uuid );
  28. }
  29. else if ( ! builder.optimize || data.deps == 1 ) {
  30. return THREE.GLNode.prototype.build.call( this, builder, output, uuid );
  31. }
  32. uuid = this.getUuid( false );
  33. var name = this.getTemp( builder, uuid );
  34. var type = data.output || this.getType( builder );
  35. if ( name ) {
  36. return builder.format( name, type, output );
  37. }
  38. else {
  39. name = THREE.TempNode.prototype.generate.call( this, builder, output, uuid, data.output, ns );
  40. var code = this.generate( builder, type, uuid );
  41. if ( builder.isShader( 'vertex' ) ) material.addVertexNode( name + '=' + code + ';' );
  42. else material.addFragmentNode( name + '=' + code + ';' );
  43. return builder.format( name, type, output );
  44. }
  45. }
  46. else {
  47. return builder.format( this.generate( builder, this.getType( builder ), uuid ), this.getType( builder ), output );
  48. }
  49. };
  50. THREE.TempNode.prototype.isShared = function() {
  51. return this.shared;
  52. };
  53. THREE.TempNode.prototype.isUnique = function() {
  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() ) 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. };