MaskPass.js 1.6 KB

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