babylon.directionalLight.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. public autoUpdateExtends = true;
  9. // Cache
  10. private _orthoLeft = Number.MAX_VALUE;
  11. private _orthoRight = Number.MIN_VALUE;
  12. private _orthoTop = Number.MIN_VALUE;
  13. private _orthoBottom = Number.MAX_VALUE;
  14. constructor(name: string, public direction: Vector3, scene: Scene) {
  15. super(name, scene);
  16. this.position = direction.scale(-1);
  17. }
  18. public getAbsolutePosition(): Vector3 {
  19. return this.transformedPosition ? this.transformedPosition : this.position;
  20. }
  21. public setDirectionToTarget(target: Vector3): Vector3 {
  22. this.direction = Vector3.Normalize(target.subtract(this.position));
  23. return this.direction;
  24. }
  25. public setShadowProjectionMatrix(matrix: Matrix, viewMatrix: Matrix, renderList: Array<AbstractMesh>): void {
  26. var activeCamera = this.getScene().activeCamera;
  27. // Check extends
  28. if (this.autoUpdateExtends || this._orthoLeft === Number.MAX_VALUE) {
  29. var tempVector3 = Vector3.Zero();
  30. this._orthoLeft = Number.MAX_VALUE;
  31. this._orthoRight = Number.MIN_VALUE;
  32. this._orthoTop = Number.MIN_VALUE;
  33. this._orthoBottom = Number.MAX_VALUE;
  34. for (var meshIndex = 0; meshIndex < renderList.length; meshIndex++) {
  35. var mesh = renderList[meshIndex];
  36. if (!mesh) {
  37. continue;
  38. }
  39. var boundingInfo = mesh.getBoundingInfo();
  40. if (!boundingInfo) {
  41. continue;
  42. }
  43. var boundingBox = boundingInfo.boundingBox;
  44. for (var index = 0; index < boundingBox.vectorsWorld.length; index++) {
  45. Vector3.TransformCoordinatesToRef(boundingBox.vectorsWorld[index], viewMatrix, tempVector3);
  46. if (tempVector3.x < this._orthoLeft)
  47. this._orthoLeft = tempVector3.x;
  48. if (tempVector3.y < this._orthoBottom)
  49. this._orthoBottom = tempVector3.y;
  50. if (tempVector3.x > this._orthoRight)
  51. this._orthoRight = tempVector3.x;
  52. if (tempVector3.y > this._orthoTop)
  53. this._orthoTop = tempVector3.y;
  54. }
  55. }
  56. }
  57. var xOffset = this._orthoRight - this._orthoLeft;
  58. var yOffset = this._orthoTop - this._orthoBottom;
  59. Matrix.OrthoOffCenterLHToRef(this._orthoLeft - xOffset * this.shadowOrthoScale, this._orthoRight + xOffset * this.shadowOrthoScale,
  60. this._orthoBottom - yOffset * this.shadowOrthoScale, this._orthoTop + yOffset * this.shadowOrthoScale,
  61. -activeCamera.maxZ, activeCamera.maxZ, matrix);
  62. }
  63. public supportsVSM(): boolean {
  64. return true;
  65. }
  66. public needRefreshPerFrame(): boolean {
  67. return true;
  68. }
  69. public needCube(): boolean {
  70. return false;
  71. }
  72. public getShadowDirection(faceIndex?: number): Vector3 {
  73. return this.direction;
  74. }
  75. public computeTransformedPosition(): boolean {
  76. if (this.parent && this.parent.getWorldMatrix) {
  77. if (!this.transformedPosition) {
  78. this.transformedPosition = Vector3.Zero();
  79. }
  80. Vector3.TransformCoordinatesToRef(this.position, this.parent.getWorldMatrix(), this.transformedPosition);
  81. return true;
  82. }
  83. return false;
  84. }
  85. public transferToEffect(effect: Effect, directionUniformName: string): void {
  86. if (this.parent && this.parent.getWorldMatrix) {
  87. if (!this._transformedDirection) {
  88. this._transformedDirection = Vector3.Zero();
  89. }
  90. Vector3.TransformNormalToRef(this.direction, this.parent.getWorldMatrix(), this._transformedDirection);
  91. effect.setFloat4(directionUniformName, this._transformedDirection.x, this._transformedDirection.y, this._transformedDirection.z, 1);
  92. return;
  93. }
  94. effect.setFloat4(directionUniformName, this.direction.x, this.direction.y, this.direction.z, 1);
  95. }
  96. public _getWorldMatrix(): Matrix {
  97. if (!this._worldMatrix) {
  98. this._worldMatrix = Matrix.Identity();
  99. }
  100. Matrix.TranslationToRef(this.position.x, this.position.y, this.position.z, this._worldMatrix);
  101. return this._worldMatrix;
  102. }
  103. public serialize(): any {
  104. var serializationObject = super.serialize();
  105. serializationObject.type = 1;
  106. serializationObject.position = this.position.asArray();
  107. serializationObject.direction = this.direction.asArray();
  108. return serializationObject;
  109. }
  110. }
  111. }