NodePostProcessing.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { NodeMaterial } from '../materials/NodeMaterial.js';
  5. import { ScreenNode } from '../inputs/ScreenNode.js';
  6. function NodePostProcessing( renderer, renderTarget ) {
  7. if ( renderTarget === undefined ) {
  8. var parameters = {
  9. minFilter: THREE.LinearFilter,
  10. magFilter: THREE.LinearFilter,
  11. format: THREE.RGBAFormat,
  12. stencilBuffer: false
  13. };
  14. var size = renderer.getDrawingBufferSize();
  15. renderTarget = new THREE.WebGLRenderTarget( size.width, size.height, parameters );
  16. }
  17. this.renderer = renderer;
  18. this.renderTarget = renderTarget;
  19. this.output = new ScreenNode();
  20. this.material = new NodeMaterial();
  21. this.camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  22. this.scene = new THREE.Scene();
  23. this.quad = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2, 2 ), this.material );
  24. this.quad.frustumCulled = false; // Avoid getting clipped
  25. this.scene.add( this.quad );
  26. this.needsUpdate = true;
  27. }
  28. NodePostProcessing.prototype = {
  29. constructor: NodePostProcessing,
  30. render: function ( scene, camera, frame ) {
  31. if ( this.needsUpdate ) {
  32. this.material.dispose();
  33. this.material.fragment.value = this.output;
  34. this.material.build();
  35. if ( this.material.uniforms.renderTexture ) {
  36. this.material.uniforms.renderTexture.value = this.renderTarget.texture;
  37. }
  38. this.needsUpdate = false;
  39. }
  40. frame.setRenderer( this.renderer )
  41. .setRenderTexture( this.renderTarget.texture );
  42. this.renderer.setRenderTarget( this.renderTarget );
  43. this.renderer.render( scene, camera );
  44. frame.updateNode( this.material );
  45. this.renderer.setRenderTarget( null );
  46. this.renderer.render( this.scene, this.camera );
  47. },
  48. setSize: function ( width, height ) {
  49. this.renderTarget.setSize( width, height );
  50. this.renderer.setSize( width, height );
  51. },
  52. copy: function ( source ) {
  53. this.output = source.output;
  54. },
  55. toJSON: function ( meta ) {
  56. var isRootObject = ( meta === undefined || typeof meta === 'string' );
  57. if ( isRootObject ) {
  58. meta = {
  59. nodes: {}
  60. };
  61. }
  62. if ( meta && ! meta.post ) meta.post = {};
  63. if ( ! meta.post[ this.uuid ] ) {
  64. var data = {};
  65. data.uuid = this.uuid;
  66. data.type = "NodePostProcessing";
  67. meta.post[ this.uuid ] = data;
  68. if ( this.name !== "" ) data.name = this.name;
  69. if ( JSON.stringify( this.userData ) !== '{}' ) data.userData = this.userData;
  70. data.output = this.output.toJSON( meta ).uuid;
  71. }
  72. meta.post = this.uuid;
  73. return meta;
  74. }
  75. };
  76. export { NodePostProcessing };