pointLight.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import { serialize } from "../Misc/decorators";
  2. import { Scene } from "../scene";
  3. import { Matrix, Vector3 } from "../Maths/math";
  4. import { Node } from "../node";
  5. import { AbstractMesh } from "../Meshes/abstractMesh";
  6. import { Light } from "./light";
  7. import { ShadowLight } from "./shadowLight";
  8. import { _TimeToken } from "../Instrumentation/timeToken";
  9. import { _DepthCullingState, _StencilState, _AlphaState } from "../States/index";
  10. import { Effect } from "../Materials/effect";
  11. Node.AddNodeConstructor("Light_Type_0", (name, scene) => {
  12. return () => new PointLight(name, Vector3.Zero(), scene);
  13. });
  14. /**
  15. * A point light is a light defined by an unique point in world space.
  16. * The light is emitted in every direction from this point.
  17. * A good example of a point light is a standard light bulb.
  18. * Documentation: https://doc.babylonjs.com/babylon101/lights
  19. */
  20. export class PointLight extends ShadowLight {
  21. private _shadowAngle = Math.PI / 2;
  22. /**
  23. * Getter: In case of direction provided, the shadow will not use a cube texture but simulate a spot shadow as a fallback
  24. * This specifies what angle the shadow will use to be created.
  25. *
  26. * It default to 90 degrees to work nicely with the cube texture generation for point lights shadow maps.
  27. */
  28. @serialize()
  29. public get shadowAngle(): number {
  30. return this._shadowAngle;
  31. }
  32. /**
  33. * Setter: In case of direction provided, the shadow will not use a cube texture but simulate a spot shadow as a fallback
  34. * This specifies what angle the shadow will use to be created.
  35. *
  36. * It default to 90 degrees to work nicely with the cube texture generation for point lights shadow maps.
  37. */
  38. public set shadowAngle(value: number) {
  39. this._shadowAngle = value;
  40. this.forceProjectionMatrixCompute();
  41. }
  42. /**
  43. * Gets the direction if it has been set.
  44. * In case of direction provided, the shadow will not use a cube texture but simulate a spot shadow as a fallback
  45. */
  46. public get direction(): Vector3 {
  47. return this._direction;
  48. }
  49. /**
  50. * In case of direction provided, the shadow will not use a cube texture but simulate a spot shadow as a fallback
  51. */
  52. public set direction(value: Vector3) {
  53. var previousNeedCube = this.needCube();
  54. this._direction = value;
  55. if (this.needCube() !== previousNeedCube && this._shadowGenerator) {
  56. this._shadowGenerator.recreateShadowMap();
  57. }
  58. }
  59. /**
  60. * Creates a PointLight object from the passed name and position (Vector3) and adds it in the scene.
  61. * A PointLight emits the light in every direction.
  62. * It can cast shadows.
  63. * If the scene camera is already defined and you want to set your PointLight at the camera position, just set it :
  64. * ```javascript
  65. * var pointLight = new PointLight("pl", camera.position, scene);
  66. * ```
  67. * Documentation : https://doc.babylonjs.com/babylon101/lights
  68. * @param name The light friendly name
  69. * @param position The position of the point light in the scene
  70. * @param scene The scene the lights belongs to
  71. */
  72. constructor(name: string, position: Vector3, scene: Scene) {
  73. super(name, scene);
  74. this.position = position;
  75. }
  76. /**
  77. * Returns the string "PointLight"
  78. * @returns the class name
  79. */
  80. public getClassName(): string {
  81. return "PointLight";
  82. }
  83. /**
  84. * Returns the integer 0.
  85. * @returns The light Type id as a constant defines in Light.LIGHTTYPEID_x
  86. */
  87. public getTypeID(): number {
  88. return Light.LIGHTTYPEID_POINTLIGHT;
  89. }
  90. /**
  91. * Specifies wether or not the shadowmap should be a cube texture.
  92. * @returns true if the shadowmap needs to be a cube texture.
  93. */
  94. public needCube(): boolean {
  95. return !this.direction;
  96. }
  97. /**
  98. * Returns a new Vector3 aligned with the PointLight cube system according to the passed cube face index (integer).
  99. * @param faceIndex The index of the face we are computed the direction to generate shadow
  100. * @returns The set direction in 2d mode otherwise the direction to the cubemap face if needCube() is true
  101. */
  102. public getShadowDirection(faceIndex?: number): Vector3 {
  103. if (this.direction) {
  104. return super.getShadowDirection(faceIndex);
  105. }
  106. else {
  107. switch (faceIndex) {
  108. case 0:
  109. return new Vector3(1.0, 0.0, 0.0);
  110. case 1:
  111. return new Vector3(-1.0, 0.0, 0.0);
  112. case 2:
  113. return new Vector3(0.0, -1.0, 0.0);
  114. case 3:
  115. return new Vector3(0.0, 1.0, 0.0);
  116. case 4:
  117. return new Vector3(0.0, 0.0, 1.0);
  118. case 5:
  119. return new Vector3(0.0, 0.0, -1.0);
  120. }
  121. }
  122. return Vector3.Zero();
  123. }
  124. /**
  125. * Sets the passed matrix "matrix" as a left-handed perspective projection matrix with the following settings :
  126. * - fov = PI / 2
  127. * - aspect ratio : 1.0
  128. * - z-near and far equal to the active camera minZ and maxZ.
  129. * Returns the PointLight.
  130. */
  131. protected _setDefaultShadowProjectionMatrix(matrix: Matrix, viewMatrix: Matrix, renderList: Array<AbstractMesh>): void {
  132. var activeCamera = this.getScene().activeCamera;
  133. if (!activeCamera) {
  134. return;
  135. }
  136. Matrix.PerspectiveFovLHToRef(this.shadowAngle, 1.0,
  137. this.getDepthMinZ(activeCamera), this.getDepthMaxZ(activeCamera), matrix);
  138. }
  139. protected _buildUniformLayout(): void {
  140. this._uniformBuffer.addUniform("vLightData", 4);
  141. this._uniformBuffer.addUniform("vLightDiffuse", 4);
  142. this._uniformBuffer.addUniform("vLightSpecular", 3);
  143. this._uniformBuffer.addUniform("vLightFalloff", 4);
  144. this._uniformBuffer.addUniform("shadowsInfo", 3);
  145. this._uniformBuffer.addUniform("depthValues", 2);
  146. this._uniformBuffer.create();
  147. }
  148. /**
  149. * Sets the passed Effect "effect" with the PointLight transformed position (or position, if none) and passed name (string).
  150. * @param effect The effect to update
  151. * @param lightIndex The index of the light in the effect to update
  152. * @returns The point light
  153. */
  154. public transferToEffect(effect: Effect, lightIndex: string): PointLight {
  155. if (this.computeTransformedInformation()) {
  156. this._uniformBuffer.updateFloat4("vLightData",
  157. this.transformedPosition.x,
  158. this.transformedPosition.y,
  159. this.transformedPosition.z,
  160. 0.0,
  161. lightIndex);
  162. }
  163. else {
  164. this._uniformBuffer.updateFloat4("vLightData", this.position.x, this.position.y, this.position.z, 0, lightIndex);
  165. }
  166. this._uniformBuffer.updateFloat4("vLightFalloff",
  167. this.range,
  168. this._inverseSquaredRange,
  169. 0,
  170. 0,
  171. lightIndex
  172. );
  173. return this;
  174. }
  175. /**
  176. * Prepares the list of defines specific to the light type.
  177. * @param defines the list of defines
  178. * @param lightIndex defines the index of the light for the effect
  179. */
  180. public prepareLightSpecificDefines(defines: any, lightIndex: number): void {
  181. defines["POINTLIGHT" + lightIndex] = true;
  182. }
  183. }