hemisphericLight.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { __decorate } from "../tslib.es6.js";
  2. import { serializeAsColor3, serializeAsVector3 } from "../Misc/decorators.js";
  3. import { Matrix, Vector3 } from "../Maths/math.vector.js";
  4. import { Color3 } from "../Maths/math.color.js";
  5. import { Node } from "../node.js";
  6. import { Light } from "./light.js";
  7. Node.AddNodeConstructor("Light_Type_3", (name, scene) => {
  8. return () => new HemisphericLight(name, Vector3.Zero(), scene);
  9. });
  10. /**
  11. * The HemisphericLight simulates the ambient environment light,
  12. * so the passed direction is the light reflection direction, not the incoming direction.
  13. */
  14. export class HemisphericLight extends Light {
  15. /**
  16. * Creates a HemisphericLight object in the scene according to the passed direction (Vector3).
  17. * The HemisphericLight simulates the ambient environment light, so the passed direction is the light reflection direction, not the incoming direction.
  18. * The HemisphericLight can't cast shadows.
  19. * Documentation : https://doc.babylonjs.com/features/featuresDeepDive/lights/lights_introduction
  20. * @param name The friendly name of the light
  21. * @param direction The direction of the light reflection
  22. * @param scene The scene the light belongs to
  23. */
  24. constructor(name, direction, scene) {
  25. super(name, scene);
  26. /**
  27. * The groundColor is the light in the opposite direction to the one specified during creation.
  28. * You can think of the diffuse and specular light as coming from the centre of the object in the given direction and the groundColor light in the opposite direction.
  29. */
  30. this.groundColor = new Color3(0.0, 0.0, 0.0);
  31. this.direction = direction || Vector3.Up();
  32. }
  33. _buildUniformLayout() {
  34. this._uniformBuffer.addUniform("vLightData", 4);
  35. this._uniformBuffer.addUniform("vLightDiffuse", 4);
  36. this._uniformBuffer.addUniform("vLightSpecular", 4);
  37. this._uniformBuffer.addUniform("vLightGround", 3);
  38. this._uniformBuffer.addUniform("shadowsInfo", 3);
  39. this._uniformBuffer.addUniform("depthValues", 2);
  40. this._uniformBuffer.create();
  41. }
  42. /**
  43. * Returns the string "HemisphericLight".
  44. * @returns The class name
  45. */
  46. getClassName() {
  47. return "HemisphericLight";
  48. }
  49. /**
  50. * Sets the HemisphericLight direction towards the passed target (Vector3).
  51. * Returns the updated direction.
  52. * @param target The target the direction should point to
  53. * @returns The computed direction
  54. */
  55. setDirectionToTarget(target) {
  56. this.direction = Vector3.Normalize(target.subtract(Vector3.Zero()));
  57. return this.direction;
  58. }
  59. /**
  60. * Returns the shadow generator associated to the light.
  61. * @returns Always null for hemispheric lights because it does not support shadows.
  62. */
  63. getShadowGenerator() {
  64. return null;
  65. }
  66. /**
  67. * Sets the passed Effect object with the HemisphericLight normalized direction and color and the passed name (string).
  68. * @param _effect The effect to update
  69. * @param lightIndex The index of the light in the effect to update
  70. * @returns The hemispheric light
  71. */
  72. transferToEffect(_effect, lightIndex) {
  73. const normalizeDirection = Vector3.Normalize(this.direction);
  74. this._uniformBuffer.updateFloat4("vLightData", normalizeDirection.x, normalizeDirection.y, normalizeDirection.z, 0.0, lightIndex);
  75. this._uniformBuffer.updateColor3("vLightGround", this.groundColor.scale(this.intensity), lightIndex);
  76. return this;
  77. }
  78. transferToNodeMaterialEffect(effect, lightDataUniformName) {
  79. const normalizeDirection = Vector3.Normalize(this.direction);
  80. effect.setFloat3(lightDataUniformName, normalizeDirection.x, normalizeDirection.y, normalizeDirection.z);
  81. return this;
  82. }
  83. /**
  84. * Computes the world matrix of the node
  85. * @returns the world matrix
  86. */
  87. computeWorldMatrix() {
  88. if (!this._worldMatrix) {
  89. this._worldMatrix = Matrix.Identity();
  90. }
  91. return this._worldMatrix;
  92. }
  93. /**
  94. * Returns the integer 3.
  95. * @returns The light Type id as a constant defines in Light.LIGHTTYPEID_x
  96. */
  97. getTypeID() {
  98. return Light.LIGHTTYPEID_HEMISPHERICLIGHT;
  99. }
  100. /**
  101. * Prepares the list of defines specific to the light type.
  102. * @param defines the list of defines
  103. * @param lightIndex defines the index of the light for the effect
  104. */
  105. prepareLightSpecificDefines(defines, lightIndex) {
  106. defines["HEMILIGHT" + lightIndex] = true;
  107. }
  108. }
  109. __decorate([
  110. serializeAsColor3()
  111. ], HemisphericLight.prototype, "groundColor", void 0);
  112. __decorate([
  113. serializeAsVector3()
  114. ], HemisphericLight.prototype, "direction", void 0);
  115. //# sourceMappingURL=hemisphericLight.js.map