|
@@ -25,6 +25,11 @@ export class DepthRenderer {
|
|
|
private _scene: Scene;
|
|
|
private _depthMap: RenderTargetTexture;
|
|
|
private _effect: Effect;
|
|
|
+ private readonly _storeNonLinearDepth: boolean;
|
|
|
+ private readonly _clearColor: Color4;
|
|
|
+
|
|
|
+ /** Get if the depth renderer is using packed depth or not */
|
|
|
+ public readonly isPacked: boolean;
|
|
|
|
|
|
private _cachedDefines: string;
|
|
|
private _camera: Nullable<Camera>;
|
|
@@ -46,16 +51,29 @@ export class DepthRenderer {
|
|
|
* @param scene The scene the renderer belongs to
|
|
|
* @param type The texture type of the depth map (default: Engine.TEXTURETYPE_FLOAT)
|
|
|
* @param camera The camera to be used to render the depth map (default: scene's active camera)
|
|
|
+ * @param storeNonLinearDepth Defines whether the depth is stored linearly like in Babylon Shadows or directly like glFragCoord.z
|
|
|
*/
|
|
|
- constructor(scene: Scene, type: number = Constants.TEXTURETYPE_FLOAT, camera: Nullable<Camera> = null) {
|
|
|
+ constructor(scene: Scene, type: number = Constants.TEXTURETYPE_FLOAT, camera: Nullable<Camera> = null, storeNonLinearDepth = false) {
|
|
|
this._scene = scene;
|
|
|
+ this._storeNonLinearDepth = storeNonLinearDepth;
|
|
|
+ this.isPacked = type === Constants.TEXTURETYPE_UNSIGNED_BYTE;
|
|
|
+ if (this.isPacked) {
|
|
|
+ this._clearColor = new Color4(1.0, 1.0, 1.0, 1.0);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ this._clearColor = new Color4(1.0, 0.0, 0.0, 1.0);
|
|
|
+ }
|
|
|
+
|
|
|
DepthRenderer._SceneComponentInitialization(this._scene);
|
|
|
|
|
|
this._camera = camera;
|
|
|
var engine = scene.getEngine();
|
|
|
|
|
|
// Render target
|
|
|
- this._depthMap = new RenderTargetTexture("depthMap", { width: engine.getRenderWidth(), height: engine.getRenderHeight() }, this._scene, false, true, type);
|
|
|
+ var format = (this.isPacked || engine.webGLVersion === 1) ? Constants.TEXTUREFORMAT_RGBA : Constants.TEXTUREFORMAT_R;
|
|
|
+ this._depthMap = new RenderTargetTexture("depthMap", { width: engine.getRenderWidth(), height: engine.getRenderHeight() }, this._scene, false, true, type,
|
|
|
+ false, undefined, undefined, undefined, undefined,
|
|
|
+ format);
|
|
|
this._depthMap.wrapU = Texture.CLAMP_ADDRESSMODE;
|
|
|
this._depthMap.wrapV = Texture.CLAMP_ADDRESSMODE;
|
|
|
this._depthMap.refreshRate = 1;
|
|
@@ -69,7 +87,7 @@ export class DepthRenderer {
|
|
|
|
|
|
// set default depth value to 1.0 (far away)
|
|
|
this._depthMap.onClearObservable.add((engine) => {
|
|
|
- engine.clear(new Color4(1.0, 1.0, 1.0, 1.0), true, true, true);
|
|
|
+ engine.clear(this._clearColor, true, true, true);
|
|
|
});
|
|
|
|
|
|
// Custom render function
|
|
@@ -214,6 +232,16 @@ export class DepthRenderer {
|
|
|
MaterialHelper.PushAttributesForInstances(attribs);
|
|
|
}
|
|
|
|
|
|
+ // None linear depth
|
|
|
+ if (this._storeNonLinearDepth) {
|
|
|
+ defines.push("#define NONLINEARDEPTH");
|
|
|
+ }
|
|
|
+
|
|
|
+ // Float Mode
|
|
|
+ if (this.isPacked) {
|
|
|
+ defines.push("#define PACKED");
|
|
|
+ }
|
|
|
+
|
|
|
// Get correct effect
|
|
|
var join = defines.join("\n");
|
|
|
if (this._cachedDefines !== join) {
|