Skip to content

Commit af5a0b2

Browse files
committed
fix: fix camera desync updates in 3rd view and starfield
1 parent eedd9f1 commit af5a0b2

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed

renderer/viewer/lib/worldrendererCommon.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export abstract class WorldRendererCommon<WorkerSend = any, WorkerReceive = any>
106106
}>
107107
customTexturesDataUrl = undefined as string | undefined
108108
workers: any[] = []
109-
viewerPosition?: Vec3
109+
viewerChunkPosition?: Vec3
110110
lastCamUpdate = 0
111111
droppedFpsPercentage = 0
112112
initialChunkLoadWasStartedIn: number | undefined
@@ -499,7 +499,7 @@ export abstract class WorldRendererCommon<WorkerSend = any, WorkerReceive = any>
499499
timeUpdated? (newTime: number): void
500500

501501
updateViewerPosition (pos: Vec3) {
502-
this.viewerPosition = pos
502+
this.viewerChunkPosition = pos
503503
for (const [key, value] of Object.entries(this.loadedChunks)) {
504504
if (!value) continue
505505
this.updatePosDataChunk?.(key)
@@ -513,7 +513,7 @@ export abstract class WorldRendererCommon<WorkerSend = any, WorkerReceive = any>
513513
}
514514

515515
getDistance (posAbsolute: Vec3) {
516-
const [botX, botZ] = chunkPos(this.viewerPosition!)
516+
const [botX, botZ] = chunkPos(this.viewerChunkPosition!)
517517
const dx = Math.abs(botX - Math.floor(posAbsolute.x / 16))
518518
const dz = Math.abs(botZ - Math.floor(posAbsolute.z / 16))
519519
return [dx, dz] as [number, number]

renderer/viewer/three/entities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ export class Entities {
344344
}
345345

346346
const dt = this.clock.getDelta()
347-
const botPos = this.worldRenderer.viewerPosition
347+
const botPos = this.worldRenderer.viewerChunkPosition
348348
const VISIBLE_DISTANCE = 10 * 10
349349

350350
// Update regular entities

renderer/viewer/three/worldrendererThree.ts

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class WorldRendererThree extends WorldRendererCommon {
8888

8989
this.renderer = renderer
9090
displayOptions.rendererState.renderer = WorldRendererThree.getRendererInfo(renderer) ?? '...'
91-
this.starField = new StarField(this.scene)
91+
this.starField = new StarField(this)
9292
this.cursorBlock = new CursorBlock(this)
9393
this.holdingBlock = new HoldingBlock(this)
9494
this.holdingBlockLeft = new HoldingBlock(this, true)
@@ -318,10 +318,11 @@ export class WorldRendererThree extends WorldRendererCommon {
318318
section.renderOrder = 500 - chunkDistance
319319
}
320320

321-
updateViewerPosition (pos: Vec3): void {
322-
this.viewerPosition = pos
323-
const cameraPos = this.cameraObject.position.toArray().map(x => Math.floor(x / 16)) as [number, number, number]
324-
this.cameraSectionPos = new Vec3(...cameraPos)
321+
override updateViewerPosition (pos: Vec3): void {
322+
this.viewerChunkPosition = pos
323+
}
324+
325+
cameraSectionPositionUpdate () {
325326
// eslint-disable-next-line guard-for-in
326327
for (const key in this.sectionObjects) {
327328
const value = this.sectionObjects[key]
@@ -447,11 +448,35 @@ export class WorldRendererThree extends WorldRendererCommon {
447448
return tex
448449
}
449450

451+
getCameraPosition () {
452+
const worldPos = new THREE.Vector3()
453+
this.camera.getWorldPosition(worldPos)
454+
return worldPos
455+
}
456+
457+
getWorldCameraPosition () {
458+
const pos = this.getCameraPosition()
459+
return new Vec3(
460+
Math.floor(pos.x / 16),
461+
Math.floor(pos.y / 16),
462+
Math.floor(pos.z / 16)
463+
)
464+
}
465+
466+
updateCameraSectionPos () {
467+
const newSectionPos = this.getWorldCameraPosition()
468+
if (!this.cameraSectionPos.equals(newSectionPos)) {
469+
this.cameraSectionPos = newSectionPos
470+
this.cameraSectionPositionUpdate()
471+
}
472+
}
473+
450474
setFirstPersonCamera (pos: Vec3 | null, yaw: number, pitch: number) {
451475
const yOffset = this.playerStateReactive.eyeHeight
452476

453477
this.updateCamera(pos?.offset(0, yOffset, 0) ?? null, yaw, pitch)
454478
this.media.tryIntersectMedia()
479+
this.updateCameraSectionPos()
455480
}
456481

457482
getThirdPersonCamera (pos: THREE.Vector3 | null, yaw: number, pitch: number) {
@@ -636,6 +661,8 @@ export class WorldRendererThree extends WorldRendererCommon {
636661
}
637662
}
638663
}
664+
665+
this.updateCameraSectionPos()
639666
}
640667

641668
debugChunksVisibilityOverride () {
@@ -988,7 +1015,9 @@ class StarField {
9881015
}
9891016
}
9901017

991-
constructor (private readonly scene: THREE.Scene) {
1018+
constructor (
1019+
private readonly worldRenderer: WorldRendererThree
1020+
) {
9921021
}
9931022

9941023
addToScene () {
@@ -1030,11 +1059,11 @@ class StarField {
10301059

10311060
// Create points and add them to the scene
10321061
this.points = new THREE.Points(geometry, material)
1033-
this.scene.add(this.points)
1062+
this.worldRenderer.scene.add(this.points)
10341063

10351064
const clock = new THREE.Clock()
10361065
this.points.onBeforeRender = (renderer, scene, camera) => {
1037-
this.points?.position.copy?.(camera.position)
1066+
this.points?.position.copy?.(this.worldRenderer.getCameraPosition())
10381067
material.uniforms.time.value = clock.getElapsedTime() * speed
10391068
}
10401069
this.points.renderOrder = -1
@@ -1044,7 +1073,7 @@ class StarField {
10441073
if (this.points) {
10451074
this.points.geometry.dispose();
10461075
(this.points.material as THREE.Material).dispose()
1047-
this.scene.remove(this.points)
1076+
this.worldRenderer.scene.remove(this.points)
10481077

10491078
this.points = undefined
10501079
}

0 commit comments

Comments
 (0)