pointLight.ts 7.9 KB

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