MaskPass.js 2.1 KB

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