RTTNode.js 2.8 KB

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