Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
HexaField committed May 25, 2024
1 parent ae3eaa8 commit b9fbb10
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 55 deletions.
104 changes: 52 additions & 52 deletions packages/editor/src/functions/EditorControlFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import {
findCommonAncestors,
iterateEntityNode
} from '@etherealengine/spatial/src/transform/components/EntityTree'
import { SourceComponent } from '@etherealengine/spatial/src/transform/components/SourceComponent'
import { SourceComponent, SourceID } from '@etherealengine/spatial/src/transform/components/SourceComponent'
import { TransformComponent } from '@etherealengine/spatial/src/transform/components/TransformComponent'
import { computeTransformMatrix } from '@etherealengine/spatial/src/transform/systems/TransformSystem'

Expand All @@ -69,13 +69,13 @@ const tempMatrix4 = new Matrix4()
const tempVector = new Vector3()

const getSourcesForEntities = (entities: Entity[]) => {
const scenes: Record<string, Entity[]> = {}
const sources: Record<SourceID, Entity[]> = {}
for (const entity of entities) {
const sceneID = getComponent(entity, SourceComponent)
scenes[sceneID] ??= []
scenes[sceneID].push(entity)
const sourceID = getComponent(entity, SourceComponent)
sources[sourceID] ??= []
sources[sourceID].push(entity)
}
return scenes
return sources
}

const getGLTFNodeByUUID = (gltf: GLTF.IGLTF, uuid: NodeID) => {
Expand All @@ -89,18 +89,18 @@ const getParentNodeByUUID = (gltf: GLTF.IGLTF, uuid: NodeID) => {
}

const hasComponentInAuthoringLayer = <C extends Component<any, any>>(entity: Entity, component: C) => {
const sceneComponentID = component.jsonID
if (!sceneComponentID) return
const componentID = component.jsonID
if (!componentID) return

const scenes = getSourcesForEntities([entity])
const sources = getSourcesForEntities([entity])

for (const [sceneID, entities] of Object.entries(scenes)) {
const gltf = GLTFSnapshotState.cloneCurrentSnapshot(sceneID)
for (const [sourceID, entities] of Object.entries(sources)) {
const gltf = GLTFSnapshotState.cloneCurrentSnapshot(sourceID)
const entityUUID = getComponent(entity, NodeIDComponent)
const node = getGLTFNodeByUUID(gltf.data, entityUUID)
if (!node) continue

return node.extensions?.[sceneComponentID] !== undefined
return node.extensions?.[componentID] !== undefined
}
return false
}
Expand All @@ -110,35 +110,35 @@ const addOrRemoveComponent = <C extends Component<any, any>>(
add: boolean,
args: SetComponentType<C> | undefined = undefined
) => {
const sceneComponentID = component.jsonID
if (!sceneComponentID) return
const componentID = component.jsonID
if (!componentID) return

const scenes = getSourcesForEntities(entities)
const sources = getSourcesForEntities(entities)

for (const [sceneID, entities] of Object.entries(scenes)) {
const gltf = GLTFSnapshotState.cloneCurrentSnapshot(sceneID)
for (const [sourceID, entities] of Object.entries(sources)) {
const gltf = GLTFSnapshotState.cloneCurrentSnapshot(sourceID)
for (const entity of entities) {
const entityNodeID = getComponent(entity, NodeIDComponent)
const node = getGLTFNodeByUUID(gltf.data, entityNodeID)
if (!node) continue
if (add) {
node.extensions![sceneComponentID] = {
...componentJsonDefaults(ComponentJSONIDMap.get(sceneComponentID)!),
node.extensions![componentID] = {
...componentJsonDefaults(ComponentJSONIDMap.get(componentID)!),
...args
}
} else {
delete node.extensions?.[sceneComponentID]
delete node.extensions?.[componentID]
}
}
dispatchAction(GLTFSnapshotAction.createSnapshot(gltf))
}
}

const modifyName = (entities: Entity[], name: string) => {
const scenes = getSourcesForEntities(entities)
const sources = getSourcesForEntities(entities)

for (const [sceneID, entities] of Object.entries(scenes)) {
const gltf = GLTFSnapshotState.cloneCurrentSnapshot(sceneID)
for (const [sourceID, entities] of Object.entries(sources)) {
const gltf = GLTFSnapshotState.cloneCurrentSnapshot(sourceID)

for (const entity of entities) {
const entityNodeID = getComponent(entity, NodeIDComponent)
Expand All @@ -159,10 +159,10 @@ const modifyProperty = <C extends Component<any, any>>(
component: C,
properties: Partial<SerializedComponentType<C>>
) => {
const scenes = getSourcesForEntities(entities)
const sources = getSourcesForEntities(entities)

for (const [sceneID, entities] of Object.entries(scenes)) {
const gltf = GLTFSnapshotState.cloneCurrentSnapshot(sceneID)
for (const [sourceID, entities] of Object.entries(sources)) {
const gltf = GLTFSnapshotState.cloneCurrentSnapshot(sourceID)

for (const entity of entities) {
const entityNodeID = getComponent(entity, NodeIDComponent)
Expand Down Expand Up @@ -211,11 +211,11 @@ const overwriteLookdevObject = (
parentEntity = getState(EditorState).rootEntity,
beforeEntity?: Entity
) => {
const scenes = getSourcesForEntities([parentEntity])
const sources = getSourcesForEntities([parentEntity])

for (const [sceneID] of Object.entries(scenes)) {
if (getState(GLTFSourceState)[sceneID]) {
const gltf = GLTFSnapshotState.cloneCurrentSnapshot(sceneID)
for (const [sourceID] of Object.entries(sources)) {
if (getState(GLTFSourceState)[sourceID]) {
const gltf = GLTFSnapshotState.cloneCurrentSnapshot(sourceID)
const extensions = {} as Record<string, any>
for (const comp of componentJson) {
extensions[comp.name] = {
Expand Down Expand Up @@ -258,14 +258,14 @@ const createObjectFromSceneElement = (
parentEntity = getState(EditorState).rootEntity,
beforeEntity?: Entity
) => {
const scenes = getSourcesForEntities([parentEntity])
const sources = getSourcesForEntities([parentEntity])

const entityUUID: EntityUUID =
componentJson.find((comp) => comp.name === NodeIDComponent.jsonID)?.props.uuid ?? generateEntityUUID()
const sceneIDUsed = Object.keys(scenes)[0]
for (const [sceneID, entities] of Object.entries(scenes)) {
const sourceIDUsed = Object.keys(sources)[0] /** @todo we need to fully support multi-source editing */
for (const [sourceID, entities] of Object.entries(sources)) {
const name = 'New Object'
const gltf = GLTFSnapshotState.cloneCurrentSnapshot(sceneID)
const gltf = GLTFSnapshotState.cloneCurrentSnapshot(sourceID)

const nodeIndex = gltf.data.nodes!.length

Expand Down Expand Up @@ -336,20 +336,20 @@ const createObjectFromSceneElement = (
}
dispatchAction(GLTFSnapshotAction.createSnapshot(gltf))
}
return { entityUUID, sceneID: sceneIDUsed }
return { entityUUID, sourceID: sourceIDUsed }
}

/**
* @todo copying an object should be rooted to which object is currently selected
*/
const duplicateObject = (entities: Entity[]) => {
const scenes = getSourcesForEntities(entities)
const sources = getSourcesForEntities(entities)
const copyMap = {} as { [entityUUID: EntityUUID]: EntityUUID }

for (const [sceneID, entities] of Object.entries(scenes)) {
for (const [sourceID, entities] of Object.entries(sources)) {
const rootEntities = findCommonAncestors(entities)

const gltf = GLTFSnapshotState.cloneCurrentSnapshot(sceneID)
const gltf = GLTFSnapshotState.cloneCurrentSnapshot(sourceID)

/** Depth first */
const duplicateNode = (startIndex: number, entityNodeID: NodeID) => {
Expand Down Expand Up @@ -532,10 +532,10 @@ const scaleObject = (entities: Entity[], scales: Vector3[], overrideScale = fals
}

const reparentObject = (entities: Entity[], before?: Entity | null, parent = getState(EditorState).rootEntity) => {
const scenes = getSourcesForEntities(entities)
const sources = getSourcesForEntities(entities)

for (const [sceneID, entities] of Object.entries(scenes)) {
const gltf = GLTFSnapshotState.cloneCurrentSnapshot(sceneID)
for (const [sourceID, entities] of Object.entries(sources)) {
const gltf = GLTFSnapshotState.cloneCurrentSnapshot(sourceID)

for (const entity of entities) {
if (entity === parent) continue
Expand Down Expand Up @@ -611,11 +611,11 @@ const groupObjects = (entities: Entity[]) => {
* - it works by modifying both sources
*/

const scenes = getSourcesForEntities(entities)
const sources = getSourcesForEntities(entities)
const newGroupUUIDs = {} as Record<number, EntityUUID>

for (const [sceneID, entities] of Object.entries(scenes)) {
const gltf = GLTFSnapshotState.cloneCurrentSnapshot(sceneID)
for (const [sourceID, entities] of Object.entries(sources)) {
const gltf = GLTFSnapshotState.cloneCurrentSnapshot(sourceID)

/** 1. create new group node */
const groupNode = {
Expand All @@ -628,7 +628,7 @@ const groupObjects = (entities: Entity[]) => {
}
}

newGroupUUIDs[sceneID] = groupNode.extensions![NodeIDComponent.jsonID]
newGroupUUIDs[sourceID] = groupNode.extensions![NodeIDComponent.jsonID]

const groupIndex = gltf.data.nodes!.push(groupNode) - 1

Expand Down Expand Up @@ -664,13 +664,13 @@ const removeObject = (entities: Entity[]) => {
/** we have to manually set this here or it will cause react errors when entities are removed */
getMutableState(SelectionState).selectedEntities.set([])

const scenes = getSourcesForEntities(entities)
const sources = getSourcesForEntities(entities)

for (const [sceneID, entities] of Object.entries(scenes)) {
const rootEntity = getState(GLTFSourceState)[sceneID]
for (const [sourceID, entities] of Object.entries(sources)) {
const rootEntity = getState(GLTFSourceState)[sourceID]
const removedParentNodes = filterParentEntities(rootEntity, entities, undefined, true, false)

const gltf = GLTFSnapshotState.cloneCurrentSnapshot(sceneID)
const gltf = GLTFSnapshotState.cloneCurrentSnapshot(sourceID)

for (let i = 0; i < removedParentNodes.length; i++) {
const entity = removedParentNodes[i]
Expand Down Expand Up @@ -771,9 +771,9 @@ const addToSelection = (entities: EntityUUID[]) => {
}

const commitTransformSave = (entities: Entity[]) => {
const scenes = getSourcesForEntities(entities)
for (const sceneID of Object.keys(scenes)) {
const gltf = GLTFSnapshotState.cloneCurrentSnapshot(sceneID)
const sources = getSourcesForEntities(entities)
for (const sourceID of Object.keys(sources)) {
const gltf = GLTFSnapshotState.cloneCurrentSnapshot(sourceID)
for (const entity of entities) {
const entityUUID = getComponent(entity, NodeIDComponent)
const node = getGLTFNodeByUUID(gltf.data, entityUUID)
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/functions/addMediaNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ import { defineQuery } from '@etherealengine/ecs/src/QueryFunctions'
import { AssetLoaderState } from '@etherealengine/engine/src/assets/state/AssetLoaderState'
import { PositionalAudioComponent } from '@etherealengine/engine/src/audio/components/PositionalAudioComponent'
import { addAuthoringHook } from '@etherealengine/engine/src/gltf/AuthoringHookState'
import { ComponentJsonType } from '@etherealengine/engine/src/gltf/convertJsonToGLTF'
import { ImageComponent } from '@etherealengine/engine/src/scene/components/ImageComponent'
import { MediaComponent } from '@etherealengine/engine/src/scene/components/MediaComponent'
import { ModelComponent } from '@etherealengine/engine/src/scene/components/ModelComponent'
import { VideoComponent } from '@etherealengine/engine/src/scene/components/VideoComponent'
import { VolumetricComponent } from '@etherealengine/engine/src/scene/components/VolumetricComponent'
import { createMaterialEntity } from '@etherealengine/engine/src/scene/materials/functions/materialSourcingFunctions'
import { ComponentJsonType } from '@etherealengine/engine/src/scene/types/SceneTypes'
import { getState } from '@etherealengine/hyperflux'
import { CameraComponent } from '@etherealengine/spatial/src/camera/components/CameraComponent'
import iterateObject3D from '@etherealengine/spatial/src/common/functions/iterateObject3D'
Expand Down Expand Up @@ -123,7 +123,7 @@ export async function addMediaNode(
)
})
} else if (contentType.startsWith('model/prefab')) {
const { entityUUID, sceneID } = EditorControlFunctions.createObjectFromSceneElement(
const { entityUUID, sourceID: sceneID } = EditorControlFunctions.createObjectFromSceneElement(
[{ name: ModelComponent.jsonID, props: { src: url } }, ...extraComponentJson],
parent!,
before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ export const createMaterialInstance = (path: string, sourceEntity: Entity, mater
export const createMaterialEntity = (material: Material, path: string, user?: Entity) => {
const materialEntity = createEntity()
setComponent(materialEntity, UUIDComponent, material.uuid as EntityUUID)
setComponent(materialEntity, SourceComponent, path)
setComponent(
materialEntity,
SourceComponent,
path as any
) /** @todo materials use just the path as it's source to deduplicate materials loaded from the same model multiple times */
const prototypeEntity = prototypeByName[material.type]
setComponent(materialEntity, MaterialComponent[MaterialComponents.State], {
material,
Expand Down

0 comments on commit b9fbb10

Please sign in to comment.