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

Fix head decap race condition #9442

Merged
merged 2 commits into from
Dec 14, 2023
Merged
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
15 changes: 10 additions & 5 deletions packages/engine/src/avatar/components/AvatarIKComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ import { Quaternion, Vector3 } from 'three'

import { Types } from 'bitecs'
import { Entity } from '../../ecs/classes/Entity'
import { defineComponent, getComponent, useOptionalComponent } from '../../ecs/functions/ComponentFunctions'
import {
defineComponent,
getComponent,
useComponent,
useOptionalComponent
} from '../../ecs/functions/ComponentFunctions'
import { useEntityContext } from '../../ecs/functions/EntityFunctions'
import { NetworkObjectComponent } from '../../networking/components/NetworkObjectComponent'
import { NameComponent } from '../../scene/components/NameComponent'
Expand All @@ -43,16 +48,16 @@ export const AvatarHeadDecapComponent = defineComponent({
reactor: function () {
const entity = useEntityContext()

const headDecap = useOptionalComponent(entity, AvatarHeadDecapComponent)
const headDecap = useComponent(entity, AvatarHeadDecapComponent)
const rig = useOptionalComponent(entity, AvatarRigComponent)

useEffect(() => {
if (!rig?.value?.vrm?.humanoid?.rawHumanBones?.head?.node || !headDecap?.value) return
if (!rig?.value?.rawRig?.head?.node || !headDecap?.value) return

rig.value.vrm.humanoid.rawHumanBones.head.node.scale.setScalar(EPSILON)
rig.value.rawRig.head.node.scale.setScalar(EPSILON)

return () => {
rig.value.vrm.humanoid.rawHumanBones.head.node.scale.setScalar(1)
rig.value.rawRig.head.node.scale.setScalar(1)
}
}, [headDecap, rig])

Expand Down
20 changes: 11 additions & 9 deletions packages/engine/src/avatar/systems/AvatarControllerSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import { NetworkState } from '../../networking/NetworkState'
import { NetworkObjectAuthorityTag, NetworkObjectComponent } from '../../networking/components/NetworkObjectComponent'
import { WorldNetworkAction } from '../../networking/functions/WorldNetworkAction'
import { RigidBodyComponent } from '../../physics/components/RigidBodyComponent'
import { XRAction } from '../../xr/XRState'
import { XRAction, XRControlsState } from '../../xr/XRState'
import { AvatarControllerComponent } from '../components/AvatarControllerComponent'
import { AvatarHeadDecapComponent } from '../components/AvatarIKComponents'
import { respawnAvatar } from '../functions/respawnAvatar'
Expand Down Expand Up @@ -83,15 +83,17 @@ const execute = () => {
})
}

for (const entity of controllerQuery()) {
const controller = getComponent(entity, AvatarControllerComponent)
const followCamera = getOptionalComponent(controller.cameraEntity, FollowCameraComponent)
if (followCamera) {
// todo calculate head size and use that as the bound #7263
if (followCamera.distance < 0.6) setComponent(entity, AvatarHeadDecapComponent, true)
else removeComponent(entity, AvatarHeadDecapComponent)
/** @todo non-immersive camera should utilize isCameraAttachedToAvatar */
if (!getState(XRControlsState).isCameraAttachedToAvatar)
for (const entity of controllerQuery()) {
const controller = getComponent(entity, AvatarControllerComponent)
const followCamera = getOptionalComponent(controller.cameraEntity, FollowCameraComponent)
if (followCamera) {
// todo calculate head size and use that as the bound #7263
if (followCamera.distance < 0.6) setComponent(entity, AvatarHeadDecapComponent, true)
else removeComponent(entity, AvatarHeadDecapComponent)
}
}
}

const controlledEntity = Engine.instance.localClientEntity

Expand Down