light.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. import { serialize, SerializationHelper, serializeAsColor3, expandToProperty } from "../Misc/decorators";
  2. import { Nullable } from "../types";
  3. import { Scene } from "../scene";
  4. import { Vector3 } from "../Maths/math";
  5. import { Color3 } from "../Maths/math.color";
  6. import { Node } from "../node";
  7. import { AbstractMesh } from "../Meshes/abstractMesh";
  8. import { Effect } from "../Materials/effect";
  9. import { UniformBuffer } from "../Materials/uniformBuffer";
  10. import { IShadowGenerator } from "./Shadows/shadowGenerator";
  11. import { _TypeStore } from '../Misc/typeStore';
  12. /**
  13. * Base class of all the lights in Babylon. It groups all the generic information about lights.
  14. * Lights are used, as you would expect, to affect how meshes are seen, in terms of both illumination and colour.
  15. * 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.
  16. */
  17. export abstract class Light extends Node {
  18. /**
  19. * Falloff Default: light is falling off following the material specification:
  20. * standard material is using standard falloff whereas pbr material can request special falloff per materials.
  21. */
  22. public static readonly FALLOFF_DEFAULT = 0;
  23. /**
  24. * Falloff Physical: light is falling off following the inverse squared distance law.
  25. */
  26. public static readonly FALLOFF_PHYSICAL = 1;
  27. /**
  28. * Falloff gltf: light is falling off as described in the gltf moving to PBR document
  29. * to enhance interoperability with other engines.
  30. */
  31. public static readonly FALLOFF_GLTF = 2;
  32. /**
  33. * Falloff Standard: light is falling off like in the standard material
  34. * to enhance interoperability with other materials.
  35. */
  36. public static readonly FALLOFF_STANDARD = 3;
  37. //lightmapMode Consts
  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. public 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. public 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. public static readonly LIGHTMAP_SHADOWSONLY = 2;
  57. // Intensity Mode Consts
  58. /**
  59. * Each light type uses the default quantity according to its type:
  60. * point/spot lights use luminous intensity
  61. * directional lights use illuminance
  62. */
  63. public static readonly INTENSITYMODE_AUTOMATIC = 0;
  64. /**
  65. * lumen (lm)
  66. */
  67. public static readonly INTENSITYMODE_LUMINOUSPOWER = 1;
  68. /**
  69. * candela (lm/sr)
  70. */
  71. public static readonly INTENSITYMODE_LUMINOUSINTENSITY = 2;
  72. /**
  73. * lux (lm/m^2)
  74. */
  75. public static readonly INTENSITYMODE_ILLUMINANCE = 3;
  76. /**
  77. * nit (cd/m^2)
  78. */
  79. public static readonly INTENSITYMODE_LUMINANCE = 4;
  80. // Light types ids const.
  81. /**
  82. * Light type const id of the point light.
  83. */
  84. public static readonly LIGHTTYPEID_POINTLIGHT = 0;
  85. /**
  86. * Light type const id of the directional light.
  87. */
  88. public static readonly LIGHTTYPEID_DIRECTIONALLIGHT = 1;
  89. /**
  90. * Light type const id of the spot light.
  91. */
  92. public static readonly LIGHTTYPEID_SPOTLIGHT = 2;
  93. /**
  94. * Light type const id of the hemispheric light.
  95. */
  96. public static readonly LIGHTTYPEID_HEMISPHERICLIGHT = 3;
  97. /**
  98. * Diffuse gives the basic color to an object.
  99. */
  100. @serializeAsColor3()
  101. public diffuse = new Color3(1.0, 1.0, 1.0);
  102. /**
  103. * Specular produces a highlight color on an object.
  104. * Note: This is note affecting PBR materials.
  105. */
  106. @serializeAsColor3()
  107. public specular = new Color3(1.0, 1.0, 1.0);
  108. /**
  109. * Defines the falloff type for this light. This lets overrriding how punctual light are
  110. * falling off base on range or angle.
  111. * This can be set to any values in Light.FALLOFF_x.
  112. *
  113. * Note: This is only useful for PBR Materials at the moment. This could be extended if required to
  114. * other types of materials.
  115. */
  116. @serialize()
  117. public falloffType = Light.FALLOFF_DEFAULT;
  118. /**
  119. * Strength of the light.
  120. * Note: By default it is define in the framework own unit.
  121. * Note: In PBR materials the intensityMode can be use to chose what unit the intensity is defined in.
  122. */
  123. @serialize()
  124. public intensity = 1.0;
  125. private _range = Number.MAX_VALUE;
  126. protected _inverseSquaredRange = 0;
  127. /**
  128. * Defines how far from the source the light is impacting in scene units.
  129. * Note: Unused in PBR material as the distance light falloff is defined following the inverse squared falloff.
  130. */
  131. @serialize()
  132. public get range(): number {
  133. return this._range;
  134. }
  135. /**
  136. * Defines how far from the source the light is impacting in scene units.
  137. * Note: Unused in PBR material as the distance light falloff is defined following the inverse squared falloff.
  138. */
  139. public set range(value: number) {
  140. this._range = value;
  141. this._inverseSquaredRange = 1.0 / (this.range * this.range);
  142. }
  143. /**
  144. * Cached photometric scale default to 1.0 as the automatic intensity mode defaults to 1.0 for every type
  145. * of light.
  146. */
  147. private _photometricScale = 1.0;
  148. private _intensityMode: number = Light.INTENSITYMODE_AUTOMATIC;
  149. /**
  150. * Gets the photometric scale used to interpret the intensity.
  151. * This is only relevant with PBR Materials where the light intensity can be defined in a physical way.
  152. */
  153. @serialize()
  154. public get intensityMode(): number {
  155. return this._intensityMode;
  156. }
  157. /**
  158. * Sets the photometric scale used to interpret the intensity.
  159. * This is only relevant with PBR Materials where the light intensity can be defined in a physical way.
  160. */
  161. public set intensityMode(value: number) {
  162. this._intensityMode = value;
  163. this._computePhotometricScale();
  164. }
  165. private _radius = 0.00001;
  166. /**
  167. * Gets the light radius used by PBR Materials to simulate soft area lights.
  168. */
  169. @serialize()
  170. public get radius(): number {
  171. return this._radius;
  172. }
  173. /**
  174. * sets the light radius used by PBR Materials to simulate soft area lights.
  175. */
  176. public set radius(value: number) {
  177. this._radius = value;
  178. this._computePhotometricScale();
  179. }
  180. @serialize()
  181. private _renderPriority: number;
  182. /**
  183. * Defines the rendering priority of the lights. It can help in case of fallback or number of lights
  184. * exceeding the number allowed of the materials.
  185. */
  186. @expandToProperty("_reorderLightsInScene")
  187. public renderPriority: number = 0;
  188. @serialize("shadowEnabled")
  189. private _shadowEnabled: boolean = true;
  190. /**
  191. * Gets wether or not the shadows are enabled for this light. This can help turning off/on shadow without detaching
  192. * the current shadow generator.
  193. */
  194. public get shadowEnabled(): boolean {
  195. return this._shadowEnabled;
  196. }
  197. /**
  198. * Sets wether or not the shadows are enabled for this light. This can help turning off/on shadow without detaching
  199. * the current shadow generator.
  200. */
  201. public set shadowEnabled(value: boolean) {
  202. if (this._shadowEnabled === value) {
  203. return;
  204. }
  205. this._shadowEnabled = value;
  206. this._markMeshesAsLightDirty();
  207. }
  208. private _includedOnlyMeshes: AbstractMesh[];
  209. /**
  210. * Gets the only meshes impacted by this light.
  211. */
  212. public get includedOnlyMeshes(): AbstractMesh[] {
  213. return this._includedOnlyMeshes;
  214. }
  215. /**
  216. * Sets the only meshes impacted by this light.
  217. */
  218. public set includedOnlyMeshes(value: AbstractMesh[]) {
  219. this._includedOnlyMeshes = value;
  220. this._hookArrayForIncludedOnly(value);
  221. }
  222. private _excludedMeshes: AbstractMesh[];
  223. /**
  224. * Gets the meshes not impacted by this light.
  225. */
  226. public get excludedMeshes(): AbstractMesh[] {
  227. return this._excludedMeshes;
  228. }
  229. /**
  230. * Sets the meshes not impacted by this light.
  231. */
  232. public set excludedMeshes(value: AbstractMesh[]) {
  233. this._excludedMeshes = value;
  234. this._hookArrayForExcluded(value);
  235. }
  236. @serialize("excludeWithLayerMask")
  237. private _excludeWithLayerMask = 0;
  238. /**
  239. * Gets the layer id use to find what meshes are not impacted by the light.
  240. * Inactive if 0
  241. */
  242. public get excludeWithLayerMask(): number {
  243. return this._excludeWithLayerMask;
  244. }
  245. /**
  246. * Sets the layer id use to find what meshes are not impacted by the light.
  247. * Inactive if 0
  248. */
  249. public set excludeWithLayerMask(value: number) {
  250. this._excludeWithLayerMask = value;
  251. this._resyncMeshes();
  252. }
  253. @serialize("includeOnlyWithLayerMask")
  254. private _includeOnlyWithLayerMask = 0;
  255. /**
  256. * Gets the layer id use to find what meshes are impacted by the light.
  257. * Inactive if 0
  258. */
  259. public get includeOnlyWithLayerMask(): number {
  260. return this._includeOnlyWithLayerMask;
  261. }
  262. /**
  263. * Sets the layer id use to find what meshes are impacted by the light.
  264. * Inactive if 0
  265. */
  266. public set includeOnlyWithLayerMask(value: number) {
  267. this._includeOnlyWithLayerMask = value;
  268. this._resyncMeshes();
  269. }
  270. @serialize("lightmapMode")
  271. private _lightmapMode = 0;
  272. /**
  273. * Gets the lightmap mode of this light (should be one of the constants defined by Light.LIGHTMAP_x)
  274. */
  275. public get lightmapMode(): number {
  276. return this._lightmapMode;
  277. }
  278. /**
  279. * Sets the lightmap mode of this light (should be one of the constants defined by Light.LIGHTMAP_x)
  280. */
  281. public set lightmapMode(value: number) {
  282. if (this._lightmapMode === value) {
  283. return;
  284. }
  285. this._lightmapMode = value;
  286. this._markMeshesAsLightDirty();
  287. }
  288. /**
  289. * Shadow generator associted to the light.
  290. * @hidden Internal use only.
  291. */
  292. public _shadowGenerator: Nullable<IShadowGenerator>;
  293. /**
  294. * @hidden Internal use only.
  295. */
  296. public _excludedMeshesIds = new Array<string>();
  297. /**
  298. * @hidden Internal use only.
  299. */
  300. public _includedOnlyMeshesIds = new Array<string>();
  301. /**
  302. * The current light unifom buffer.
  303. * @hidden Internal use only.
  304. */
  305. public _uniformBuffer: UniformBuffer;
  306. /**
  307. * Creates a Light object in the scene.
  308. * Documentation : https://doc.babylonjs.com/babylon101/lights
  309. * @param name The firendly name of the light
  310. * @param scene The scene the light belongs too
  311. */
  312. constructor(name: string, scene: Scene) {
  313. super(name, scene);
  314. this.getScene().addLight(this);
  315. this._uniformBuffer = new UniformBuffer(this.getScene().getEngine());
  316. this._buildUniformLayout();
  317. this.includedOnlyMeshes = new Array<AbstractMesh>();
  318. this.excludedMeshes = new Array<AbstractMesh>();
  319. this._resyncMeshes();
  320. }
  321. protected abstract _buildUniformLayout(): void;
  322. /**
  323. * Sets the passed Effect "effect" with the Light information.
  324. * @param effect The effect to update
  325. * @param lightIndex The index of the light in the effect to update
  326. * @returns The light
  327. */
  328. public abstract transferToEffect(effect: Effect, lightIndex: string): Light;
  329. /**
  330. * Returns the string "Light".
  331. * @returns the class name
  332. */
  333. public getClassName(): string {
  334. return "Light";
  335. }
  336. /** @hidden */
  337. public readonly _isLight = true;
  338. /**
  339. * Converts the light information to a readable string for debug purpose.
  340. * @param fullDetails Supports for multiple levels of logging within scene loading
  341. * @returns the human readable light info
  342. */
  343. public toString(fullDetails?: boolean): string {
  344. var ret = "Name: " + this.name;
  345. ret += ", type: " + (["Point", "Directional", "Spot", "Hemispheric"])[this.getTypeID()];
  346. if (this.animations) {
  347. for (var i = 0; i < this.animations.length; i++) {
  348. ret += ", animation[0]: " + this.animations[i].toString(fullDetails);
  349. }
  350. }
  351. if (fullDetails) {
  352. }
  353. return ret;
  354. }
  355. /** @hidden */
  356. protected _syncParentEnabledState() {
  357. super._syncParentEnabledState();
  358. this._resyncMeshes();
  359. }
  360. /**
  361. * Set the enabled state of this node.
  362. * @param value - the new enabled state
  363. */
  364. public setEnabled(value: boolean): void {
  365. super.setEnabled(value);
  366. this._resyncMeshes();
  367. }
  368. /**
  369. * Returns the Light associated shadow generator if any.
  370. * @return the associated shadow generator.
  371. */
  372. public getShadowGenerator(): Nullable<IShadowGenerator> {
  373. return this._shadowGenerator;
  374. }
  375. /**
  376. * Returns a Vector3, the absolute light position in the World.
  377. * @returns the world space position of the light
  378. */
  379. public getAbsolutePosition(): Vector3 {
  380. return Vector3.Zero();
  381. }
  382. /**
  383. * Specifies if the light will affect the passed mesh.
  384. * @param mesh The mesh to test against the light
  385. * @return true the mesh is affected otherwise, false.
  386. */
  387. public canAffectMesh(mesh: AbstractMesh): boolean {
  388. if (!mesh) {
  389. return true;
  390. }
  391. if (this.includedOnlyMeshes && this.includedOnlyMeshes.length > 0 && this.includedOnlyMeshes.indexOf(mesh) === -1) {
  392. return false;
  393. }
  394. if (this.excludedMeshes && this.excludedMeshes.length > 0 && this.excludedMeshes.indexOf(mesh) !== -1) {
  395. return false;
  396. }
  397. if (this.includeOnlyWithLayerMask !== 0 && (this.includeOnlyWithLayerMask & mesh.layerMask) === 0) {
  398. return false;
  399. }
  400. if (this.excludeWithLayerMask !== 0 && this.excludeWithLayerMask & mesh.layerMask) {
  401. return false;
  402. }
  403. return true;
  404. }
  405. /**
  406. * Sort function to order lights for rendering.
  407. * @param a First Light object to compare to second.
  408. * @param b Second Light object to compare first.
  409. * @return -1 to reduce's a's index relative to be, 0 for no change, 1 to increase a's index relative to b.
  410. */
  411. public static CompareLightsPriority(a: Light, b: Light): number {
  412. //shadow-casting lights have priority over non-shadow-casting lights
  413. //the renderPrioirty is a secondary sort criterion
  414. if (a.shadowEnabled !== b.shadowEnabled) {
  415. return (b.shadowEnabled ? 1 : 0) - (a.shadowEnabled ? 1 : 0);
  416. }
  417. return b.renderPriority - a.renderPriority;
  418. }
  419. /**
  420. * Releases resources associated with this node.
  421. * @param doNotRecurse Set to true to not recurse into each children (recurse into each children by default)
  422. * @param disposeMaterialAndTextures Set to true to also dispose referenced materials and textures (false by default)
  423. */
  424. public dispose(doNotRecurse?: boolean, disposeMaterialAndTextures = false): void {
  425. if (this._shadowGenerator) {
  426. this._shadowGenerator.dispose();
  427. this._shadowGenerator = null;
  428. }
  429. // Animations
  430. this.getScene().stopAnimation(this);
  431. // Remove from meshes
  432. for (var mesh of this.getScene().meshes) {
  433. mesh._removeLightSource(this);
  434. }
  435. this._uniformBuffer.dispose();
  436. // Remove from scene
  437. this.getScene().removeLight(this);
  438. super.dispose(doNotRecurse, disposeMaterialAndTextures);
  439. }
  440. /**
  441. * Returns the light type ID (integer).
  442. * @returns The light Type id as a constant defines in Light.LIGHTTYPEID_x
  443. */
  444. public getTypeID(): number {
  445. return 0;
  446. }
  447. /**
  448. * Returns the intensity scaled by the Photometric Scale according to the light type and intensity mode.
  449. * @returns the scaled intensity in intensity mode unit
  450. */
  451. public getScaledIntensity() {
  452. return this._photometricScale * this.intensity;
  453. }
  454. /**
  455. * Returns a new Light object, named "name", from the current one.
  456. * @param name The name of the cloned light
  457. * @returns the new created light
  458. */
  459. public clone(name: string): Nullable<Light> {
  460. let constructor = Light.GetConstructorFromName(this.getTypeID(), name, this.getScene());
  461. if (!constructor) {
  462. return null;
  463. }
  464. return SerializationHelper.Clone(constructor, this);
  465. }
  466. /**
  467. * Serializes the current light into a Serialization object.
  468. * @returns the serialized object.
  469. */
  470. public serialize(): any {
  471. var serializationObject = SerializationHelper.Serialize(this);
  472. // Type
  473. serializationObject.type = this.getTypeID();
  474. // Parent
  475. if (this.parent) {
  476. serializationObject.parentId = this.parent.id;
  477. }
  478. // Inclusion / exclusions
  479. if (this.excludedMeshes.length > 0) {
  480. serializationObject.excludedMeshesIds = [];
  481. this.excludedMeshes.forEach((mesh: AbstractMesh) => {
  482. serializationObject.excludedMeshesIds.push(mesh.id);
  483. });
  484. }
  485. if (this.includedOnlyMeshes.length > 0) {
  486. serializationObject.includedOnlyMeshesIds = [];
  487. this.includedOnlyMeshes.forEach((mesh: AbstractMesh) => {
  488. serializationObject.includedOnlyMeshesIds.push(mesh.id);
  489. });
  490. }
  491. // Animations
  492. SerializationHelper.AppendSerializedAnimations(this, serializationObject);
  493. serializationObject.ranges = this.serializeAnimationRanges();
  494. return serializationObject;
  495. }
  496. /**
  497. * Creates a new typed light from the passed type (integer) : point light = 0, directional light = 1, spot light = 2, hemispheric light = 3.
  498. * This new light is named "name" and added to the passed scene.
  499. * @param type Type according to the types available in Light.LIGHTTYPEID_x
  500. * @param name The friendly name of the light
  501. * @param scene The scene the new light will belong to
  502. * @returns the constructor function
  503. */
  504. static GetConstructorFromName(type: number, name: string, scene: Scene): Nullable<() => Light> {
  505. let constructorFunc = Node.Construct("Light_Type_" + type, name, scene);
  506. if (constructorFunc) {
  507. return <() => Light>constructorFunc;
  508. }
  509. // Default to no light for none present once.
  510. return null;
  511. }
  512. /**
  513. * Parses the passed "parsedLight" and returns a new instanced Light from this parsing.
  514. * @param parsedLight The JSON representation of the light
  515. * @param scene The scene to create the parsed light in
  516. * @returns the created light after parsing
  517. */
  518. public static Parse(parsedLight: any, scene: Scene): Nullable<Light> {
  519. let constructor = Light.GetConstructorFromName(parsedLight.type, parsedLight.name, scene);
  520. if (!constructor) {
  521. return null;
  522. }
  523. var light = SerializationHelper.Parse(constructor, parsedLight, scene);
  524. // Inclusion / exclusions
  525. if (parsedLight.excludedMeshesIds) {
  526. light._excludedMeshesIds = parsedLight.excludedMeshesIds;
  527. }
  528. if (parsedLight.includedOnlyMeshesIds) {
  529. light._includedOnlyMeshesIds = parsedLight.includedOnlyMeshesIds;
  530. }
  531. // Parent
  532. if (parsedLight.parentId) {
  533. light._waitingParentId = parsedLight.parentId;
  534. }
  535. // Falloff
  536. if (parsedLight.falloffType !== undefined) {
  537. light.falloffType = parsedLight.falloffType;
  538. }
  539. // Lightmaps
  540. if (parsedLight.lightmapMode !== undefined) {
  541. light.lightmapMode = parsedLight.lightmapMode;
  542. }
  543. // Animations
  544. if (parsedLight.animations) {
  545. for (var animationIndex = 0; animationIndex < parsedLight.animations.length; animationIndex++) {
  546. var parsedAnimation = parsedLight.animations[animationIndex];
  547. const internalClass = _TypeStore.GetClass("BABYLON.Animation");
  548. if (internalClass) {
  549. light.animations.push(internalClass.Parse(parsedAnimation));
  550. }
  551. }
  552. Node.ParseAnimationRanges(light, parsedLight, scene);
  553. }
  554. if (parsedLight.autoAnimate) {
  555. scene.beginAnimation(light, parsedLight.autoAnimateFrom, parsedLight.autoAnimateTo, parsedLight.autoAnimateLoop, parsedLight.autoAnimateSpeed || 1.0);
  556. }
  557. return light;
  558. }
  559. private _hookArrayForExcluded(array: AbstractMesh[]): void {
  560. var oldPush = array.push;
  561. array.push = (...items: AbstractMesh[]) => {
  562. var result = oldPush.apply(array, items);
  563. for (var item of items) {
  564. item._resyncLighSource(this);
  565. }
  566. return result;
  567. };
  568. var oldSplice = array.splice;
  569. array.splice = (index: number, deleteCount?: number) => {
  570. var deleted = oldSplice.apply(array, [index, deleteCount]);
  571. for (var item of deleted) {
  572. item._resyncLighSource(this);
  573. }
  574. return deleted;
  575. };
  576. for (var item of array) {
  577. item._resyncLighSource(this);
  578. }
  579. }
  580. private _hookArrayForIncludedOnly(array: AbstractMesh[]): void {
  581. var oldPush = array.push;
  582. array.push = (...items: AbstractMesh[]) => {
  583. var result = oldPush.apply(array, items);
  584. this._resyncMeshes();
  585. return result;
  586. };
  587. var oldSplice = array.splice;
  588. array.splice = (index: number, deleteCount?: number) => {
  589. var deleted = oldSplice.apply(array, [index, deleteCount]);
  590. this._resyncMeshes();
  591. return deleted;
  592. };
  593. this._resyncMeshes();
  594. }
  595. private _resyncMeshes() {
  596. for (var mesh of this.getScene().meshes) {
  597. mesh._resyncLighSource(this);
  598. }
  599. }
  600. /**
  601. * Forces the meshes to update their light related information in their rendering used effects
  602. * @hidden Internal Use Only
  603. */
  604. public _markMeshesAsLightDirty() {
  605. for (var mesh of this.getScene().meshes) {
  606. if (mesh.lightSources.indexOf(this) !== -1) {
  607. mesh._markSubMeshesAsLightDirty();
  608. }
  609. }
  610. }
  611. /**
  612. * Recomputes the cached photometric scale if needed.
  613. */
  614. private _computePhotometricScale(): void {
  615. this._photometricScale = this._getPhotometricScale();
  616. this.getScene().resetCachedMaterial();
  617. }
  618. /**
  619. * Returns the Photometric Scale according to the light type and intensity mode.
  620. */
  621. private _getPhotometricScale() {
  622. let photometricScale = 0.0;
  623. let lightTypeID = this.getTypeID();
  624. //get photometric mode
  625. let photometricMode = this.intensityMode;
  626. if (photometricMode === Light.INTENSITYMODE_AUTOMATIC) {
  627. if (lightTypeID === Light.LIGHTTYPEID_DIRECTIONALLIGHT) {
  628. photometricMode = Light.INTENSITYMODE_ILLUMINANCE;
  629. } else {
  630. photometricMode = Light.INTENSITYMODE_LUMINOUSINTENSITY;
  631. }
  632. }
  633. //compute photometric scale
  634. switch (lightTypeID) {
  635. case Light.LIGHTTYPEID_POINTLIGHT:
  636. case Light.LIGHTTYPEID_SPOTLIGHT:
  637. switch (photometricMode) {
  638. case Light.INTENSITYMODE_LUMINOUSPOWER:
  639. photometricScale = 1.0 / (4.0 * Math.PI);
  640. break;
  641. case Light.INTENSITYMODE_LUMINOUSINTENSITY:
  642. photometricScale = 1.0;
  643. break;
  644. case Light.INTENSITYMODE_LUMINANCE:
  645. photometricScale = this.radius * this.radius;
  646. break;
  647. }
  648. break;
  649. case Light.LIGHTTYPEID_DIRECTIONALLIGHT:
  650. switch (photometricMode) {
  651. case Light.INTENSITYMODE_ILLUMINANCE:
  652. photometricScale = 1.0;
  653. break;
  654. case Light.INTENSITYMODE_LUMINANCE:
  655. // When radius (and therefore solid angle) is non-zero a directional lights brightness can be specified via central (peak) luminance.
  656. // For a directional light the 'radius' defines the angular radius (in radians) rather than world-space radius (e.g. in metres).
  657. let apexAngleRadians = this.radius;
  658. // Impose a minimum light angular size to avoid the light becoming an infinitely small angular light source (i.e. a dirac delta function).
  659. apexAngleRadians = Math.max(apexAngleRadians, 0.001);
  660. let solidAngle = 2.0 * Math.PI * (1.0 - Math.cos(apexAngleRadians));
  661. photometricScale = solidAngle;
  662. break;
  663. }
  664. break;
  665. case Light.LIGHTTYPEID_HEMISPHERICLIGHT:
  666. // No fall off in hemisperic light.
  667. photometricScale = 1.0;
  668. break;
  669. }
  670. return photometricScale;
  671. }
  672. /**
  673. * Reorder the light in the scene according to their defined priority.
  674. * @hidden Internal Use Only
  675. */
  676. public _reorderLightsInScene(): void {
  677. var scene = this.getScene();
  678. if (this._renderPriority != 0) {
  679. scene.requireLightSorting = true;
  680. }
  681. this.getScene().sortLightsByPriority();
  682. }
  683. /**
  684. * Prepares the list of defines specific to the light type.
  685. * @param defines the list of defines
  686. * @param lightIndex defines the index of the light for the effect
  687. */
  688. public abstract prepareLightSpecificDefines(defines: any, lightIndex: number): void;
  689. }