Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add update environment option #307

Merged
merged 5 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 36 additions & 29 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,42 +364,49 @@ AFRAME.registerComponent('street-environment', {
schema: {
preset: { type: 'string', default: 'day', oneOf: ['day', 'night'] }
},
init: function () {
var data = this.data;
var el = this.el;
if (data.preset === 'night') {
const light = document.createElement('a-entity');
light.setAttribute('id', 'light');
light.setAttribute('light', { type: 'ambient', color: '#FFF', intensity: 0.5 });
el.appendChild(light);
const light2 = document.createElement('a-entity');
light2.setAttribute('id', 'light');
light2.setAttribute('position', { x: 0.5, y: 1, z: -1 });
light2.setAttribute('light', { type: 'directional', color: '#FFF', intensity: 0.15 });
el.appendChild(light2);
const sky = document.createElement('a-sky');
sky.setAttribute('id', 'sky');
setEnvOption: function () {
const sky = this.sky;
const light1 = this.light1;
const light2 = this.light2;

if (this.data.preset === 'night') {
light1.setAttribute('light', 'intensity', 0.5 );
light2.setAttribute('light', 'intensity', 0.15 );
sky.setAttribute('color', '#444');
sky.setAttribute('src', '#sky-night');

el.appendChild(sky);
sky.setAttribute('rotation', '0 0 0');
} else { // day
// TODO: create a parent with children
const light = document.createElement('a-entity');
light.setAttribute('id', 'light');
light.setAttribute('light', { type: 'ambient', color: '#FFF', intensity: 2 });
el.appendChild(light);
const light2 = document.createElement('a-entity');
light2.setAttribute('id', 'light');
light2.setAttribute('position', { x: 0.5, y: 1, z: -1 });
light2.setAttribute('light', { type: 'directional', color: '#FFF', intensity: 0.6 });
el.appendChild(light2);
const sky = document.createElement('a-sky');
sky.setAttribute('id', 'sky');
light1.setAttribute('light', 'intensity', 2 );
light2.setAttribute('light', 'intensity', 0.6 );
sky.setAttribute('color', '#FFF');
sky.setAttribute('src', '#sky');
sky.setAttribute('rotation', '0 255 0');
el.appendChild(sky);
}
},
init: function () {
const el = this.el;

this.light1 = document.createElement('a-entity');
const light1 = this.light1;
light1.setAttribute('id', 'env-light1');
light1.setAttribute('light', { type: 'ambient', color: '#FFF'});
el.appendChild(light1);

this.light2 = document.createElement('a-entity');
const light2 = this.light2;
light2.setAttribute('id', 'env-light2');
light2.setAttribute('position', { x: 0.5, y: 1, z: -1 });
light2.setAttribute('light', { type: 'directional', color: '#FFF'});
el.appendChild(light2);

this.sky = document.createElement('a-sky');
const sky = this.sky;
sky.setAttribute('id', 'env-sky');
el.appendChild(sky);
},
update: function (oldData) {
this.setEnvOption();
}
});

Expand Down
41 changes: 24 additions & 17 deletions src/json-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ and returns a Javascript object
*/
function convertDOMElToObject (entity) {
const data = [];
if (entity.length) {
for (const entry of entity) {
data.push(getElementData(entry));
const environmentElement = document.querySelector('#environment');
const sceneEntities = [entity, environmentElement];

for (const entry of sceneEntities) {
const entityData = getElementData(entry);
if (entityData) {
data.push(entityData);
}
} else {
data.push(getElementData(entity));
}
return {
title: 'scene',
Expand All @@ -21,6 +23,9 @@ function convertDOMElToObject (entity) {
}

function getElementData (entity) {
if (!entity.isEntity) {
return;
}
const elementTree = getAttributes(entity);
const children = entity.childNodes;
if (children.length) {
Expand All @@ -36,7 +41,8 @@ function getElementData (entity) {

function getAttributes (entity) {
const elemObj = {};
elemObj['element'] = entity.tagName.toLowerCase();

elemObj['element'] = entity.tagName.toLowerCase();

if (entity.id) {
elemObj['id'] = entity.id;
Expand Down Expand Up @@ -112,7 +118,8 @@ const removeProps = {
normalMap: {},
'set-loader-from-hash': '*',
'create-from-json': '*',
street: { JSON: '*' }
street: { JSON: '*' },
'street-environment': '*'
};
// a list of component_name:new_component_name pairs to rename in JSON string
const renameProps = {
Expand All @@ -122,7 +129,6 @@ const renameProps = {

function filterJSONstreet (removeProps, renameProps, streetJSON) {
function removeValueCheck (removeVal, value) {
// console.error(removeVal, value, AFRAME.utils.deepEqual(removeVal, value))
if (AFRAME.utils.deepEqual(removeVal, value) || removeVal === '*') {
return true;
}
Expand Down Expand Up @@ -256,7 +262,6 @@ function getModifiedProperty (entity, componentName) {
return data;
}
}

const diff = {};
for (const key in data) {
const defaultValue = defaultData[key].default;
Expand All @@ -270,19 +275,19 @@ function getModifiedProperty (entity, componentName) {
diff[key] = data[key];
}
}

return diff;
}

function createEntities (entitiesData, parentEl) {
function createEntities (entitiesData, parentEl) {
const sceneElement = document.querySelector('a-scene');
for (const entityData of entitiesData) {
if (entityData.id === 'street-container' &&
entityData.children &&
entityData.children[0].id === 'default-street' &&
entityData.children[0].components.hasOwnProperty('set-loader-from-hash')) {
delete entityData.children[0].components['set-loader-from-hash'];
}
createEntityFromObj(entityData, parentEl);
createEntityFromObj(entityData, sceneElement);
}
}

Expand Down Expand Up @@ -330,12 +335,14 @@ function createEntityFromObj (entityData, parentEl) {
entity.setAttribute('mixin', entityData.mixin);
}
// Ensure the components are loaded before update the UI

entity.emit('entitycreated', {}, false);
});

if (entityData.children) {
for (const childEntityData of entityData.children) {
createEntityFromObj(childEntityData, entity);
}
}
if (entityData.children) {
for (const childEntityData of entityData.children) {
createEntityFromObj(childEntityData, entity);
}
}
});
}