RTTNode.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.updateFrameSaveToRTT = function ( frame ) {
  33. this.saveToRTT.render = false;
  34. if (this.saveToRTT !== this.saveToRTTCurrent) {
  35. if (this.saveToRTTMaterial) this.saveToRTTMaterial.dispose();
  36. var material = new THREE.NodeMaterial();
  37. material.fragment.value = this;
  38. material.build();
  39. var scene = new THREE.Scene();
  40. var quad = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2, 2 ), material );
  41. quad.frustumCulled = false; // Avoid getting clipped
  42. scene.add( quad );
  43. this.saveToRTTScene = scene;
  44. this.saveToRTTMaterial = material;
  45. }
  46. this.saveToRTTCurrent = this.saveToRTT;
  47. frame.renderer.render( this.saveToRTTScene, this.camera, this.saveToRTT.renderTarget, this.saveToRTT.clear );
  48. };
  49. RTTNode.prototype.updateFrame = function ( frame ) {
  50. if ( frame.renderer ) {
  51. // from the second frame
  52. if (this.saveToRTT && this.saveToRTT.render === false) {
  53. this.updateFrameSaveToRTT( frame );
  54. }
  55. if (this.render) {
  56. if (this.material.uniforms.renderTexture) {
  57. this.material.uniforms.renderTexture.value = frame.renderTexture;
  58. }
  59. frame.renderer.render( this.scene, this.camera, this.renderTarget, this.clear );
  60. }
  61. // first frame
  62. if (this.saveToRTT && this.saveToRTT.render === true) {
  63. this.updateFrameSaveToRTT( frame );
  64. }
  65. } else {
  66. console.warn("RTTNode need a renderer in NodeFrame");
  67. }
  68. };
  69. RTTNode.prototype.copy = function ( source ) {
  70. TextureNode.prototype.copy.call( this, source );
  71. this.saveToRTT = source.saveToRTT;
  72. };
  73. RTTNode.prototype.toJSON = function ( meta ) {
  74. var data = this.getJSONNode( meta );
  75. if ( ! data ) {
  76. data = THREE.TextureNode.prototype.toJSON.call( this, meta );
  77. if (this.saveToRTT) data.saveToRTT = this.saveToRTT.toJSON( meta ).uuid;
  78. }
  79. return data;
  80. };
  81. export { RTTNode };