babylon.directionalLight.js 6.0 KB

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