shadowLight.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import type { Camera } from "../Cameras/camera";
  2. import type { Scene } from "../scene";
  3. import { Matrix, Vector3 } from "../Maths/math.vector";
  4. import type { AbstractMesh } from "../Meshes/abstractMesh";
  5. import { Light } from "./light";
  6. import type { Nullable } from "../types.js";
  7. /**
  8. * Interface describing all the common properties and methods a shadow light needs to implement.
  9. * This helps both the shadow generator and materials to generate the corresponding shadow maps
  10. * as well as binding the different shadow properties to the effects.
  11. */
  12. export interface IShadowLight extends Light {
  13. /**
  14. * The light id in the scene (used in scene.getLightById for instance)
  15. */
  16. id: string;
  17. /**
  18. * The position the shadow will be casted from.
  19. */
  20. position: Vector3;
  21. /**
  22. * In 2d mode (needCube being false), the direction used to cast the shadow.
  23. */
  24. direction: Vector3;
  25. /**
  26. * The transformed position. Position of the light in world space taking parenting in account.
  27. */
  28. transformedPosition: Vector3;
  29. /**
  30. * The transformed direction. Direction of the light in world space taking parenting in account.
  31. */
  32. transformedDirection: Vector3;
  33. /**
  34. * The friendly name of the light in the scene.
  35. */
  36. name: string;
  37. /**
  38. * Defines the shadow projection clipping minimum z value.
  39. */
  40. shadowMinZ: number;
  41. /**
  42. * Defines the shadow projection clipping maximum z value.
  43. */
  44. shadowMaxZ: number;
  45. /**
  46. * Computes the transformed information (transformedPosition and transformedDirection in World space) of the current light
  47. * @returns true if the information has been computed, false if it does not need to (no parenting)
  48. */
  49. computeTransformedInformation(): boolean;
  50. /**
  51. * Gets the scene the light belongs to.
  52. * @returns The scene
  53. */
  54. getScene(): Scene;
  55. /**
  56. * Callback defining a custom Projection Matrix Builder.
  57. * This can be used to override the default projection matrix computation.
  58. */
  59. customProjectionMatrixBuilder: (viewMatrix: Matrix, renderList: Array<AbstractMesh>, result: Matrix) => void;
  60. /**
  61. * Sets the shadow projection matrix in parameter to the generated projection matrix.
  62. * @param matrix The matrix to update with the projection information
  63. * @param viewMatrix The transform matrix of the light
  64. * @param renderList The list of mesh to render in the map
  65. * @returns The current light
  66. */
  67. setShadowProjectionMatrix(matrix: Matrix, viewMatrix: Matrix, renderList: Array<AbstractMesh>): IShadowLight;
  68. /**
  69. * Gets the current depth scale used in ESM.
  70. * @returns The scale
  71. */
  72. getDepthScale(): number;
  73. /**
  74. * Returns whether or not the shadow generation require a cube texture or a 2d texture.
  75. * @returns true if a cube texture needs to be use
  76. */
  77. needCube(): boolean;
  78. /**
  79. * Detects if the projection matrix requires to be recomputed this frame.
  80. * @returns true if it requires to be recomputed otherwise, false.
  81. */
  82. needProjectionMatrixCompute(): boolean;
  83. /**
  84. * Forces the shadow generator to recompute the projection matrix even if position and direction did not changed.
  85. */
  86. forceProjectionMatrixCompute(): void;
  87. /**
  88. * Get the direction to use to render the shadow map. In case of cube texture, the face index can be passed.
  89. * @param faceIndex The index of the face we are computed the direction to generate shadow
  90. * @returns The set direction in 2d mode otherwise the direction to the cubemap face if needCube() is true
  91. */
  92. getShadowDirection(faceIndex?: number): Vector3;
  93. /**
  94. * Gets the minZ used for shadow according to both the scene and the light.
  95. * @param activeCamera The camera we are returning the min for
  96. * @returns the depth min z
  97. */
  98. getDepthMinZ(activeCamera: Camera): number;
  99. /**
  100. * Gets the maxZ used for shadow according to both the scene and the light.
  101. * @param activeCamera The camera we are returning the max for
  102. * @returns the depth max z
  103. */
  104. getDepthMaxZ(activeCamera: Camera): number;
  105. }
  106. /**
  107. * Base implementation IShadowLight
  108. * It groups all the common behaviour in order to reduce duplication and better follow the DRY pattern.
  109. */
  110. export declare abstract class ShadowLight extends Light implements IShadowLight {
  111. protected abstract _setDefaultShadowProjectionMatrix(matrix: Matrix, viewMatrix: Matrix, renderList: Array<AbstractMesh>): void;
  112. protected _position: Vector3;
  113. protected _setPosition(value: Vector3): void;
  114. /**
  115. * Sets the position the shadow will be casted from. Also use as the light position for both
  116. * point and spot lights.
  117. */
  118. get position(): Vector3;
  119. /**
  120. * Sets the position the shadow will be casted from. Also use as the light position for both
  121. * point and spot lights.
  122. */
  123. set position(value: Vector3);
  124. protected _direction: Vector3;
  125. protected _setDirection(value: Vector3): void;
  126. /**
  127. * In 2d mode (needCube being false), gets the direction used to cast the shadow.
  128. * Also use as the light direction on spot and directional lights.
  129. */
  130. get direction(): Vector3;
  131. /**
  132. * In 2d mode (needCube being false), sets the direction used to cast the shadow.
  133. * Also use as the light direction on spot and directional lights.
  134. */
  135. set direction(value: Vector3);
  136. protected _shadowMinZ: number;
  137. /**
  138. * Gets the shadow projection clipping minimum z value.
  139. */
  140. get shadowMinZ(): number;
  141. /**
  142. * Sets the shadow projection clipping minimum z value.
  143. */
  144. set shadowMinZ(value: number);
  145. protected _shadowMaxZ: number;
  146. /**
  147. * Sets the shadow projection clipping maximum z value.
  148. */
  149. get shadowMaxZ(): number;
  150. /**
  151. * Gets the shadow projection clipping maximum z value.
  152. */
  153. set shadowMaxZ(value: number);
  154. /**
  155. * Callback defining a custom Projection Matrix Builder.
  156. * This can be used to override the default projection matrix computation.
  157. */
  158. customProjectionMatrixBuilder: (viewMatrix: Matrix, renderList: Array<AbstractMesh>, result: Matrix) => void;
  159. /**
  160. * The transformed position. Position of the light in world space taking parenting in account. Needs to be computed by calling computeTransformedInformation.
  161. */
  162. transformedPosition: Vector3;
  163. /**
  164. * The transformed direction. Direction of the light in world space taking parenting in account.
  165. */
  166. transformedDirection: Vector3;
  167. private _needProjectionMatrixCompute;
  168. /**
  169. * Computes the transformed information (transformedPosition and transformedDirection in World space) of the current light
  170. * @returns true if the information has been computed, false if it does not need to (no parenting)
  171. */
  172. computeTransformedInformation(): boolean;
  173. /**
  174. * Return the depth scale used for the shadow map.
  175. * @returns the depth scale.
  176. */
  177. getDepthScale(): number;
  178. /**
  179. * Get the direction to use to render the shadow map. In case of cube texture, the face index can be passed.
  180. * @param faceIndex The index of the face we are computed the direction to generate shadow
  181. * @returns The set direction in 2d mode otherwise the direction to the cubemap face if needCube() is true
  182. */
  183. getShadowDirection(faceIndex?: number): Vector3;
  184. /**
  185. * If computeTransformedInformation has been called, returns the ShadowLight absolute position in the world. Otherwise, returns the local position.
  186. * @returns the position vector in world space
  187. */
  188. getAbsolutePosition(): Vector3;
  189. /**
  190. * Sets the ShadowLight direction toward the passed target.
  191. * @param target The point to target in local space
  192. * @returns the updated ShadowLight direction
  193. */
  194. setDirectionToTarget(target: Vector3): Vector3;
  195. /**
  196. * Returns the light rotation in euler definition.
  197. * @returns the x y z rotation in local space.
  198. */
  199. getRotation(): Vector3;
  200. /**
  201. * Returns whether or not the shadow generation require a cube texture or a 2d texture.
  202. * @returns true if a cube texture needs to be use
  203. */
  204. needCube(): boolean;
  205. /**
  206. * Detects if the projection matrix requires to be recomputed this frame.
  207. * @returns true if it requires to be recomputed otherwise, false.
  208. */
  209. needProjectionMatrixCompute(): boolean;
  210. /**
  211. * Forces the shadow generator to recompute the projection matrix even if position and direction did not changed.
  212. */
  213. forceProjectionMatrixCompute(): void;
  214. /** @internal */
  215. _initCache(): void;
  216. /** @internal */
  217. _isSynchronized(): boolean;
  218. /**
  219. * Computes the world matrix of the node
  220. * @param force defines if the cache version should be invalidated forcing the world matrix to be created from scratch
  221. * @returns the world matrix
  222. */
  223. computeWorldMatrix(force?: boolean): Matrix;
  224. /**
  225. * Gets the minZ used for shadow according to both the scene and the light.
  226. * @param activeCamera The camera we are returning the min for
  227. * @returns the depth min z
  228. */
  229. getDepthMinZ(activeCamera: Camera): number;
  230. /**
  231. * Gets the maxZ used for shadow according to both the scene and the light.
  232. * @param activeCamera The camera we are returning the max for
  233. * @returns the depth max z
  234. */
  235. getDepthMaxZ(activeCamera: Camera): number;
  236. /**
  237. * Sets the shadow projection matrix in parameter to the generated projection matrix.
  238. * @param matrix The matrix to updated with the projection information
  239. * @param viewMatrix The transform matrix of the light
  240. * @param renderList The list of mesh to render in the map
  241. * @returns The current light
  242. */
  243. setShadowProjectionMatrix(matrix: Matrix, viewMatrix: Matrix, renderList: Array<AbstractMesh>): IShadowLight;
  244. /** @internal */
  245. protected _syncParentEnabledState(): void;
  246. protected _viewMatrix: Matrix;
  247. protected _projectionMatrix: Matrix;
  248. /**
  249. * Returns the view matrix.
  250. * @param faceIndex The index of the face for which we want to extract the view matrix. Only used for point light types.
  251. * @returns The view matrix. Can be null, if a view matrix cannot be defined for the type of light considered (as for a hemispherical light, for example).
  252. */
  253. getViewMatrix(faceIndex?: number): Nullable<Matrix>;
  254. /**
  255. * Returns the projection matrix.
  256. * Note that viewMatrix and renderList are optional and are only used by lights that calculate the projection matrix from a list of meshes (e.g. directional lights with automatic extents calculation).
  257. * @param viewMatrix The view transform matrix of the light (optional).
  258. * @param renderList The list of meshes to take into account when calculating the projection matrix (optional).
  259. * @returns The projection matrix. Can be null, if a projection matrix cannot be defined for the type of light considered (as for a hemispherical light, for example).
  260. */
  261. getProjectionMatrix(viewMatrix?: Matrix, renderList?: Array<AbstractMesh>): Nullable<Matrix>;
  262. }