MaskPass.js 1.8 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.create( THREE.Pass.prototype );
  13. THREE.MaskPass.prototype = {
  14. constructor: THREE.MaskPass,
  15. render: function ( renderer, writeBuffer, readBuffer, delta, maskActive ) {
  16. var context = renderer.context;
  17. // don't update color or depth
  18. context.colorMask( false, false, false, false );
  19. context.depthMask( false );
  20. // set up stencil
  21. var writeValue, clearValue;
  22. if ( this.inverse ) {
  23. writeValue = 0;
  24. clearValue = 1;
  25. } else {
  26. writeValue = 1;
  27. clearValue = 0;
  28. }
  29. context.enable( context.STENCIL_TEST );
  30. context.stencilOp( context.REPLACE, context.REPLACE, context.REPLACE );
  31. context.stencilFunc( context.ALWAYS, writeValue, 0xffffffff );
  32. context.clearStencil( clearValue );
  33. // draw into the stencil buffer
  34. renderer.render( this.scene, this.camera, readBuffer, this.clear );
  35. renderer.render( this.scene, this.camera, writeBuffer, this.clear );
  36. // re-enable update of color and depth
  37. context.colorMask( true, true, true, true );
  38. context.depthMask( true );
  39. // only render where stencil is set to 1
  40. context.stencilFunc( context.EQUAL, 1, 0xffffffff ); // draw if == 1
  41. context.stencilOp( context.KEEP, context.KEEP, context.KEEP );
  42. }
  43. };
  44. THREE.ClearMaskPass = function () {
  45. THREE.Pass.call( this );
  46. this.needsSwap = false;
  47. };
  48. THREE.ClearMaskPass.prototype = Object.create( THREE.Pass.prototype );
  49. THREE.ClearMaskPass.prototype = {
  50. constructor: THREE.ClearMaskPass,
  51. render: function ( renderer, writeBuffer, readBuffer, delta, maskActive ) {
  52. var context = renderer.context;
  53. context.disable( context.STENCIL_TEST );
  54. }
  55. };