Skip to content

Commit

Permalink
fix save/load environment node
Browse files Browse the repository at this point in the history
  • Loading branch information
Algorush committed Jul 6, 2023
1 parent 58f110e commit 0da63f6
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions src/json-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ function getElementData (entity) {
if (!entity.isEntity) {
return;
}
// node id's that should save without child nodes
const skipChildrenNodes = ['environment'];
const elementTree = getAttributes(entity);
const children = entity.childNodes;
if (children.length) {
if (children.length && !skipChildrenNodes.includes(elementTree.id)) {
elementTree['children'] = [];
for (const child of children) {
if (child.nodeType === Node.ELEMENT_NODE) {
Expand All @@ -54,6 +56,7 @@ function getAttributes (entity) {
if (entity.getAttribute('mixin')) {
elemObj['mixin'] = entity.getAttribute('mixin');
}

const entityComponents = entity.components;

if (entityComponents) {
Expand Down Expand Up @@ -118,8 +121,7 @@ const removeProps = {
normalMap: {},
'set-loader-from-hash': '*',
'create-from-json': '*',
street: { JSON: '*' },
'street-environment': '*'
street: { JSON: '*' }
};
// a list of component_name:new_component_name pairs to rename in JSON string
const renameProps = {
Expand Down Expand Up @@ -280,13 +282,26 @@ function getModifiedProperty (entity, componentName) {

function createEntities (entitiesData, parentEl) {
const sceneElement = document.querySelector('a-scene');
const removeEntities = ['environment', 'layers-2d'];
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'];
}

const sceneChildElement = document.getElementById(entityData.id);
if (sceneChildElement) {
if (removeEntities.includes(entityData.id)) {
// remove existing elements from scene
sceneChildElement.remove();
} else {
// or save link to the element
entityData.entityElement = sceneChildElement;
}
}

createEntityFromObj(entityData, sceneElement);
}
}
Expand All @@ -306,17 +321,17 @@ Add a new entity with a list of components and children (if exists)
* @return {Element} Entity created
*/
function createEntityFromObj (entityData, parentEl) {
const entity = document.createElement(entityData.element);
const entity = entityData.entityElement || document.createElement(entityData.element);

if (parentEl) {
if (!entity.parentEl && parentEl) {
parentEl.appendChild(entity);
}

if (entityData['primitive']) {
// define a primitive in advance to apply other primitive-specific geometry properties
entity.setAttribute('geometry', 'primitive', entityData['primitive']);
}

if (entityData.id) {
entity.setAttribute('id', entityData.id);
}
Expand All @@ -339,10 +354,9 @@ function createEntityFromObj (entityData, parentEl) {
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);
}
}
}

0 comments on commit 0da63f6

Please sign in to comment.