babylon.light.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 Light = (function (_super) {
  9. __extends(Light, _super);
  10. function Light(name, scene) {
  11. _super.call(this, name, scene);
  12. this.diffuse = new BABYLON.Color3(1.0, 1.0, 1.0);
  13. this.specular = new BABYLON.Color3(1.0, 1.0, 1.0);
  14. this.intensity = 1.0;
  15. this.range = Number.MAX_VALUE;
  16. this.includeOnlyWithLayerMask = 0;
  17. this.includedOnlyMeshes = new Array();
  18. this.excludedMeshes = new Array();
  19. this.excludeWithLayerMask = 0;
  20. this._excludedMeshesIds = new Array();
  21. this._includedOnlyMeshesIds = new Array();
  22. scene.addLight(this);
  23. }
  24. Light.prototype.getShadowGenerator = function () {
  25. return this._shadowGenerator;
  26. };
  27. Light.prototype.getAbsolutePosition = function () {
  28. return BABYLON.Vector3.Zero();
  29. };
  30. Light.prototype.transferToEffect = function (effect, uniformName0, uniformName1) {
  31. };
  32. Light.prototype._getWorldMatrix = function () {
  33. return BABYLON.Matrix.Identity();
  34. };
  35. Light.prototype.canAffectMesh = function (mesh) {
  36. if (!mesh) {
  37. return true;
  38. }
  39. if (this.includedOnlyMeshes.length > 0 && this.includedOnlyMeshes.indexOf(mesh) === -1) {
  40. return false;
  41. }
  42. if (this.excludedMeshes.length > 0 && this.excludedMeshes.indexOf(mesh) !== -1) {
  43. return false;
  44. }
  45. if (this.includeOnlyWithLayerMask !== 0 && (this.includeOnlyWithLayerMask & mesh.layerMask) === 0) {
  46. return false;
  47. }
  48. if (this.excludeWithLayerMask !== 0 && this.excludeWithLayerMask & mesh.layerMask) {
  49. return false;
  50. }
  51. return true;
  52. };
  53. Light.prototype.getWorldMatrix = function () {
  54. this._currentRenderId = this.getScene().getRenderId();
  55. var worldMatrix = this._getWorldMatrix();
  56. if (this.parent && this.parent.getWorldMatrix) {
  57. if (!this._parentedWorldMatrix) {
  58. this._parentedWorldMatrix = BABYLON.Matrix.Identity();
  59. }
  60. worldMatrix.multiplyToRef(this.parent.getWorldMatrix(), this._parentedWorldMatrix);
  61. this._markSyncedWithParent();
  62. return this._parentedWorldMatrix;
  63. }
  64. return worldMatrix;
  65. };
  66. Light.prototype.dispose = function () {
  67. if (this._shadowGenerator) {
  68. this._shadowGenerator.dispose();
  69. this._shadowGenerator = null;
  70. }
  71. // Animations
  72. this.getScene().stopAnimation(this);
  73. // Remove from scene
  74. this.getScene().removeLight(this);
  75. };
  76. Light.prototype.serialize = function () {
  77. var serializationObject = {};
  78. serializationObject.name = this.name;
  79. serializationObject.id = this.id;
  80. serializationObject.tags = BABYLON.Tags.GetTags(this);
  81. if (this.intensity) {
  82. serializationObject.intensity = this.intensity;
  83. }
  84. serializationObject.range = this.range;
  85. serializationObject.diffuse = this.diffuse.asArray();
  86. serializationObject.specular = this.specular.asArray();
  87. return serializationObject;
  88. };
  89. Light.Parse = function (parsedLight, scene) {
  90. var light;
  91. switch (parsedLight.type) {
  92. case 0:
  93. light = new BABYLON.PointLight(parsedLight.name, BABYLON.Vector3.FromArray(parsedLight.position), scene);
  94. break;
  95. case 1:
  96. light = new BABYLON.DirectionalLight(parsedLight.name, BABYLON.Vector3.FromArray(parsedLight.direction), scene);
  97. light.position = BABYLON.Vector3.FromArray(parsedLight.position);
  98. break;
  99. case 2:
  100. light = new BABYLON.SpotLight(parsedLight.name, BABYLON.Vector3.FromArray(parsedLight.position), BABYLON.Vector3.FromArray(parsedLight.direction), parsedLight.angle, parsedLight.exponent, scene);
  101. break;
  102. case 3:
  103. light = new BABYLON.HemisphericLight(parsedLight.name, BABYLON.Vector3.FromArray(parsedLight.direction), scene);
  104. light.groundColor = BABYLON.Color3.FromArray(parsedLight.groundColor);
  105. break;
  106. }
  107. light.id = parsedLight.id;
  108. BABYLON.Tags.AddTagsTo(light, parsedLight.tags);
  109. if (parsedLight.intensity !== undefined) {
  110. light.intensity = parsedLight.intensity;
  111. }
  112. if (parsedLight.range) {
  113. light.range = parsedLight.range;
  114. }
  115. light.diffuse = BABYLON.Color3.FromArray(parsedLight.diffuse);
  116. light.specular = BABYLON.Color3.FromArray(parsedLight.specular);
  117. if (parsedLight.excludedMeshesIds) {
  118. light._excludedMeshesIds = parsedLight.excludedMeshesIds;
  119. }
  120. // Parent
  121. if (parsedLight.parentId) {
  122. light._waitingParentId = parsedLight.parentId;
  123. }
  124. if (parsedLight.includedOnlyMeshesIds) {
  125. light._includedOnlyMeshesIds = parsedLight.includedOnlyMeshesIds;
  126. }
  127. // Animations
  128. if (parsedLight.animations) {
  129. for (var animationIndex = 0; animationIndex < parsedLight.animations.length; animationIndex++) {
  130. var parsedAnimation = parsedLight.animations[animationIndex];
  131. light.animations.push(BABYLON.Animation.Parse(parsedAnimation));
  132. }
  133. BABYLON.Node.ParseAnimationRanges(light, parsedLight, scene);
  134. }
  135. if (parsedLight.autoAnimate) {
  136. scene.beginAnimation(light, parsedLight.autoAnimateFrom, parsedLight.autoAnimateTo, parsedLight.autoAnimateLoop, 1.0);
  137. }
  138. return light;
  139. };
  140. return Light;
  141. })(BABYLON.Node);
  142. BABYLON.Light = Light;
  143. })(BABYLON || (BABYLON = {}));