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

Add Network Pose Update Component #8043

Merged
merged 1 commit into from
Jun 1, 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import {
} from '../../ecs/functions/ComponentFunctions'
import { LocalAvatarTagComponent } from '../../input/components/LocalAvatarTagComponent'
import { LocalInputTagComponent } from '../../input/components/LocalInputTagComponent'
import { NetworkObjectAuthorityTag } from '../../networking/components/NetworkObjectComponent'
import {
NetworkObjectAuthorityTag,
NetworkObjectSendPeriodicUpdatesTag
} from '../../networking/components/NetworkObjectComponent'
import { NetworkPeerFunctions } from '../../networking/functions/NetworkPeerFunctions'
import { WorldNetworkAction } from '../../networking/functions/WorldNetworkAction'
import { WorldState } from '../../networking/interfaces/WorldState'
Expand Down Expand Up @@ -116,6 +119,8 @@ export const spawnAvatarReceptor = (spawnAction: typeof WorldNetworkAction.spawn
createAvatarCollider(entity)
}

setComponent(entity, NetworkObjectSendPeriodicUpdatesTag)

setComponent(entity, ShadowComponent)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,5 @@ export const NetworkObjectComponent = defineComponent({
export const NetworkObjectAuthorityTag = defineComponent({ name: 'NetworkObjectAuthorityTag' })

export const NetworkObjectOwnedTag = defineComponent({ name: 'NetworkObjectOwnedTag' })

export const NetworkObjectSendPeriodicUpdatesTag = defineComponent({ name: 'NetworkObjectSendPeriodicUpdatesTag' })
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,7 @@ const receiveSpawnObject = (action: typeof WorldNetworkAction.spawnObject.matche
if (action.position) position.copy(action.position)
if (action.rotation) rotation.copy(action.rotation)

setTransformComponent(entity, position, rotation)
const transform = getComponent(entity, TransformComponent)

// set cached action refs to the new components so they stay up to date with future movements
action.position = transform.position
action.rotation = transform.rotation
setComponent(entity, TransformComponent, { position, rotation })
}

const receiveRegisterSceneObject = (action: typeof WorldNetworkAction.registerSceneObject.matches._TYPE) => {
Expand Down
22 changes: 12 additions & 10 deletions packages/engine/src/networking/serialization/DataReader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
} from '../../transform/TransformSerialization'
import { Network } from '../classes/Network'
// import { XRHandBones } from '../../xr/XRHandBones'
import { NetworkObjectAuthorityTag } from '../components/NetworkObjectComponent'
import { NetworkObjectAuthorityTag, NetworkObjectSendPeriodicUpdatesTag } from '../components/NetworkObjectComponent'
import { NetworkObjectComponent } from '../components/NetworkObjectComponent'
import { NetworkState } from '../NetworkState'
import {
Expand Down Expand Up @@ -99,7 +99,7 @@ describe('DataReader', () => {

it('should readComponent', () => {
const view = createViewCursor()
const entity = 42 as Entity
const entity = createEntity()

const [x, y, z] = [1.5, 2.5, 3.5]
TransformComponent.position.x[entity] = x
Expand Down Expand Up @@ -142,7 +142,7 @@ describe('DataReader', () => {

it('should readComponentProp', () => {
const view = createViewCursor()
const entity = 42 as Entity
const entity = createEntity()

const prop = TransformComponent.position.x as unknown as TypedArray

Expand All @@ -161,7 +161,7 @@ describe('DataReader', () => {

it('should readVector3', () => {
const view = createViewCursor()
const entity = 42 as Entity
const entity = createEntity()
const position = TransformComponent.position as unknown as Vector3SoA
const [x, y, z] = [1.5, 2.5, 3.5]
position.x[entity] = x
Expand Down Expand Up @@ -197,7 +197,7 @@ describe('DataReader', () => {

it('should readVector4', () => {
const view = createViewCursor()
const entity = 42 as Entity
const entity = createEntity()
const rotation = TransformComponent.rotation
const [x, y, z, w] = [1.5, 2.5, 3.5, 4.5]
rotation.x[entity] = x
Expand Down Expand Up @@ -239,7 +239,7 @@ describe('DataReader', () => {

it('should readPosition', () => {
const view = createViewCursor()
const entity = 42 as Entity
const entity = createEntity()
const position = TransformComponent.position
const [x, y, z] = [1.5, 2.5, 3.5]
position.x[entity] = x
Expand Down Expand Up @@ -273,8 +273,9 @@ describe('DataReader', () => {

it('should readCompressedRotation', () => {
const view = createViewCursor()
const entity = 42 as Entity
const entity = createEntity()
const rotation = TransformComponent.rotation
setComponent(entity, NetworkObjectSendPeriodicUpdatesTag)

// construct values for a valid quaternion
const [a, b, c] = [0.167, 0.167, 0.167]
Expand All @@ -286,7 +287,7 @@ describe('DataReader', () => {
rotation.z[entity] = z
rotation.w[entity] = w

writeRotation(view, entity, true)
writeRotation(view, entity)

rotation.x[entity] = 0
rotation.y[entity] = 0
Expand All @@ -308,14 +309,15 @@ describe('DataReader', () => {

it('should readCompressedVector3', () => {
const view = createViewCursor()
const entity = 42 as Entity
const entity = createEntity()
setComponent(entity, NetworkObjectSendPeriodicUpdatesTag)

const [x, y, z] = [1.333, 2.333, 3.333]
RigidBodyComponent.linearVelocity.x[entity] = x
RigidBodyComponent.linearVelocity.y[entity] = y
RigidBodyComponent.linearVelocity.z[entity] = z

writeCompressedVector3(RigidBodyComponent.linearVelocity)(view, entity, true)
writeCompressedVector3(RigidBodyComponent.linearVelocity)(view, entity)

RigidBodyComponent.linearVelocity.x[entity] = 0
RigidBodyComponent.linearVelocity.y[entity] = 0
Expand Down
20 changes: 11 additions & 9 deletions packages/engine/src/networking/serialization/DataWriter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { proxifyQuaternion, proxifyVector3 } from '../../common/proxies/createTh
import { destroyEngine, Engine } from '../../ecs/classes/Engine'
import { EngineState } from '../../ecs/classes/EngineState'
import { Entity } from '../../ecs/classes/Entity'
import { addComponent } from '../../ecs/functions/ComponentFunctions'
import { addComponent, setComponent } from '../../ecs/functions/ComponentFunctions'
import { createEntity } from '../../ecs/functions/EntityFunctions'
import { createEngine } from '../../initializeEngine'
import { RigidBodyComponent } from '../../physics/components/RigidBodyComponent'
Expand All @@ -25,7 +25,7 @@ import {
writeTransform
} from '../../transform/TransformSerialization'
import { Network } from '../classes/Network'
import { NetworkObjectComponent } from '../components/NetworkObjectComponent'
import { NetworkObjectComponent, NetworkObjectSendPeriodicUpdatesTag } from '../components/NetworkObjectComponent'
import { NetworkState } from '../NetworkState'
import { readCompressedRotation, readCompressedVector3 } from './DataReader'
import {
Expand Down Expand Up @@ -66,7 +66,7 @@ describe('DataWriter', () => {

it('should writeComponent', () => {
const writeView = createViewCursor()
const entity = 42 as Entity
const entity = createEntity()

const [x, y, z] = [1.5, 2.5, 3.5]
TransformComponent.position.x[entity] = x
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('DataWriter', () => {

it('should writeVector3', () => {
const writeView = createViewCursor()
const entity = 42 as Entity
const entity = createEntity()

const [x, y, z] = [1.5, 2.5, 3.5]
TransformComponent.position.x[entity] = x
Expand Down Expand Up @@ -140,7 +140,7 @@ describe('DataWriter', () => {

it('should writePosition', () => {
const writeView = createViewCursor()
const entity = 42 as Entity
const entity = createEntity()

const [x, y, z] = [1.5, 2.5, 3.5]
TransformComponent.position.x[entity] = x
Expand All @@ -161,7 +161,8 @@ describe('DataWriter', () => {

it('should writeCompressedRotation', () => {
const writeView = createViewCursor()
const entity = 42 as Entity
const entity = createEntity()
setComponent(entity, NetworkObjectSendPeriodicUpdatesTag)

// construct values for a valid quaternion
const [a, b, c] = [0.167, 0.167, 0.167]
Expand All @@ -173,7 +174,7 @@ describe('DataWriter', () => {
TransformComponent.rotation.z[entity] = z
TransformComponent.rotation.w[entity] = w

writeCompressedRotation(TransformComponent.rotation)(writeView, entity, true)
writeCompressedRotation(TransformComponent.rotation)(writeView, entity)

const readView = createViewCursor(writeView.buffer)
readCompressedRotation(TransformComponent.rotation)(readView, entity)
Expand All @@ -189,14 +190,15 @@ describe('DataWriter', () => {

it('should writeCompressedVector3', () => {
const writeView = createViewCursor()
const entity = 42 as Entity
const entity = createEntity()
setComponent(entity, NetworkObjectSendPeriodicUpdatesTag)

const [x, y, z] = [1.333, 2.333, 3.333]
RigidBodyComponent.linearVelocity.x[entity] = x
RigidBodyComponent.linearVelocity.y[entity] = y
RigidBodyComponent.linearVelocity.z[entity] = z

writeCompressedVector3(RigidBodyComponent.linearVelocity)(writeView, entity, true)
writeCompressedVector3(RigidBodyComponent.linearVelocity)(writeView, entity)

const readView = createViewCursor(writeView.buffer)
readCompressedVector3(RigidBodyComponent.linearVelocity)(readView, entity)
Expand Down
Loading