pointLight.ts 7.8 KB

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