MaskPass.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. import { Pass } from "../postprocessing/Pass.js";
  5. var MaskPass = function ( scene, camera ) {
  6. Pass.call( this );
  7. this.scene = scene;
  8. this.camera = camera;
  9. this.clear = true;
  10. this.needsSwap = false;
  11. this.inverse = false;
  12. };
  13. MaskPass.prototype = Object.assign( Object.create( Pass.prototype ), {
  14. constructor: MaskPass,
  15. render: function ( renderer, writeBuffer, readBuffer /*, deltaTime, maskActive */ ) {
  16. var context = renderer.getContext();
  17. var state = renderer.state;
  18. // don't update color or depth
  19. state.buffers.color.setMask( false );
  20. state.buffers.depth.setMask( false );
  21. // lock buffers
  22. state.buffers.color.setLocked( true );
  23. state.buffers.depth.setLocked( true );
  24. // set up stencil
  25. var writeValue, clearValue;
  26. if ( this.inverse ) {
  27. writeValue = 0;
  28. clearValue = 1;
  29. } else {
  30. writeValue = 1;
  31. clearValue = 0;
  32. }
  33. state.buffers.stencil.setTest( true );
  34. state.buffers.stencil.setOp( context.REPLACE, context.REPLACE, context.REPLACE );
  35. state.buffers.stencil.setFunc( context.ALWAYS, writeValue, 0xffffffff );
  36. state.buffers.stencil.setClear( clearValue );
  37. state.buffers.stencil.setLocked( true );
  38. // draw into the stencil buffer
  39. renderer.setRenderTarget( readBuffer );
  40. if ( this.clear ) renderer.clear();
  41. renderer.render( this.scene, this.camera );
  42. renderer.setRenderTarget( writeBuffer );
  43. if ( this.clear ) renderer.clear();
  44. renderer.render( this.scene, this.camera );
  45. // unlock color and depth buffer for subsequent rendering
  46. state.buffers.color.setLocked( false );
  47. state.buffers.depth.setLocked( false );
  48. // only render where stencil is set to 1
  49. state.buffers.stencil.setLocked( false );
  50. state.buffers.stencil.setFunc( context.EQUAL, 1, 0xffffffff ); // draw if == 1
  51. state.buffers.stencil.setOp( context.KEEP, context.KEEP, context.KEEP );
  52. state.buffers.stencil.setLocked( true );
  53. }
  54. } );
  55. var ClearMaskPass = function () {
  56. Pass.call( this );
  57. this.needsSwap = false;
  58. };
  59. ClearMaskPass.prototype = Object.create( Pass.prototype );
  60. Object.assign( ClearMaskPass.prototype, {
  61. render: function ( renderer /*, writeBuffer, readBuffer, deltaTime, maskActive */ ) {
  62. renderer.state.buffers.stencil.setLocked( false );
  63. renderer.state.buffers.stencil.setTest( false );
  64. }
  65. } );
  66. export { MaskPass, ClearMaskPass };