light.d.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. import type { Nullable } from "../types";
  2. import type { Scene } from "../scene";
  3. import type { Matrix } from "../Maths/math.vector";
  4. import { Vector3 } from "../Maths/math.vector";
  5. import { Color3 } from "../Maths/math.color";
  6. import { Node } from "../node";
  7. import type { AbstractMesh } from "../Meshes/abstractMesh";
  8. import type { Effect } from "../Materials/effect";
  9. import { UniformBuffer } from "../Materials/uniformBuffer";
  10. import type { IShadowGenerator } from "./Shadows/shadowGenerator";
  11. import type { ISortableLight } from "./lightConstants";
  12. import type { Camera } from "../Cameras/camera";
  13. /**
  14. * Base class of all the lights in Babylon. It groups all the generic information about lights.
  15. * Lights are used, as you would expect, to affect how meshes are seen, in terms of both illumination and colour.
  16. * All meshes allow light to pass through them unless shadow generation is activated. The default number of lights allowed is four but this can be increased.
  17. */
  18. export declare abstract class Light extends Node implements ISortableLight {
  19. /**
  20. * Falloff Default: light is falling off following the material specification:
  21. * standard material is using standard falloff whereas pbr material can request special falloff per materials.
  22. */
  23. static readonly FALLOFF_DEFAULT = 0;
  24. /**
  25. * Falloff Physical: light is falling off following the inverse squared distance law.
  26. */
  27. static readonly FALLOFF_PHYSICAL = 1;
  28. /**
  29. * Falloff gltf: light is falling off as described in the gltf moving to PBR document
  30. * to enhance interoperability with other engines.
  31. */
  32. static readonly FALLOFF_GLTF = 2;
  33. /**
  34. * Falloff Standard: light is falling off like in the standard material
  35. * to enhance interoperability with other materials.
  36. */
  37. static readonly FALLOFF_STANDARD = 3;
  38. /**
  39. * If every light affecting the material is in this lightmapMode,
  40. * material.lightmapTexture adds or multiplies
  41. * (depends on material.useLightmapAsShadowmap)
  42. * after every other light calculations.
  43. */
  44. static readonly LIGHTMAP_DEFAULT = 0;
  45. /**
  46. * material.lightmapTexture as only diffuse lighting from this light
  47. * adds only specular lighting from this light
  48. * adds dynamic shadows
  49. */
  50. static readonly LIGHTMAP_SPECULAR = 1;
  51. /**
  52. * material.lightmapTexture as only lighting
  53. * no light calculation from this light
  54. * only adds dynamic shadows from this light
  55. */
  56. static readonly LIGHTMAP_SHADOWSONLY = 2;
  57. /**
  58. * Each light type uses the default quantity according to its type:
  59. * point/spot lights use luminous intensity
  60. * directional lights use illuminance
  61. */
  62. static readonly INTENSITYMODE_AUTOMATIC = 0;
  63. /**
  64. * lumen (lm)
  65. */
  66. static readonly INTENSITYMODE_LUMINOUSPOWER = 1;
  67. /**
  68. * candela (lm/sr)
  69. */
  70. static readonly INTENSITYMODE_LUMINOUSINTENSITY = 2;
  71. /**
  72. * lux (lm/m^2)
  73. */
  74. static readonly INTENSITYMODE_ILLUMINANCE = 3;
  75. /**
  76. * nit (cd/m^2)
  77. */
  78. static readonly INTENSITYMODE_LUMINANCE = 4;
  79. /**
  80. * Light type const id of the point light.
  81. */
  82. static readonly LIGHTTYPEID_POINTLIGHT = 0;
  83. /**
  84. * Light type const id of the directional light.
  85. */
  86. static readonly LIGHTTYPEID_DIRECTIONALLIGHT = 1;
  87. /**
  88. * Light type const id of the spot light.
  89. */
  90. static readonly LIGHTTYPEID_SPOTLIGHT = 2;
  91. /**
  92. * Light type const id of the hemispheric light.
  93. */
  94. static readonly LIGHTTYPEID_HEMISPHERICLIGHT = 3;
  95. /**
  96. * Diffuse gives the basic color to an object.
  97. */
  98. diffuse: Color3;
  99. /**
  100. * Specular produces a highlight color on an object.
  101. * Note: This is not affecting PBR materials.
  102. */
  103. specular: Color3;
  104. /**
  105. * Defines the falloff type for this light. This lets overriding how punctual light are
  106. * falling off base on range or angle.
  107. * This can be set to any values in Light.FALLOFF_x.
  108. *
  109. * Note: This is only useful for PBR Materials at the moment. This could be extended if required to
  110. * other types of materials.
  111. */
  112. falloffType: number;
  113. /**
  114. * Strength of the light.
  115. * Note: By default it is define in the framework own unit.
  116. * Note: In PBR materials the intensityMode can be use to chose what unit the intensity is defined in.
  117. */
  118. intensity: number;
  119. private _range;
  120. protected _inverseSquaredRange: number;
  121. /**
  122. * Defines how far from the source the light is impacting in scene units.
  123. * Note: Unused in PBR material as the distance light falloff is defined following the inverse squared falloff.
  124. */
  125. get range(): number;
  126. /**
  127. * Defines how far from the source the light is impacting in scene units.
  128. * Note: Unused in PBR material as the distance light falloff is defined following the inverse squared falloff.
  129. */
  130. set range(value: number);
  131. /**
  132. * Cached photometric scale default to 1.0 as the automatic intensity mode defaults to 1.0 for every type
  133. * of light.
  134. */
  135. private _photometricScale;
  136. private _intensityMode;
  137. /**
  138. * Gets the photometric scale used to interpret the intensity.
  139. * This is only relevant with PBR Materials where the light intensity can be defined in a physical way.
  140. */
  141. get intensityMode(): number;
  142. /**
  143. * Sets the photometric scale used to interpret the intensity.
  144. * This is only relevant with PBR Materials where the light intensity can be defined in a physical way.
  145. */
  146. set intensityMode(value: number);
  147. private _radius;
  148. /**
  149. * Gets the light radius used by PBR Materials to simulate soft area lights.
  150. */
  151. get radius(): number;
  152. /**
  153. * sets the light radius used by PBR Materials to simulate soft area lights.
  154. */
  155. set radius(value: number);
  156. private _renderPriority;
  157. /**
  158. * Defines the rendering priority of the lights. It can help in case of fallback or number of lights
  159. * exceeding the number allowed of the materials.
  160. */
  161. renderPriority: number;
  162. private _shadowEnabled;
  163. /**
  164. * Gets whether or not the shadows are enabled for this light. This can help turning off/on shadow without detaching
  165. * the current shadow generator.
  166. */
  167. get shadowEnabled(): boolean;
  168. /**
  169. * Sets whether or not the shadows are enabled for this light. This can help turning off/on shadow without detaching
  170. * the current shadow generator.
  171. */
  172. set shadowEnabled(value: boolean);
  173. private _includedOnlyMeshes;
  174. /**
  175. * Gets the only meshes impacted by this light.
  176. */
  177. get includedOnlyMeshes(): AbstractMesh[];
  178. /**
  179. * Sets the only meshes impacted by this light.
  180. */
  181. set includedOnlyMeshes(value: AbstractMesh[]);
  182. private _excludedMeshes;
  183. /**
  184. * Gets the meshes not impacted by this light.
  185. */
  186. get excludedMeshes(): AbstractMesh[];
  187. /**
  188. * Sets the meshes not impacted by this light.
  189. */
  190. set excludedMeshes(value: AbstractMesh[]);
  191. private _excludeWithLayerMask;
  192. /**
  193. * Gets the layer id use to find what meshes are not impacted by the light.
  194. * Inactive if 0
  195. */
  196. get excludeWithLayerMask(): number;
  197. /**
  198. * Sets the layer id use to find what meshes are not impacted by the light.
  199. * Inactive if 0
  200. */
  201. set excludeWithLayerMask(value: number);
  202. private _includeOnlyWithLayerMask;
  203. /**
  204. * Gets the layer id use to find what meshes are impacted by the light.
  205. * Inactive if 0
  206. */
  207. get includeOnlyWithLayerMask(): number;
  208. /**
  209. * Sets the layer id use to find what meshes are impacted by the light.
  210. * Inactive if 0
  211. */
  212. set includeOnlyWithLayerMask(value: number);
  213. private _lightmapMode;
  214. /**
  215. * Gets the lightmap mode of this light (should be one of the constants defined by Light.LIGHTMAP_x)
  216. */
  217. get lightmapMode(): number;
  218. /**
  219. * Sets the lightmap mode of this light (should be one of the constants defined by Light.LIGHTMAP_x)
  220. */
  221. set lightmapMode(value: number);
  222. /**
  223. * Returns the view matrix.
  224. * @param _faceIndex The index of the face for which we want to extract the view matrix. Only used for point light types.
  225. * @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).
  226. */
  227. getViewMatrix(_faceIndex?: number): Nullable<Matrix>;
  228. /**
  229. * Returns the projection matrix.
  230. * 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).
  231. * @param _viewMatrix The view transform matrix of the light (optional).
  232. * @param _renderList The list of meshes to take into account when calculating the projection matrix (optional).
  233. * @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).
  234. */
  235. getProjectionMatrix(_viewMatrix?: Matrix, _renderList?: Array<AbstractMesh>): Nullable<Matrix>;
  236. /**
  237. * Shadow generators associated to the light.
  238. * @internal Internal use only.
  239. */
  240. _shadowGenerators: Nullable<Map<Nullable<Camera>, IShadowGenerator>>;
  241. /**
  242. * @internal Internal use only.
  243. */
  244. _excludedMeshesIds: string[];
  245. /**
  246. * @internal Internal use only.
  247. */
  248. _includedOnlyMeshesIds: string[];
  249. /**
  250. * The current light uniform buffer.
  251. * @internal Internal use only.
  252. */
  253. _uniformBuffer: UniformBuffer;
  254. /** @internal */
  255. _renderId: number;
  256. private _lastUseSpecular;
  257. /**
  258. * Creates a Light object in the scene.
  259. * Documentation : https://doc.babylonjs.com/features/featuresDeepDive/lights/lights_introduction
  260. * @param name The friendly name of the light
  261. * @param scene The scene the light belongs too
  262. */
  263. constructor(name: string, scene?: Scene);
  264. protected abstract _buildUniformLayout(): void;
  265. /**
  266. * Sets the passed Effect "effect" with the Light information.
  267. * @param effect The effect to update
  268. * @param lightIndex The index of the light in the effect to update
  269. * @returns The light
  270. */
  271. abstract transferToEffect(effect: Effect, lightIndex: string): Light;
  272. /**
  273. * Sets the passed Effect "effect" with the Light textures.
  274. * @param effect The effect to update
  275. * @param lightIndex The index of the light in the effect to update
  276. * @returns The light
  277. */
  278. transferTexturesToEffect(effect: Effect, lightIndex: string): Light;
  279. /**
  280. * Binds the lights information from the scene to the effect for the given mesh.
  281. * @param lightIndex Light index
  282. * @param scene The scene where the light belongs to
  283. * @param effect The effect we are binding the data to
  284. * @param useSpecular Defines if specular is supported
  285. * @param receiveShadows Defines if the effect (mesh) we bind the light for receives shadows
  286. */
  287. _bindLight(lightIndex: number, scene: Scene, effect: Effect, useSpecular: boolean, receiveShadows?: boolean): void;
  288. /**
  289. * Sets the passed Effect "effect" with the Light information.
  290. * @param effect The effect to update
  291. * @param lightDataUniformName The uniform used to store light data (position or direction)
  292. * @returns The light
  293. */
  294. abstract transferToNodeMaterialEffect(effect: Effect, lightDataUniformName: string): Light;
  295. /**
  296. * Returns the string "Light".
  297. * @returns the class name
  298. */
  299. getClassName(): string;
  300. /** @internal */
  301. readonly _isLight = true;
  302. /**
  303. * Converts the light information to a readable string for debug purpose.
  304. * @param fullDetails Supports for multiple levels of logging within scene loading
  305. * @returns the human readable light info
  306. */
  307. toString(fullDetails?: boolean): string;
  308. /** @internal */
  309. protected _syncParentEnabledState(): void;
  310. /**
  311. * Set the enabled state of this node.
  312. * @param value - the new enabled state
  313. */
  314. setEnabled(value: boolean): void;
  315. /**
  316. * Returns the Light associated shadow generator if any.
  317. * @param camera Camera for which the shadow generator should be retrieved (default: null). If null, retrieves the default shadow generator
  318. * @returns the associated shadow generator.
  319. */
  320. getShadowGenerator(camera?: Nullable<Camera>): Nullable<IShadowGenerator>;
  321. /**
  322. * Returns all the shadow generators associated to this light
  323. * @returns
  324. */
  325. getShadowGenerators(): Nullable<Map<Nullable<Camera>, IShadowGenerator>>;
  326. /**
  327. * Returns a Vector3, the absolute light position in the World.
  328. * @returns the world space position of the light
  329. */
  330. getAbsolutePosition(): Vector3;
  331. /**
  332. * Specifies if the light will affect the passed mesh.
  333. * @param mesh The mesh to test against the light
  334. * @returns true the mesh is affected otherwise, false.
  335. */
  336. canAffectMesh(mesh: AbstractMesh): boolean;
  337. /**
  338. * Releases resources associated with this node.
  339. * @param doNotRecurse Set to true to not recurse into each children (recurse into each children by default)
  340. * @param disposeMaterialAndTextures Set to true to also dispose referenced materials and textures (false by default)
  341. */
  342. dispose(doNotRecurse?: boolean, disposeMaterialAndTextures?: boolean): void;
  343. /**
  344. * Returns the light type ID (integer).
  345. * @returns The light Type id as a constant defines in Light.LIGHTTYPEID_x
  346. */
  347. getTypeID(): number;
  348. /**
  349. * Returns the intensity scaled by the Photometric Scale according to the light type and intensity mode.
  350. * @returns the scaled intensity in intensity mode unit
  351. */
  352. getScaledIntensity(): number;
  353. /**
  354. * Returns a new Light object, named "name", from the current one.
  355. * @param name The name of the cloned light
  356. * @param newParent The parent of this light, if it has one
  357. * @returns the new created light
  358. */
  359. clone(name: string, newParent?: Nullable<Node>): Nullable<Light>;
  360. /**
  361. * Serializes the current light into a Serialization object.
  362. * @returns the serialized object.
  363. */
  364. serialize(): any;
  365. /**
  366. * Creates a new typed light from the passed type (integer) : point light = 0, directional light = 1, spot light = 2, hemispheric light = 3.
  367. * This new light is named "name" and added to the passed scene.
  368. * @param type Type according to the types available in Light.LIGHTTYPEID_x
  369. * @param name The friendly name of the light
  370. * @param scene The scene the new light will belong to
  371. * @returns the constructor function
  372. */
  373. static GetConstructorFromName(type: number, name: string, scene: Scene): Nullable<() => Light>;
  374. /**
  375. * Parses the passed "parsedLight" and returns a new instanced Light from this parsing.
  376. * @param parsedLight The JSON representation of the light
  377. * @param scene The scene to create the parsed light in
  378. * @returns the created light after parsing
  379. */
  380. static Parse(parsedLight: any, scene: Scene): Nullable<Light>;
  381. private _hookArrayForExcluded;
  382. private _hookArrayForIncludedOnly;
  383. private _resyncMeshes;
  384. /**
  385. * Forces the meshes to update their light related information in their rendering used effects
  386. * @internal Internal Use Only
  387. */
  388. _markMeshesAsLightDirty(): void;
  389. /**
  390. * Recomputes the cached photometric scale if needed.
  391. */
  392. private _computePhotometricScale;
  393. /**
  394. * @returns the Photometric Scale according to the light type and intensity mode.
  395. */
  396. private _getPhotometricScale;
  397. /**
  398. * Reorder the light in the scene according to their defined priority.
  399. * @internal Internal Use Only
  400. */
  401. _reorderLightsInScene(): void;
  402. /**
  403. * Prepares the list of defines specific to the light type.
  404. * @param defines the list of defines
  405. * @param lightIndex defines the index of the light for the effect
  406. */
  407. abstract prepareLightSpecificDefines(defines: any, lightIndex: number): void;
  408. }