babylon.light.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 Light = (function (_super) {
  15. __extends(Light, _super);
  16. function Light(name, scene) {
  17. _super.call(this, name, scene);
  18. this.diffuse = new BABYLON.Color3(1.0, 1.0, 1.0);
  19. this.specular = new BABYLON.Color3(1.0, 1.0, 1.0);
  20. this.intensity = 1.0;
  21. this.range = Number.MAX_VALUE;
  22. this.includeOnlyWithLayerMask = 0;
  23. this.includedOnlyMeshes = new Array();
  24. this.excludedMeshes = new Array();
  25. this.excludeWithLayerMask = 0;
  26. // PBR Properties.
  27. this.radius = 0.00001;
  28. this._excludedMeshesIds = new Array();
  29. this._includedOnlyMeshesIds = new Array();
  30. scene.addLight(this);
  31. }
  32. /**
  33. * @param {boolean} fullDetails - support for multiple levels of logging within scene loading
  34. */
  35. Light.prototype.toString = function (fullDetails) {
  36. var ret = "Name: " + this.name;
  37. ret += ", type: " + (["Point", "Directional", "Spot", "Hemispheric"])[this.getTypeID()];
  38. if (this.animations) {
  39. for (var i = 0; i < this.animations.length; i++) {
  40. ret += ", animation[0]: " + this.animations[i].toString(fullDetails);
  41. }
  42. }
  43. if (fullDetails) {
  44. }
  45. return ret;
  46. };
  47. Light.prototype.getShadowGenerator = function () {
  48. return this._shadowGenerator;
  49. };
  50. Light.prototype.getAbsolutePosition = function () {
  51. return BABYLON.Vector3.Zero();
  52. };
  53. Light.prototype.transferToEffect = function (effect, uniformName0, uniformName1) {
  54. };
  55. Light.prototype._getWorldMatrix = function () {
  56. return BABYLON.Matrix.Identity();
  57. };
  58. Light.prototype.canAffectMesh = function (mesh) {
  59. if (!mesh) {
  60. return true;
  61. }
  62. if (this.includedOnlyMeshes.length > 0 && this.includedOnlyMeshes.indexOf(mesh) === -1) {
  63. return false;
  64. }
  65. if (this.excludedMeshes.length > 0 && this.excludedMeshes.indexOf(mesh) !== -1) {
  66. return false;
  67. }
  68. if (this.includeOnlyWithLayerMask !== 0 && (this.includeOnlyWithLayerMask & mesh.layerMask) === 0) {
  69. return false;
  70. }
  71. if (this.excludeWithLayerMask !== 0 && this.excludeWithLayerMask & mesh.layerMask) {
  72. return false;
  73. }
  74. return true;
  75. };
  76. Light.prototype.getWorldMatrix = function () {
  77. this._currentRenderId = this.getScene().getRenderId();
  78. var worldMatrix = this._getWorldMatrix();
  79. if (this.parent && this.parent.getWorldMatrix) {
  80. if (!this._parentedWorldMatrix) {
  81. this._parentedWorldMatrix = BABYLON.Matrix.Identity();
  82. }
  83. worldMatrix.multiplyToRef(this.parent.getWorldMatrix(), this._parentedWorldMatrix);
  84. this._markSyncedWithParent();
  85. return this._parentedWorldMatrix;
  86. }
  87. return worldMatrix;
  88. };
  89. Light.prototype.dispose = function () {
  90. if (this._shadowGenerator) {
  91. this._shadowGenerator.dispose();
  92. this._shadowGenerator = null;
  93. }
  94. // Animations
  95. this.getScene().stopAnimation(this);
  96. // Remove from scene
  97. this.getScene().removeLight(this);
  98. };
  99. Light.prototype.getTypeID = function () {
  100. return 0;
  101. };
  102. Light.prototype.clone = function (name) {
  103. return BABYLON.SerializationHelper.Clone(Light.GetConstructorFromName(this.getTypeID(), name, this.getScene()), this);
  104. };
  105. Light.prototype.serialize = function () {
  106. var serializationObject = BABYLON.SerializationHelper.Serialize(this);
  107. // Type
  108. serializationObject.type = this.getTypeID();
  109. // Parent
  110. if (this.parent) {
  111. serializationObject.parentId = this.parent.id;
  112. }
  113. // Animations
  114. BABYLON.Animation.AppendSerializedAnimations(this, serializationObject);
  115. serializationObject.ranges = this.serializeAnimationRanges();
  116. return serializationObject;
  117. };
  118. Light.GetConstructorFromName = function (type, name, scene) {
  119. switch (type) {
  120. case 0:
  121. return function () { return new BABYLON.PointLight(name, BABYLON.Vector3.Zero(), scene); };
  122. case 1:
  123. return function () { return new BABYLON.DirectionalLight(name, BABYLON.Vector3.Zero(), scene); };
  124. case 2:
  125. return function () { return new BABYLON.SpotLight(name, BABYLON.Vector3.Zero(), BABYLON.Vector3.Zero(), 0, 0, scene); };
  126. case 3:
  127. return function () { return new BABYLON.HemisphericLight(name, BABYLON.Vector3.Zero(), scene); };
  128. }
  129. };
  130. Light.Parse = function (parsedLight, scene) {
  131. var light = BABYLON.SerializationHelper.Parse(Light.GetConstructorFromName(parsedLight.type, parsedLight.name, scene), parsedLight, scene);
  132. // Inclusion / exclusions
  133. if (parsedLight.excludedMeshesIds) {
  134. light._excludedMeshesIds = parsedLight.excludedMeshesIds;
  135. }
  136. if (parsedLight.includedOnlyMeshesIds) {
  137. light._includedOnlyMeshesIds = parsedLight.includedOnlyMeshesIds;
  138. }
  139. // Parent
  140. if (parsedLight.parentId) {
  141. light._waitingParentId = parsedLight.parentId;
  142. }
  143. // Animations
  144. if (parsedLight.animations) {
  145. for (var animationIndex = 0; animationIndex < parsedLight.animations.length; animationIndex++) {
  146. var parsedAnimation = parsedLight.animations[animationIndex];
  147. light.animations.push(BABYLON.Animation.Parse(parsedAnimation));
  148. }
  149. BABYLON.Node.ParseAnimationRanges(light, parsedLight, scene);
  150. }
  151. if (parsedLight.autoAnimate) {
  152. scene.beginAnimation(light, parsedLight.autoAnimateFrom, parsedLight.autoAnimateTo, parsedLight.autoAnimateLoop, parsedLight.autoAnimateSpeed || 1.0);
  153. }
  154. return light;
  155. };
  156. __decorate([
  157. BABYLON.serializeAsColor3()
  158. ], Light.prototype, "diffuse", void 0);
  159. __decorate([
  160. BABYLON.serializeAsColor3()
  161. ], Light.prototype, "specular", void 0);
  162. __decorate([
  163. BABYLON.serialize()
  164. ], Light.prototype, "intensity", void 0);
  165. __decorate([
  166. BABYLON.serialize()
  167. ], Light.prototype, "range", void 0);
  168. __decorate([
  169. BABYLON.serialize()
  170. ], Light.prototype, "includeOnlyWithLayerMask", void 0);
  171. __decorate([
  172. BABYLON.serialize()
  173. ], Light.prototype, "radius", void 0);
  174. return Light;
  175. }(BABYLON.Node));
  176. BABYLON.Light = Light;
  177. })(BABYLON || (BABYLON = {}));