babylon.directionalLight.js 6.6 KB

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