UNPKG

6.36 kBTypeScriptView Raw
1import { Camera } from "../cameras/Camera.js";
2import { Object3DJSONObject } from "../core/Object3D.js";
3import { Frustum } from "../math/Frustum.js";
4import { Matrix4 } from "../math/Matrix4.js";
5import { Vector2, Vector2Tuple } from "../math/Vector2.js";
6import { Vector4 } from "../math/Vector4.js";
7import { WebGLRenderTarget } from "../renderers/WebGLRenderTarget.js";
8import { Light } from "./Light.js";
9
10export interface LightShadowJSON {
11 intensity?: number;
12 bias?: number;
13 normalBias?: number;
14 radius?: number;
15 mapSize?: Vector2Tuple;
16
17 camera: Omit<Object3DJSONObject, "matrix">;
18}
19
20/**
21 * Serves as a base class for the other shadow classes.
22 * @see {@link https://threejs.org/docs/index.html#api/en/lights/shadows/LightShadow | Official Documentation}
23 * @see {@link https://github.com/mrdoob/three.js/blob/master/src/lights/LightShadow.js | Source}
24 */
25export class LightShadow<TCamera extends Camera = Camera> {
26 /**
27 * Create a new instance of {@link LightShadow}
28 * @param camera The light's view of the world.
29 */
30 constructor(camera: TCamera);
31
32 /**
33 * The light's view of the world.
34 * @remark This is used to generate a depth map of the scene; objects behind other objects from the light's perspective will be in shadow.
35 */
36 camera: TCamera;
37
38 /**
39 * The intensity of the shadow. The default is `1`. Valid values are in the range `[0, 1]`.
40 */
41 intensity: number;
42
43 /**
44 * Shadow map bias, how much to add or subtract from the normalized depth when deciding whether a surface is in shadow.
45 * @remark The Very tiny adjustments here (in the order of 0.0001) may help reduce artifacts in shadows.
46 * @remarks Expects a `Float`
47 * @defaultValue `0`
48 */
49 bias: number;
50
51 /**
52 * Defines how much the position used to query the shadow map is offset along the object normal.
53 * @remark The Increasing this value can be used to reduce shadow acne especially in large scenes where light shines onto geometry at a shallow angle.
54 * @remark The cost is that shadows may appear distorted.
55 * @remarks Expects a `Float`
56 * @defaultValue `0`
57 */
58 normalBias: number;
59
60 /**
61 * Setting this to values greater than 1 will blur the edges of the shadow.toi
62 * @remark High values will cause unwanted banding effects in the shadows - a greater {@link LightShadow.mapSize | mapSize
63 * will allow for a higher value to be used here before these effects become visible.
64 * @remark If {@link THREE.WebGLRenderer.shadowMap.type | WebGLRenderer.shadowMap.type} is set to {@link Renderer | PCFSoftShadowMap},
65 * radius has no effect and it is recommended to increase softness by decreasing {@link LightShadow.mapSize | mapSize} instead.
66 * @remark Note that this has no effect if the {@link THREE.WebGLRenderer.shadowMap | WebGLRenderer.shadowMap}.{@link THREE.WebGLShadowMap.type | type}
67 * is set to {@link THREE.BasicShadowMap | BasicShadowMap}.
68 * @remarks Expects a `Float`
69 * @defaultValue `1`
70 */
71 radius: number;
72
73 /**
74 * The amount of samples to use when blurring a VSM shadow map.
75 * @remarks Expects a `Integer`
76 * @defaultValue `8`
77 */
78 blurSamples: number;
79
80 /**
81 * A {@link THREE.Vector2 | Vector2} defining the width and height of the shadow map.
82 * @remarks Higher values give better quality shadows at the cost of computation time.
83 * @remarks Values must be powers of 2, up to the {@link THREE.WebGLRenderer.capabilities | WebGLRenderer.capabilities}.maxTextureSize for a given device,
84 * although the width and height don't have to be the same (so, for example, (512, 1024) is valid).
85 * @defaultValue `new THREE.Vector2(512, 512)`
86 */
87 mapSize: Vector2;
88
89 /**
90 * The depth map generated using the internal camera; a location beyond a pixel's depth is in shadow. Computed internally during rendering.
91 * @defaultValue null
92 */
93 map: WebGLRenderTarget | null;
94
95 /**
96 * The distribution map generated using the internal camera; an occlusion is calculated based on the distribution of depths. Computed internally during rendering.
97 * @defaultValue null
98 */
99 mapPass: WebGLRenderTarget | null;
100
101 /**
102 * Model to shadow camera space, to compute location and depth in shadow map.
103 * Stored in a {@link Matrix4 | Matrix4}.
104 * @remarks This is computed internally during rendering.
105 * @defaultValue new THREE.Matrix4()
106 */
107 matrix: Matrix4;
108
109 /**
110 * Enables automatic updates of the light's shadow. If you do not require dynamic lighting / shadows, you may set this to `false`.
111 * @defaultValue `true`
112 */
113 autoUpdate: boolean;
114
115 /**
116 * When set to `true`, shadow maps will be updated in the next `render` call.
117 * If you have set {@link autoUpdate} to `false`, you will need to set this property to `true` and then make a render call to update the light's shadow.
118 * @defaultValue `false`
119 */
120 needsUpdate: boolean;
121
122 /**
123 * Used internally by the renderer to get the number of viewports that need to be rendered for this shadow.
124 */
125 getViewportCount(): number;
126
127 /**
128 * Copies value of all the properties from the {@link {@link LightShadow} | source} to this Light.
129 * @param source
130 */
131 copy(source: LightShadow): this;
132
133 /**
134 * Creates a new {@link LightShadow} with the same properties as this one.
135 */
136 clone(recursive?: boolean): this;
137
138 /**
139 * Serialize this LightShadow.
140 */
141 toJSON(): LightShadowJSON;
142
143 /**
144 * Gets the shadow cameras frustum
145 * @remarks
146 * Used internally by the renderer to cull objects.
147 */
148 getFrustum(): Frustum;
149
150 /**
151 * Update the matrices for the camera and shadow, used internally by the renderer.
152 * @param light The light for which the shadow is being rendered.
153 */
154 updateMatrices(light: Light): void;
155
156 getViewport(viewportIndex: number): Vector4;
157
158 /**
159 * Used internally by the renderer to extend the shadow map to contain all viewports
160 */
161 getFrameExtents(): Vector2;
162
163 /**
164 * Frees the GPU-related resources allocated by this instance
165 * @remarks
166 * Call this method whenever this instance is no longer used in your app.
167 */
168 dispose(): void;
169}