Skip to content

IR 9803 dynamic authoring #42

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

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
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
86 changes: 86 additions & 0 deletions src/examples/DynamicAuthoring.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import {
Entity,
Layers,
QueryReactor,
UUIDComponent,
getAuthoringCounterpart,
loadEntitiesIntoAuthoring,
unloadEntitiesFromAuthoring,
useQuery
} from '@ir-engine/ecs'
import { AuthoringState } from '@ir-engine/engine/src/authoring/AuthoringState'
import { GLTFComponent } from '@ir-engine/engine/src/gltf/GLTFComponent'
import { getState } from '@ir-engine/hyperflux'
import Button from '@ir-engine/ui/src/primitives/tailwind/Button'
import React, { useCallback } from 'react'

export default function DynamicAuthoring() {
// invoke authoring state
getState(AuthoringState)

return (
<>
<div className="pointer-events-auto left-0 z-50 overflow-y-auto">
<QueryReactor Components={[GLTFComponent]} ChildEntityReactor={SourceLoaded} />
</div>
</>
)
}

const SourceLoaded = (props: { entity: Entity }) => {
const loaded = GLTFComponent.useSceneLoaded(props.entity)
if (!loaded) return null
return <SelectEditReactor entity={props.entity} />
}

/**
* Contains a button and name of the entity to select and edit it.
*/
const SelectEditReactor = (props: { entity: Entity }) => {
const { entity } = props
const isInAuthoring = !!getAuthoringCounterpart(entity)

useQuery([UUIDComponent], Layers.Authoring) // trigger rerender

// Handle loading entity into authoring
const handleLoadIntoAuthoring = useCallback(() => {
try {
const source = GLTFComponent.getSourceID(entity)
const entities = UUIDComponent.getEntitiesBySource(source, Layers.Simulation)
loadEntitiesIntoAuthoring([entity, ...entities])
console.log('Loaded entities into authoring:', [entity, ...entities])
} catch (error) {
console.error('Failed to load entity into authoring:', error)
}
}, [])

// Handle unloading entity from authoring
const handleUnloadFromAuthoring = useCallback(() => {
try {
const authoringEntity = getAuthoringCounterpart(entity)
const source = GLTFComponent.getSourceID(authoringEntity)
const entities = UUIDComponent.getEntitiesBySource(source, Layers.Authoring)
unloadEntitiesFromAuthoring([authoringEntity, ...entities])
console.log('Unloaded entities from authoring:', [authoringEntity, ...entities])
} catch (error) {
console.error('Failed to unload entity from authoring:', error)
}
}, [])

return (
<div className="flex flex-col gap-2 rounded border border-ui-outline p-2">
<div className="text-sm font-medium">Entity ID: {entity}</div>
<div className="flex gap-2">
{isInAuthoring ? (
<Button onClick={handleUnloadFromAuthoring} variant="red" size="sm">
Unload from Authoring
</Button>
) : (
<Button onClick={handleLoadIntoAuthoring} variant="primary" size="sm">
Load into Authoring
</Button>
)}
</div>
</div>
)
}
7 changes: 7 additions & 0 deletions src/examplesRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react'

import '@ir-engine/client/src/engine'

import DynamicAuthoring from './examples/DynamicAuthoring'
import { gltfRoutes } from './examples/GLTFs'
import InstanceConnection from './examples/InstanceConnection'
import InstancedLODs from './examples/InstancedLODs'
Expand Down Expand Up @@ -117,6 +118,12 @@ export const examples: RouteCategories = [
name: 'Multiple Canvases with different cameras',
description: 'View the same scene from different cameras',
entry: MultipleCanvasCameras
},
{
name: 'Dynamic Authoring',
description: 'Allows you to use editor APIs to edit entities at runtime',
sceneKey: 'projects/ir-engine/default-project/public/scenes/apartment.gltf',
entry: DynamicAuthoring
}
]
},
Expand Down