babylon.directionalLight.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. module BABYLON {
  2. export class DirectionalLight extends Light implements IShadowLight {
  3. public position: Vector3;
  4. private _transformedDirection: Vector3;
  5. public transformedPosition: Vector3;
  6. private _worldMatrix: Matrix;
  7. public shadowOrthoScale = 0.5;
  8. constructor(name: string, public direction: Vector3, scene: Scene) {
  9. super(name, scene);
  10. this.position = direction.scale(-1);
  11. }
  12. public getAbsolutePosition(): Vector3 {
  13. return this.transformedPosition ? this.transformedPosition : this.position;
  14. }
  15. public setDirectionToTarget(target: Vector3): Vector3 {
  16. this.direction = Vector3.Normalize(target.subtract(this.position));
  17. return this.direction;
  18. }
  19. public setShadowProjectionMatrix(matrix: Matrix, viewMatrix: Matrix, renderList: Array<AbstractMesh>): void {
  20. var orthoLeft = Number.MAX_VALUE;
  21. var orthoRight = Number.MIN_VALUE;
  22. var orthoTop = Number.MIN_VALUE;
  23. var orthoBottom = Number.MAX_VALUE;
  24. var tempVector3 = Vector3.Zero();
  25. var activeCamera = this.getScene().activeCamera;
  26. // Check extends
  27. for (var meshIndex = 0; meshIndex < renderList.length; meshIndex++) {
  28. var mesh = renderList[meshIndex];
  29. if (!mesh) {
  30. continue;
  31. }
  32. var boundingInfo = mesh.getBoundingInfo();
  33. if (!boundingInfo) {
  34. continue;
  35. }
  36. var boundingBox = boundingInfo.boundingBox;
  37. for (var index = 0; index < boundingBox.vectorsWorld.length; index++) {
  38. Vector3.TransformCoordinatesToRef(boundingBox.vectorsWorld[index], viewMatrix, tempVector3);
  39. if (tempVector3.x < orthoLeft)
  40. orthoLeft = tempVector3.x;
  41. if (tempVector3.y < orthoBottom)
  42. orthoBottom = tempVector3.y;
  43. if (tempVector3.x > orthoRight)
  44. orthoRight = tempVector3.x;
  45. if (tempVector3.y > orthoTop)
  46. orthoTop = tempVector3.y;
  47. }
  48. }
  49. var xOffset = orthoRight - orthoLeft;
  50. var yOffset = orthoTop - orthoBottom;
  51. Matrix.OrthoOffCenterLHToRef( orthoLeft - xOffset * this.shadowOrthoScale, orthoRight + xOffset * this.shadowOrthoScale,
  52. orthoBottom - yOffset * this.shadowOrthoScale, orthoTop + yOffset * this.shadowOrthoScale,
  53. -activeCamera.maxZ, activeCamera.maxZ, matrix);
  54. }
  55. public supportsVSM(): boolean {
  56. return true;
  57. }
  58. public needRefreshPerFrame(): boolean {
  59. return true;
  60. }
  61. public computeTransformedPosition(): boolean {
  62. if (this.parent && this.parent.getWorldMatrix) {
  63. if (!this.transformedPosition) {
  64. this.transformedPosition = Vector3.Zero();
  65. }
  66. Vector3.TransformCoordinatesToRef(this.position, this.parent.getWorldMatrix(), this.transformedPosition);
  67. return true;
  68. }
  69. return false;
  70. }
  71. public transferToEffect(effect: Effect, directionUniformName: string): void {
  72. if (this.parent && this.parent.getWorldMatrix) {
  73. if (!this._transformedDirection) {
  74. this._transformedDirection = Vector3.Zero();
  75. }
  76. Vector3.TransformNormalToRef(this.direction, this.parent.getWorldMatrix(), this._transformedDirection);
  77. effect.setFloat4(directionUniformName, this._transformedDirection.x, this._transformedDirection.y, this._transformedDirection.z, 1);
  78. return;
  79. }
  80. effect.setFloat4(directionUniformName, this.direction.x, this.direction.y, this.direction.z, 1);
  81. }
  82. public _getWorldMatrix(): Matrix {
  83. if (!this._worldMatrix) {
  84. this._worldMatrix = Matrix.Identity();
  85. }
  86. Matrix.TranslationToRef(this.position.x, this.position.y, this.position.z, this._worldMatrix);
  87. return this._worldMatrix;
  88. }
  89. }
  90. }