Skip to content

Commit 4fdf9d6

Browse files
committed
add dynamic authoring example
1 parent ccc57a3 commit 4fdf9d6

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

src/examples/DynamicAuthoring.tsx

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import {
2+
Entity,
3+
Layers,
4+
QueryReactor,
5+
getAuthoringCounterpart,
6+
loadEntitiesIntoAuthoring,
7+
unloadEntitiesFromAuthoring,
8+
useQuery
9+
} from '@ir-engine/ecs'
10+
import { AuthoringState } from '@ir-engine/engine/src/authoring/AuthoringState'
11+
import { GLTFComponent } from '@ir-engine/engine/src/gltf/GLTFComponent'
12+
import { SourceComponent } from '@ir-engine/engine/src/scene/components/SourceComponent'
13+
import { getState } from '@ir-engine/hyperflux'
14+
import Button from '@ir-engine/ui/src/primitives/tailwind/Button'
15+
import React, { useCallback } from 'react'
16+
17+
export default function DynamicAuthoring() {
18+
// invoke authoring state
19+
getState(AuthoringState)
20+
21+
return (
22+
<>
23+
<div className="pointer-events-auto left-0 z-50 overflow-y-auto">
24+
<QueryReactor Components={[GLTFComponent]} ChildEntityReactor={SourceLoaded} />
25+
</div>
26+
</>
27+
)
28+
}
29+
30+
const SourceLoaded = (props: { entity: Entity }) => {
31+
const loaded = GLTFComponent.useSceneLoaded(props.entity)
32+
if (!loaded) return null
33+
return <SelectEditReactor entity={props.entity} />
34+
}
35+
36+
/**
37+
* Contains a button and name of the entity to select and edit it.
38+
*/
39+
const SelectEditReactor = (props: { entity: Entity }) => {
40+
const { entity } = props
41+
const isInAuthoring = !!getAuthoringCounterpart(entity)
42+
43+
useQuery([SourceComponent], Layers.Authoring) // trigger rerender
44+
45+
// Handle loading entity into authoring
46+
const handleLoadIntoAuthoring = useCallback(() => {
47+
try {
48+
const source = GLTFComponent.getInstanceID(entity)
49+
const entities = SourceComponent.getEntitiesBySource(source)
50+
loadEntitiesIntoAuthoring([entity, ...entities])
51+
console.log('Loaded entities into authoring:', [entity, ...entities])
52+
} catch (error) {
53+
console.error('Failed to load entity into authoring:', error)
54+
}
55+
}, [])
56+
57+
// Handle unloading entity from authoring
58+
const handleUnloadFromAuthoring = useCallback(() => {
59+
try {
60+
const authoringEntity = getAuthoringCounterpart(entity)
61+
const source = GLTFComponent.getInstanceID(authoringEntity)
62+
const entities = SourceComponent.getEntitiesBySource(source, Layers.Authoring)
63+
unloadEntitiesFromAuthoring([authoringEntity, ...entities])
64+
console.log('Unloaded entities from authoring:', [authoringEntity, ...entities])
65+
} catch (error) {
66+
console.error('Failed to unload entity from authoring:', error)
67+
}
68+
}, [])
69+
70+
return (
71+
<div className="flex flex-col gap-2 rounded border border-ui-outline p-2">
72+
<div className="text-sm font-medium">Entity ID: {entity}</div>
73+
<div className="flex gap-2">
74+
{isInAuthoring ? (
75+
<Button onClick={handleUnloadFromAuthoring} variant="red" size="sm">
76+
Unload from Authoring
77+
</Button>
78+
) : (
79+
<Button onClick={handleLoadIntoAuthoring} variant="primary" size="sm">
80+
Load into Authoring
81+
</Button>
82+
)}
83+
</div>
84+
</div>
85+
)
86+
}

src/examplesRoute.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react'
22

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

5+
import DynamicAuthoring from './examples/DynamicAuthoring'
56
import { gltfRoutes } from './examples/GLTFs'
67
import InstanceConnection from './examples/InstanceConnection'
78
import InstancedLODs from './examples/InstancedLODs'
@@ -117,6 +118,12 @@ export const examples: RouteCategories = [
117118
name: 'Multiple Canvases with different cameras',
118119
description: 'View the same scene from different cameras',
119120
entry: MultipleCanvasCameras
121+
},
122+
{
123+
name: 'Dynamic Authoring',
124+
description: 'Allows you to use editor APIs to edit entities at runtime',
125+
sceneKey: 'projects/ir-engine/default-project/public/scenes/apartment.gltf',
126+
entry: DynamicAuthoring
120127
}
121128
]
122129
},

0 commit comments

Comments
 (0)