RTTNode.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { NodeMaterial } from '../materials/NodeMaterial.js';
  5. import { TextureNode } from './TextureNode.js';
  6. function RTTNode( width, height, input, options ) {
  7. options = options || {};
  8. this.input = input;
  9. this.clear = options.clear !== undefined ? options.clear : true;
  10. this.renderTarget = new THREE.WebGLRenderTarget( width, height, options );
  11. this.material = new NodeMaterial();
  12. this.camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  13. this.scene = new THREE.Scene();
  14. this.quad = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2, 2 ), this.material );
  15. this.quad.frustumCulled = false; // Avoid getting clipped
  16. this.scene.add( this.quad );
  17. this.render = true;
  18. TextureNode.call( this, this.renderTarget.texture );
  19. }
  20. RTTNode.prototype = Object.create( TextureNode.prototype );
  21. RTTNode.prototype.constructor = RTTNode;
  22. RTTNode.prototype.nodeType = "RTT";
  23. RTTNode.prototype.build = function ( builder, output, uuid ) {
  24. var rttBuilder = new THREE.NodeBuilder();
  25. rttBuilder.nodes = builder.nodes;
  26. rttBuilder.updaters = builder.updaters;
  27. this.material.fragment.value = this.input;
  28. this.material.build( { builder: rttBuilder } );
  29. return TextureNode.prototype.build.call( this, builder, output, uuid );
  30. };
  31. RTTNode.prototype.updateFramesaveTo = function ( frame ) {
  32. this.saveTo.render = false;
  33. if ( this.saveTo !== this.saveToCurrent ) {
  34. if ( this.saveToMaterial ) this.saveToMaterial.dispose();
  35. var material = new NodeMaterial();
  36. material.fragment.value = this;
  37. material.build();
  38. var scene = new THREE.Scene();
  39. var quad = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2, 2 ), material );
  40. quad.frustumCulled = false; // Avoid getting clipped
  41. scene.add( quad );
  42. this.saveToScene = scene;
  43. this.saveToMaterial = material;
  44. }
  45. this.saveToCurrent = this.saveTo;
  46. frame.renderer.render( this.saveToScene, this.camera, this.saveTo.renderTarget, this.saveTo.clear );
  47. };
  48. RTTNode.prototype.updateFrame = function ( frame ) {
  49. if ( frame.renderer ) {
  50. // from the second frame
  51. if ( this.saveTo && this.saveTo.render === false ) {
  52. this.updateFramesaveTo( frame );
  53. }
  54. if ( this.render ) {
  55. if ( this.material.uniforms.renderTexture ) {
  56. this.material.uniforms.renderTexture.value = frame.renderTexture;
  57. }
  58. frame.renderer.render( this.scene, this.camera, this.renderTarget, this.clear );
  59. }
  60. // first frame
  61. if ( this.saveTo && this.saveTo.render === true ) {
  62. this.updateFramesaveTo( frame );
  63. }
  64. } else {
  65. console.warn( "RTTNode need a renderer in NodeFrame" );
  66. }
  67. };
  68. RTTNode.prototype.copy = function ( source ) {
  69. TextureNode.prototype.copy.call( this, source );
  70. this.saveTo = source.saveTo;
  71. };
  72. RTTNode.prototype.toJSON = function ( meta ) {
  73. var data = this.getJSONNode( meta );
  74. if ( ! data ) {
  75. data = THREE.TextureNode.prototype.toJSON.call( this, meta );
  76. if ( this.saveTo ) data.saveTo = this.saveTo.toJSON( meta ).uuid;
  77. }
  78. return data;
  79. };
  80. export { RTTNode };