pointLight.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { __decorate } from "../tslib.es6.js";
  2. import { serialize } from "../Misc/decorators.js";
  3. import { Matrix, Vector3 } from "../Maths/math.vector.js";
  4. import { Node } from "../node.js";
  5. import { Light } from "./light.js";
  6. import { ShadowLight } from "./shadowLight.js";
  7. Node.AddNodeConstructor("Light_Type_0", (name, scene) => {
  8. return () => new PointLight(name, Vector3.Zero(), scene);
  9. });
  10. /**
  11. * A point light is a light defined by an unique point in world space.
  12. * The light is emitted in every direction from this point.
  13. * A good example of a point light is a standard light bulb.
  14. * Documentation: https://doc.babylonjs.com/features/featuresDeepDive/lights/lights_introduction
  15. */
  16. export class PointLight extends ShadowLight {
  17. /**
  18. * Getter: In case of direction provided, the shadow will not use a cube texture but simulate a spot shadow as a fallback
  19. * This specifies what angle the shadow will use to be created.
  20. *
  21. * It default to 90 degrees to work nicely with the cube texture generation for point lights shadow maps.
  22. */
  23. get shadowAngle() {
  24. return this._shadowAngle;
  25. }
  26. /**
  27. * Setter: In case of direction provided, the shadow will not use a cube texture but simulate a spot shadow as a fallback
  28. * This specifies what angle the shadow will use to be created.
  29. *
  30. * It default to 90 degrees to work nicely with the cube texture generation for point lights shadow maps.
  31. */
  32. set shadowAngle(value) {
  33. this._shadowAngle = value;
  34. this.forceProjectionMatrixCompute();
  35. }
  36. /**
  37. * Gets the direction if it has been set.
  38. * In case of direction provided, the shadow will not use a cube texture but simulate a spot shadow as a fallback
  39. */
  40. get direction() {
  41. return this._direction;
  42. }
  43. /**
  44. * In case of direction provided, the shadow will not use a cube texture but simulate a spot shadow as a fallback
  45. */
  46. set direction(value) {
  47. const previousNeedCube = this.needCube();
  48. this._direction = value;
  49. if (this.needCube() !== previousNeedCube && this._shadowGenerators) {
  50. const iterator = this._shadowGenerators.values();
  51. for (let key = iterator.next(); key.done !== true; key = iterator.next()) {
  52. const shadowGenerator = key.value;
  53. shadowGenerator.recreateShadowMap();
  54. }
  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/features/featuresDeepDive/lights/lights_introduction
  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, position, scene) {
  71. super(name, scene);
  72. this._shadowAngle = Math.PI / 2;
  73. this.position = position;
  74. }
  75. /**
  76. * Returns the string "PointLight"
  77. * @returns the class name
  78. */
  79. getClassName() {
  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. getTypeID() {
  87. return Light.LIGHTTYPEID_POINTLIGHT;
  88. }
  89. /**
  90. * Specifies whether or not the shadowmap should be a cube texture.
  91. * @returns true if the shadowmap needs to be a cube texture.
  92. */
  93. needCube() {
  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. getShadowDirection(faceIndex) {
  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. * @param matrix
  130. * @param viewMatrix
  131. * @param renderList
  132. */
  133. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  134. _setDefaultShadowProjectionMatrix(matrix, viewMatrix, renderList) {
  135. const activeCamera = this.getScene().activeCamera;
  136. if (!activeCamera) {
  137. return;
  138. }
  139. const minZ = this.shadowMinZ !== undefined ? this.shadowMinZ : activeCamera.minZ;
  140. const maxZ = this.shadowMaxZ !== undefined ? this.shadowMaxZ : activeCamera.maxZ;
  141. const useReverseDepthBuffer = this.getScene().getEngine().useReverseDepthBuffer;
  142. Matrix.PerspectiveFovLHToRef(this.shadowAngle, 1.0, useReverseDepthBuffer ? maxZ : minZ, useReverseDepthBuffer ? minZ : maxZ, matrix, true, this._scene.getEngine().isNDCHalfZRange, undefined, useReverseDepthBuffer);
  143. }
  144. _buildUniformLayout() {
  145. this._uniformBuffer.addUniform("vLightData", 4);
  146. this._uniformBuffer.addUniform("vLightDiffuse", 4);
  147. this._uniformBuffer.addUniform("vLightSpecular", 4);
  148. this._uniformBuffer.addUniform("vLightFalloff", 4);
  149. this._uniformBuffer.addUniform("shadowsInfo", 3);
  150. this._uniformBuffer.addUniform("depthValues", 2);
  151. this._uniformBuffer.create();
  152. }
  153. /**
  154. * Sets the passed Effect "effect" with the PointLight transformed position (or position, if none) and passed name (string).
  155. * @param effect The effect to update
  156. * @param lightIndex The index of the light in the effect to update
  157. * @returns The point light
  158. */
  159. transferToEffect(effect, lightIndex) {
  160. if (this.computeTransformedInformation()) {
  161. this._uniformBuffer.updateFloat4("vLightData", this.transformedPosition.x, this.transformedPosition.y, this.transformedPosition.z, 0.0, 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", this.range, this._inverseSquaredRange, 0, 0, lightIndex);
  167. return this;
  168. }
  169. transferToNodeMaterialEffect(effect, lightDataUniformName) {
  170. if (this.computeTransformedInformation()) {
  171. effect.setFloat3(lightDataUniformName, this.transformedPosition.x, this.transformedPosition.y, this.transformedPosition.z);
  172. }
  173. else {
  174. effect.setFloat3(lightDataUniformName, this.position.x, this.position.y, this.position.z);
  175. }
  176. return this;
  177. }
  178. /**
  179. * Prepares the list of defines specific to the light type.
  180. * @param defines the list of defines
  181. * @param lightIndex defines the index of the light for the effect
  182. */
  183. prepareLightSpecificDefines(defines, lightIndex) {
  184. defines["POINTLIGHT" + lightIndex] = true;
  185. }
  186. }
  187. __decorate([
  188. serialize()
  189. ], PointLight.prototype, "shadowAngle", null);
  190. //# sourceMappingURL=pointLight.js.map