babylon.directionalLight.js 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. var __extends = 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.1;
  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. for (var meshIndex = 0; meshIndex < renderList.length; meshIndex++) {
  32. var mesh = renderList[meshIndex];
  33. if (!mesh) {
  34. continue;
  35. }
  36. var boundingInfo = mesh.getBoundingInfo();
  37. if (!boundingInfo) {
  38. continue;
  39. }
  40. var boundingBox = boundingInfo.boundingBox;
  41. for (var index = 0; index < boundingBox.vectorsWorld.length; index++) {
  42. BABYLON.Vector3.TransformCoordinatesToRef(boundingBox.vectorsWorld[index], viewMatrix, tempVector3);
  43. if (tempVector3.x < orthoLeft)
  44. orthoLeft = tempVector3.x;
  45. if (tempVector3.y < orthoBottom)
  46. orthoBottom = tempVector3.y;
  47. if (tempVector3.x > orthoRight)
  48. orthoRight = tempVector3.x;
  49. if (tempVector3.y > orthoTop)
  50. orthoTop = tempVector3.y;
  51. }
  52. }
  53. var xOffset = orthoRight - orthoLeft;
  54. var yOffset = orthoTop - orthoBottom;
  55. BABYLON.Matrix.OrthoOffCenterLHToRef(orthoLeft - xOffset * this.shadowOrthoScale, orthoRight + xOffset * this.shadowOrthoScale, orthoBottom - yOffset * this.shadowOrthoScale, orthoTop + yOffset * this.shadowOrthoScale, -activeCamera.maxZ, activeCamera.maxZ, matrix);
  56. };
  57. DirectionalLight.prototype.supportsVSM = function () {
  58. return true;
  59. };
  60. DirectionalLight.prototype.needRefreshPerFrame = function () {
  61. return true;
  62. };
  63. DirectionalLight.prototype.computeTransformedPosition = function () {
  64. if (this.parent && this.parent.getWorldMatrix) {
  65. if (!this.transformedPosition) {
  66. this.transformedPosition = BABYLON.Vector3.Zero();
  67. }
  68. BABYLON.Vector3.TransformCoordinatesToRef(this.position, this.parent.getWorldMatrix(), this.transformedPosition);
  69. return true;
  70. }
  71. return false;
  72. };
  73. DirectionalLight.prototype.transferToEffect = function (effect, directionUniformName) {
  74. if (this.parent && this.parent.getWorldMatrix) {
  75. if (!this._transformedDirection) {
  76. this._transformedDirection = BABYLON.Vector3.Zero();
  77. }
  78. BABYLON.Vector3.TransformNormalToRef(this.direction, this.parent.getWorldMatrix(), this._transformedDirection);
  79. effect.setFloat4(directionUniformName, this._transformedDirection.x, this._transformedDirection.y, this._transformedDirection.z, 1);
  80. return;
  81. }
  82. effect.setFloat4(directionUniformName, this.direction.x, this.direction.y, this.direction.z, 1);
  83. };
  84. DirectionalLight.prototype._getWorldMatrix = function () {
  85. if (!this._worldMatrix) {
  86. this._worldMatrix = BABYLON.Matrix.Identity();
  87. }
  88. BABYLON.Matrix.TranslationToRef(this.position.x, this.position.y, this.position.z, this._worldMatrix);
  89. return this._worldMatrix;
  90. };
  91. return DirectionalLight;
  92. })(BABYLON.Light);
  93. BABYLON.DirectionalLight = DirectionalLight;
  94. })(BABYLON || (BABYLON = {}));
  95. //# sourceMappingURL=babylon.directionalLight.js.map