@babylonjs/core 5.31.1 → 5.31.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -25,18 +25,20 @@ export interface IEffectRendererOptions {
|
|
|
25
25
|
* You can access the previous rendering in your shader by declaring a sampler named textureSampler
|
|
26
26
|
*/
|
|
27
27
|
export declare class EffectRenderer {
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
/**
|
|
29
|
+
* The engine the effect renderer has been created for.
|
|
30
|
+
*/
|
|
31
|
+
readonly engine: ThinEngine;
|
|
30
32
|
private _vertexBuffers;
|
|
31
33
|
private _indexBuffer;
|
|
32
34
|
private _fullscreenViewport;
|
|
33
35
|
private _onContextRestoredObserver;
|
|
34
36
|
/**
|
|
35
37
|
* Creates an effect renderer
|
|
36
|
-
* @param
|
|
38
|
+
* @param engine the engine to use for rendering
|
|
37
39
|
* @param options defines the options of the effect renderer
|
|
38
40
|
*/
|
|
39
|
-
constructor(
|
|
41
|
+
constructor(engine: ThinEngine, options?: IEffectRendererOptions);
|
|
40
42
|
/**
|
|
41
43
|
* Sets the current viewport in normalized coordinates 0-1
|
|
42
44
|
* @param viewport Defines the viewport to set (defaults to 0 0 1 1)
|
|
@@ -6,6 +6,11 @@ import { Effect } from "./effect.js";
|
|
|
6
6
|
import { DrawWrapper } from "./drawWrapper.js";
|
|
7
7
|
// Prevents ES6 Crash if not imported.
|
|
8
8
|
import "../Shaders/postprocess.vertex.js";
|
|
9
|
+
// Fullscreen quad buffers by default.
|
|
10
|
+
const defaultOptions = {
|
|
11
|
+
positions: [1, 1, -1, 1, -1, -1, 1, -1],
|
|
12
|
+
indices: [0, 1, 2, 0, 2, 3],
|
|
13
|
+
};
|
|
9
14
|
/**
|
|
10
15
|
* Helper class to render one or more effects.
|
|
11
16
|
* You can access the previous rendering in your shader by declaring a sampler named textureSampler
|
|
@@ -13,22 +18,21 @@ import "../Shaders/postprocess.vertex.js";
|
|
|
13
18
|
export class EffectRenderer {
|
|
14
19
|
/**
|
|
15
20
|
* Creates an effect renderer
|
|
16
|
-
* @param
|
|
21
|
+
* @param engine the engine to use for rendering
|
|
17
22
|
* @param options defines the options of the effect renderer
|
|
18
23
|
*/
|
|
19
|
-
constructor(
|
|
20
|
-
|
|
24
|
+
constructor(engine, options = defaultOptions) {
|
|
25
|
+
var _a, _b;
|
|
21
26
|
this._fullscreenViewport = new Viewport(0, 0, 1, 1);
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
};
|
|
27
|
+
const positions = (_a = options.positions) !== null && _a !== void 0 ? _a : defaultOptions.positions;
|
|
28
|
+
const indices = (_b = options.indices) !== null && _b !== void 0 ? _b : defaultOptions.indices;
|
|
29
|
+
this.engine = engine;
|
|
26
30
|
this._vertexBuffers = {
|
|
27
|
-
[VertexBuffer.PositionKind]: new VertexBuffer(
|
|
31
|
+
[VertexBuffer.PositionKind]: new VertexBuffer(engine, positions, VertexBuffer.PositionKind, false, false, 2),
|
|
28
32
|
};
|
|
29
|
-
this._indexBuffer =
|
|
30
|
-
this._onContextRestoredObserver =
|
|
31
|
-
this._indexBuffer =
|
|
33
|
+
this._indexBuffer = engine.createIndexBuffer(indices);
|
|
34
|
+
this._onContextRestoredObserver = engine.onContextRestoredObservable.add(() => {
|
|
35
|
+
this._indexBuffer = engine.createIndexBuffer(indices);
|
|
32
36
|
for (const key in this._vertexBuffers) {
|
|
33
37
|
const vertexBuffer = this._vertexBuffers[key];
|
|
34
38
|
vertexBuffer._rebuild();
|
|
@@ -40,14 +44,14 @@ export class EffectRenderer {
|
|
|
40
44
|
* @param viewport Defines the viewport to set (defaults to 0 0 1 1)
|
|
41
45
|
*/
|
|
42
46
|
setViewport(viewport = this._fullscreenViewport) {
|
|
43
|
-
this.
|
|
47
|
+
this.engine.setViewport(viewport);
|
|
44
48
|
}
|
|
45
49
|
/**
|
|
46
50
|
* Binds the embedded attributes buffer to the effect.
|
|
47
51
|
* @param effect Defines the effect to bind the attributes for
|
|
48
52
|
*/
|
|
49
53
|
bindBuffers(effect) {
|
|
50
|
-
this.
|
|
54
|
+
this.engine.bindBuffers(this._vertexBuffers, this._indexBuffer, effect);
|
|
51
55
|
}
|
|
52
56
|
/**
|
|
53
57
|
* Sets the current effect wrapper to use during draw.
|
|
@@ -56,9 +60,9 @@ export class EffectRenderer {
|
|
|
56
60
|
* @param effectWrapper Defines the effect to draw with
|
|
57
61
|
*/
|
|
58
62
|
applyEffectWrapper(effectWrapper) {
|
|
59
|
-
this.
|
|
60
|
-
this.
|
|
61
|
-
this.
|
|
63
|
+
this.engine.depthCullingState.depthTest = false;
|
|
64
|
+
this.engine.stencilState.stencilTest = false;
|
|
65
|
+
this.engine.enableEffect(effectWrapper._drawWrapper);
|
|
62
66
|
this.bindBuffers(effectWrapper.effect);
|
|
63
67
|
effectWrapper.onApplyObservable.notifyObservers({});
|
|
64
68
|
}
|
|
@@ -66,14 +70,14 @@ export class EffectRenderer {
|
|
|
66
70
|
* Restores engine states
|
|
67
71
|
*/
|
|
68
72
|
restoreStates() {
|
|
69
|
-
this.
|
|
70
|
-
this.
|
|
73
|
+
this.engine.depthCullingState.depthTest = true;
|
|
74
|
+
this.engine.stencilState.stencilTest = true;
|
|
71
75
|
}
|
|
72
76
|
/**
|
|
73
77
|
* Draws a full screen quad.
|
|
74
78
|
*/
|
|
75
79
|
draw() {
|
|
76
|
-
this.
|
|
80
|
+
this.engine.drawElementsType(0, 0, 6);
|
|
77
81
|
}
|
|
78
82
|
_isRenderTargetTexture(texture) {
|
|
79
83
|
return texture.renderTarget !== undefined;
|
|
@@ -92,12 +96,12 @@ export class EffectRenderer {
|
|
|
92
96
|
this.setViewport();
|
|
93
97
|
const out = outputTexture === null ? null : this._isRenderTargetTexture(outputTexture) ? outputTexture.renderTarget : outputTexture;
|
|
94
98
|
if (out) {
|
|
95
|
-
this.
|
|
99
|
+
this.engine.bindFramebuffer(out);
|
|
96
100
|
}
|
|
97
101
|
this.applyEffectWrapper(effectWrapper);
|
|
98
102
|
this.draw();
|
|
99
103
|
if (out) {
|
|
100
|
-
this.
|
|
104
|
+
this.engine.unBindFramebuffer(out);
|
|
101
105
|
}
|
|
102
106
|
this.restoreStates();
|
|
103
107
|
}
|
|
@@ -111,19 +115,14 @@ export class EffectRenderer {
|
|
|
111
115
|
delete this._vertexBuffers[VertexBuffer.PositionKind];
|
|
112
116
|
}
|
|
113
117
|
if (this._indexBuffer) {
|
|
114
|
-
this.
|
|
118
|
+
this.engine._releaseBuffer(this._indexBuffer);
|
|
115
119
|
}
|
|
116
120
|
if (this._onContextRestoredObserver) {
|
|
117
|
-
this.
|
|
121
|
+
this.engine.onContextRestoredObservable.remove(this._onContextRestoredObserver);
|
|
118
122
|
this._onContextRestoredObserver = null;
|
|
119
123
|
}
|
|
120
124
|
}
|
|
121
125
|
}
|
|
122
|
-
// Fullscreen quad buffers by default.
|
|
123
|
-
EffectRenderer._DefaultOptions = {
|
|
124
|
-
positions: [1, 1, -1, 1, -1, -1, 1, -1],
|
|
125
|
-
indices: [0, 1, 2, 0, 2, 3],
|
|
126
|
-
};
|
|
127
126
|
/**
|
|
128
127
|
* Wraps an effect to be used for rendering
|
|
129
128
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"effectRenderer.js","sourceRoot":"","sources":["../../../../../lts/core/generated/Materials/effectRenderer.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEjD,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAI5C,sCAAsC;AACtC,OAAO,+BAA+B,CAAC;AAgBvC;;;GAGG;AACH,MAAM,O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AAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;IAC1B,CAAC;CACJ","sourcesContent":["import type { Nullable } from \"../types\";\r\nimport type { ThinEngine } from \"../Engines/thinEngine\";\r\nimport { VertexBuffer } from \"../Buffers/buffer\";\r\nimport { Viewport } from \"../Maths/math.viewport\";\r\nimport { Constants } from \"../Engines/constants\";\r\nimport type { Observer } from \"../Misc/observable\";\r\nimport { Observable } from \"../Misc/observable\";\r\nimport { Effect } from \"./effect\";\r\nimport type { DataBuffer } from \"../Buffers/dataBuffer\";\r\nimport { DrawWrapper } from \"./drawWrapper\";\r\nimport type { IRenderTargetTexture, RenderTargetWrapper } from \"../Engines/renderTargetWrapper\";\r\nimport type { ShaderLanguage } from \"./shaderLanguage\";\r\n\r\n// Prevents ES6 Crash if not imported.\r\nimport \"../Shaders/postprocess.vertex\";\r\n\r\n/**\r\n * Effect Render Options\r\n */\r\nexport interface IEffectRendererOptions {\r\n /**\r\n * Defines the vertices positions.\r\n */\r\n positions?: number[];\r\n /**\r\n * Defines the indices.\r\n */\r\n indices?: number[];\r\n}\r\n\r\n/**\r\n * Helper class to render one or more effects.\r\n * You can access the previous rendering in your shader by declaring a sampler named textureSampler\r\n */\r\nexport class EffectRenderer {\r\n // Fullscreen quad buffers by default.\r\n private static _DefaultOptions: IEffectRendererOptions = {\r\n positions: [1, 1, -1, 1, -1, -1, 1, -1],\r\n indices: [0, 1, 2, 0, 2, 3],\r\n };\r\n\r\n private _vertexBuffers: { [key: string]: VertexBuffer };\r\n private _indexBuffer: DataBuffer;\r\n\r\n private _fullscreenViewport = new Viewport(0, 0, 1, 1);\r\n private _onContextRestoredObserver: Nullable<Observer<ThinEngine>>;\r\n\r\n /**\r\n * Creates an effect renderer\r\n * @param _engine the engine to use for rendering\r\n * @param options defines the options of the effect renderer\r\n */\r\n constructor(private _engine: ThinEngine, options: IEffectRendererOptions = EffectRenderer._DefaultOptions) {\r\n options = {\r\n ...EffectRenderer._DefaultOptions,\r\n ...options,\r\n };\r\n\r\n this._vertexBuffers = {\r\n [VertexBuffer.PositionKind]: new VertexBuffer(_engine, options.positions!, VertexBuffer.PositionKind, false, false, 2),\r\n };\r\n this._indexBuffer = _engine.createIndexBuffer(options.indices!);\r\n\r\n this._onContextRestoredObserver = _engine.onContextRestoredObservable.add(() => {\r\n this._indexBuffer = _engine.createIndexBuffer(options.indices!);\r\n\r\n for (const key in this._vertexBuffers) {\r\n const vertexBuffer = <VertexBuffer>this._vertexBuffers[key];\r\n vertexBuffer._rebuild();\r\n }\r\n });\r\n }\r\n\r\n /**\r\n * Sets the current viewport in normalized coordinates 0-1\r\n * @param viewport Defines the viewport to set (defaults to 0 0 1 1)\r\n */\r\n public setViewport(viewport = this._fullscreenViewport): void {\r\n this._engine.setViewport(viewport);\r\n }\r\n\r\n /**\r\n * Binds the embedded attributes buffer to the effect.\r\n * @param effect Defines the effect to bind the attributes for\r\n */\r\n public bindBuffers(effect: Effect): void {\r\n this._engine.bindBuffers(this._vertexBuffers, this._indexBuffer, effect);\r\n }\r\n\r\n /**\r\n * Sets the current effect wrapper to use during draw.\r\n * The effect needs to be ready before calling this api.\r\n * This also sets the default full screen position attribute.\r\n * @param effectWrapper Defines the effect to draw with\r\n */\r\n public applyEffectWrapper(effectWrapper: EffectWrapper): void {\r\n this._engine.depthCullingState.depthTest = false;\r\n this._engine.stencilState.stencilTest = false;\r\n this._engine.enableEffect(effectWrapper._drawWrapper);\r\n this.bindBuffers(effectWrapper.effect);\r\n effectWrapper.onApplyObservable.notifyObservers({});\r\n }\r\n\r\n /**\r\n * Restores engine states\r\n */\r\n public restoreStates(): void {\r\n this._engine.depthCullingState.depthTest = true;\r\n this._engine.stencilState.stencilTest = true;\r\n }\r\n\r\n /**\r\n * Draws a full screen quad.\r\n */\r\n public draw(): void {\r\n this._engine.drawElementsType(Constants.MATERIAL_TriangleFillMode, 0, 6);\r\n }\r\n\r\n private _isRenderTargetTexture(texture: RenderTargetWrapper | IRenderTargetTexture): texture is IRenderTargetTexture {\r\n return (texture as IRenderTargetTexture).renderTarget !== undefined;\r\n }\r\n\r\n /**\r\n * renders one or more effects to a specified texture\r\n * @param effectWrapper the effect to renderer\r\n * @param outputTexture texture to draw to, if null it will render to the screen.\r\n */\r\n public render(effectWrapper: EffectWrapper, outputTexture: Nullable<RenderTargetWrapper | IRenderTargetTexture> = null) {\r\n // Ensure effect is ready\r\n if (!effectWrapper.effect.isReady()) {\r\n return;\r\n }\r\n\r\n // Reset state\r\n this.setViewport();\r\n\r\n const out = outputTexture === null ? null : this._isRenderTargetTexture(outputTexture) ? outputTexture.renderTarget! : outputTexture;\r\n\r\n if (out) {\r\n this._engine.bindFramebuffer(out);\r\n }\r\n\r\n this.applyEffectWrapper(effectWrapper);\r\n\r\n this.draw();\r\n\r\n if (out) {\r\n this._engine.unBindFramebuffer(out);\r\n }\r\n\r\n this.restoreStates();\r\n }\r\n\r\n /**\r\n * Disposes of the effect renderer\r\n */\r\n dispose() {\r\n const vertexBuffer = this._vertexBuffers[VertexBuffer.PositionKind];\r\n if (vertexBuffer) {\r\n vertexBuffer.dispose();\r\n delete this._vertexBuffers[VertexBuffer.PositionKind];\r\n }\r\n\r\n if (this._indexBuffer) {\r\n this._engine._releaseBuffer(this._indexBuffer);\r\n }\r\n\r\n if (this._onContextRestoredObserver) {\r\n this._engine.onContextRestoredObservable.remove(this._onContextRestoredObserver);\r\n this._onContextRestoredObserver = null;\r\n }\r\n }\r\n}\r\n\r\n/**\r\n * Options to create an EffectWrapper\r\n */\r\ninterface EffectWrapperCreationOptions {\r\n /**\r\n * Engine to use to create the effect\r\n */\r\n engine: ThinEngine;\r\n /**\r\n * Fragment shader for the effect\r\n */\r\n fragmentShader: string;\r\n /**\r\n * Use the shader store instead of direct source code\r\n */\r\n useShaderStore?: boolean;\r\n /**\r\n * Vertex shader for the effect\r\n */\r\n vertexShader?: string;\r\n /**\r\n * Attributes to use in the shader\r\n */\r\n attributeNames?: Array<string>;\r\n /**\r\n * Uniforms to use in the shader\r\n */\r\n uniformNames?: Array<string>;\r\n /**\r\n * Texture sampler names to use in the shader\r\n */\r\n samplerNames?: Array<string>;\r\n /**\r\n * Defines to use in the shader\r\n */\r\n defines?: Array<string>;\r\n /**\r\n * Callback when effect is compiled\r\n */\r\n onCompiled?: Nullable<(effect: Effect) => void>;\r\n /**\r\n * The friendly name of the effect displayed in Spector.\r\n */\r\n name?: string;\r\n /**\r\n * The language the shader is written in (default: GLSL)\r\n */\r\n shaderLanguage?: ShaderLanguage;\r\n}\r\n\r\n/**\r\n * Wraps an effect to be used for rendering\r\n */\r\nexport class EffectWrapper {\r\n /**\r\n * Event that is fired right before the effect is drawn (should be used to update uniforms)\r\n */\r\n public onApplyObservable = new Observable<{}>();\r\n /**\r\n * The underlying effect\r\n */\r\n public get effect(): Effect {\r\n return this._drawWrapper.effect!;\r\n }\r\n\r\n public set effect(effect: Effect) {\r\n this._drawWrapper.effect = effect;\r\n }\r\n\r\n /** @internal */\r\n public _drawWrapper: DrawWrapper;\r\n\r\n private _onContextRestoredObserver: Nullable<Observer<ThinEngine>>;\r\n\r\n /**\r\n * Creates an effect to be renderer\r\n * @param creationOptions options to create the effect\r\n */\r\n constructor(creationOptions: EffectWrapperCreationOptions) {\r\n let effectCreationOptions: any;\r\n const uniformNames = creationOptions.uniformNames || [];\r\n\r\n if (creationOptions.vertexShader) {\r\n effectCreationOptions = {\r\n fragmentSource: creationOptions.fragmentShader,\r\n vertexSource: creationOptions.vertexShader,\r\n spectorName: creationOptions.name || \"effectWrapper\",\r\n };\r\n } else {\r\n // Default scale to use in post process vertex shader.\r\n uniformNames.push(\"scale\");\r\n\r\n effectCreationOptions = {\r\n fragmentSource: creationOptions.fragmentShader,\r\n vertex: \"postprocess\",\r\n spectorName: creationOptions.name || \"effectWrapper\",\r\n };\r\n\r\n // Sets the default scale to identity for the post process vertex shader.\r\n this.onApplyObservable.add(() => {\r\n this.effect.setFloat2(\"scale\", 1, 1);\r\n });\r\n }\r\n\r\n const defines = creationOptions.defines ? creationOptions.defines.join(\"\\n\") : \"\";\r\n this._drawWrapper = new DrawWrapper(creationOptions.engine);\r\n\r\n if (creationOptions.useShaderStore) {\r\n effectCreationOptions.fragment = effectCreationOptions.fragmentSource;\r\n if (!effectCreationOptions.vertex) {\r\n effectCreationOptions.vertex = effectCreationOptions.vertexSource;\r\n }\r\n\r\n delete effectCreationOptions.fragmentSource;\r\n delete effectCreationOptions.vertexSource;\r\n\r\n this.effect = creationOptions.engine.createEffect(\r\n effectCreationOptions,\r\n creationOptions.attributeNames || [\"position\"],\r\n uniformNames,\r\n creationOptions.samplerNames,\r\n defines,\r\n undefined,\r\n creationOptions.onCompiled,\r\n undefined,\r\n undefined,\r\n creationOptions.shaderLanguage\r\n );\r\n } else {\r\n this.effect = new Effect(\r\n effectCreationOptions,\r\n creationOptions.attributeNames || [\"position\"],\r\n uniformNames,\r\n creationOptions.samplerNames,\r\n creationOptions.engine,\r\n defines,\r\n undefined,\r\n creationOptions.onCompiled,\r\n undefined,\r\n undefined,\r\n undefined,\r\n creationOptions.shaderLanguage\r\n );\r\n\r\n this._onContextRestoredObserver = creationOptions.engine.onContextRestoredObservable.add(() => {\r\n this.effect._pipelineContext = null; // because _prepareEffect will try to dispose this pipeline before recreating it and that would lead to webgl errors\r\n this.effect._wasPreviouslyReady = false;\r\n this.effect._prepareEffect();\r\n });\r\n }\r\n }\r\n\r\n /**\r\n * Disposes of the effect wrapper\r\n */\r\n public dispose() {\r\n if (this._onContextRestoredObserver) {\r\n this.effect.getEngine().onContextRestoredObservable.remove(this._onContextRestoredObserver);\r\n this._onContextRestoredObserver = null;\r\n }\r\n this.effect.dispose();\r\n }\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"file":"effectRenderer.js","sourceRoot":"","sources":["../../../../../lts/core/generated/Materials/effectRenderer.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAA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C;IAED,IAAW,MAAM,CAAC,MAAc;QAC5B,IAAI,CAAC,YAAY,CAAC,MAAM,GAAG,MAAM,CAAC;IACtC,CAAC;IAqFD;;OAEG;IACI,OAAO;QACV,IAAI,IAAI,CAAC,0BAA0B,EAAE;YACjC,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC,2BAA2B,CAAC,MAAM,CAAC,IAAI,CAAC,0BAA0B,CAAC,CAAC;YAC5F,IAAI,CAAC,0BAA0B,GAAG,IAAI,CAAC;SAC1C;QACD,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;IAC1B,CAAC;CACJ","sourcesContent":["import type { Nullable } from \"../types\";\r\nimport type { ThinEngine } from \"../Engines/thinEngine\";\r\nimport { VertexBuffer } from \"../Buffers/buffer\";\r\nimport { Viewport } from \"../Maths/math.viewport\";\r\nimport { Constants } from \"../Engines/constants\";\r\nimport type { Observer } from \"../Misc/observable\";\r\nimport { Observable } from \"../Misc/observable\";\r\nimport { Effect } from \"./effect\";\r\nimport type { DataBuffer } from \"../Buffers/dataBuffer\";\r\nimport { DrawWrapper } from \"./drawWrapper\";\r\nimport type { IRenderTargetTexture, RenderTargetWrapper } from \"../Engines/renderTargetWrapper\";\r\nimport type { ShaderLanguage } from \"./shaderLanguage\";\r\n\r\n// Prevents ES6 Crash if not imported.\r\nimport \"../Shaders/postprocess.vertex\";\r\n\r\n/**\r\n * Effect Render Options\r\n */\r\nexport interface IEffectRendererOptions {\r\n /**\r\n * Defines the vertices positions.\r\n */\r\n positions?: number[];\r\n /**\r\n * Defines the indices.\r\n */\r\n indices?: number[];\r\n}\r\n\r\n// Fullscreen quad buffers by default.\r\nconst defaultOptions = {\r\n positions: [1, 1, -1, 1, -1, -1, 1, -1],\r\n indices: [0, 1, 2, 0, 2, 3],\r\n};\r\n\r\n/**\r\n * Helper class to render one or more effects.\r\n * You can access the previous rendering in your shader by declaring a sampler named textureSampler\r\n */\r\nexport class EffectRenderer {\r\n /**\r\n * The engine the effect renderer has been created for.\r\n */\r\n public readonly engine: ThinEngine;\r\n\r\n private _vertexBuffers: { [key: string]: VertexBuffer };\r\n private _indexBuffer: DataBuffer;\r\n\r\n private _fullscreenViewport = new Viewport(0, 0, 1, 1);\r\n private _onContextRestoredObserver: Nullable<Observer<ThinEngine>>;\r\n\r\n /**\r\n * Creates an effect renderer\r\n * @param engine the engine to use for rendering\r\n * @param options defines the options of the effect renderer\r\n */\r\n constructor(engine: ThinEngine, options: IEffectRendererOptions = defaultOptions) {\r\n const positions = options.positions ?? defaultOptions.positions;\r\n const indices = options.indices ?? defaultOptions.indices;\r\n\r\n this.engine = engine;\r\n this._vertexBuffers = {\r\n [VertexBuffer.PositionKind]: new VertexBuffer(engine, positions, VertexBuffer.PositionKind, false, false, 2),\r\n };\r\n this._indexBuffer = engine.createIndexBuffer(indices);\r\n\r\n this._onContextRestoredObserver = engine.onContextRestoredObservable.add(() => {\r\n this._indexBuffer = engine.createIndexBuffer(indices);\r\n\r\n for (const key in this._vertexBuffers) {\r\n const vertexBuffer = this._vertexBuffers[key];\r\n vertexBuffer._rebuild();\r\n }\r\n });\r\n }\r\n\r\n /**\r\n * Sets the current viewport in normalized coordinates 0-1\r\n * @param viewport Defines the viewport to set (defaults to 0 0 1 1)\r\n */\r\n public setViewport(viewport = this._fullscreenViewport): void {\r\n this.engine.setViewport(viewport);\r\n }\r\n\r\n /**\r\n * Binds the embedded attributes buffer to the effect.\r\n * @param effect Defines the effect to bind the attributes for\r\n */\r\n public bindBuffers(effect: Effect): void {\r\n this.engine.bindBuffers(this._vertexBuffers, this._indexBuffer, effect);\r\n }\r\n\r\n /**\r\n * Sets the current effect wrapper to use during draw.\r\n * The effect needs to be ready before calling this api.\r\n * This also sets the default full screen position attribute.\r\n * @param effectWrapper Defines the effect to draw with\r\n */\r\n public applyEffectWrapper(effectWrapper: EffectWrapper): void {\r\n this.engine.depthCullingState.depthTest = false;\r\n this.engine.stencilState.stencilTest = false;\r\n this.engine.enableEffect(effectWrapper._drawWrapper);\r\n this.bindBuffers(effectWrapper.effect);\r\n effectWrapper.onApplyObservable.notifyObservers({});\r\n }\r\n\r\n /**\r\n * Restores engine states\r\n */\r\n public restoreStates(): void {\r\n this.engine.depthCullingState.depthTest = true;\r\n this.engine.stencilState.stencilTest = true;\r\n }\r\n\r\n /**\r\n * Draws a full screen quad.\r\n */\r\n public draw(): void {\r\n this.engine.drawElementsType(Constants.MATERIAL_TriangleFillMode, 0, 6);\r\n }\r\n\r\n private _isRenderTargetTexture(texture: RenderTargetWrapper | IRenderTargetTexture): texture is IRenderTargetTexture {\r\n return (texture as IRenderTargetTexture).renderTarget !== undefined;\r\n }\r\n\r\n /**\r\n * renders one or more effects to a specified texture\r\n * @param effectWrapper the effect to renderer\r\n * @param outputTexture texture to draw to, if null it will render to the screen.\r\n */\r\n public render(effectWrapper: EffectWrapper, outputTexture: Nullable<RenderTargetWrapper | IRenderTargetTexture> = null) {\r\n // Ensure effect is ready\r\n if (!effectWrapper.effect.isReady()) {\r\n return;\r\n }\r\n\r\n // Reset state\r\n this.setViewport();\r\n\r\n const out = outputTexture === null ? null : this._isRenderTargetTexture(outputTexture) ? outputTexture.renderTarget! : outputTexture;\r\n\r\n if (out) {\r\n this.engine.bindFramebuffer(out);\r\n }\r\n\r\n this.applyEffectWrapper(effectWrapper);\r\n\r\n this.draw();\r\n\r\n if (out) {\r\n this.engine.unBindFramebuffer(out);\r\n }\r\n\r\n this.restoreStates();\r\n }\r\n\r\n /**\r\n * Disposes of the effect renderer\r\n */\r\n dispose() {\r\n const vertexBuffer = this._vertexBuffers[VertexBuffer.PositionKind];\r\n if (vertexBuffer) {\r\n vertexBuffer.dispose();\r\n delete this._vertexBuffers[VertexBuffer.PositionKind];\r\n }\r\n\r\n if (this._indexBuffer) {\r\n this.engine._releaseBuffer(this._indexBuffer);\r\n }\r\n\r\n if (this._onContextRestoredObserver) {\r\n this.engine.onContextRestoredObservable.remove(this._onContextRestoredObserver);\r\n this._onContextRestoredObserver = null;\r\n }\r\n }\r\n}\r\n\r\n/**\r\n * Options to create an EffectWrapper\r\n */\r\ninterface EffectWrapperCreationOptions {\r\n /**\r\n * Engine to use to create the effect\r\n */\r\n engine: ThinEngine;\r\n /**\r\n * Fragment shader for the effect\r\n */\r\n fragmentShader: string;\r\n /**\r\n * Use the shader store instead of direct source code\r\n */\r\n useShaderStore?: boolean;\r\n /**\r\n * Vertex shader for the effect\r\n */\r\n vertexShader?: string;\r\n /**\r\n * Attributes to use in the shader\r\n */\r\n attributeNames?: Array<string>;\r\n /**\r\n * Uniforms to use in the shader\r\n */\r\n uniformNames?: Array<string>;\r\n /**\r\n * Texture sampler names to use in the shader\r\n */\r\n samplerNames?: Array<string>;\r\n /**\r\n * Defines to use in the shader\r\n */\r\n defines?: Array<string>;\r\n /**\r\n * Callback when effect is compiled\r\n */\r\n onCompiled?: Nullable<(effect: Effect) => void>;\r\n /**\r\n * The friendly name of the effect displayed in Spector.\r\n */\r\n name?: string;\r\n /**\r\n * The language the shader is written in (default: GLSL)\r\n */\r\n shaderLanguage?: ShaderLanguage;\r\n}\r\n\r\n/**\r\n * Wraps an effect to be used for rendering\r\n */\r\nexport class EffectWrapper {\r\n /**\r\n * Event that is fired right before the effect is drawn (should be used to update uniforms)\r\n */\r\n public onApplyObservable = new Observable<{}>();\r\n /**\r\n * The underlying effect\r\n */\r\n public get effect(): Effect {\r\n return this._drawWrapper.effect!;\r\n }\r\n\r\n public set effect(effect: Effect) {\r\n this._drawWrapper.effect = effect;\r\n }\r\n\r\n /** @internal */\r\n public _drawWrapper: DrawWrapper;\r\n\r\n private _onContextRestoredObserver: Nullable<Observer<ThinEngine>>;\r\n\r\n /**\r\n * Creates an effect to be renderer\r\n * @param creationOptions options to create the effect\r\n */\r\n constructor(creationOptions: EffectWrapperCreationOptions) {\r\n let effectCreationOptions: any;\r\n const uniformNames = creationOptions.uniformNames || [];\r\n\r\n if (creationOptions.vertexShader) {\r\n effectCreationOptions = {\r\n fragmentSource: creationOptions.fragmentShader,\r\n vertexSource: creationOptions.vertexShader,\r\n spectorName: creationOptions.name || \"effectWrapper\",\r\n };\r\n } else {\r\n // Default scale to use in post process vertex shader.\r\n uniformNames.push(\"scale\");\r\n\r\n effectCreationOptions = {\r\n fragmentSource: creationOptions.fragmentShader,\r\n vertex: \"postprocess\",\r\n spectorName: creationOptions.name || \"effectWrapper\",\r\n };\r\n\r\n // Sets the default scale to identity for the post process vertex shader.\r\n this.onApplyObservable.add(() => {\r\n this.effect.setFloat2(\"scale\", 1, 1);\r\n });\r\n }\r\n\r\n const defines = creationOptions.defines ? creationOptions.defines.join(\"\\n\") : \"\";\r\n this._drawWrapper = new DrawWrapper(creationOptions.engine);\r\n\r\n if (creationOptions.useShaderStore) {\r\n effectCreationOptions.fragment = effectCreationOptions.fragmentSource;\r\n if (!effectCreationOptions.vertex) {\r\n effectCreationOptions.vertex = effectCreationOptions.vertexSource;\r\n }\r\n\r\n delete effectCreationOptions.fragmentSource;\r\n delete effectCreationOptions.vertexSource;\r\n\r\n this.effect = creationOptions.engine.createEffect(\r\n effectCreationOptions,\r\n creationOptions.attributeNames || [\"position\"],\r\n uniformNames,\r\n creationOptions.samplerNames,\r\n defines,\r\n undefined,\r\n creationOptions.onCompiled,\r\n undefined,\r\n undefined,\r\n creationOptions.shaderLanguage\r\n );\r\n } else {\r\n this.effect = new Effect(\r\n effectCreationOptions,\r\n creationOptions.attributeNames || [\"position\"],\r\n uniformNames,\r\n creationOptions.samplerNames,\r\n creationOptions.engine,\r\n defines,\r\n undefined,\r\n creationOptions.onCompiled,\r\n undefined,\r\n undefined,\r\n undefined,\r\n creationOptions.shaderLanguage\r\n );\r\n\r\n this._onContextRestoredObserver = creationOptions.engine.onContextRestoredObservable.add(() => {\r\n this.effect._pipelineContext = null; // because _prepareEffect will try to dispose this pipeline before recreating it and that would lead to webgl errors\r\n this.effect._wasPreviouslyReady = false;\r\n this.effect._prepareEffect();\r\n });\r\n }\r\n }\r\n\r\n /**\r\n * Disposes of the effect wrapper\r\n */\r\n public dispose() {\r\n if (this._onContextRestoredObserver) {\r\n this.effect.getEngine().onContextRestoredObservable.remove(this._onContextRestoredObserver);\r\n this._onContextRestoredObserver = null;\r\n }\r\n this.effect.dispose();\r\n }\r\n}\r\n"]}
|