@babylonjs/viewer
Version:
The Babylon Viewer aims to simplify a specific but common Babylon.js use case: loading, viewing, and interacting with a 3D model.
430 lines (424 loc) • 18 kB
JavaScript
import { C as Constants, M as Matrix } from './index-MZPybX0H.esm.js';
/**
* Configuration needed for prepass-capable materials
*/
class PrePassConfiguration {
constructor() {
/**
* Previous world matrices of meshes carrying this material
* Used for computing velocity
*/
this.previousWorldMatrices = {};
/**
* Previous bones of meshes carrying this material
* Used for computing velocity
*/
this.previousBones = {};
}
/**
* Add the required uniforms to the current list.
* @param uniforms defines the current uniform list.
*/
static AddUniforms(uniforms) {
uniforms.push("previousWorld", "previousViewProjection", "mPreviousBones");
}
/**
* Add the required samplers to the current list.
* @param samplers defines the current sampler list.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
static AddSamplers(samplers) {
// pass
}
/**
* Binds the material data.
* @param effect defines the effect to update
* @param scene defines the scene the material belongs to.
* @param mesh The mesh
* @param world World matrix of this mesh
* @param isFrozen Is the material frozen
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
bindForSubMesh(effect, scene, mesh, world, isFrozen) {
if (scene.prePassRenderer && scene.prePassRenderer.enabled && scene.prePassRenderer.currentRTisSceneRT) {
if (scene.prePassRenderer.getIndex(Constants.PREPASS_VELOCITY_TEXTURE_TYPE) !== -1 ||
scene.prePassRenderer.getIndex(Constants.PREPASS_VELOCITY_LINEAR_TEXTURE_TYPE) !== -1) {
if (!this.previousWorldMatrices[mesh.uniqueId]) {
this.previousWorldMatrices[mesh.uniqueId] = world.clone();
}
if (!this.previousViewProjection) {
this.previousViewProjection = scene.getTransformMatrix().clone();
this.currentViewProjection = scene.getTransformMatrix().clone();
}
const engine = scene.getEngine();
if (this.currentViewProjection.updateFlag !== scene.getTransformMatrix().updateFlag) {
// First update of the prepass configuration for this rendering pass
this._lastUpdateFrameId = engine.frameId;
this.previousViewProjection.copyFrom(this.currentViewProjection);
this.currentViewProjection.copyFrom(scene.getTransformMatrix());
}
else if (this._lastUpdateFrameId !== engine.frameId) {
// The scene transformation did not change from the previous frame (so no camera motion), we must update previousViewProjection accordingly
this._lastUpdateFrameId = engine.frameId;
this.previousViewProjection.copyFrom(this.currentViewProjection);
}
effect.setMatrix("previousWorld", this.previousWorldMatrices[mesh.uniqueId]);
effect.setMatrix("previousViewProjection", this.previousViewProjection);
this.previousWorldMatrices[mesh.uniqueId] = world.clone();
}
}
}
}
/**
* Type of clear operation to perform on a geometry texture.
*/
var GeometryRenderingTextureClearType;
(function (GeometryRenderingTextureClearType) {
/**
* Clear the texture with zero.
*/
GeometryRenderingTextureClearType[GeometryRenderingTextureClearType["Zero"] = 0] = "Zero";
/**
* Clear the texture with one.
*/
GeometryRenderingTextureClearType[GeometryRenderingTextureClearType["One"] = 1] = "One";
/**
* Clear the texture with the maximum view Z value.
*/
GeometryRenderingTextureClearType[GeometryRenderingTextureClearType["MaxViewZ"] = 2] = "MaxViewZ";
/**
* Do not clear the texture.
*/
GeometryRenderingTextureClearType[GeometryRenderingTextureClearType["NoClear"] = 3] = "NoClear";
})(GeometryRenderingTextureClearType || (GeometryRenderingTextureClearType = {}));
/**
* Helper class to manage geometry rendering.
*/
class MaterialHelperGeometryRendering {
/**
* Creates a new geometry rendering configuration.
* @param renderPassId Render pass id the configuration is created for.
* @returns The created configuration.
*/
static CreateConfiguration(renderPassId) {
MaterialHelperGeometryRendering._Configurations[renderPassId] = {
defines: {},
previousWorldMatrices: {},
previousViewProjection: Matrix.Zero(),
currentViewProjection: Matrix.Zero(),
previousBones: {},
lastUpdateFrameId: -1,
excludedSkinnedMesh: [],
reverseCulling: false,
};
return MaterialHelperGeometryRendering._Configurations[renderPassId];
}
/**
* Deletes a geometry rendering configuration.
* @param renderPassId The render pass id of the configuration to delete.
*/
static DeleteConfiguration(renderPassId) {
delete MaterialHelperGeometryRendering._Configurations[renderPassId];
}
/**
* Gets a geometry rendering configuration.
* @param renderPassId The render pass id of the configuration to get.
* @returns The configuration.
*/
static GetConfiguration(renderPassId) {
return MaterialHelperGeometryRendering._Configurations[renderPassId];
}
/**
* Adds uniforms and samplers for geometry rendering.
* @param uniforms The array of uniforms to add to.
* @param _samplers The array of samplers to add to.
*/
static AddUniformsAndSamplers(uniforms, _samplers) {
uniforms.push("previousWorld", "previousViewProjection", "mPreviousBones");
}
/**
* Marks a list of meshes as dirty for geometry rendering.
* @param renderPassId The render pass id the meshes are marked as dirty for.
* @param meshes The list of meshes to mark as dirty.
*/
static MarkAsDirty(renderPassId, meshes) {
for (const mesh of meshes) {
if (!mesh.subMeshes) {
continue;
}
for (const subMesh of mesh.subMeshes) {
subMesh._removeDrawWrapper(renderPassId);
}
}
}
/**
* Prepares defines for geometry rendering.
* @param renderPassId The render pass id the defines are prepared for.
* @param mesh The mesh the defines are prepared for.
* @param defines The defines to update according to the geometry rendering configuration.
*/
static PrepareDefines(renderPassId, mesh, defines) {
if (!defines._arePrePassDirty) {
return;
}
const configuration = MaterialHelperGeometryRendering._Configurations[renderPassId];
if (!configuration) {
return;
}
defines["PREPASS"] = true;
let numMRT = 0;
for (let i = 0; i < MaterialHelperGeometryRendering.GeometryTextureDescriptions.length; i++) {
const geometryTextureDescription = MaterialHelperGeometryRendering.GeometryTextureDescriptions[i];
const defineName = geometryTextureDescription.define;
const defineIndex = geometryTextureDescription.defineIndex;
const index = configuration.defines[defineIndex];
if (index !== undefined) {
defines[defineName] = true;
defines[defineIndex] = index;
numMRT++;
}
else {
defines[defineName] = false;
delete defines[defineIndex];
}
}
defines["SCENE_MRT_COUNT"] = numMRT;
defines["BONES_VELOCITY_ENABLED"] =
mesh.useBones && mesh.computeBonesUsingShaders && mesh.skeleton && !mesh.skeleton.isUsingTextureForMatrices && configuration.excludedSkinnedMesh.indexOf(mesh) === -1;
}
/**
* Binds geometry rendering data for a mesh.
* @param renderPassId The render pass id the geometry rendering data is bound for.
* @param effect The effect to bind the geometry rendering data to.
* @param mesh The mesh to bind the geometry rendering data for.
* @param world The world matrix of the mesh.
* @param material The material of the mesh.
*/
static Bind(renderPassId, effect, mesh, world, material) {
const configuration = MaterialHelperGeometryRendering._Configurations[renderPassId];
if (!configuration) {
return;
}
const scene = mesh.getScene();
const engine = scene.getEngine();
if (configuration.reverseCulling) {
engine.setStateCullFaceType(scene._mirroredCameraPosition ? material.cullBackFaces : !material.cullBackFaces);
}
if (configuration.defines["PREPASS_VELOCITY_INDEX"] !== undefined || configuration.defines["PREPASS_VELOCITY_LINEAR_INDEX"] !== undefined) {
if (!configuration.previousWorldMatrices[mesh.uniqueId]) {
configuration.previousWorldMatrices[mesh.uniqueId] = world.clone();
}
if (!configuration.previousViewProjection) {
configuration.previousViewProjection = scene.getTransformMatrix().clone();
configuration.currentViewProjection = scene.getTransformMatrix().clone();
}
if (configuration.currentViewProjection.updateFlag !== scene.getTransformMatrix().updateFlag) {
// First update of the prepass configuration for this rendering pass
configuration.lastUpdateFrameId = engine.frameId;
configuration.previousViewProjection.copyFrom(configuration.currentViewProjection);
configuration.currentViewProjection.copyFrom(scene.getTransformMatrix());
}
else if (configuration.lastUpdateFrameId !== engine.frameId) {
// The scene transformation did not change from the previous frame (so no camera motion), we must update previousViewProjection accordingly
configuration.lastUpdateFrameId = engine.frameId;
configuration.previousViewProjection.copyFrom(configuration.currentViewProjection);
}
effect.setMatrix("previousWorld", configuration.previousWorldMatrices[mesh.uniqueId]);
effect.setMatrix("previousViewProjection", configuration.previousViewProjection);
configuration.previousWorldMatrices[mesh.uniqueId] = world.clone();
if (mesh.useBones && mesh.computeBonesUsingShaders && mesh.skeleton) {
const skeleton = mesh.skeleton;
if (!skeleton.isUsingTextureForMatrices || effect.getUniformIndex("boneTextureInfo") === -1) {
const matrices = skeleton.getTransformMatrices(mesh);
if (matrices) {
if (!configuration.previousBones[mesh.uniqueId]) {
configuration.previousBones[mesh.uniqueId] = matrices.slice();
}
effect.setMatrices("mPreviousBones", configuration.previousBones[mesh.uniqueId]);
configuration.previousBones[mesh.uniqueId].set(matrices);
}
}
}
}
}
}
/**
* Descriptions of the geometry textures.
*/
MaterialHelperGeometryRendering.GeometryTextureDescriptions = [
{
type: Constants.PREPASS_IRRADIANCE_LEGACY_TEXTURE_TYPE,
name: "IrradianceLegacy",
clearType: 0 /* GeometryRenderingTextureClearType.Zero */,
define: "PREPASS_IRRADIANCE_LEGACY",
defineIndex: "PREPASS_IRRADIANCE_LEGACY_INDEX",
},
{
type: Constants.PREPASS_POSITION_TEXTURE_TYPE,
name: "WorldPosition",
clearType: 0 /* GeometryRenderingTextureClearType.Zero */,
define: "PREPASS_POSITION",
defineIndex: "PREPASS_POSITION_INDEX",
},
{
type: Constants.PREPASS_VELOCITY_TEXTURE_TYPE,
name: "Velocity",
clearType: 0 /* GeometryRenderingTextureClearType.Zero */,
define: "PREPASS_VELOCITY",
defineIndex: "PREPASS_VELOCITY_INDEX",
},
{
type: Constants.PREPASS_REFLECTIVITY_TEXTURE_TYPE,
name: "Reflectivity",
clearType: 0 /* GeometryRenderingTextureClearType.Zero */,
define: "PREPASS_REFLECTIVITY",
defineIndex: "PREPASS_REFLECTIVITY_INDEX",
},
{
type: Constants.PREPASS_DEPTH_TEXTURE_TYPE,
name: "ViewDepth",
clearType: 2 /* GeometryRenderingTextureClearType.MaxViewZ */,
define: "PREPASS_DEPTH",
defineIndex: "PREPASS_DEPTH_INDEX",
},
{
type: Constants.PREPASS_NORMAL_TEXTURE_TYPE,
name: "ViewNormal",
clearType: 0 /* GeometryRenderingTextureClearType.Zero */,
define: "PREPASS_NORMAL",
defineIndex: "PREPASS_NORMAL_INDEX",
},
{
type: Constants.PREPASS_ALBEDO_SQRT_TEXTURE_TYPE,
name: "AlbedoSqrt",
clearType: 0 /* GeometryRenderingTextureClearType.Zero */,
define: "PREPASS_ALBEDO_SQRT",
defineIndex: "PREPASS_ALBEDO_SQRT_INDEX",
},
{
type: Constants.PREPASS_WORLD_NORMAL_TEXTURE_TYPE,
name: "WorldNormal",
clearType: 0 /* GeometryRenderingTextureClearType.Zero */,
define: "PREPASS_WORLD_NORMAL",
defineIndex: "PREPASS_WORLD_NORMAL_INDEX",
},
{
type: Constants.PREPASS_LOCAL_POSITION_TEXTURE_TYPE,
name: "LocalPosition",
clearType: 0 /* GeometryRenderingTextureClearType.Zero */,
define: "PREPASS_LOCAL_POSITION",
defineIndex: "PREPASS_LOCAL_POSITION_INDEX",
},
{
type: Constants.PREPASS_SCREENSPACE_DEPTH_TEXTURE_TYPE,
name: "ScreenDepth",
clearType: 1 /* GeometryRenderingTextureClearType.One */,
define: "PREPASS_SCREENSPACE_DEPTH",
defineIndex: "PREPASS_SCREENSPACE_DEPTH_INDEX",
},
{
type: Constants.PREPASS_VELOCITY_LINEAR_TEXTURE_TYPE,
name: "LinearVelocity",
clearType: 0 /* GeometryRenderingTextureClearType.Zero */,
define: "PREPASS_VELOCITY_LINEAR",
defineIndex: "PREPASS_VELOCITY_LINEAR_INDEX",
},
{
type: Constants.PREPASS_ALBEDO_TEXTURE_TYPE,
name: "Albedo",
clearType: 0 /* GeometryRenderingTextureClearType.Zero */,
define: "PREPASS_ALBEDO",
defineIndex: "PREPASS_ALBEDO_INDEX",
},
{
type: Constants.PREPASS_NORMALIZED_VIEW_DEPTH_TEXTURE_TYPE,
name: "NormalizedViewDepth",
clearType: 1 /* GeometryRenderingTextureClearType.One */,
define: "PREPASS_NORMALIZED_VIEW_DEPTH",
defineIndex: "PREPASS_NORMALIZED_VIEW_DEPTH_INDEX",
},
{
type: Constants.PREPASS_COLOR_TEXTURE_TYPE,
name: "Color",
clearType: 3 /* GeometryRenderingTextureClearType.NoClear */,
define: "PREPASS_COLOR",
defineIndex: "PREPASS_COLOR_INDEX",
},
{
type: Constants.PREPASS_IRRADIANCE_TEXTURE_TYPE,
name: "Irradiance",
clearType: 0 /* GeometryRenderingTextureClearType.Zero */,
define: "PREPASS_IRRADIANCE",
defineIndex: "PREPASS_IRRADIANCE_INDEX",
},
];
MaterialHelperGeometryRendering._Configurations = {};
/**
* Mixin to add UV defines to your material defines
* @internal
*/
function UVDefinesMixin(base) {
return class extends base {
constructor() {
super(...arguments);
this.MAINUV1 = false;
this.MAINUV2 = false;
this.MAINUV3 = false;
this.MAINUV4 = false;
this.MAINUV5 = false;
this.MAINUV6 = false;
this.UV1 = false;
this.UV2 = false;
this.UV3 = false;
this.UV4 = false;
this.UV5 = false;
this.UV6 = false;
}
};
}
/**
* Mixin to add prepass defines to your material defines
* @internal
*/
function PrepassDefinesMixin(base) {
return class extends base {
constructor() {
super(...arguments);
this.PREPASS = false;
this.PREPASS_COLOR = false;
this.PREPASS_COLOR_INDEX = -1;
this.PREPASS_IRRADIANCE_LEGACY = false;
this.PREPASS_IRRADIANCE_LEGACY_INDEX = -1;
this.PREPASS_IRRADIANCE = false;
this.PREPASS_IRRADIANCE_INDEX = -1;
this.PREPASS_ALBEDO = false;
this.PREPASS_ALBEDO_INDEX = -1;
this.PREPASS_ALBEDO_SQRT = false;
this.PREPASS_ALBEDO_SQRT_INDEX = -1;
this.PREPASS_DEPTH = false;
this.PREPASS_DEPTH_INDEX = -1;
this.PREPASS_SCREENSPACE_DEPTH = false;
this.PREPASS_SCREENSPACE_DEPTH_INDEX = -1;
this.PREPASS_NORMALIZED_VIEW_DEPTH = false;
this.PREPASS_NORMALIZED_VIEW_DEPTH_INDEX = -1;
this.PREPASS_NORMAL = false;
this.PREPASS_NORMAL_INDEX = -1;
this.PREPASS_NORMAL_WORLDSPACE = false;
this.PREPASS_WORLD_NORMAL = false;
this.PREPASS_WORLD_NORMAL_INDEX = -1;
this.PREPASS_POSITION = false;
this.PREPASS_POSITION_INDEX = -1;
this.PREPASS_LOCAL_POSITION = false;
this.PREPASS_LOCAL_POSITION_INDEX = -1;
this.PREPASS_VELOCITY = false;
this.PREPASS_VELOCITY_INDEX = -1;
this.PREPASS_VELOCITY_LINEAR = false;
this.PREPASS_VELOCITY_LINEAR_INDEX = -1;
this.PREPASS_REFLECTIVITY = false;
this.PREPASS_REFLECTIVITY_INDEX = -1;
this.SCENE_MRT_COUNT = 0;
}
};
}
export { MaterialHelperGeometryRendering as M, PrePassConfiguration as P, UVDefinesMixin as U, PrepassDefinesMixin as a };
//# sourceMappingURL=prepass.defines-D50C_zO6.esm.js.map