babylon.directionalLight.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. module BABYLON {
  2. export class DirectionalLight extends ShadowLight {
  3. private _shadowFrustumSize = 0;
  4. /**
  5. * Fix frustum size for the shadow generation. This is disabled if the value is 0.
  6. */
  7. @serialize()
  8. public get shadowFrustumSize(): number {
  9. return this._shadowFrustumSize
  10. }
  11. /**
  12. * Specifies a fix frustum size for the shadow generation.
  13. */
  14. public set shadowFrustumSize(value: number) {
  15. this._shadowFrustumSize = value;
  16. this.forceProjectionMatrixCompute();
  17. }
  18. private _shadowOrthoScale = 0.5;
  19. @serialize()
  20. public get shadowOrthoScale(): number {
  21. return this._shadowOrthoScale
  22. }
  23. public set shadowOrthoScale(value: number) {
  24. this._shadowOrthoScale = value;
  25. this.forceProjectionMatrixCompute();
  26. }
  27. @serialize()
  28. public autoUpdateExtends = true;
  29. // Cache
  30. private _orthoLeft = Number.MAX_VALUE;
  31. private _orthoRight = Number.MIN_VALUE;
  32. private _orthoTop = Number.MIN_VALUE;
  33. private _orthoBottom = Number.MAX_VALUE;
  34. /**
  35. * Creates a DirectionalLight object in the scene, oriented towards the passed direction (Vector3).
  36. * The directional light is emitted from everywhere in the given direction.
  37. * It can cast shawdows.
  38. * Documentation : http://doc.babylonjs.com/tutorials/lights
  39. */
  40. constructor(name: string, direction: Vector3, scene: Scene) {
  41. super(name, scene);
  42. this.position = direction.scale(-1.0);
  43. this.direction = direction;
  44. }
  45. /**
  46. * Returns the string "DirectionalLight".
  47. */
  48. public getClassName(): string {
  49. return "DirectionalLight";
  50. }
  51. /**
  52. * Returns the integer 1.
  53. */
  54. public getTypeID(): number {
  55. return Light.LIGHTTYPEID_DIRECTIONALLIGHT;
  56. }
  57. /**
  58. * Sets the passed matrix "matrix" as projection matrix for the shadows cast by the light according to the passed view matrix.
  59. * Returns the DirectionalLight Shadow projection matrix.
  60. */
  61. protected _setDefaultShadowProjectionMatrix(matrix: Matrix, viewMatrix: Matrix, renderList: Array<AbstractMesh>): void {
  62. if (this.shadowFrustumSize > 0) {
  63. this._setDefaultFixedFrustumShadowProjectionMatrix(matrix, viewMatrix);
  64. }
  65. else {
  66. this._setDefaultAutoExtendShadowProjectionMatrix(matrix, viewMatrix, renderList);
  67. }
  68. }
  69. /**
  70. * Sets the passed matrix "matrix" as fixed frustum projection matrix for the shadows cast by the light according to the passed view matrix.
  71. * Returns the DirectionalLight Shadow projection matrix.
  72. */
  73. protected _setDefaultFixedFrustumShadowProjectionMatrix(matrix: Matrix, viewMatrix: Matrix): void {
  74. var activeCamera = this.getScene().activeCamera;
  75. if (!activeCamera) {
  76. return;
  77. }
  78. Matrix.OrthoLHToRef(this.shadowFrustumSize, this.shadowFrustumSize,
  79. this.shadowMinZ !== undefined ? this.shadowMinZ : activeCamera.minZ, this.shadowMaxZ !== undefined ? this.shadowMaxZ : activeCamera.maxZ, matrix);
  80. }
  81. /**
  82. * Sets the passed matrix "matrix" as auto extend projection matrix for the shadows cast by the light according to the passed view matrix.
  83. * Returns the DirectionalLight Shadow projection matrix.
  84. */
  85. protected _setDefaultAutoExtendShadowProjectionMatrix(matrix: Matrix, viewMatrix: Matrix, renderList: Array<AbstractMesh>): void {
  86. var activeCamera = this.getScene().activeCamera;
  87. if (!activeCamera) {
  88. return;
  89. }
  90. // Check extends
  91. if (this.autoUpdateExtends || this._orthoLeft === Number.MAX_VALUE) {
  92. var tempVector3 = Vector3.Zero();
  93. this._orthoLeft = Number.MAX_VALUE;
  94. this._orthoRight = Number.MIN_VALUE;
  95. this._orthoTop = Number.MIN_VALUE;
  96. this._orthoBottom = Number.MAX_VALUE;
  97. for (var meshIndex = 0; meshIndex < renderList.length; meshIndex++) {
  98. var mesh = renderList[meshIndex];
  99. if (!mesh) {
  100. continue;
  101. }
  102. var boundingInfo = mesh.getBoundingInfo();
  103. var boundingBox = boundingInfo.boundingBox;
  104. for (var index = 0; index < boundingBox.vectorsWorld.length; index++) {
  105. Vector3.TransformCoordinatesToRef(boundingBox.vectorsWorld[index], viewMatrix, tempVector3);
  106. if (tempVector3.x < this._orthoLeft)
  107. this._orthoLeft = tempVector3.x;
  108. if (tempVector3.y < this._orthoBottom)
  109. this._orthoBottom = tempVector3.y;
  110. if (tempVector3.x > this._orthoRight)
  111. this._orthoRight = tempVector3.x;
  112. if (tempVector3.y > this._orthoTop)
  113. this._orthoTop = tempVector3.y;
  114. }
  115. }
  116. }
  117. var xOffset = this._orthoRight - this._orthoLeft;
  118. var yOffset = this._orthoTop - this._orthoBottom;
  119. Matrix.OrthoOffCenterLHToRef(this._orthoLeft - xOffset * this.shadowOrthoScale, this._orthoRight + xOffset * this.shadowOrthoScale,
  120. this._orthoBottom - yOffset * this.shadowOrthoScale, this._orthoTop + yOffset * this.shadowOrthoScale,
  121. this.shadowMinZ !== undefined ? this.shadowMinZ : activeCamera.minZ, this.shadowMaxZ !== undefined ? this.shadowMaxZ : activeCamera.maxZ, matrix);
  122. }
  123. protected _buildUniformLayout(): void {
  124. this._uniformBuffer.addUniform("vLightData", 4);
  125. this._uniformBuffer.addUniform("vLightDiffuse", 4);
  126. this._uniformBuffer.addUniform("vLightSpecular", 3);
  127. this._uniformBuffer.addUniform("shadowsInfo", 3);
  128. this._uniformBuffer.addUniform("depthValues", 2);
  129. this._uniformBuffer.create();
  130. }
  131. /**
  132. * Sets the passed Effect object with the DirectionalLight transformed position (or position if not parented) and the passed name.
  133. * Returns the DirectionalLight.
  134. */
  135. public transferToEffect(effect: Effect, lightIndex: string): DirectionalLight {
  136. if (this.computeTransformedInformation()) {
  137. this._uniformBuffer.updateFloat4("vLightData", this.transformedDirection.x, this.transformedDirection.y, this.transformedDirection.z, 1, lightIndex);
  138. return this;
  139. }
  140. this._uniformBuffer.updateFloat4("vLightData", this.direction.x, this.direction.y, this.direction.z, 1, lightIndex);
  141. return this;
  142. }
  143. /**
  144. * Gets the minZ used for shadow according to both the scene and the light.
  145. *
  146. * Values are fixed on directional lights as it relies on an ortho projection hence the need to convert being
  147. * -1 and 1 to 0 and 1 doing (depth + min) / (min + max) -> (depth + 1) / (1 + 1) -> (depth * 0.5) + 0.5.
  148. * @param activeCamera
  149. */
  150. public getDepthMinZ(activeCamera: Camera): number {
  151. return 1;
  152. }
  153. /**
  154. * Gets the maxZ used for shadow according to both the scene and the light.
  155. *
  156. * Values are fixed on directional lights as it relies on an ortho projection hence the need to convert being
  157. * -1 and 1 to 0 and 1 doing (depth + min) / (min + max) -> (depth + 1) / (1 + 1) -> (depth * 0.5) + 0.5.
  158. * @param activeCamera
  159. */
  160. public getDepthMaxZ(activeCamera: Camera): number {
  161. return 1;
  162. }
  163. }
  164. }