MaskPass.js 2.0 KB

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