babylon.light.ts 28 KB

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