@zephyr3d/scene 0.1.0
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.
- package/dist/animation/animation.js +173 -0
- package/dist/animation/animation.js.map +1 -0
- package/dist/animation/animationset.js +95 -0
- package/dist/animation/animationset.js.map +1 -0
- package/dist/animation/animationtrack.js +38 -0
- package/dist/animation/animationtrack.js.map +1 -0
- package/dist/animation/eulerrotationtrack.js +33 -0
- package/dist/animation/eulerrotationtrack.js.map +1 -0
- package/dist/animation/rotationtrack.js +37 -0
- package/dist/animation/rotationtrack.js.map +1 -0
- package/dist/animation/scaletrack.js +36 -0
- package/dist/animation/scaletrack.js.map +1 -0
- package/dist/animation/skeleton.js +97 -0
- package/dist/animation/skeleton.js.map +1 -0
- package/dist/animation/translationtrack.js +36 -0
- package/dist/animation/translationtrack.js.map +1 -0
- package/dist/animation/usertrack.js +47 -0
- package/dist/animation/usertrack.js.map +1 -0
- package/dist/app.js +173 -0
- package/dist/app.js.map +1 -0
- package/dist/asset/assetmanager.js +476 -0
- package/dist/asset/assetmanager.js.map +1 -0
- package/dist/asset/builtin.js +373 -0
- package/dist/asset/builtin.js.map +1 -0
- package/dist/asset/loaders/dds/dds.js +472 -0
- package/dist/asset/loaders/dds/dds.js.map +1 -0
- package/dist/asset/loaders/dds/dds_loader.js +38 -0
- package/dist/asset/loaders/dds/dds_loader.js.map +1 -0
- package/dist/asset/loaders/gltf/gltf_loader.js +981 -0
- package/dist/asset/loaders/gltf/gltf_loader.js.map +1 -0
- package/dist/asset/loaders/gltf/helpers.js +314 -0
- package/dist/asset/loaders/gltf/helpers.js.map +1 -0
- package/dist/asset/loaders/hdr/hdr.js +175 -0
- package/dist/asset/loaders/hdr/hdr.js.map +1 -0
- package/dist/asset/loaders/image/tga_Loader.js +117 -0
- package/dist/asset/loaders/image/tga_Loader.js.map +1 -0
- package/dist/asset/loaders/image/webimage_loader.js +50 -0
- package/dist/asset/loaders/image/webimage_loader.js.map +1 -0
- package/dist/asset/loaders/loader.js +45 -0
- package/dist/asset/loaders/loader.js.map +1 -0
- package/dist/asset/model.js +264 -0
- package/dist/asset/model.js.map +1 -0
- package/dist/blitter/blitter.js +389 -0
- package/dist/blitter/blitter.js.map +1 -0
- package/dist/blitter/box.js +118 -0
- package/dist/blitter/box.js.map +1 -0
- package/dist/blitter/copy.js +22 -0
- package/dist/blitter/copy.js.map +1 -0
- package/dist/blitter/depthlimitedgaussion.js +166 -0
- package/dist/blitter/depthlimitedgaussion.js.map +1 -0
- package/dist/blitter/gaussianblur.js +229 -0
- package/dist/blitter/gaussianblur.js.map +1 -0
- package/dist/camera/base.js +90 -0
- package/dist/camera/base.js.map +1 -0
- package/dist/camera/camera.js +358 -0
- package/dist/camera/camera.js.map +1 -0
- package/dist/camera/fps.js +246 -0
- package/dist/camera/fps.js.map +1 -0
- package/dist/camera/orbit.js +157 -0
- package/dist/camera/orbit.js.map +1 -0
- package/dist/camera/orthocamera.js +126 -0
- package/dist/camera/orthocamera.js.map +1 -0
- package/dist/camera/perspectivecamera.js +133 -0
- package/dist/camera/perspectivecamera.js.map +1 -0
- package/dist/index.d.ts +8402 -0
- package/dist/index.js +87 -0
- package/dist/index.js.map +1 -0
- package/dist/input/inputmgr.js +242 -0
- package/dist/input/inputmgr.js.map +1 -0
- package/dist/material/blinn.js +75 -0
- package/dist/material/blinn.js.map +1 -0
- package/dist/material/grassmaterial.js +221 -0
- package/dist/material/grassmaterial.js.map +1 -0
- package/dist/material/lambert.js +52 -0
- package/dist/material/lambert.js.map +1 -0
- package/dist/material/lightmodel.js +2074 -0
- package/dist/material/lightmodel.js.map +1 -0
- package/dist/material/lit.js +578 -0
- package/dist/material/lit.js.map +1 -0
- package/dist/material/material.js +458 -0
- package/dist/material/material.js.map +1 -0
- package/dist/material/meshmaterial.js +311 -0
- package/dist/material/meshmaterial.js.map +1 -0
- package/dist/material/mixins/albedocolor.js +130 -0
- package/dist/material/mixins/albedocolor.js.map +1 -0
- package/dist/material/mixins/texture.js +110 -0
- package/dist/material/mixins/texture.js.map +1 -0
- package/dist/material/mixins/vertexcolor.js +45 -0
- package/dist/material/mixins/vertexcolor.js.map +1 -0
- package/dist/material/pbr.js +27 -0
- package/dist/material/pbr.js.map +1 -0
- package/dist/material/standard.js +282 -0
- package/dist/material/standard.js.map +1 -0
- package/dist/material/terrainlightmodel.js +259 -0
- package/dist/material/terrainlightmodel.js.map +1 -0
- package/dist/material/terrainmaterial.js +139 -0
- package/dist/material/terrainmaterial.js.map +1 -0
- package/dist/material/unlit.js +29 -0
- package/dist/material/unlit.js.map +1 -0
- package/dist/posteffect/bloom.js +398 -0
- package/dist/posteffect/bloom.js.map +1 -0
- package/dist/posteffect/compositor.js +264 -0
- package/dist/posteffect/compositor.js.map +1 -0
- package/dist/posteffect/fxaa.js +291 -0
- package/dist/posteffect/fxaa.js.map +1 -0
- package/dist/posteffect/grayscale.js +87 -0
- package/dist/posteffect/grayscale.js.map +1 -0
- package/dist/posteffect/posteffect.js +165 -0
- package/dist/posteffect/posteffect.js.map +1 -0
- package/dist/posteffect/sao.js +327 -0
- package/dist/posteffect/sao.js.map +1 -0
- package/dist/posteffect/tonemap.js +112 -0
- package/dist/posteffect/tonemap.js.map +1 -0
- package/dist/posteffect/water.js +535 -0
- package/dist/posteffect/water.js.map +1 -0
- package/dist/render/clipmap.js +462 -0
- package/dist/render/clipmap.js.map +1 -0
- package/dist/render/cluster_light.js +329 -0
- package/dist/render/cluster_light.js.map +1 -0
- package/dist/render/cull_visitor.js +124 -0
- package/dist/render/cull_visitor.js.map +1 -0
- package/dist/render/depth_pass.js +47 -0
- package/dist/render/depth_pass.js.map +1 -0
- package/dist/render/envlight.js +282 -0
- package/dist/render/envlight.js.map +1 -0
- package/dist/render/forward.js +186 -0
- package/dist/render/forward.js.map +1 -0
- package/dist/render/forward_pass.js +137 -0
- package/dist/render/forward_pass.js.map +1 -0
- package/dist/render/helper.js +38 -0
- package/dist/render/helper.js.map +1 -0
- package/dist/render/primitive.js +246 -0
- package/dist/render/primitive.js.map +1 -0
- package/dist/render/render_queue.js +163 -0
- package/dist/render/render_queue.js.map +1 -0
- package/dist/render/renderpass.js +151 -0
- package/dist/render/renderpass.js.map +1 -0
- package/dist/render/renderscheme.js +61 -0
- package/dist/render/renderscheme.js.map +1 -0
- package/dist/render/scatteringlut.js +634 -0
- package/dist/render/scatteringlut.js.map +1 -0
- package/dist/render/shadowmap_pass.js +70 -0
- package/dist/render/shadowmap_pass.js.map +1 -0
- package/dist/render/sky.js +881 -0
- package/dist/render/sky.js.map +1 -0
- package/dist/render/temporalcache.js +222 -0
- package/dist/render/temporalcache.js.map +1 -0
- package/dist/render/watermesh.js +835 -0
- package/dist/render/watermesh.js.map +1 -0
- package/dist/scene/environment.js +146 -0
- package/dist/scene/environment.js.map +1 -0
- package/dist/scene/graph_node.js +69 -0
- package/dist/scene/graph_node.js.map +1 -0
- package/dist/scene/light.js +436 -0
- package/dist/scene/light.js.map +1 -0
- package/dist/scene/mesh.js +215 -0
- package/dist/scene/mesh.js.map +1 -0
- package/dist/scene/model.js +111 -0
- package/dist/scene/model.js.map +1 -0
- package/dist/scene/octree.js +651 -0
- package/dist/scene/octree.js.map +1 -0
- package/dist/scene/octree_update_visitor.js +16 -0
- package/dist/scene/octree_update_visitor.js.map +1 -0
- package/dist/scene/raycast_visitor.js +72 -0
- package/dist/scene/raycast_visitor.js.map +1 -0
- package/dist/scene/scene.js +225 -0
- package/dist/scene/scene.js.map +1 -0
- package/dist/scene/scene_node.js +299 -0
- package/dist/scene/scene_node.js.map +1 -0
- package/dist/scene/terrain/grass.js +277 -0
- package/dist/scene/terrain/grass.js.map +1 -0
- package/dist/scene/terrain/heightfield.js +391 -0
- package/dist/scene/terrain/heightfield.js.map +1 -0
- package/dist/scene/terrain/patch.js +530 -0
- package/dist/scene/terrain/patch.js.map +1 -0
- package/dist/scene/terrain/quadtree.js +430 -0
- package/dist/scene/terrain/quadtree.js.map +1 -0
- package/dist/scene/terrain/terrain.js +258 -0
- package/dist/scene/terrain/terrain.js.map +1 -0
- package/dist/scene/xform.js +224 -0
- package/dist/scene/xform.js.map +1 -0
- package/dist/shaders/builtins.js +110 -0
- package/dist/shaders/builtins.js.map +1 -0
- package/dist/shaders/framework.js +709 -0
- package/dist/shaders/framework.js.map +1 -0
- package/dist/shaders/lighting.js +335 -0
- package/dist/shaders/lighting.js.map +1 -0
- package/dist/shaders/misc.js +405 -0
- package/dist/shaders/misc.js.map +1 -0
- package/dist/shaders/noise.js +157 -0
- package/dist/shaders/noise.js.map +1 -0
- package/dist/shaders/pbr.js +132 -0
- package/dist/shaders/pbr.js.map +1 -0
- package/dist/shaders/shadow.js +642 -0
- package/dist/shaders/shadow.js.map +1 -0
- package/dist/shaders/water.js +630 -0
- package/dist/shaders/water.js.map +1 -0
- package/dist/shadow/esm.js +235 -0
- package/dist/shadow/esm.js.map +1 -0
- package/dist/shadow/pcf_opt.js +182 -0
- package/dist/shadow/pcf_opt.js.map +1 -0
- package/dist/shadow/pcf_pd.js +190 -0
- package/dist/shadow/pcf_pd.js.map +1 -0
- package/dist/shadow/shadow_impl.js +15 -0
- package/dist/shadow/shadow_impl.js.map +1 -0
- package/dist/shadow/shadowmapper.js +709 -0
- package/dist/shadow/shadowmapper.js.map +1 -0
- package/dist/shadow/ssm.js +194 -0
- package/dist/shadow/ssm.js.map +1 -0
- package/dist/shadow/vsm.js +298 -0
- package/dist/shadow/vsm.js.map +1 -0
- package/dist/shapes/box.js +313 -0
- package/dist/shapes/box.js.map +1 -0
- package/dist/shapes/cylinder.js +74 -0
- package/dist/shapes/cylinder.js.map +1 -0
- package/dist/shapes/plane.js +48 -0
- package/dist/shapes/plane.js.map +1 -0
- package/dist/shapes/shape.js +33 -0
- package/dist/shapes/shape.js.map +1 -0
- package/dist/shapes/sphere.js +91 -0
- package/dist/shapes/sphere.js.map +1 -0
- package/dist/shapes/torus.js +100 -0
- package/dist/shapes/torus.js.map +1 -0
- package/dist/utility/aabbtree.js +390 -0
- package/dist/utility/aabbtree.js.map +1 -0
- package/dist/utility/bounding_volume.js +78 -0
- package/dist/utility/bounding_volume.js.map +1 -0
- package/dist/utility/panorama.js +163 -0
- package/dist/utility/panorama.js.map +1 -0
- package/dist/utility/pmrem.js +345 -0
- package/dist/utility/pmrem.js.map +1 -0
- package/dist/utility/shprojection.js +448 -0
- package/dist/utility/shprojection.js.map +1 -0
- package/dist/values.js +48 -0
- package/dist/values.js.map +1 -0
- package/package.json +70 -0
|
@@ -0,0 +1,709 @@
|
|
|
1
|
+
import { Matrix4x4, Frustum, Vector3, Vector4, CubeFace, AABB } from '@zephyr3d/base';
|
|
2
|
+
import { Camera } from '../camera/camera.js';
|
|
3
|
+
import { SSM } from './ssm.js';
|
|
4
|
+
import { ESM } from './esm.js';
|
|
5
|
+
import { VSM } from './vsm.js';
|
|
6
|
+
import { PCFPD } from './pcf_pd.js';
|
|
7
|
+
import { PCFOPT } from './pcf_opt.js';
|
|
8
|
+
import { nonLinearDepthToLinearNormalized } from '../shaders/misc.js';
|
|
9
|
+
import { ShaderFramework } from '../shaders/framework.js';
|
|
10
|
+
import { Application } from '../app.js';
|
|
11
|
+
import '../scene/octree.js';
|
|
12
|
+
import { LIGHT_TYPE_NONE, LIGHT_TYPE_DIRECTIONAL } from '../values.js';
|
|
13
|
+
import '../material/material.js';
|
|
14
|
+
import '@zephyr3d/device';
|
|
15
|
+
import '../render/scatteringlut.js';
|
|
16
|
+
import '../material/lambert.js';
|
|
17
|
+
import '../material/blinn.js';
|
|
18
|
+
import '../material/unlit.js';
|
|
19
|
+
import '../material/lightmodel.js';
|
|
20
|
+
import '../render/forward.js';
|
|
21
|
+
import '../render/sky.js';
|
|
22
|
+
import '../render/clipmap.js';
|
|
23
|
+
import { TemporalCache } from '../render/temporalcache.js';
|
|
24
|
+
import '../render/watermesh.js';
|
|
25
|
+
|
|
26
|
+
const tmpMatrix = new Matrix4x4();
|
|
27
|
+
const tmpFrustum = new Frustum(Matrix4x4.identity());
|
|
28
|
+
// const zeroPosition = Vector3.zero();
|
|
29
|
+
/**
|
|
30
|
+
* The shadow map generator
|
|
31
|
+
* @public
|
|
32
|
+
*/ class ShadowMapper {
|
|
33
|
+
/** @internal */ static _snapMatrix = new Matrix4x4();
|
|
34
|
+
/** @internal */ static _target = new Vector3();
|
|
35
|
+
/** @internal */ static _up = new Vector3();
|
|
36
|
+
/** @internal */ static _frustumMin = new Vector3();
|
|
37
|
+
/** @internal */ static _frustumMax = new Vector3();
|
|
38
|
+
/** @internal */ static _frustumCenter = new Vector3();
|
|
39
|
+
/** @internal */ static _lightCameras = new WeakMap();
|
|
40
|
+
/** @internal */ static _shadowMapParams = [];
|
|
41
|
+
/** @internal */ _light;
|
|
42
|
+
/** @internal */ _config;
|
|
43
|
+
/** @internal */ _resourceDirty;
|
|
44
|
+
/** @internal */ _shadowMode;
|
|
45
|
+
/** @internal */ _shadowDistance;
|
|
46
|
+
/** @internal */ _impl;
|
|
47
|
+
/** @internal */ _pdSampleCount;
|
|
48
|
+
/** @internal */ _pdSampleRadius;
|
|
49
|
+
/** @internal */ _pcfKernelSize;
|
|
50
|
+
/** @internal */ _vsmBlurKernelSize;
|
|
51
|
+
/** @internal */ _vsmBlurRadius;
|
|
52
|
+
/** @internal */ _vsmDarkness;
|
|
53
|
+
/** @internal */ _esmBlur;
|
|
54
|
+
/** @internal */ _esmBlurKernelSize;
|
|
55
|
+
/** @internal */ _esmBlurRadius;
|
|
56
|
+
/** @internal */ _esmDepthScale;
|
|
57
|
+
/**
|
|
58
|
+
* Creates an instance of ShadowMapper
|
|
59
|
+
* @param light - The light that is used to generate shadow map
|
|
60
|
+
*/ constructor(light){
|
|
61
|
+
this._light = light;
|
|
62
|
+
this._config = {
|
|
63
|
+
shadowMapSize: 1024,
|
|
64
|
+
numCascades: 1,
|
|
65
|
+
splitLambda: 0.5,
|
|
66
|
+
depthBias: 0.05,
|
|
67
|
+
normalBias: 0.05,
|
|
68
|
+
nearClip: 1
|
|
69
|
+
};
|
|
70
|
+
this._resourceDirty = true;
|
|
71
|
+
this._shadowMode = 'hard';
|
|
72
|
+
this._shadowDistance = 2000;
|
|
73
|
+
this._impl = null;
|
|
74
|
+
this._pdSampleCount = 12;
|
|
75
|
+
this._pdSampleRadius = 4;
|
|
76
|
+
this._pcfKernelSize = 5;
|
|
77
|
+
this._vsmBlurKernelSize = 5;
|
|
78
|
+
this._vsmBlurRadius = 4;
|
|
79
|
+
this._vsmDarkness = 0.3;
|
|
80
|
+
this._esmBlur = true;
|
|
81
|
+
this._esmBlurKernelSize = 5;
|
|
82
|
+
this._esmBlurRadius = 4;
|
|
83
|
+
this._esmDepthScale = 200;
|
|
84
|
+
this.applyMode(this._shadowMode);
|
|
85
|
+
}
|
|
86
|
+
/** The light that is used to generate shadow map */ get light() {
|
|
87
|
+
return this._light;
|
|
88
|
+
}
|
|
89
|
+
/** Size of the shadow map */ get shadowMapSize() {
|
|
90
|
+
return this._config.shadowMapSize;
|
|
91
|
+
}
|
|
92
|
+
set shadowMapSize(num) {
|
|
93
|
+
if (!Number.isInteger(num) || num < 1) {
|
|
94
|
+
console.error(`invalid shadow map size: ${num}`);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (this._config.shadowMapSize !== num) {
|
|
98
|
+
this._config.shadowMapSize = num;
|
|
99
|
+
this._resourceDirty = true;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/** Maximum distance from the camera, shadow will not be rendered beyond this range */ get shadowDistance() {
|
|
103
|
+
return this._shadowDistance;
|
|
104
|
+
}
|
|
105
|
+
set shadowDistance(val) {
|
|
106
|
+
this._shadowDistance = Math.max(0, val);
|
|
107
|
+
}
|
|
108
|
+
/** Count of the cascades, The maximum value is 4 */ get numShadowCascades() {
|
|
109
|
+
return this._config.numCascades;
|
|
110
|
+
}
|
|
111
|
+
set numShadowCascades(num) {
|
|
112
|
+
if (num !== 1 && num !== 2 && num !== 3 && num !== 4) {
|
|
113
|
+
console.error(`invalid shadow cascade number: ${num}`);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (!this._light.isDirectionLight() && num > 1) {
|
|
117
|
+
console.error(`only directional light can have more than one shadow cascades`);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (num !== this._config.numCascades) {
|
|
121
|
+
this._config.numCascades = num;
|
|
122
|
+
this._resourceDirty = true;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/** The split lambda for cascaded shadow mapping */ get splitLambda() {
|
|
126
|
+
return this._config.splitLambda;
|
|
127
|
+
}
|
|
128
|
+
set splitLambda(val) {
|
|
129
|
+
if (this._config.splitLambda !== val) {
|
|
130
|
+
this._config.splitLambda = val;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/** Depth bias for the shadow map */ get depthBias() {
|
|
134
|
+
return this._config.depthBias;
|
|
135
|
+
}
|
|
136
|
+
set depthBias(val) {
|
|
137
|
+
this._config.depthBias = val;
|
|
138
|
+
}
|
|
139
|
+
/** Normal bias for the shadow map */ get normalBias() {
|
|
140
|
+
return this._config.normalBias;
|
|
141
|
+
}
|
|
142
|
+
set normalBias(val) {
|
|
143
|
+
this._config.normalBias = val;
|
|
144
|
+
}
|
|
145
|
+
/** Near clip plane */ get nearClip() {
|
|
146
|
+
return this._config.nearClip;
|
|
147
|
+
}
|
|
148
|
+
set nearClip(val) {
|
|
149
|
+
if (this._config.nearClip !== val) {
|
|
150
|
+
this._config.nearClip = val;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/** Shadow map mode */ get mode() {
|
|
154
|
+
return this._shadowMode;
|
|
155
|
+
}
|
|
156
|
+
set mode(mode) {
|
|
157
|
+
if (mode !== this._shadowMode) {
|
|
158
|
+
this._shadowMode = mode;
|
|
159
|
+
this.applyMode(this._shadowMode);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/** Generated shadow map */ /*
|
|
163
|
+
get shadowMap(): ShadowMapType {
|
|
164
|
+
return (this._impl.getShadowMap(this) ?? this._framebuffer?.getColorAttachments()[0] ?? null) as ShadowMapType;
|
|
165
|
+
}
|
|
166
|
+
*/ /** Sampler of the shadow map */ /*
|
|
167
|
+
get shadowMapSampler(): TextureSampler {
|
|
168
|
+
return this._impl.getShadowMapSampler(this);
|
|
169
|
+
}
|
|
170
|
+
*/ /** @internal */ getShaderHash(shadowMapParams) {
|
|
171
|
+
return `${shadowMapParams.impl.constructor.name}_${shadowMapParams.impl.getShaderHash()}_${shadowMapParams.lightType}_${shadowMapParams.shadowMap.target}_${Number(shadowMapParams.numShadowCascades > 1)}_${Number(Application.instance.device.getDeviceCaps().textureCaps.getTextureFormatInfo(shadowMapParams.shadowMap.format).filterable)}`;
|
|
172
|
+
}
|
|
173
|
+
/** Sample count for poisson disc PCF */ get pdSampleCount() {
|
|
174
|
+
return this._pdSampleCount;
|
|
175
|
+
}
|
|
176
|
+
set pdSampleCount(val) {
|
|
177
|
+
val = Math.min(Math.max(1, Number(val) >> 0), 64);
|
|
178
|
+
if (val !== this._pdSampleCount) {
|
|
179
|
+
this._pdSampleCount = val;
|
|
180
|
+
this.asPCFPD() && (this.asPCFPD().tapCount = this._pdSampleCount);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
/** Radius for poisson disc PCF */ get pdSampleRadius() {
|
|
184
|
+
return this._pdSampleRadius;
|
|
185
|
+
}
|
|
186
|
+
set pdSampleRadius(val) {
|
|
187
|
+
val = Math.max(0, Number(val) >> 0);
|
|
188
|
+
if (val !== this._pdSampleRadius) {
|
|
189
|
+
this._pdSampleRadius = val;
|
|
190
|
+
this.asPCFPD()?.setDepthScale(this._pdSampleRadius);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
/** Kernel size for optimized PCF */ get pcfKernelSize() {
|
|
194
|
+
return this._pcfKernelSize;
|
|
195
|
+
}
|
|
196
|
+
set pcfKernelSize(val) {
|
|
197
|
+
val = val !== 3 && val !== 5 && val !== 7 ? 5 : val;
|
|
198
|
+
if (val !== this._pcfKernelSize) {
|
|
199
|
+
this._pcfKernelSize = val;
|
|
200
|
+
this.asPCFOPT() && (this.asPCFOPT().kernelSize = this._pcfKernelSize);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
/** Kernel size of VSM */ get vsmBlurKernelSize() {
|
|
204
|
+
return this._vsmBlurKernelSize;
|
|
205
|
+
}
|
|
206
|
+
set vsmBlurKernelSize(val) {
|
|
207
|
+
val = Math.max(3, Number(val) >> 0) | 1;
|
|
208
|
+
if (val !== this._vsmBlurKernelSize) {
|
|
209
|
+
this._vsmBlurKernelSize = val;
|
|
210
|
+
this.asVSM() && (this.asVSM().kernelSize = this._vsmBlurKernelSize);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
/** Blur radius for VSM */ get vsmBlurRadius() {
|
|
214
|
+
return this._vsmBlurRadius;
|
|
215
|
+
}
|
|
216
|
+
set vsmBlurRadius(val) {
|
|
217
|
+
val = Math.max(0, Number(val) || 0);
|
|
218
|
+
if (val !== this._vsmBlurRadius) {
|
|
219
|
+
this._vsmBlurRadius = val;
|
|
220
|
+
this.asVSM() && (this.asVSM().blurSize = this._vsmBlurRadius);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
/** Darkness for VSM */ get vsmDarkness() {
|
|
224
|
+
return this._vsmDarkness;
|
|
225
|
+
}
|
|
226
|
+
set vsmDarkness(val) {
|
|
227
|
+
val = Math.min(0.999, Math.max(0, Number(val) || 0));
|
|
228
|
+
if (val !== this._vsmDarkness) {
|
|
229
|
+
this._vsmDarkness = val;
|
|
230
|
+
this.asVSM()?.setDepthScale(this._vsmDarkness);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
/** Whether to enable ESM blur */ get esmBlur() {
|
|
234
|
+
return this._esmBlur;
|
|
235
|
+
}
|
|
236
|
+
set esmBlur(val) {
|
|
237
|
+
if (!!val !== this.esmBlur) {
|
|
238
|
+
this._esmBlur = !!val;
|
|
239
|
+
this.asESM() && (this.asESM().blur = this._esmBlur);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
/** Kernel size for ESM */ get esmBlurKernelSize() {
|
|
243
|
+
return this._esmBlurKernelSize;
|
|
244
|
+
}
|
|
245
|
+
set esmBlurKernelSize(val) {
|
|
246
|
+
val = Math.max(3, Number(val) >> 0) | 1;
|
|
247
|
+
if (val !== this._esmBlurKernelSize) {
|
|
248
|
+
this._esmBlurKernelSize = val;
|
|
249
|
+
this.asESM() && (this.asESM().kernelSize = this._esmBlurKernelSize);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
/** Blur radius for ESM */ get esmBlurRadius() {
|
|
253
|
+
return this._esmBlurRadius;
|
|
254
|
+
}
|
|
255
|
+
set esmBlurRadius(val) {
|
|
256
|
+
val = Math.max(0, Number(val) || 0);
|
|
257
|
+
if (val !== this._esmBlurRadius) {
|
|
258
|
+
this._esmBlurRadius = val;
|
|
259
|
+
this.asESM() && (this.asESM().blurSize = this._esmBlurRadius);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
/** Depth scale for ESM */ get esmDepthScale() {
|
|
263
|
+
return this._esmDepthScale;
|
|
264
|
+
}
|
|
265
|
+
set esmDepthScale(val) {
|
|
266
|
+
val = Math.max(0, Number(val) || 0);
|
|
267
|
+
if (val !== this._esmDepthScale) {
|
|
268
|
+
this._esmDepthScale = val;
|
|
269
|
+
this.asESM()?.setDepthScale(this._esmDepthScale);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
/** @internal */ computeShadowMapDepth(shadowMapParams, scope) {
|
|
273
|
+
return this._impl.computeShadowMapDepth(shadowMapParams, scope);
|
|
274
|
+
}
|
|
275
|
+
/** @internal */ computeShadow(shadowMapParams, scope, shadowVertex, NdotL) {
|
|
276
|
+
return this._impl.computeShadow(shadowMapParams, scope, shadowVertex, NdotL);
|
|
277
|
+
}
|
|
278
|
+
/** @internal */ computeShadowCSM(shadowMapParams, scope, shadowVertex, NdotL, split) {
|
|
279
|
+
return this._impl.computeShadowCSM(shadowMapParams, scope, shadowVertex, NdotL, split);
|
|
280
|
+
}
|
|
281
|
+
/** @internal */ static releaseTemporalResources(ctx) {
|
|
282
|
+
if (ctx.shadowMapInfo) {
|
|
283
|
+
for (const k of ctx.shadowMapInfo.keys()){
|
|
284
|
+
const shadowMapParams = ctx.shadowMapInfo.get(k);
|
|
285
|
+
TemporalCache.releaseFramebuffer(shadowMapParams.shadowMapFramebuffer);
|
|
286
|
+
shadowMapParams.impl.releaseTemporalResources(shadowMapParams);
|
|
287
|
+
shadowMapParams.lightType = LIGHT_TYPE_NONE;
|
|
288
|
+
shadowMapParams.depthClampEnabled = false;
|
|
289
|
+
shadowMapParams.shaderHash = '';
|
|
290
|
+
shadowMapParams.numShadowCascades = 1;
|
|
291
|
+
shadowMapParams.shadowMapFramebuffer = null;
|
|
292
|
+
shadowMapParams.impl = null;
|
|
293
|
+
shadowMapParams.shadowMap = null;
|
|
294
|
+
shadowMapParams.shadowMapSampler = null;
|
|
295
|
+
shadowMapParams.implData = null;
|
|
296
|
+
this._shadowMapParams.push(shadowMapParams);
|
|
297
|
+
}
|
|
298
|
+
ctx.shadowMapInfo = null;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
/** @internal */ static computeShadowBias(shadowMapParams, scope, z, NdotL, linear) {
|
|
302
|
+
const pb = scope.$builder;
|
|
303
|
+
const depthBiasParam = ShaderFramework.getDepthBiasValues(scope);
|
|
304
|
+
if (shadowMapParams.lightType === LIGHT_TYPE_DIRECTIONAL) {
|
|
305
|
+
return pb.dot(pb.mul(depthBiasParam.xy, pb.vec2(1, pb.sub(1, NdotL))), pb.vec2(1, 1));
|
|
306
|
+
} else {
|
|
307
|
+
const nearFar = ShaderFramework.getShadowCameraParams(scope).xy;
|
|
308
|
+
const linearDepth = linear ? z : nonLinearDepthToLinearNormalized(scope, z, nearFar);
|
|
309
|
+
const biasScaleFactor = pb.mix(1, depthBiasParam.w, linearDepth);
|
|
310
|
+
return pb.dot(pb.mul(depthBiasParam.xy, pb.vec2(1, pb.sub(1, NdotL)), biasScaleFactor), pb.vec2(1, 1));
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
/** @internal */ static computeShadowBiasCSM(shadowMapParams, scope, NdotL, split) {
|
|
314
|
+
const pb = scope.$builder;
|
|
315
|
+
const depthBiasParam = ShaderFramework.getDepthBiasValues(scope);
|
|
316
|
+
const splitFlags = pb.vec4(pb.float(pb.equal(split, 0)), pb.float(pb.equal(split, 1)), pb.float(pb.equal(split, 2)), pb.float(pb.equal(split, 3)));
|
|
317
|
+
const depthBiasScale = pb.dot(ShaderFramework.getDepthBiasScales(scope), splitFlags);
|
|
318
|
+
return pb.dot(pb.mul(depthBiasParam.xy, pb.vec2(1, pb.sub(1, NdotL)), depthBiasScale), pb.vec2(1, 1));
|
|
319
|
+
}
|
|
320
|
+
/** @internal */ isTextureInvalid(texture, target, format, width, height) {
|
|
321
|
+
return texture && (texture.target !== target || texture.format !== format || texture.width !== width || texture.height !== height || texture.depth !== this.numShadowCascades);
|
|
322
|
+
}
|
|
323
|
+
/** @internal */ createTexture(target, format, width, height, depth) {
|
|
324
|
+
const device = Application.instance.device;
|
|
325
|
+
const options = {
|
|
326
|
+
samplerOptions: {
|
|
327
|
+
mipFilter: 'none'
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
switch(target){
|
|
331
|
+
case '2d':
|
|
332
|
+
return device.createTexture2D(format, width, height, options);
|
|
333
|
+
case 'cube':
|
|
334
|
+
return device.createCubeTexture(format, width, options);
|
|
335
|
+
case '2darray':
|
|
336
|
+
return device.createTexture2DArray(format, width, height, depth, options);
|
|
337
|
+
default:
|
|
338
|
+
return null;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
/** @internal */ updateResources(shadowMapParams) {
|
|
342
|
+
const device = Application.instance.device;
|
|
343
|
+
const colorFormat = shadowMapParams.impl.getShadowMapColorFormat(shadowMapParams);
|
|
344
|
+
const depthFormat = shadowMapParams.impl.getShadowMapDepthFormat(shadowMapParams);
|
|
345
|
+
const numCascades = shadowMapParams.numShadowCascades;
|
|
346
|
+
const useTextureArray = numCascades > 1 && device.type !== 'webgl';
|
|
347
|
+
const shadowMapWidth = numCascades > 1 && !useTextureArray ? 2 * this._config.shadowMapSize : this._config.shadowMapSize;
|
|
348
|
+
const shadowMapHeight = numCascades > 2 && !useTextureArray ? 2 * this._config.shadowMapSize : this._config.shadowMapSize;
|
|
349
|
+
const colorTarget = useTextureArray ? '2darray' : this._light.isPointLight() ? 'cube' : '2d';
|
|
350
|
+
const depthTarget = device.type === 'webgl' ? '2d' : colorTarget;
|
|
351
|
+
shadowMapParams.shadowMapFramebuffer = TemporalCache.getFramebufferFixedSize(shadowMapWidth, shadowMapHeight, numCascades, colorFormat, depthFormat, colorTarget, depthTarget, false, 1);
|
|
352
|
+
shadowMapParams.impl = this._impl;
|
|
353
|
+
this._impl.updateResources(shadowMapParams);
|
|
354
|
+
}
|
|
355
|
+
/** @internal */ createLightCameraPoint(lightCamera) {
|
|
356
|
+
//lightCamera.reparent(this._light);
|
|
357
|
+
lightCamera.reparent(lightCamera.scene.rootNode);
|
|
358
|
+
lightCamera.resetTransform();
|
|
359
|
+
lightCamera.setPerspective(Math.PI / 2, 1, this._config.nearClip, Math.min(this._shadowDistance, this._light.range));
|
|
360
|
+
lightCamera.position.set(this._light.positionAndRange.xyz());
|
|
361
|
+
}
|
|
362
|
+
/** @internal */ createLightCameraSpot(lightCamera) {
|
|
363
|
+
lightCamera.reparent(this._light);
|
|
364
|
+
lightCamera.resetTransform();
|
|
365
|
+
lightCamera.setPerspective(2 * this._light.cutoff, 1, this._config.nearClip, Math.min((this._shadowDistance, this._light).range));
|
|
366
|
+
}
|
|
367
|
+
/** @internal */ createLightCameraDirectional(sceneAABB, sceneCamera, lightCamera, cropMatrix, border) {
|
|
368
|
+
let frustum = sceneCamera.frustumViewSpace;
|
|
369
|
+
if (this._shadowDistance < sceneCamera.getFarPlane()) {
|
|
370
|
+
tmpMatrix.set(sceneCamera.getProjectionMatrix());
|
|
371
|
+
tmpMatrix.setNearFar(tmpMatrix.getNearPlane(), this._shadowDistance);
|
|
372
|
+
//tmpMatrix.multiplyRight(sceneCamera.viewMatrix);
|
|
373
|
+
tmpFrustum.initWithMatrix(tmpMatrix);
|
|
374
|
+
frustum = tmpFrustum;
|
|
375
|
+
}
|
|
376
|
+
border = border || 0;
|
|
377
|
+
const expand = (this.shadowMapSize - 2 * border) / this.shadowMapSize;
|
|
378
|
+
//const frustum = sceneCamera.frustum;
|
|
379
|
+
const frustumMin = ShadowMapper._frustumMin;
|
|
380
|
+
const frustumMax = ShadowMapper._frustumMax;
|
|
381
|
+
const frustumCenter = ShadowMapper._frustumCenter;
|
|
382
|
+
const target = ShadowMapper._target;
|
|
383
|
+
const up = ShadowMapper._up;
|
|
384
|
+
// const frustum = new Frustum(sceneCamera.viewProjectionMatrix);
|
|
385
|
+
frustumMin.setXYZ(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
|
|
386
|
+
frustumMax.setXYZ(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
|
|
387
|
+
frustum.corners.forEach((p)=>{
|
|
388
|
+
frustumMin.inplaceMin(p);
|
|
389
|
+
frustumMax.inplaceMax(p);
|
|
390
|
+
});
|
|
391
|
+
let radius = Vector3.distance(frustumMin, frustumMax) * 0.5 * expand;
|
|
392
|
+
const center = sceneCamera.thisToWorld(Vector3.add(frustumMin, frustumMax, frustumCenter).scaleBy(0.5), frustumCenter);
|
|
393
|
+
// Bounding sphere of the shadow camera should not be larger than bounding sphere of the scene.
|
|
394
|
+
const sceneRadius = sceneAABB.diagonalLength * 0.5 * expand;
|
|
395
|
+
if (sceneRadius < radius) {
|
|
396
|
+
radius = sceneRadius;
|
|
397
|
+
Vector3.add(sceneAABB.minPoint, sceneAABB.maxPoint, center).scaleBy(0.5);
|
|
398
|
+
}
|
|
399
|
+
target.setXYZ(center.x + this._light.directionAndCutoff.x, center.y + this._light.directionAndCutoff.y, center.z + this._light.directionAndCutoff.z);
|
|
400
|
+
up.setXYZ(0, 1, 0);
|
|
401
|
+
lightCamera.lookAt(center, target, up);
|
|
402
|
+
lightCamera.position.set(center);
|
|
403
|
+
lightCamera.setOrtho(-radius, radius, -radius, radius, -radius, radius);
|
|
404
|
+
center.setXYZ(0, 0, 0);
|
|
405
|
+
lightCamera.viewProjectionMatrix.transformPointP(center, center);
|
|
406
|
+
if (cropMatrix) {
|
|
407
|
+
const tx = center.x * this.shadowMapSize * 0.5;
|
|
408
|
+
const ty = center.y * this.shadowMapSize * 0.5;
|
|
409
|
+
const rx = Math.round(tx);
|
|
410
|
+
const ry = Math.round(ty);
|
|
411
|
+
center.setXYZ((rx - tx) * 2 / this.shadowMapSize, (ry - ty) * 2 / this.shadowMapSize, 0);
|
|
412
|
+
cropMatrix.translation(center);
|
|
413
|
+
}
|
|
414
|
+
/*
|
|
415
|
+
const minx = frustumMin.x;
|
|
416
|
+
const maxx = frustumMax.x;
|
|
417
|
+
const miny = frustumMin.y;
|
|
418
|
+
const maxy = frustumMax.y;
|
|
419
|
+
const minz = frustumMin.z;
|
|
420
|
+
const maxz = frustumMax.z;
|
|
421
|
+
const texelSizeW = (maxx - minx) / this.shadowMapSize;
|
|
422
|
+
const texelSizeH = (maxy - miny) / this.shadowMapSize;
|
|
423
|
+
const cx = Math.floor((minx + maxx) / 2 / texelSizeW) * texelSizeW;
|
|
424
|
+
const cy = Math.floor((miny + maxy) / 2 / texelSizeH) * texelSizeH;
|
|
425
|
+
const cz = (minz + maxz) / 2;
|
|
426
|
+
const hx = Math.floor(((maxx - minx) * (expand + 0.5)) / texelSizeW) * texelSizeW;
|
|
427
|
+
const hy = Math.floor(((maxy - miny) * (expand + 0.5)) / texelSizeH) * texelSizeH;
|
|
428
|
+
lightCamera.position.setXYZ(cx, cy, cz);
|
|
429
|
+
lightCamera.setOrtho(
|
|
430
|
+
-hx,
|
|
431
|
+
hx,
|
|
432
|
+
-hy,
|
|
433
|
+
hy,
|
|
434
|
+
sceneMax.z - maxz,
|
|
435
|
+
sceneMax.z - minz + 1
|
|
436
|
+
);
|
|
437
|
+
if (cropMatrix) {
|
|
438
|
+
// compute crop matrix
|
|
439
|
+
let clipMaxX = 0,
|
|
440
|
+
clipMaxY = 0;
|
|
441
|
+
let clipMinX = Number.MAX_VALUE,
|
|
442
|
+
clipMinY = Number.MAX_VALUE;
|
|
443
|
+
frustum.corners.forEach((p) => {
|
|
444
|
+
const clipPos = lightCamera.viewProjectionMatrix.transformPoint(p);
|
|
445
|
+
clipPos.x = Math.min(1, Math.max(-1, clipPos.x / clipPos.w));
|
|
446
|
+
clipPos.y = Math.min(1, Math.max(-1, clipPos.y / clipPos.w));
|
|
447
|
+
if (clipPos.x > clipMaxX) {
|
|
448
|
+
clipMaxX = clipPos.x;
|
|
449
|
+
}
|
|
450
|
+
if (clipPos.x < clipMinX) {
|
|
451
|
+
clipMinX = clipPos.x;
|
|
452
|
+
}
|
|
453
|
+
if (clipPos.y > clipMaxY) {
|
|
454
|
+
clipMaxY = clipPos.y;
|
|
455
|
+
}
|
|
456
|
+
if (clipPos.y < clipMinY) {
|
|
457
|
+
clipMinY = clipPos.y;
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
const clipW = clipMaxX - clipMinX;
|
|
461
|
+
const clipH = clipMaxY - clipMinY;
|
|
462
|
+
clipMinX -= expand * clipW;
|
|
463
|
+
clipMinY -= expand * clipH;
|
|
464
|
+
clipMaxX += expand * clipW;
|
|
465
|
+
clipMaxY += expand * clipH;
|
|
466
|
+
const scaleX = 2 / (clipMaxX - clipMinX);
|
|
467
|
+
const scaleY = 2 / (clipMaxY - clipMinY);
|
|
468
|
+
const offsetX = -0.5 * (clipMaxX + clipMinX) * scaleX;
|
|
469
|
+
const offsetY = -0.5 * (clipMaxY + clipMinY) * scaleY;
|
|
470
|
+
cropMatrix.identity();
|
|
471
|
+
cropMatrix.m00 = scaleX;
|
|
472
|
+
cropMatrix.m11 = scaleY;
|
|
473
|
+
cropMatrix.m03 = offsetX;
|
|
474
|
+
cropMatrix.m13 = offsetY;
|
|
475
|
+
}
|
|
476
|
+
*/ }
|
|
477
|
+
/** @internal */ static fetchShadowMapParams() {
|
|
478
|
+
if (this._shadowMapParams.length > 0) {
|
|
479
|
+
return this._shadowMapParams.pop();
|
|
480
|
+
} else {
|
|
481
|
+
return {
|
|
482
|
+
lightType: LIGHT_TYPE_NONE,
|
|
483
|
+
depthClampEnabled: false,
|
|
484
|
+
shaderHash: '',
|
|
485
|
+
cameraParams: new Vector4(),
|
|
486
|
+
cascadeDistances: new Vector4(),
|
|
487
|
+
depthBiasScales: new Vector4(),
|
|
488
|
+
depthBiasValues: [
|
|
489
|
+
new Vector4(),
|
|
490
|
+
new Vector4(),
|
|
491
|
+
new Vector4(),
|
|
492
|
+
new Vector4()
|
|
493
|
+
],
|
|
494
|
+
numShadowCascades: 1,
|
|
495
|
+
shadowMatrices: new Float32Array(16 * 4),
|
|
496
|
+
shadowMap: null,
|
|
497
|
+
shadowMapSampler: null,
|
|
498
|
+
shadowMapFramebuffer: null,
|
|
499
|
+
impl: null,
|
|
500
|
+
implData: null
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
/** @internal */ static fetchCameraForScene(scene) {
|
|
505
|
+
const cameras = this._lightCameras.get(scene);
|
|
506
|
+
if (!cameras || cameras.length === 0) {
|
|
507
|
+
return new Camera(scene);
|
|
508
|
+
} else {
|
|
509
|
+
const camera = cameras.pop();
|
|
510
|
+
camera.parent = scene.rootNode;
|
|
511
|
+
camera.resetTransform();
|
|
512
|
+
camera.clipMask = 0;
|
|
513
|
+
return camera;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
/** @internal */ static releaseCamera(camera) {
|
|
517
|
+
let cameras = this._lightCameras.get(camera.scene);
|
|
518
|
+
if (!cameras) {
|
|
519
|
+
cameras = [];
|
|
520
|
+
this._lightCameras.set(camera.scene, cameras);
|
|
521
|
+
}
|
|
522
|
+
camera.remove();
|
|
523
|
+
cameras.push(camera);
|
|
524
|
+
}
|
|
525
|
+
/** @internal */ calcSplitDistances(nearPlane, farPlane, numCascades) {
|
|
526
|
+
const result = [
|
|
527
|
+
0,
|
|
528
|
+
0,
|
|
529
|
+
0,
|
|
530
|
+
0,
|
|
531
|
+
0
|
|
532
|
+
];
|
|
533
|
+
for(let i = 0; i <= numCascades; ++i){
|
|
534
|
+
const fIDM = i / numCascades;
|
|
535
|
+
const fLog = nearPlane * Math.pow(farPlane / nearPlane, fIDM);
|
|
536
|
+
const fUniform = nearPlane + (farPlane - nearPlane) * fIDM;
|
|
537
|
+
result[i] = fLog * this._config.splitLambda + fUniform * (1 - this._config.splitLambda);
|
|
538
|
+
}
|
|
539
|
+
return result;
|
|
540
|
+
}
|
|
541
|
+
/** @internal */ calcDepthBiasParams(shadowMapCamera, shadowMapSize, depthBias, normalBias, depthScale, result) {
|
|
542
|
+
const sizeNear = Math.min(shadowMapCamera.getProjectionMatrix().getNearPlaneWidth(), shadowMapCamera.getProjectionMatrix().getNearPlaneHeight());
|
|
543
|
+
const sizeFar = Math.min(shadowMapCamera.getProjectionMatrix().getFarPlaneWidth(), shadowMapCamera.getProjectionMatrix().getFarPlaneHeight());
|
|
544
|
+
const scaleFactor = sizeNear / shadowMapSize / 2;
|
|
545
|
+
result.setXYZW(depthBias * scaleFactor, normalBias * scaleFactor, depthScale, sizeFar / sizeNear);
|
|
546
|
+
}
|
|
547
|
+
/** @internal */ postRenderShadowMap(shadowMapParams) {
|
|
548
|
+
this._impl.postRenderShadowMap(shadowMapParams);
|
|
549
|
+
}
|
|
550
|
+
/** @internal */ render(ctx, renderPass) {
|
|
551
|
+
if (!ctx.shadowMapInfo) {
|
|
552
|
+
ctx.shadowMapInfo = new Map();
|
|
553
|
+
}
|
|
554
|
+
const shadowMapParams = ShadowMapper.fetchShadowMapParams();
|
|
555
|
+
shadowMapParams.impl = this._impl;
|
|
556
|
+
shadowMapParams.lightType = this.light.lightType;
|
|
557
|
+
shadowMapParams.numShadowCascades = shadowMapParams.lightType === LIGHT_TYPE_DIRECTIONAL ? this._config.numCascades : 1;
|
|
558
|
+
ctx.shadowMapInfo.set(this.light, shadowMapParams);
|
|
559
|
+
const scene = ctx.scene;
|
|
560
|
+
const camera = ctx.camera;
|
|
561
|
+
renderPass.light = this._light;
|
|
562
|
+
this.updateResources(shadowMapParams);
|
|
563
|
+
shadowMapParams.shaderHash = this.getShaderHash(shadowMapParams);
|
|
564
|
+
const device = Application.instance.device;
|
|
565
|
+
const fb = shadowMapParams.shadowMapFramebuffer;
|
|
566
|
+
shadowMapParams.depthClampEnabled = false;
|
|
567
|
+
renderPass.clearColor = fb.getColorAttachments()[0] ? fb.getColorAttachments()[0].isFloatFormat() ? new Vector4(1, 1, 1, 1) : new Vector4(0, 0, 0, 1) : null;
|
|
568
|
+
const depthScale = this._impl.getDepthScale();
|
|
569
|
+
if (this._light.isPointLight()) {
|
|
570
|
+
const shadowMapRenderCamera = ShadowMapper.fetchCameraForScene(scene);
|
|
571
|
+
this.createLightCameraPoint(shadowMapRenderCamera);
|
|
572
|
+
this.calcDepthBiasParams(shadowMapRenderCamera, this._config.shadowMapSize, this._config.depthBias, this._config.normalBias, depthScale, shadowMapParams.depthBiasValues[0]);
|
|
573
|
+
shadowMapParams.cameraParams.setXYZW(shadowMapRenderCamera.getNearPlane(), shadowMapRenderCamera.getFarPlane(), this._config.shadowMapSize, this._shadowDistance);
|
|
574
|
+
device.setFramebuffer(fb);
|
|
575
|
+
shadowMapParams.shadowMatrices.set(Matrix4x4.transpose(shadowMapRenderCamera.viewMatrix));
|
|
576
|
+
for (const face of [
|
|
577
|
+
CubeFace.PX,
|
|
578
|
+
CubeFace.NX,
|
|
579
|
+
CubeFace.PY,
|
|
580
|
+
CubeFace.NY,
|
|
581
|
+
CubeFace.PZ,
|
|
582
|
+
CubeFace.NZ
|
|
583
|
+
]){
|
|
584
|
+
shadowMapRenderCamera.lookAtCubeFace(face);
|
|
585
|
+
fb.setColorAttachmentCubeFace(0, face);
|
|
586
|
+
fb.setDepthAttachmentCubeFace(face);
|
|
587
|
+
ctx.camera = shadowMapRenderCamera;
|
|
588
|
+
renderPass.render(ctx);
|
|
589
|
+
}
|
|
590
|
+
shadowMapParams.shadowMatrices.set(Matrix4x4.identity());
|
|
591
|
+
ShadowMapper.releaseCamera(shadowMapRenderCamera);
|
|
592
|
+
} else {
|
|
593
|
+
if (this._config.numCascades > 1) {
|
|
594
|
+
const distances = this.calcSplitDistances(camera.getNearPlane(), Math.min(this._shadowDistance, camera.getFarPlane()), this._config.numCascades);
|
|
595
|
+
const cascadeCamera = ShadowMapper.fetchCameraForScene(scene);
|
|
596
|
+
const shadowMapRenderCamera = ShadowMapper.fetchCameraForScene(scene);
|
|
597
|
+
const shadowMapCullCamera = ShadowMapper.fetchCameraForScene(scene);
|
|
598
|
+
shadowMapCullCamera.clipMask = AABB.ClipLeft | AABB.ClipRight | AABB.ClipBottom | AABB.ClipTop;
|
|
599
|
+
cascadeCamera.reparent(camera);
|
|
600
|
+
shadowMapParams.depthClampEnabled = Application.instance.device.getDeviceCaps().shaderCaps.supportFragmentDepth;
|
|
601
|
+
for(let split = 0; split < this._config.numCascades; split++){
|
|
602
|
+
cascadeCamera.setPerspective(camera.getFOV(), camera.getAspect(), distances[split], distances[split + 1]);
|
|
603
|
+
const snapMatrix = ShadowMapper._snapMatrix;
|
|
604
|
+
const border = shadowMapParams.impl.getShadowMapBorder(shadowMapParams); //20 / this._config.shadowMapSize;
|
|
605
|
+
this.createLightCameraDirectional(ctx.scene.boundingBox, cascadeCamera, shadowMapRenderCamera, snapMatrix, border);
|
|
606
|
+
this.createLightCameraDirectional(ctx.scene.boundingBox, cascadeCamera, shadowMapCullCamera, null, border);
|
|
607
|
+
this.calcDepthBiasParams(shadowMapRenderCamera, this._config.shadowMapSize, this._config.depthBias, this._config.normalBias, depthScale, shadowMapParams.depthBiasValues[split]);
|
|
608
|
+
shadowMapParams.depthBiasScales[split] = 1;
|
|
609
|
+
// Incorrect calculation
|
|
610
|
+
// shadowMapParams.depthBiasScales[split] = shadowMapParams.depthBiasValues[0].x !== 0 ? shadowMapParams.depthBiasValues[split].x / shadowMapParams.depthBiasValues[0].x : 1;
|
|
611
|
+
shadowMapParams.cameraParams.setXYZW(shadowMapRenderCamera.getNearPlane(), shadowMapRenderCamera.getFarPlane(), this._config.shadowMapSize, this._shadowDistance);
|
|
612
|
+
let scissor = null;
|
|
613
|
+
if (fb.getColorAttachments()[0]?.isTexture2DArray() || fb.getDepthAttachment()?.isTexture2DArray()) {
|
|
614
|
+
shadowMapRenderCamera.setProjectionMatrix(Matrix4x4.multiply(snapMatrix, shadowMapRenderCamera.getProjectionMatrix()));
|
|
615
|
+
fb.setColorAttachmentLayer(0, split);
|
|
616
|
+
fb.setDepthAttachmentLayer(split);
|
|
617
|
+
} else {
|
|
618
|
+
const numRows = this._config.numCascades > 2 ? 2 : 1;
|
|
619
|
+
const numCols = this._config.numCascades > 1 ? 2 : 1;
|
|
620
|
+
const adjMatrix = new Matrix4x4();
|
|
621
|
+
const col = split % 2;
|
|
622
|
+
const row = split >> 1;
|
|
623
|
+
adjMatrix.setRowXYZW(0, 1.5 - 0.5 * numCols, 0, 0, 0);
|
|
624
|
+
adjMatrix.setRowXYZW(1, 0, 1.5 - 0.5 * numRows, 0, 0);
|
|
625
|
+
adjMatrix.setRowXYZW(2, 0, 0, 1, 0);
|
|
626
|
+
adjMatrix.setRowXYZW(3, col - 0.5 * numCols + 0.5, row - 0.5 * numRows + 0.5, 0, 1);
|
|
627
|
+
shadowMapRenderCamera.setProjectionMatrix(Matrix4x4.multiply(adjMatrix, Matrix4x4.multiply(snapMatrix, shadowMapRenderCamera.getProjectionMatrix())));
|
|
628
|
+
if (device.type === 'webgpu') {
|
|
629
|
+
scissor = [
|
|
630
|
+
col * this._config.shadowMapSize,
|
|
631
|
+
(numRows - 1 - row) * this._config.shadowMapSize,
|
|
632
|
+
this._config.shadowMapSize,
|
|
633
|
+
this._config.shadowMapSize
|
|
634
|
+
];
|
|
635
|
+
} else {
|
|
636
|
+
scissor = [
|
|
637
|
+
col * this._config.shadowMapSize,
|
|
638
|
+
row * this._config.shadowMapSize,
|
|
639
|
+
this._config.shadowMapSize,
|
|
640
|
+
this._config.shadowMapSize
|
|
641
|
+
];
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
device.setFramebuffer(fb);
|
|
645
|
+
device.setScissor(scissor);
|
|
646
|
+
ctx.camera = shadowMapRenderCamera;
|
|
647
|
+
renderPass.render(ctx, shadowMapCullCamera);
|
|
648
|
+
shadowMapParams.shadowMatrices.set(Matrix4x4.transpose(shadowMapRenderCamera.viewProjectionMatrix), split * 16);
|
|
649
|
+
shadowMapParams.cascadeDistances[split] = distances[split + 1];
|
|
650
|
+
}
|
|
651
|
+
ShadowMapper.releaseCamera(cascadeCamera);
|
|
652
|
+
ShadowMapper.releaseCamera(shadowMapRenderCamera);
|
|
653
|
+
ShadowMapper.releaseCamera(shadowMapCullCamera);
|
|
654
|
+
} else {
|
|
655
|
+
const shadowMapRenderCamera = ShadowMapper.fetchCameraForScene(scene);
|
|
656
|
+
const snapMatrix = ShadowMapper._snapMatrix;
|
|
657
|
+
shadowMapRenderCamera.clipMask = AABB.ClipLeft | AABB.ClipRight | AABB.ClipBottom | AABB.ClipTop;
|
|
658
|
+
if (this._light.isDirectionLight()) {
|
|
659
|
+
this.createLightCameraDirectional(ctx.scene.boundingBox, camera, shadowMapRenderCamera, snapMatrix, shadowMapParams.impl.getShadowMapBorder(shadowMapParams));
|
|
660
|
+
} else {
|
|
661
|
+
this.createLightCameraSpot(shadowMapRenderCamera);
|
|
662
|
+
}
|
|
663
|
+
this.calcDepthBiasParams(shadowMapRenderCamera, this._config.shadowMapSize, this._config.depthBias, this._config.normalBias, depthScale, shadowMapParams.depthBiasValues[0]);
|
|
664
|
+
shadowMapParams.cameraParams.setXYZW(shadowMapRenderCamera.getNearPlane(), shadowMapRenderCamera.getFarPlane(), this._config.shadowMapSize, this._shadowDistance);
|
|
665
|
+
device.setFramebuffer(fb);
|
|
666
|
+
shadowMapRenderCamera.setProjectionMatrix(Matrix4x4.multiply(snapMatrix, shadowMapRenderCamera.getProjectionMatrix()));
|
|
667
|
+
ctx.camera = shadowMapRenderCamera;
|
|
668
|
+
renderPass.render(ctx);
|
|
669
|
+
shadowMapParams.shadowMatrices.set(Matrix4x4.transpose(shadowMapRenderCamera.viewProjectionMatrix));
|
|
670
|
+
ShadowMapper.releaseCamera(shadowMapRenderCamera);
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
ctx.camera = camera;
|
|
674
|
+
this.postRenderShadowMap(shadowMapParams);
|
|
675
|
+
}
|
|
676
|
+
/** @internal */ applyMode(mode) {
|
|
677
|
+
if (mode !== 'hard' && mode !== 'vsm' && mode !== 'esm' && mode !== 'pcf-pd' && mode !== 'pcf-opt') {
|
|
678
|
+
console.error(`ShadowMapper.setShadowMode() failed: invalid mode: ${mode}`);
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
this._impl = null;
|
|
682
|
+
if (mode === 'hard') {
|
|
683
|
+
this._impl = new SSM();
|
|
684
|
+
} else if (mode === 'vsm') {
|
|
685
|
+
this._impl = new VSM(this._vsmBlurKernelSize, this._vsmBlurRadius, this._vsmDarkness);
|
|
686
|
+
} else if (mode === 'esm') {
|
|
687
|
+
this._impl = new ESM(this._esmBlurKernelSize, this._esmBlurRadius, this._esmDepthScale);
|
|
688
|
+
} else if (mode === 'pcf-pd') {
|
|
689
|
+
this._impl = new PCFPD(this._pdSampleCount, this._pdSampleRadius);
|
|
690
|
+
} else if (mode === 'pcf-opt') {
|
|
691
|
+
this._impl = new PCFOPT(this._pcfKernelSize);
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
/** @internal */ asVSM() {
|
|
695
|
+
return this._impl?.getType() === 'vsm' ? this._impl : null;
|
|
696
|
+
}
|
|
697
|
+
/** @internal */ asESM() {
|
|
698
|
+
return this._impl?.getType() === 'esm' ? this._impl : null;
|
|
699
|
+
}
|
|
700
|
+
/** @internal */ asPCFPD() {
|
|
701
|
+
return this._impl?.getType() === 'pcf-pd' ? this._impl : null;
|
|
702
|
+
}
|
|
703
|
+
/** @internal */ asPCFOPT() {
|
|
704
|
+
return this._impl?.getType() === 'pcf-opt' ? this._impl : null;
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
export { ShadowMapper };
|
|
709
|
+
//# sourceMappingURL=shadowmapper.js.map
|