babylon.directionalLight.js 4.7 KB

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