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

Commit

Permalink
Remove onRemoves
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelEstes committed Aug 15, 2024
1 parent 2257ba7 commit 8ff6c21
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,7 @@ export const TransformGizmoControlComponent = defineComponent({
if (typeof json.showY === 'number') component.showY.set(json.showY)
if (typeof json.showZ === 'number') component.showZ.set(json.showZ)
},
onRemove: (entity, component) => {
component.controlledEntities.set([])
component.visualEntity.set(UndefinedEntity)
component.planeEntity.set(UndefinedEntity)
component.pivotEntity.set(UndefinedEntity)
},

reactor: function (props) {
const gizmoControlEntity = useEntityContext()
const gizmoControlComponent = useComponent(gizmoControlEntity, TransformGizmoControlComponent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ export const TransformGizmoControlledComponent = defineComponent({
controller: UndefinedEntity
}
},
onRemove: (entity, component) => {
component.controller.set(UndefinedEntity)
},

reactor: function (props) {
const entity = useEntityContext()
Expand Down
13 changes: 6 additions & 7 deletions packages/editor/src/classes/TransformGizmoVisualComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,11 @@ export const TransformGizmoVisualComponent = defineComponent({
}
return visual
},

onSet(entity, component, json) {
if (!json) return
},
onRemove: (entity, component) => {
for (const mode in TransformMode) {
removeEntity(component.gizmo[mode])
removeEntity(component.picker[mode])
removeEntity(component.helper[mode])
}
},

reactor: function (props) {
const gizmoVisualEntity = useEntityContext()
const visualComponent = useComponent(gizmoVisualEntity, TransformGizmoVisualComponent)
Expand Down Expand Up @@ -179,6 +174,10 @@ export const TransformGizmoVisualComponent = defineComponent({
cleanupGizmo(pickerObject[mode])
removeObjectFromGroup(helper[mode], helperObject[mode])
cleanupGizmo(helperObject[mode])

removeEntity(gizmo[mode])
removeEntity(picker[mode])
removeEntity(helper[mode])
}
}
}, [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { AnimationAction, Group, Matrix4, SkeletonHelper, Vector3 } from 'three'
import {
defineComponent,
getComponent,
removeComponent,
setComponent,
useComponent,
useOptionalComponent
Expand Down Expand Up @@ -117,11 +116,6 @@ export const AvatarRigComponent = defineComponent({
if (matches.string.test(json.avatarURL)) component.avatarURL.set(json.avatarURL)
},

onRemove: (entity, component) => {
// ensure synchronously removed
if (component.helperEntity.value) removeComponent(component.helperEntity.value, ComputedTransformComponent)
},

reactor: function () {
const entity = useEntityContext()
const debugEnabled = useHookstate(getMutableState(RendererState).avatarDebug)
Expand Down
29 changes: 14 additions & 15 deletions packages/engine/src/scene/components/HyperspaceTagComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import { Engine } from '@etherealengine/ecs/src/Engine'
import { Entity, UndefinedEntity } from '@etherealengine/ecs/src/Entity'
import { createEntity, removeEntity, useEntityContext } from '@etherealengine/ecs/src/EntityFunctions'
import { useExecute } from '@etherealengine/ecs/src/SystemFunctions'
import { getMutableState, getState } from '@etherealengine/hyperflux'
import { getMutableState, getState, useHookstate } from '@etherealengine/hyperflux'
import { CameraComponent } from '@etherealengine/spatial/src/camera/components/CameraComponent'
import { ObjectDirection } from '@etherealengine/spatial/src/common/constants/MathConstants'
import { createTransitionState } from '@etherealengine/spatial/src/common/functions/createTransitionState'
Expand Down Expand Up @@ -172,26 +172,23 @@ export const HyperspaceTagComponent = defineComponent({
return {
// all internals
sceneVisible: true,
transition: createTransitionState(0.5, 'OUT'),
hyperspaceEffectEntity: UndefinedEntity,
ambientLightEntity: UndefinedEntity
transition: createTransitionState(0.5, 'OUT')
}
},

onRemove(entity, component) {
removeEntity(component.ambientLightEntity.value)
destroyEntityTree(component.hyperspaceEffectEntity.value)
},

reactor: () => {
const entity = useEntityContext()
const [galaxyTexture] = useTexture(
`${config.client.fileServer}/projects/default-project/assets/galaxyTexture.jpg`,
entity
)
const hyperspaceEffectEntityState = useHookstate(createEntity)
const ambientLightEntityState = useHookstate(createEntity)

useEffect(() => {
const hyperspaceEffectEntity = createEntity()
const hyperspaceEffectEntity = hyperspaceEffectEntityState.value
const ambientLightEntity = ambientLightEntityState.value

const hyperspaceEffect = new PortalEffect(hyperspaceEffectEntity)
addObjectToGroup(hyperspaceEffectEntity, hyperspaceEffect)
setObjectLayers(hyperspaceEffect, ObjectLayers.Portal)
Expand All @@ -200,7 +197,6 @@ export const HyperspaceTagComponent = defineComponent({
setComponent(hyperspaceEffectEntity, EntityTreeComponent, { parentEntity: entity })
setComponent(hyperspaceEffectEntity, VisibleComponent)

const ambientLightEntity = createEntity()
const light = new AmbientLight('#aaa')
light.layers.enable(ObjectLayers.Portal)
addObjectToGroup(ambientLightEntity, light)
Expand All @@ -222,14 +218,16 @@ export const HyperspaceTagComponent = defineComponent({
new Vector3(0, 0, 1).applyQuaternion(cameraTransform.rotation).setY(0).normalize()
)

getMutableComponent(entity, HyperspaceTagComponent).hyperspaceEffectEntity.set(hyperspaceEffectEntity)
getMutableComponent(entity, HyperspaceTagComponent).ambientLightEntity.set(ambientLightEntity)
return () => {
removeEntity(ambientLightEntity)
destroyEntityTree(hyperspaceEffectEntity)
}
}, [])

useEffect(() => {
if (!galaxyTexture) return

const hyperspaceEffectEntity = getComponent(entity, HyperspaceTagComponent).hyperspaceEffectEntity
const hyperspaceEffectEntity = hyperspaceEffectEntityState.value
const hyperspaceEffect = getComponent(hyperspaceEffectEntity, GroupComponent)[0] as any as PortalEffect
hyperspaceEffect.texture = galaxyTexture
}, [galaxyTexture])
Expand All @@ -238,8 +236,9 @@ export const HyperspaceTagComponent = defineComponent({
() => {
if (!hasComponent(entity, HyperspaceTagComponent)) return

const { transition, hyperspaceEffectEntity } = getComponent(entity, HyperspaceTagComponent)
const hyperspaceEffectEntity = hyperspaceEffectEntityState.value
if (!hyperspaceEffectEntity) return
const { transition } = getComponent(entity, HyperspaceTagComponent)

const hyperspaceEffect = getComponent(hyperspaceEffectEntity, GroupComponent)[0] as any as PortalEffect
const cameraTransform = getComponent(Engine.instance.cameraEntity, TransformComponent)
Expand Down
43 changes: 25 additions & 18 deletions packages/engine/src/scene/components/MediaComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Ethereal Engine. All Rights Reserved.
*/

import type Hls from 'hls.js'
import { startTransition, useEffect } from 'react'
import { startTransition, useEffect, useLayoutEffect } from 'react'
import { DoubleSide, MeshBasicMaterial, PlaneGeometry, Vector3 } from 'three'

import { isClient } from '@etherealengine/common/src/utils/getEnvironment'
Expand Down Expand Up @@ -103,19 +103,26 @@ export const MediaElementComponent = defineComponent({
component.element.set(json.element as HTMLMediaElement)
},

onRemove: (entity, component) => {
const element = component.element.get({ noproxy: true }) as HTMLMediaElement
component.hls.value?.destroy()
component.hls.set(none)
const audioNodeGroup = AudioNodeGroups.get(element)
if (audioNodeGroup && audioNodeGroup.panner) removePannerNode(audioNodeGroup)
AudioNodeGroups.delete(element)
element.pause()
element.removeAttribute('src')
element.load()
element.remove()
component.element.set(none)
component.abortController.value.abort()
reactor: () => {
const entity = useEntityContext()
const mediaElementComponent = useComponent(entity, MediaElementComponent)

useLayoutEffect(() => {
return () => {
const element = mediaElementComponent.element.get({ noproxy: true }) as HTMLMediaElement
mediaElementComponent.hls.value?.destroy()
mediaElementComponent.hls.set(none)
const audioNodeGroup = AudioNodeGroups.get(element)
if (audioNodeGroup && audioNodeGroup.panner) removePannerNode(audioNodeGroup)
AudioNodeGroups.delete(element)
element.pause()
element.removeAttribute('src')
element.load()
element.remove()
mediaElementComponent.element.set(none)
mediaElementComponent.abortController.value.abort()
}
}, [])
},

errors: ['MEDIA_ERROR', 'HLS_ERROR']
Expand Down Expand Up @@ -156,10 +163,6 @@ export const MediaComponent = defineComponent({
}
},

onRemove: (entity, component) => {
removeComponent(entity, MediaElementComponent)
},

toJSON: (entity, component) => {
return {
controls: component.controls.value,
Expand Down Expand Up @@ -286,6 +289,10 @@ export function MediaReactor() {
document.body.removeEventListener('touchend', handleAutoplay)
renderer.domElement.removeEventListener('pointerup', handleAutoplay)
renderer.domElement.removeEventListener('touchend', handleAutoplay)

removeComponent(entity, BoundingBoxComponent)
removeComponent(entity, InputComponent)
removeComponent(entity, MediaElementComponent)
}
}, [])

Expand Down

0 comments on commit 8ff6c21

Please sign in to comment.