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

addMediaNode now returns the entity UUID | null of whatever media was added #10984

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion packages/editor/src/functions/EditorControlFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ const createObjectFromSceneElement = (
componentJson: ComponentJsonType[] = [],
parentEntity = getState(EditorState).rootEntity,
beforeEntity?: Entity
) => {
): { entityUUID: EntityUUID; sceneID: string } => {
const scenes = getSourcesForEntities([parentEntity])
const entityUUID: EntityUUID =
componentJson.find((comp) => comp.name === UUIDComponent.jsonID)?.props.uuid ?? generateEntityUUID()
Expand Down
82 changes: 48 additions & 34 deletions packages/editor/src/functions/addMediaNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { getContentType } from '@etherealengine/common/src/utils/getContentType'
import { UUIDComponent } from '@etherealengine/ecs'
import { getComponent, useOptionalComponent } from '@etherealengine/ecs/src/ComponentFunctions'
import { Engine } from '@etherealengine/ecs/src/Engine'
import { Entity } from '@etherealengine/ecs/src/Entity'
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'
Expand Down Expand Up @@ -64,7 +64,7 @@ export async function addMediaNode(
parent?: Entity,
before?: Entity,
extraComponentJson: ComponentJsonType[] = []
) {
): Promise<EntityUUID | null> {
const contentType = (await getContentType(url)) || ''
const { hostname } = new URL(url)

Expand Down Expand Up @@ -92,35 +92,41 @@ export async function addMediaNode(
// const lineGeometry = new BufferGeometry().setFromPoints([lineStart, lineEnd])
// setComponent(rayEntity, LineSegmentComponent, { geometry: lineGeometry })
const gltfLoader = getState(AssetLoaderState).gltfLoader
gltfLoader.load(url, (gltf) => {
const material = iterateObject3D(
gltf.scene,
(mesh: Mesh) => mesh.material as Material,
(mesh: Mesh) => mesh?.isMesh
)[0]
if (!material) return
const materialEntity = createMaterialEntity(material)
let foundTarget = false
for (const intersection of intersections) {
iterateObject3D(intersection.object, (mesh: Mesh) => {
if (!mesh?.isMesh || !mesh.visible) return
assignMaterial(mesh.entity, materialEntity)
foundTarget = true
})
if (foundTarget) break
}
})
return await new Promise((resolve) =>
gltfLoader.load(url, (gltf) => {
const material = iterateObject3D(
gltf.scene,
(mesh: Mesh) => mesh.material as Material,
(mesh: Mesh) => mesh?.isMesh
)[0]
if (!material) return
const materialEntity = createMaterialEntity(material)
let foundTarget = false
for (const intersection of intersections) {
iterateObject3D(intersection.object, (mesh: Mesh) => {
if (!mesh?.isMesh || !mesh.visible) return
assignMaterial(mesh.entity, materialEntity)
foundTarget = true
})
if (foundTarget) break
}
resolve(getComponent(materialEntity, UUIDComponent))
})
)
} else if (contentType.startsWith('model/lookdev')) {
const gltfLoader = getState(AssetLoaderState).gltfLoader
gltfLoader.load(url, (gltf) => {
const componentJson = gltf.scene.children[0].userData.componentJson
EditorControlFunctions.overwriteLookdevObject(
[{ name: ModelComponent.jsonID, props: { src: url } }, ...extraComponentJson],
componentJson,
parent!,
before
)
})
return await new Promise((resolve) =>
gltfLoader.load(url, (gltf) => {
const componentJson = gltf.scene.children[0].userData.componentJson
EditorControlFunctions.overwriteLookdevObject(
[{ name: ModelComponent.jsonID, props: { src: url } }, ...extraComponentJson],
componentJson,
parent!,
before
)
resolve(null)
})
)
} else if (contentType.startsWith('model/prefab')) {
const { entityUUID, sceneID } = EditorControlFunctions.createObjectFromSceneElement(
[{ name: ModelComponent.jsonID, props: { src: url } }, ...extraComponentJson],
Expand All @@ -139,8 +145,9 @@ export async function addMediaNode(

return null
})
return entityUUID
} else {
EditorControlFunctions.createObjectFromSceneElement(
const { entityUUID } = EditorControlFunctions.createObjectFromSceneElement(
[
{ name: ModelComponent.jsonID, props: { src: url } },
{ name: ShadowComponent.jsonID },
Expand All @@ -150,9 +157,10 @@ export async function addMediaNode(
parent!,
before
)
return entityUUID
}
} else if (contentType.startsWith('video/') || hostname.includes('twitch.tv') || hostname.includes('youtube.com')) {
EditorControlFunctions.createObjectFromSceneElement(
const { entityUUID } = EditorControlFunctions.createObjectFromSceneElement(
[
{ name: VideoComponent.jsonID },
{ name: MediaComponent.jsonID, props: { resources: [url] } },
Expand All @@ -161,14 +169,16 @@ export async function addMediaNode(
parent!,
before
)
return entityUUID
} else if (contentType.startsWith('image/')) {
EditorControlFunctions.createObjectFromSceneElement(
const { entityUUID } = EditorControlFunctions.createObjectFromSceneElement(
[{ name: ImageComponent.jsonID, props: { source: url } }, ...extraComponentJson],
parent!,
before
)
return entityUUID
} else if (contentType.startsWith('audio/')) {
EditorControlFunctions.createObjectFromSceneElement(
const { entityUUID } = EditorControlFunctions.createObjectFromSceneElement(
[
{ name: PositionalAudioComponent.jsonID },
{ name: MediaComponent.jsonID, props: { resources: [url] } },
Expand All @@ -177,8 +187,9 @@ export async function addMediaNode(
parent!,
before
)
return entityUUID
} else if (url.includes('.uvol')) {
EditorControlFunctions.createObjectFromSceneElement(
const { entityUUID } = EditorControlFunctions.createObjectFromSceneElement(
[
{ name: VolumetricComponent.jsonID },
{ name: MediaComponent.jsonID, props: { resources: [url] } },
Expand All @@ -187,5 +198,8 @@ export async function addMediaNode(
parent!,
before
)
return entityUUID
} else {
return null
}
}
Loading