|
1 | 1 | var registerSystem = require('../core/system').registerSystem; |
2 | 2 | var constants = require('../constants/'); |
3 | | - |
4 | | -var DEFAULT_LIGHT_ATTR = 'data-aframe-default-light'; |
5 | | - |
6 | | -/** |
7 | | - * Light system. |
8 | | - * |
9 | | - * Prescribes default lighting if not specified (one ambient, one directional). |
10 | | - * Removes default lighting from the scene when a new light is added. |
11 | | - * |
12 | | - * @param {bool} defaultLightsEnabled - Whether default lighting is active. |
13 | | - */ |
14 | | -module.exports.System = registerSystem('light', { |
15 | | - init: function () { |
16 | | - this.defaultLightsEnabled = null; |
17 | | - this.setupDefaultLights(); |
18 | | - }, |
19 | | - |
20 | | - /** |
21 | | - * Notify scene that light has been added and to remove the default. |
22 | | - * |
23 | | - * @param {object} el - element holding the light component. |
24 | | - */ |
25 | | - registerLight: function (el) { |
26 | | - var defaultLights; |
27 | | - var sceneEl = this.sceneEl; |
28 | | - |
29 | | - if (this.defaultLightsEnabled && !el.hasAttribute(DEFAULT_LIGHT_ATTR)) { |
30 | | - // User added a light, remove default lights through DOM. |
31 | | - defaultLights = document.querySelectorAll('[' + DEFAULT_LIGHT_ATTR + ']'); |
32 | | - for (var i = 0; i < defaultLights.length; i++) { |
33 | | - sceneEl.removeChild(defaultLights[i]); |
34 | | - } |
35 | | - this.defaultLightsEnabled = false; |
36 | | - } |
37 | | - }, |
38 | | - |
39 | | - /** |
40 | | - * Prescibe default lights to the scene. |
41 | | - * Does so by injecting markup such that this state is not invisible. |
42 | | - * These lights are removed if the user adds any lights. |
43 | | - */ |
44 | | - setupDefaultLights: function () { |
45 | | - var sceneEl = this.sceneEl; |
46 | | - var ambientLight = document.createElement('a-entity'); |
47 | | - var directionalLight = document.createElement('a-entity'); |
48 | | - |
49 | | - ambientLight.setAttribute('light', {color: '#BBB', type: 'ambient'}); |
50 | | - ambientLight.setAttribute(DEFAULT_LIGHT_ATTR, ''); |
51 | | - ambientLight.setAttribute(constants.AFRAME_INJECTED, ''); |
52 | | - sceneEl.appendChild(ambientLight); |
53 | | - |
54 | | - directionalLight.setAttribute('light', {color: '#FFF', intensity: 0.6}); |
55 | | - directionalLight.setAttribute('position', {x: -0.5, y: 1, z: 1}); |
56 | | - directionalLight.setAttribute(DEFAULT_LIGHT_ATTR, ''); |
57 | | - directionalLight.setAttribute(constants.AFRAME_INJECTED, ''); |
58 | | - sceneEl.appendChild(directionalLight); |
59 | | - |
60 | | - this.defaultLightsEnabled = true; |
61 | | - } |
62 | | -}); |
| 3 | + |
| 4 | +var DEFAULT_LIGHT_ATTR = 'data-aframe-default-light'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Light system. |
| 8 | + * |
| 9 | + * Prescribes default lighting if not specified (one ambient, one directional). |
| 10 | + * Removes default lighting from the scene when a new light is added. |
| 11 | + * |
| 12 | + * @param {bool} defaultLights - Whether default lighting are defined. |
| 13 | + * @param {bool} userDefinedLights - Whether user lighting is defined. |
| 14 | + */ |
| 15 | +module.exports.System = registerSystem('light', { |
| 16 | + init: function () { |
| 17 | + this.defaultLights = false; |
| 18 | + this.userDefinedLights = false; |
| 19 | + // Wait for all entities to fully load before checking for existence of lights. |
| 20 | + // Since entities wait for <a-assets> to load, any lights attaching to the scene |
| 21 | + // will do so asynchronously. |
| 22 | + this.sceneEl.addEventListener('loaded', this.setupDefaultLights.bind(this)); |
| 23 | + }, |
| 24 | + |
| 25 | + /** |
| 26 | + * Notify scene that light has been added and to remove the default. |
| 27 | + * |
| 28 | + * @param {object} el - element holding the light component. |
| 29 | + */ |
| 30 | + registerLight: function (el) { |
| 31 | + if (!el.hasAttribute(DEFAULT_LIGHT_ATTR)) { |
| 32 | + // User added a light, remove default lights through DOM. |
| 33 | + this.removeDefaultLights(); |
| 34 | + this.userDefinedLights = true; |
| 35 | + } |
| 36 | + }, |
| 37 | + |
| 38 | + removeDefaultLights: function () { |
| 39 | + var defaultLights; |
| 40 | + var sceneEl = this.sceneEl; |
| 41 | + |
| 42 | + if (!this.defaultLights) { return; } |
| 43 | + defaultLights = document.querySelectorAll('[' + DEFAULT_LIGHT_ATTR + ']'); |
| 44 | + for (var i = 0; i < defaultLights.length; i++) { |
| 45 | + sceneEl.removeChild(defaultLights[i]); |
| 46 | + } |
| 47 | + this.defaultLights = false; |
| 48 | + }, |
| 49 | + |
| 50 | + /** |
| 51 | + * Prescibe default lights to the scene. |
| 52 | + * Does so by injecting markup such that this state is not invisible. |
| 53 | + * These lights are removed if the user adds any lights. |
| 54 | + */ |
| 55 | + setupDefaultLights: function () { |
| 56 | + var sceneEl = this.sceneEl; |
| 57 | + var ambientLight; |
| 58 | + var directionalLight; |
| 59 | + |
| 60 | + if (this.userDefinedLights || this.defaultLights) { return; } |
| 61 | + ambientLight = document.createElement('a-entity'); |
| 62 | + directionalLight = document.createElement('a-entity'); |
| 63 | + ambientLight.setAttribute('light', {color: '#BBB', type: 'ambient'}); |
| 64 | + ambientLight.setAttribute(DEFAULT_LIGHT_ATTR, ''); |
| 65 | + ambientLight.setAttribute(constants.AFRAME_INJECTED, ''); |
| 66 | + sceneEl.appendChild(ambientLight); |
| 67 | + |
| 68 | + directionalLight.setAttribute('light', {color: '#FFF', intensity: 0.6}); |
| 69 | + directionalLight.setAttribute('position', {x: -0.5, y: 1, z: 1}); |
| 70 | + directionalLight.setAttribute(DEFAULT_LIGHT_ATTR, ''); |
| 71 | + directionalLight.setAttribute(constants.AFRAME_INJECTED, ''); |
| 72 | + sceneEl.appendChild(directionalLight); |
| 73 | + |
| 74 | + this.defaultLights = true; |
| 75 | + } |
| 76 | +}); |
0 commit comments