hemisphericLight.ts 5.3 KB

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