MaskPass.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.MaskPass = function ( scene, camera ) {
  5. this.scene = scene;
  6. this.camera = camera;
  7. this.enabled = true;
  8. this.clear = true;
  9. this.needsSwap = false;
  10. };
  11. THREE.MaskPass.prototype = {
  12. render: function ( renderer, writeBuffer, readBuffer, delta ) {
  13. var context = renderer.context;
  14. // don't update color or depth
  15. context.colorMask( false, false, false, false );
  16. context.depthMask( false );
  17. // set up stencil
  18. context.enable( context.STENCIL_TEST );
  19. context.stencilOp( context.REPLACE, context.REPLACE, context.REPLACE );
  20. context.stencilFunc( context.ALWAYS, 1, 0xffffffff );
  21. // draw into the stencil buffer
  22. renderer.render( this.scene, this.camera, readBuffer, this.clear );
  23. renderer.render( this.scene, this.camera, writeBuffer, this.clear );
  24. // re-enable update of color and depth
  25. context.colorMask( true, true, true, true );
  26. context.depthMask( true );
  27. // only render where stencil is set to 1
  28. context.stencilFunc( context.EQUAL, 1, 0xffffffff ); // draw if == 1
  29. context.stencilOp( context.KEEP, context.KEEP, context.KEEP );
  30. }
  31. };
  32. THREE.ClearMaskPass = function () {
  33. this.enabled = true;
  34. };
  35. THREE.ClearMaskPass.prototype = {
  36. render: function ( renderer, writeBuffer, readBuffer, delta ) {
  37. var context = renderer.context;
  38. context.disable( context.STENCIL_TEST );
  39. }
  40. };