RenderPass.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import {
  2. Color
  3. } from 'three';
  4. import { Pass } from './Pass.js';
  5. class RenderPass extends Pass {
  6. constructor( scene, camera, overrideMaterial = null, clearColor = null, clearAlpha = null ) {
  7. super();
  8. this.scene = scene;
  9. this.camera = camera;
  10. this.overrideMaterial = overrideMaterial;
  11. this.clearColor = clearColor;
  12. this.clearAlpha = clearAlpha;
  13. this.clear = true;
  14. this.clearDepth = false;
  15. this.needsSwap = false;
  16. this._oldClearColor = new Color();
  17. }
  18. render( renderer, writeBuffer, readBuffer /*, deltaTime, maskActive */ ) {
  19. const oldAutoClear = renderer.autoClear;
  20. renderer.autoClear = false;
  21. let oldClearAlpha, oldOverrideMaterial;
  22. if ( this.overrideMaterial !== null ) {
  23. oldOverrideMaterial = this.scene.overrideMaterial;
  24. this.scene.overrideMaterial = this.overrideMaterial;
  25. }
  26. if ( this.clearColor !== null ) {
  27. renderer.getClearColor( this._oldClearColor );
  28. renderer.setClearColor( this.clearColor, renderer.getClearAlpha() );
  29. }
  30. if ( this.clearAlpha !== null ) {
  31. oldClearAlpha = renderer.getClearAlpha();
  32. renderer.setClearAlpha( this.clearAlpha );
  33. }
  34. if ( this.clearDepth == true ) {
  35. renderer.clearDepth();
  36. }
  37. renderer.setRenderTarget( this.renderToScreen ? null : readBuffer );
  38. if ( this.clear === true ) {
  39. // TODO: Avoid using autoClear properties, see https://github.com/mrdoob/three.js/pull/15571#issuecomment-465669600
  40. renderer.clear( renderer.autoClearColor, renderer.autoClearDepth, renderer.autoClearStencil );
  41. }
  42. renderer.render( this.scene, this.camera );
  43. // restore
  44. if ( this.clearColor !== null ) {
  45. renderer.setClearColor( this._oldClearColor );
  46. }
  47. if ( this.clearAlpha !== null ) {
  48. renderer.setClearAlpha( oldClearAlpha );
  49. }
  50. if ( this.overrideMaterial !== null ) {
  51. this.scene.overrideMaterial = oldOverrideMaterial;
  52. }
  53. renderer.autoClear = oldAutoClear;
  54. }
  55. }
  56. export { RenderPass };