import BoundingRectangle from '../Core/BoundingRectangle.js';
import Check from '../Core/Check.js';
import Color from '../Core/Color.js';
import combine from '../Core/combine.js';
import createGuid from '../Core/createGuid.js';
import defaultValue from '../Core/defaultValue.js';
import defined from '../Core/defined.js';
import defineProperties from '../Core/defineProperties.js';
import destroyObject from '../Core/destroyObject.js';
import DeveloperError from '../Core/DeveloperError.js';
import PixelFormat from '../Core/PixelFormat.js';
import Resource from '../Core/Resource.js';
import PassState from '../Renderer/PassState.js';
import PixelDatatype from '../Renderer/PixelDatatype.js';
import RenderState from '../Renderer/RenderState.js';
import Sampler from '../Renderer/Sampler.js';
import ShaderSource from '../Renderer/ShaderSource.js';
import Texture from '../Renderer/Texture.js';
import TextureMagnificationFilter from '../Renderer/TextureMagnificationFilter.js';
import TextureMinificationFilter from '../Renderer/TextureMinificationFilter.js';
import TextureWrap from '../Renderer/TextureWrap.js';
import when from '../ThirdParty/when.js';
import PostProcessStageSampleMode from './PostProcessStageSampleMode.js';
/**
* Runs a post-process stage on either the texture rendered by the scene or the output of a previous post-process stage.
*
* @alias PostProcessStage
* @constructor
*
* @param {Object} options An object with the following properties:
* @param {String} options.fragmentShader The fragment shader to use. The default sampler2D
uniforms are colorTexture
and depthTexture
. The color texture is the output of rendering the scene or the previous stage. The depth texture is the output from rendering the scene. The shader should contain one or both uniforms. There is also a vec2
varying named v_textureCoordinates
that can be used to sample the textures.
* @param {Object} [options.uniforms] An object whose properties will be used to set the shaders uniforms. The properties can be constant values or a function. A constant value can also be a URI, data URI, or HTML element to use as a texture.
* @param {Number} [options.textureScale=1.0] A number in the range (0.0, 1.0] used to scale the texture dimensions. A scale of 1.0 will render this post-process stage to a texture the size of the viewport.
* @param {Boolean} [options.forcePowerOfTwo=false] Whether or not to force the texture dimensions to be both equal powers of two. The power of two will be the next power of two of the minimum of the dimensions.
* @param {PostProcessStageSampleMode} [options.sampleMode=PostProcessStageSampleMode.NEAREST] How to sample the input color texture.
* @param {PixelFormat} [options.pixelFormat=PixelFormat.RGBA] The color pixel format of the output texture.
* @param {PixelDatatype} [options.pixelDatatype=PixelDatatype.UNSIGNED_BYTE] The pixel data type of the output texture.
* @param {Color} [options.clearColor=Color.BLACK] The color to clear the output texture to.
* @param {BoundingRectangle} [options.scissorRectangle] The rectangle to use for the scissor test.
* @param {String} [options.name=createGuid()] The unique name of this post-process stage for reference by other stages in a composite. If a name is not supplied, a GUID will be generated.
*
* @exception {DeveloperError} options.textureScale must be greater than 0.0 and less than or equal to 1.0.
* @exception {DeveloperError} options.pixelFormat must be a color format.
* @exception {DeveloperError} When options.pixelDatatype is FLOAT, this WebGL implementation must support the OES_texture_float extension. Check context.floatingPointTexture.
*
* @see PostProcessStageComposite
*
* @example
* // Simple stage to change the color
* var fs =
* 'uniform sampler2D colorTexture;\n' +
* 'varying vec2 v_textureCoordinates;\n' +
* 'uniform float scale;\n' +
* 'uniform vec3 offset;\n' +
* 'void main() {\n' +
* ' vec4 color = texture2D(colorTexture, v_textureCoordinates);\n' +
* ' gl_FragColor = vec4(color.rgb * scale + offset, 1.0);\n' +
* '}\n';
* scene.postProcessStages.add(new Cesium.PostProcessStage({
* fragmentShader : fs,
* uniforms : {
* scale : 1.1,
* offset : function() {
* return new Cesium.Cartesian3(0.1, 0.2, 0.3);
* }
* }
* }));
*
* @example
* // Simple stage to change the color of what is selected.
* // If czm_selected returns true, the current fragment belongs to geometry in the selected array.
* var fs =
* 'uniform sampler2D colorTexture;\n' +
* 'varying vec2 v_textureCoordinates;\n' +
* 'uniform vec4 highlight;\n' +
* 'void main() {\n' +
* ' vec4 color = texture2D(colorTexture, v_textureCoordinates);\n' +
* ' if (czm_selected()) {\n' +
* ' vec3 highlighted = highlight.a * highlight.rgb + (1.0 - highlight.a) * color.rgb;\n' +
* ' gl_FragColor = vec4(highlighted, 1.0);\n' +
* ' } else { \n' +
* ' gl_FragColor = color;\n' +
* ' }\n' +
* '}\n';
* var stage = scene.postProcessStages.add(new Cesium.PostProcessStage({
* fragmentShader : fs,
* uniforms : {
* highlight : function() {
* return new Cesium.Color(1.0, 0.0, 0.0, 0.5);
* }
* }
* }));
* stage.selected = [cesium3DTileFeature];
*/
function PostProcessStage(options) {
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
var fragmentShader = options.fragmentShader;
var textureScale = defaultValue(options.textureScale, 1.0);
var pixelFormat = defaultValue(options.pixelFormat, PixelFormat.RGBA);
//>>includeStart('debug', pragmas.debug);
Check.typeOf.string('options.fragmentShader', fragmentShader);
Check.typeOf.number.greaterThan('options.textureScale', textureScale, 0.0);
Check.typeOf.number.lessThanOrEquals('options.textureScale', textureScale, 1.0);
if (!PixelFormat.isColorFormat(pixelFormat)) {
throw new DeveloperError('options.pixelFormat must be a color format.');
}
//>>includeEnd('debug');
this._fragmentShader = fragmentShader;
this._uniforms = options.uniforms;
this._textureScale = textureScale;
this._forcePowerOfTwo = defaultValue(options.forcePowerOfTwo, false);
this._sampleMode = defaultValue(options.sampleMode, PostProcessStageSampleMode.NEAREST);
this._pixelFormat = pixelFormat;
this._pixelDatatype = defaultValue(options.pixelDatatype, PixelDatatype.UNSIGNED_BYTE);
this._clearColor = defaultValue(options.clearColor, Color.BLACK);
this._uniformMap = undefined;
this._command = undefined;
this._colorTexture = undefined;
this._depthTexture = undefined;
this._idTexture = undefined;
this._actualUniforms = {};
this._dirtyUniforms = [];
this._texturesToRelease = [];
this._texturesToCreate = [];
this._texturePromise = undefined;
var passState = new PassState();
passState.scissorTest = {
enabled : true,
rectangle : defined(options.scissorRectangle) ? BoundingRectangle.clone(options.scissorRectangle) : new BoundingRectangle()
};
this._passState = passState;
this._ready = false;
var name = options.name;
if (!defined(name)) {
name = createGuid();
}
this._name = name;
this._logDepthChanged = undefined;
this._useLogDepth = undefined;
this._selectedIdTexture = undefined;
this._selected = undefined;
this._selectedShadow = undefined;
this._parentSelected = undefined;
this._parentSelectedShadow = undefined;
this._combinedSelected = undefined;
this._combinedSelectedShadow = undefined;
this._selectedLength = 0;
this._parentSelectedLength = 0;
this._selectedDirty = true;
// set by PostProcessStageCollection
this._textureCache = undefined;
this._index = undefined;
/**
* Whether or not to execute this post-process stage when ready.
*
* @type {Boolean}
*/
this.enabled = true;
this._enabled = true;
}
defineProperties(PostProcessStage.prototype, {
/**
* Determines if this post-process stage is ready to be executed. A stage is only executed when both ready
* and {@link PostProcessStage#enabled} are true
. A stage will not be ready while it is waiting on textures
* to load.
*
* @memberof PostProcessStage.prototype
* @type {Boolean}
* @readonly
*/
ready : {
get : function() {
return this._ready;
}
},
/**
* The unique name of this post-process stage for reference by other stages in a {@link PostProcessStageComposite}.
*
* @memberof PostProcessStage.prototype
* @type {String}
* @readonly
*/
name : {
get : function() {
return this._name;
}
},
/**
* The fragment shader to use when execute this post-process stage.
*
* The shader must contain a sampler uniform declaration for colorTexture
, depthTexture
,
* or both.
*
* The shader must contain a vec2
varying declaration for v_textureCoordinates
for sampling
* the texture uniforms.
*
* The object property values can be either a constant or a function. The function will be called * each frame before the post-process stage is executed. *
** A constant value can also be a URI to an image, a data URI, or an HTML element that can be used as a texture, such as HTMLImageElement or HTMLCanvasElement. *
** If this post-process stage is part of a {@link PostProcessStageComposite} that does not execute in series, the constant value can also be * the name of another stage in a composite. This will set the uniform to the output texture the stage with that name. *
* * @memberof PostProcessStage.prototype * @type {Object} * @readonly */ uniforms : { get : function() { return this._uniforms; } }, /** * A number in the range (0.0, 1.0] used to scale the output texture dimensions. A scale of 1.0 will render this post-process stage to a texture the size of the viewport. * * @memberof PostProcessStage.prototype * @type {Number} * @readonly */ textureScale : { get : function() { return this._textureScale; } }, /** * Whether or not to force the output texture dimensions to be both equal powers of two. The power of two will be the next power of two of the minimum of the dimensions. * * @memberof PostProcessStage.prototype * @type {Number} * @readonly */ forcePowerOfTwo : { get : function() { return this._forcePowerOfTwo; } }, /** * How to sample the input color texture. * * @memberof PostProcessStage.prototype * @type {PostProcessStageSampleMode} * @readonly */ sampleMode : { get : function() { return this._sampleMode; } }, /** * The color pixel format of the output texture. * * @memberof PostProcessStage.prototype * @type {PixelFormat} * @readonly */ pixelFormat : { get : function() { return this._pixelFormat; } }, /** * The pixel data type of the output texture. * * @memberof PostProcessStage.prototype * @type {PixelDatatype} * @readonly */ pixelDatatype : { get : function() { return this._pixelDatatype; } }, /** * The color to clear the output texture to. * * @memberof PostProcessStage.prototype * @type {Color} * @readonly */ clearColor : { get : function() { return this._clearColor; } }, /** * The {@link BoundingRectangle} to use for the scissor test. A default bounding rectangle will disable the scissor test. * * @memberof PostProcessStage.prototype * @type {BoundingRectangle} * @readonly */ scissorRectangle : { get : function() { return this._passState.scissorTest.rectangle; } }, /** * A reference to the texture written to when executing this post process stage. * * @memberof PostProcessStage.prototype * @type {Texture} * @readonly * @private */ outputTexture : { get : function() { if (defined(this._textureCache)) { var framebuffer = this._textureCache.getFramebuffer(this._name); if (defined(framebuffer)) { return framebuffer.getColorTexture(0); } } return undefined; } }, /** * The features selected for applying the post-process. *
* In the fragment shader, use czm_selected
to determine whether or not to apply the post-process
* stage to that fragment. For example:
*
* if (czm_selected(v_textureCoordinates)) {
* // apply post-process stage
* } else {
* gl_FragColor = texture2D(colorTexture, v_textureCordinates);
* }
*
*
* If this object was destroyed, it should not be used; calling any function other than
* isDestroyed
will result in a {@link DeveloperError} exception.
*
true
if this object was destroyed; otherwise, false
.
*
* @see PostProcessStage#destroy
*/
PostProcessStage.prototype.isDestroyed = function() {
return false;
};
/**
* Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
* release of WebGL resources, instead of relying on the garbage collector to destroy this object.
*
* Once an object is destroyed, it should not be used; calling any function other than
* isDestroyed
will result in a {@link DeveloperError} exception. Therefore,
* assign the return value (undefined
) to the object as done in the example.
*