hemisphericLight.ts 5.4 KB

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