playcanvas
Version:
PlayCanvas WebGL game engine
210 lines (207 loc) • 6.96 kB
JavaScript
import { FUNC_ALWAYS, STENCILOP_KEEP } from './constants.js';
import { StringIds } from '../../core/string-ids.js';
var stringIds = new StringIds();
/**
* Holds stencil test settings.
*
* @category Graphics
*/ class StencilParameters {
/**
* Sets the comparison function that decides if the pixel should be written, based on the
* current stencil buffer value, reference value, and mask value. Can be:
*
* - {@link FUNC_NEVER}: never pass
* - {@link FUNC_LESS}: pass if (ref & mask) < (stencil & mask)
* - {@link FUNC_EQUAL}: pass if (ref & mask) == (stencil & mask)
* - {@link FUNC_LESSEQUAL}: pass if (ref & mask) <= (stencil & mask)
* - {@link FUNC_GREATER}: pass if (ref & mask) > (stencil & mask)
* - {@link FUNC_NOTEQUAL}: pass if (ref & mask) != (stencil & mask)
* - {@link FUNC_GREATEREQUAL}: pass if (ref & mask) >= (stencil & mask)
* - {@link FUNC_ALWAYS}: always pass
*
* @type {number}
*/ set func(value) {
this._func = value;
this._dirty = true;
}
/**
* Sets the comparison function that decides if the pixel should be written.
*
* @type {number}
*/ get func() {
return this._func;
}
/**
* Sets the stencil test reference value used in comparisons.
*
* @type {number}
*/ set ref(value) {
this._ref = value;
this._dirty = true;
}
/**
* Gets the stencil test reference value used in comparisons.
*
* @type {number}
*/ get ref() {
return this._ref;
}
/**
* Sets the operation to perform if stencil test is failed. Can be:
*
* - {@link STENCILOP_KEEP}: don't change the stencil buffer value
* - {@link STENCILOP_ZERO}: set value to zero
* - {@link STENCILOP_REPLACE}: replace value with the reference value.
* - {@link STENCILOP_INCREMENT}: increment the value
* - {@link STENCILOP_INCREMENTWRAP}: increment the value, but wrap it to zero when it's larger
* than a maximum representable value
* - {@link STENCILOP_DECREMENT}: decrement the value
* - {@link STENCILOP_DECREMENTWRAP}: decrement the value, but wrap it to a maximum
* representable value, if the current value is 0
* - {@link STENCILOP_INVERT}: invert the value bitwise
*
* @type {number}
*/ set fail(value) {
this._fail = value;
this._dirty = true;
}
/**
* Gets the operation to perform if stencil test is failed.
*
* @type {number}
*/ get fail() {
return this._fail;
}
/**
* Sets the operation to perform if depth test is failed. Accepts the same values as `fail`.
*
* @type {number}
*/ set zfail(value) {
this._zfail = value;
this._dirty = true;
}
/**
* Gets the operation to perform if depth test is failed.
*
* @type {number}
*/ get zfail() {
return this._zfail;
}
/**
* Sets the operation to perform if both stencil and depth test are passed. Accepts the same
* values as `fail`.
*
* @type {number}
*/ set zpass(value) {
this._zpass = value;
this._dirty = true;
}
/**
* Gets the operation to perform if both stencil and depth test are passed.
*
* @type {number}
*/ get zpass() {
return this._zpass;
}
/**
* Sets the mask applied to stencil buffer value and reference value before comparison.
*
* @type {number}
*/ set readMask(value) {
this._readMask = value;
this._dirty = true;
}
/**
* Gets the mask applied to stencil buffer value and reference value before comparison.
*
* @type {number}
*/ get readMask() {
return this._readMask;
}
/**
* Sets the bit mask applied to the stencil value when written.
*
* @type {number}
*/ set writeMask(value) {
this._writeMask = value;
this._dirty = true;
}
/**
* Gets the bit mask applied to the stencil value when written.
*
* @type {number}
*/ get writeMask() {
return this._writeMask;
}
_evalKey() {
var { _func, _ref, _fail, _zfail, _zpass, _readMask, _writeMask } = this;
var key = _func + "," + _ref + "," + _fail + "," + _zfail + "," + _zpass + "," + _readMask + "," + _writeMask;
this._key = stringIds.get(key);
this._dirty = false;
}
get key() {
if (this._dirty) {
this._evalKey();
}
return this._key;
}
/**
* Copies the contents of a source stencil parameters to this stencil parameters.
*
* @param {StencilParameters} rhs - A stencil parameters to copy from.
* @returns {StencilParameters} Self for chaining.
*/ copy(rhs) {
this._func = rhs._func;
this._ref = rhs._ref;
this._readMask = rhs._readMask;
this._writeMask = rhs._writeMask;
this._fail = rhs._fail;
this._zfail = rhs._zfail;
this._zpass = rhs._zpass;
this._dirty = rhs._dirty;
this._key = rhs._key;
return this;
}
/**
* Clone the stencil parameters.
*
* @returns {StencilParameters} A cloned StencilParameters object.
*/ clone() {
var clone = new this.constructor();
return clone.copy(this);
}
/**
* Create a new StencilParameters instance.
*
* @param {object} [options] - Options object to configure the stencil parameters.
*/ constructor(options = {}){
/**
* @type {boolean}
* @private
*/ this._dirty = true;
var _options_func;
this._func = (_options_func = options.func) != null ? _options_func : FUNC_ALWAYS;
var _options_ref;
this._ref = (_options_ref = options.ref) != null ? _options_ref : 0;
var _options_readMask;
this._readMask = (_options_readMask = options.readMask) != null ? _options_readMask : 0xFF;
var _options_writeMask;
this._writeMask = (_options_writeMask = options.writeMask) != null ? _options_writeMask : 0xFF;
var _options_fail;
this._fail = (_options_fail = options.fail) != null ? _options_fail : STENCILOP_KEEP; // keep == 0
var _options_zfail;
this._zfail = (_options_zfail = options.zfail) != null ? _options_zfail : STENCILOP_KEEP;
var _options_zpass;
this._zpass = (_options_zpass = options.zpass) != null ? _options_zpass : STENCILOP_KEEP;
// Evaluate key here. This evaluates the key for the DEFAULT instance, which is important,
// as during rendering it gets copied and the key would get evaluated each time.
this._evalKey();
}
}
/**
* A default stencil state.
*
* @type {StencilParameters}
* @readonly
*/ StencilParameters.DEFAULT = Object.freeze(new StencilParameters());
export { StencilParameters };