GLNode.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.allows = {};
  8. this.type = type;
  9. this.userData = {};
  10. };
  11. THREE.GLNode.prototype.isNode = true;
  12. THREE.GLNode.prototype.parse = function ( builder, context ) {
  13. context = context || {};
  14. builder.parsing = true;
  15. var material = builder.material;
  16. this.build( builder.addCache( context.cache, context.requires ).addSlot( context.slot ), 'v4' );
  17. material.clearVertexNode();
  18. material.clearFragmentNode();
  19. builder.removeCache().removeSlot();
  20. builder.parsing = false;
  21. };
  22. THREE.GLNode.prototype.parseAndBuildCode = function ( builder, output, context ) {
  23. context = context || {};
  24. this.parse( builder, context );
  25. return this.buildCode( builder, output, context );
  26. };
  27. THREE.GLNode.prototype.buildCode = function ( builder, output, context ) {
  28. context = context || {};
  29. var material = builder.material;
  30. var data = { result: this.build( builder.addCache( context.cache, context.requires ).addSlot( context.slot ), output ) };
  31. if ( builder.isShader( 'vertex' ) ) data.code = material.clearVertexNode();
  32. else data.code = material.clearFragmentNode();
  33. builder.removeCache().removeSlot();
  34. return data;
  35. };
  36. THREE.GLNode.prototype.build = function ( builder, output, uuid ) {
  37. output = output || this.getType( builder, output );
  38. var material = builder.material, data = material.getDataNode( uuid || this.uuid );
  39. if ( builder.parsing ) this.appendDepsNode( builder, data, output );
  40. if ( this.allows[ builder.shader ] === false ) {
  41. throw new Error( 'Shader ' + shader + ' is not compatible with this node.' );
  42. }
  43. if ( material.nodes.indexOf( this ) === - 1 ) {
  44. material.nodes.push( this );
  45. }
  46. if ( this.updateFrame !== undefined && material.updaters.indexOf( this ) === - 1 ) {
  47. material.updaters.push( this );
  48. }
  49. return this.generate( builder, output, uuid );
  50. };
  51. THREE.GLNode.prototype.appendDepsNode = function ( builder, data, output ) {
  52. data.deps = ( data.deps || 0 ) + 1;
  53. var outputLen = builder.getFormatLength( output );
  54. if ( outputLen > ( data.outputMax || 0 ) || this.getType( builder, output ) ) {
  55. data.outputMax = outputLen;
  56. data.output = output;
  57. }
  58. };
  59. THREE.GLNode.prototype.getType = function ( builder, output ) {
  60. return output === 'sampler2D' || output === 'samplerCube' ? output : this.type;
  61. };
  62. THREE.GLNode.prototype.getJSONNode = function ( meta ) {
  63. var isRootObject = ( meta === undefined || typeof meta === 'string' );
  64. if ( ! isRootObject && meta.nodes[ this.uuid ] !== undefined ) {
  65. return meta.nodes[ this.uuid ];
  66. }
  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.type = this.nodeType + "Node";
  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. };