GLNode.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.GLNode = function ( type ) {
  5. this.uuid = THREE.Math.generateUUID();
  6. this.name = "";
  7. this.type = type;
  8. this.userData = {};
  9. };
  10. THREE.GLNode.prototype.isNode = true;
  11. THREE.GLNode.prototype.parse = function ( builder, context ) {
  12. context = context || {};
  13. builder.parsing = true;
  14. var material = builder.material;
  15. this.build( builder.addCache( context.cache, context.requires ).addSlot( context.slot ), 'v4' );
  16. material.clearVertexNode();
  17. material.clearFragmentNode();
  18. builder.removeCache().removeSlot();
  19. builder.parsing = false;
  20. };
  21. THREE.GLNode.prototype.parseAndBuildCode = function ( builder, output, context ) {
  22. context = context || {};
  23. this.parse( builder, context );
  24. return this.buildCode( builder, output, context );
  25. };
  26. THREE.GLNode.prototype.buildCode = function ( builder, output, context ) {
  27. context = context || {};
  28. var material = builder.material;
  29. var data = { result: this.build( builder.addCache( context.cache, context.requires ).addSlot( context.slot ), output ) };
  30. if ( builder.isShader( 'vertex' ) ) data.code = material.clearVertexNode();
  31. else data.code = material.clearFragmentNode();
  32. builder.removeCache().removeSlot();
  33. return data;
  34. };
  35. THREE.GLNode.prototype.build = function ( builder, output, uuid ) {
  36. output = output || this.getType( builder, output );
  37. var material = builder.material, data = material.getDataNode( uuid || this.uuid );
  38. if ( builder.parsing ) this.appendDepsNode( builder, data, output );
  39. if ( material.nodes.indexOf( this ) === - 1 ) {
  40. material.nodes.push( this );
  41. }
  42. if ( this.updateFrame !== undefined && material.updaters.indexOf( this ) === - 1 ) {
  43. material.updaters.push( this );
  44. }
  45. return this.generate( builder, output, uuid );
  46. };
  47. THREE.GLNode.prototype.appendDepsNode = function ( builder, data, output ) {
  48. data.deps = ( data.deps || 0 ) + 1;
  49. var outputLen = builder.getFormatLength( output );
  50. if ( outputLen > ( data.outputMax || 0 ) || this.getType( builder, output ) ) {
  51. data.outputMax = outputLen;
  52. data.output = output;
  53. }
  54. };
  55. THREE.GLNode.prototype.getType = function ( builder, output ) {
  56. return output === 'sampler2D' || output === 'samplerCube' ? output : this.type;
  57. };
  58. THREE.GLNode.prototype.getJSONNode = function ( meta ) {
  59. var isRootObject = ( meta === undefined || typeof meta === 'string' );
  60. if ( ! isRootObject && meta.nodes[ this.uuid ] !== undefined ) {
  61. return meta.nodes[ this.uuid ];
  62. }
  63. };
  64. THREE.GLNode.prototype.copy = function ( source ) {
  65. if ( source.name !== undefined ) this.name = source.name;
  66. if ( source.userData !== undefined ) this.userData = JSON.parse( JSON.stringify( source.userData ) );
  67. };
  68. THREE.GLNode.prototype.createJSONNode = function ( meta ) {
  69. var isRootObject = ( meta === undefined || typeof meta === 'string' );
  70. var data = {};
  71. if ( typeof this.nodeType !== "string" ) throw new Error( "Node does not allow serialization." );
  72. data.uuid = this.uuid;
  73. data.nodeType = this.nodeType;
  74. if ( this.name !== "" ) data.name = this.name;
  75. if ( JSON.stringify( this.userData ) !== '{}' ) data.userData = this.userData;
  76. if ( ! isRootObject ) {
  77. meta.nodes[ this.uuid ] = data;
  78. }
  79. return data;
  80. };
  81. THREE.GLNode.prototype.toJSON = function ( meta ) {
  82. return this.getJSONNode( meta ) || this.createJSONNode( meta );
  83. };