TempNode.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.isShader( 'verify' ) ) {
  23. if ( data.deps || 0 > 0 ) {
  24. this.verifyDepsNode( builder, data, output );
  25. return '';
  26. }
  27. return THREE.GLNode.prototype.build.call( this, builder, output, uuid );
  28. }
  29. else if ( data.deps == 1 ) {
  30. return THREE.GLNode.prototype.build.call( this, builder, output, uuid );
  31. }
  32. var name = this.getTemp( builder, uuid );
  33. var type = data.output || this.getType( builder );
  34. if ( name ) {
  35. return builder.format( name, type, output );
  36. }
  37. else {
  38. name = THREE.TempNode.prototype.generate.call( this, builder, output, uuid, data.output, ns );
  39. var code = this.generate( builder, type, uuid );
  40. if ( builder.isShader( 'vertex' ) ) material.addVertexNode( name + '=' + code + ';' );
  41. else material.addFragmentNode( name + '=' + code + ';' );
  42. return builder.format( name, type, output );
  43. }
  44. }
  45. else {
  46. return builder.format( this.generate( builder, this.getType( builder ), uuid ), this.getType( builder ), output );
  47. }
  48. };
  49. THREE.TempNode.prototype.isShared = function() {
  50. return this.shared;
  51. };
  52. THREE.TempNode.prototype.isUnique = function() {
  53. return this.unique;
  54. };
  55. THREE.TempNode.prototype.getUuid = function() {
  56. return this.constructor.uuid || this.uuid;
  57. };
  58. THREE.TempNode.prototype.getTemp = function( builder, uuid ) {
  59. uuid = uuid || this.uuid;
  60. var material = builder.material;
  61. if ( builder.isShader( 'vertex' ) && material.vertexTemps[ uuid ] ) return material.vertexTemps[ uuid ].name;
  62. else if ( material.fragmentTemps[ uuid ] ) return material.fragmentTemps[ uuid ].name;
  63. };
  64. THREE.TempNode.prototype.generate = function( builder, output, uuid, type, ns ) {
  65. if ( ! this.isShared() ) console.error( "THREE.TempNode is not shared!" );
  66. uuid = uuid || this.uuid;
  67. if ( builder.isShader( 'vertex' ) ) return builder.material.getVertexTemp( uuid, type || this.getType( builder ), ns ).name;
  68. else return builder.material.getFragmentTemp( uuid, type || this.getType( builder ), ns ).name;
  69. };