Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
Dereference prefabs (#10222)
Browse files Browse the repository at this point in the history
* move default prefabs into assets folder of default-project

* add authoring hook system and use to dereference prefabs on load

* licensing

* rename authoring hook state file
  • Loading branch information
dinomut1 authored May 24, 2024
1 parent a1a6682 commit 8b1994f
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 4 deletions.
6 changes: 3 additions & 3 deletions packages/editor/src/functions/EditorControlFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,9 @@ const createObjectFromSceneElement = (
beforeEntity?: Entity
) => {
const scenes = getSourcesForEntities([parentEntity])
const entityUUID =
const entityUUID: EntityUUID =
componentJson.find((comp) => comp.name === UUIDComponent.jsonID)?.props.uuid ?? generateEntityUUID()

const sceneIDUsed = Object.keys(scenes)[0]
for (const [sceneID, entities] of Object.entries(scenes)) {
const name = 'New Object'
const gltf = GLTFSnapshotState.cloneCurrentSnapshot(sceneID)
Expand Down Expand Up @@ -331,9 +331,9 @@ const createObjectFromSceneElement = (
}
parentNode.children.splice(beforeIndex, 0, nodeIndex)
}

dispatchAction(GLTFSnapshotAction.createSnapshot(gltf))
}
return { entityUUID, sceneID: sceneIDUsed }
}

/**
Expand Down
17 changes: 16 additions & 1 deletion packages/editor/src/functions/addMediaNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { Entity, EntityUUID } from '@etherealengine/ecs/src/Entity'
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 { 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'
Expand All @@ -48,7 +49,6 @@ import { ObjectLayerComponents } from '@etherealengine/spatial/src/renderer/comp
import { ObjectLayers } from '@etherealengine/spatial/src/renderer/constants/ObjectLayers'
import { MaterialComponent, MaterialComponents } from '@etherealengine/spatial/src/renderer/materials/MaterialComponent'
import { getMaterial } from '@etherealengine/spatial/src/renderer/materials/materialFunctions'

import { EditorControlFunctions } from './EditorControlFunctions'

/**
Expand Down Expand Up @@ -122,6 +122,21 @@ export async function addMediaNode(
before
)
})
} else if (contentType.startsWith('model/prefab')) {
const { entityUUID, sceneID } = EditorControlFunctions.createObjectFromSceneElement(
[{ name: ModelComponent.jsonID, props: { src: url } }, ...extraComponentJson],
parent!,
before
)
addAuthoringHook({
entityUUID,
sceneID,
callback: (entityUUID) => {
const entity = UUIDComponent.getEntityByUUID(entityUUID)
const modelComponent = getMutableComponent(entity, ModelComponent)
modelComponent.dereference.set(true)
}
})
} else {
EditorControlFunctions.createObjectFromSceneElement(
[{ name: ModelComponent.jsonID, props: { src: url } }, ...extraComponentJson],
Expand Down
74 changes: 74 additions & 0 deletions packages/engine/src/gltf/AuthoringHookState.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
CPAL-1.0 License
The contents of this file are subject to the Common Public Attribution License
Version 1.0. (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
https://github.com/EtherealEngine/etherealengine/blob/dev/LICENSE.
The License is based on the Mozilla Public License Version 1.1, but Sections 14
and 15 have been added to cover use of software over a computer network and
provide for limited attribution for the Original Developer. In addition,
Exhibit A has been modified to be consistent with Exhibit B.
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
specific language governing rights and limitations under the License.
The Original Code is Ethereal Engine.
The Original Developer is the Initial Developer. The Initial Developer of the
Original Code is the Ethereal Engine team.
All portions of the code written by the Ethereal Engine team are Copyright Β© 2021-2023
Ethereal Engine. All Rights Reserved.
*/

import { EntityUUID, UUIDComponent } from '@etherealengine/ecs'
import { NO_PROXY, defineState, getMutableState, useState } from '@etherealengine/hyperflux'
import React, { useEffect } from 'react'
import { generateUUID } from 'three/src/math/MathUtils'
import { GLTFDocumentState } from './GLTFDocumentState'

export type AuthoringHook = {
entityUUID: EntityUUID
sceneID: string
callback: (entityUUID: EntityUUID) => void
}

type AuthoringHookJob = AuthoringHook & { hookID: string }

export function addAuthoringHook(hook: AuthoringHook): string {
const hookID = generateUUID()
getMutableState(AuthoringHookState).merge([{ ...hook, hookID }])
return hookID
}

export const AuthoringHookState = defineState({
name: 'AuthoringHookState',
initial: [] as AuthoringHookJob[],
reactor: () => {
const hooks = useState(getMutableState(AuthoringHookState))
return (
<>
{hooks.get(NO_PROXY).map((hook) => (
<HookReactor key={hook.hookID} {...hook} />
))}
</>
)
}
})

const HookReactor = (props: AuthoringHookJob) => {
const { entityUUID, sceneID, callback } = props
const authoringHooks = getMutableState(AuthoringHookState)
const gltfDocument = useState(getMutableState(GLTFDocumentState))[sceneID]
if (!gltfDocument.value) return null
useEffect(() => {
const entity = gltfDocument.value.nodes!.find((node) => node.extensions![UUIDComponent.jsonID] === entityUUID)
if (entity) {
callback(entityUUID)
authoringHooks.set(authoringHooks.value.filter((hook) => hook.hookID !== props.hookID))
}
}, [gltfDocument])
return null
}

0 comments on commit 8b1994f

Please sign in to comment.