@babylonjs/core
Version:
Getting started? Play directly with the Babylon.js API using our [playground](https://playground.babylonjs.com/). It also contains a lot of samples to learn how to use it.
144 lines (143 loc) • 7.24 kB
JavaScript
import { Vector4 } from "../../../../Maths/math.vector.js";
import { ThinCustomPostProcess } from "../../../../PostProcesses/thinCustomPostProcess.js";
import { FrameGraphTask } from "../../../frameGraphTask.js";
/**
* Task used to temporally accumulate IBL shadows.
* @internal
*/
export class FrameGraphIblShadowsAccumulationTask extends FrameGraphTask {
get remanence() {
return this._remanence;
}
set remanence(value) {
this._remanence = Math.max(0, Math.min(value, 1));
}
constructor(name, frameGraph) {
super(name, frameGraph);
this._remanence = 0.75;
this.reset = true;
this.isMoving = false;
this.voxelGridSize = 1;
this._accumulationParams = new Vector4(0, 0, 0, 0);
this.postProcess = new ThinCustomPostProcess(name, frameGraph.engine, {
fragmentShader: "iblShadowAccumulation",
uniforms: ["accumulationParameters"],
samplers: ["spatialBlurSampler", "oldAccumulationSampler", "prevPositionSampler", "motionSampler", "positionSampler"],
shaderLanguage: frameGraph.engine.isWebGPU ? 1 /* ShaderLanguage.WGSL */ : 0 /* ShaderLanguage.GLSL */,
});
this._postProcessDrawWrapper = this.postProcess.drawWrapper;
this.outputTexture = this._frameGraph.textureManager.createDanglingHandle();
}
getClassName() {
return "FrameGraphIblShadowsAccumulationTask";
}
// eslint-disable-next-line @typescript-eslint/promise-function-async, no-restricted-syntax
initAsync() {
if (this._frameGraph.engine.isWebGPU) {
return import("../../../../ShadersWGSL/iblShadowAccumulation.fragment.js");
}
return import("../../../../Shaders/iblShadowAccumulation.fragment.js");
}
isReady() {
return this.postProcess.isReady();
}
record() {
if (this.sourceTexture === undefined || this.velocityTexture === undefined || this.positionTexture === undefined) {
throw new Error(`FrameGraphIblShadowsAccumulationTask ${this.name}: sourceTexture, velocityTexture and positionTexture are required`);
}
const textureManager = this._frameGraph.textureManager;
const outputSize = textureManager.getTextureAbsoluteDimensions(this.sourceTexture);
const positionSize = textureManager.getTextureAbsoluteDimensions(this.positionTexture);
const outputCreationOptions = {
size: outputSize,
sizeIsPercentage: false,
isHistoryTexture: false,
options: {
createMipMaps: false,
samples: 1,
types: [2],
formats: [5],
useSRGBBuffers: [false],
creationFlags: [0],
labels: [`${this.name} Output`],
},
};
textureManager.resolveDanglingHandle(this.outputTexture, undefined, `${this.name} Output`, outputCreationOptions);
const accumulationHistoryCreationOptions = {
size: outputSize,
sizeIsPercentage: false,
isHistoryTexture: true,
options: {
createMipMaps: false,
samples: 1,
types: [2],
formats: [5],
useSRGBBuffers: [false],
creationFlags: [0],
labels: [`${this.name} History`],
},
};
const positionHistoryCreationOptions = {
size: positionSize,
sizeIsPercentage: false,
isHistoryTexture: true,
options: {
createMipMaps: false,
samples: 1,
types: [2],
formats: [5],
useSRGBBuffers: [false],
creationFlags: [0],
labels: [`${this.name} Position History`],
},
};
this._accumulationHistoryTexture = textureManager.createRenderTargetTexture(`${this.name} Accumulation History`, accumulationHistoryCreationOptions, this._accumulationHistoryTexture);
this._positionHistoryTexture = textureManager.createRenderTargetTexture(`${this.name} Position History`, positionHistoryCreationOptions, this._positionHistoryTexture);
const pass = this._frameGraph.addRenderPass(this.name);
pass.addDependencies(this.sourceTexture);
pass.addDependencies(this.velocityTexture);
pass.addDependencies(this.positionTexture);
pass.addDependencies(this._accumulationHistoryTexture);
pass.addDependencies(this._positionHistoryTexture);
// Accumulation writes directly to the history handle (current frame write side)
// so oldAccumulationSampler reads previous-frame data automatically.
// A dedicated copy pass then exposes a stable current-frame outputTexture.
pass.setRenderTarget(this._accumulationHistoryTexture);
pass.setExecuteFunc((context) => {
context.setTextureSamplingMode(this.sourceTexture, 1);
context.setTextureSamplingMode(this.velocityTexture, 1);
context.setTextureSamplingMode(this.positionTexture, 1);
const remanence = this.isMoving ? this.remanence : 0.99;
this._accumulationParams.set(remanence, this.reset ? 1.0 : 0.0, this.voxelizationTask?.voxelGridSize ?? this.voxelGridSize, 0.0);
context.applyFullScreenEffect(this._postProcessDrawWrapper, () => {
const effect = this._postProcessDrawWrapper.effect;
context.bindTextureHandle(effect, "spatialBlurSampler", this.sourceTexture);
context.bindTextureHandle(effect, "oldAccumulationSampler", this._accumulationHistoryTexture);
context.bindTextureHandle(effect, "prevPositionSampler", this._positionHistoryTexture);
context.bindTextureHandle(effect, "motionSampler", this.velocityTexture);
context.bindTextureHandle(effect, "positionSampler", this.positionTexture);
effect.setVector4("accumulationParameters", this._accumulationParams);
this.postProcess.bind();
}, undefined, false, false, true);
this.reset = false;
this.isMoving = false;
});
const copyPositionToHistoryPass = this._frameGraph.addRenderPass(`${this.name} CopyPositionToHistory`);
copyPositionToHistoryPass.addDependencies(this.positionTexture);
copyPositionToHistoryPass.setRenderTarget(this._positionHistoryTexture);
copyPositionToHistoryPass.setExecuteFunc((context) => {
context.copyTexture(this.positionTexture);
});
const copyAccumulationToOutputPass = this._frameGraph.addRenderPass(`${this.name} CopyAccumulationToOutput`);
copyAccumulationToOutputPass.addDependencies(this._accumulationHistoryTexture);
copyAccumulationToOutputPass.setRenderTarget(this.outputTexture);
copyAccumulationToOutputPass.setExecuteFunc((context) => {
context.copyTexture(this._accumulationHistoryTexture);
});
}
dispose() {
this.postProcess.dispose();
super.dispose();
}
}
//# sourceMappingURL=iblShadowsAccumulationTask.js.map