Skip to content

Commit 1525fac

Browse files
committed
fix: some visual camera world view issues (visible lines between blocks)
1 parent f24cb49 commit 1525fac

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

renderer/viewer/three/cameraShake.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,12 @@ export class CameraShake {
8080
camera.setRotationFromQuaternion(yawQuat)
8181
} else {
8282
// For regular camera, apply all rotations
83-
const pitchQuat = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(1, 0, 0), this.basePitch)
84-
const yawQuat = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 1, 0), this.baseYaw)
83+
// Add tiny offsets to prevent z-fighting at ideal angles (90, 180, 270 degrees)
84+
const pitchOffset = this.addAntiZfightingOffset(this.basePitch)
85+
const yawOffset = this.addAntiZfightingOffset(this.baseYaw)
86+
87+
const pitchQuat = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(1, 0, 0), pitchOffset)
88+
const yawQuat = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 1, 0), yawOffset)
8589
const rollQuat = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 0, 1), THREE.MathUtils.degToRad(this.rollAngle))
8690
// Combine rotations in the correct order: pitch -> yaw -> roll
8791
const finalQuat = yawQuat.multiply(pitchQuat).multiply(rollQuat)
@@ -96,4 +100,21 @@ export class CameraShake {
96100
private easeInOut (t: number): number {
97101
return t < 0.5 ? 2 * t * t : 1 - (-2 * t + 2) ** 2 / 2
98102
}
103+
104+
private addAntiZfightingOffset (angle: number): number {
105+
const offset = 0.001 // Very small offset in radians (about 0.057 degrees)
106+
107+
// Check if the angle is close to ideal angles (0, π/2, π, 3π/2)
108+
const normalizedAngle = ((angle % (Math.PI * 2)) + Math.PI * 2) % (Math.PI * 2)
109+
const tolerance = 0.01 // Tolerance for considering an angle "ideal"
110+
111+
if (Math.abs(normalizedAngle) < tolerance ||
112+
Math.abs(normalizedAngle - Math.PI / 2) < tolerance ||
113+
Math.abs(normalizedAngle - Math.PI) < tolerance ||
114+
Math.abs(normalizedAngle - 3 * Math.PI / 2) < tolerance) {
115+
return angle + offset
116+
}
117+
118+
return angle
119+
}
99120
}

renderer/viewer/three/world/cursorBlock.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class CursorBlock {
2828
}
2929

3030
cursorLineMaterial: LineMaterial
31-
interactionLines: null | { blockPos: Vec3, mesh: THREE.Group } = null
31+
interactionLines: null | { blockPos: Vec3, mesh: THREE.Group, shapePositions: BlocksShapes | undefined } = null
3232
prevColor: string | undefined
3333
blockBreakMesh: THREE.Mesh
3434
breakTextures: THREE.Texture[] = []
@@ -62,13 +62,23 @@ export class CursorBlock {
6262
this.worldRenderer.onReactivePlayerStateUpdated('gameMode', () => {
6363
this.updateLineMaterial()
6464
})
65+
// todo figure out why otherwise fog from skybox breaks it
66+
setTimeout(() => {
67+
this.updateLineMaterial()
68+
if (this.interactionLines) {
69+
this.setHighlightCursorBlock(this.interactionLines.blockPos, this.interactionLines.shapePositions, true)
70+
}
71+
})
6572
}
6673

6774
// Update functions
6875
updateLineMaterial () {
6976
const inCreative = this.worldRenderer.playerStateReactive.gameMode === 'creative'
7077
const pixelRatio = this.worldRenderer.renderer.getPixelRatio()
7178

79+
if (this.cursorLineMaterial) {
80+
this.cursorLineMaterial.dispose()
81+
}
7282
this.cursorLineMaterial = new LineMaterial({
7383
color: (() => {
7484
switch (this.worldRenderer.worldRendererConfig.highlightBlockColor) {
@@ -115,8 +125,8 @@ export class CursorBlock {
115125
}
116126
}
117127

118-
setHighlightCursorBlock (blockPos: Vec3 | null, shapePositions?: BlocksShapes): void {
119-
if (blockPos && this.interactionLines && blockPos.equals(this.interactionLines.blockPos)) {
128+
setHighlightCursorBlock (blockPos: Vec3 | null, shapePositions?: BlocksShapes, force = false): void {
129+
if (blockPos && this.interactionLines && blockPos.equals(this.interactionLines.blockPos) && !force) {
120130
return
121131
}
122132
if (this.interactionLines !== null) {
@@ -140,7 +150,7 @@ export class CursorBlock {
140150
}
141151
this.worldRenderer.scene.add(group)
142152
group.visible = !this.cursorLinesHidden
143-
this.interactionLines = { blockPos, mesh: group }
153+
this.interactionLines = { blockPos, mesh: group, shapePositions }
144154
}
145155

146156
render () {

0 commit comments

Comments
 (0)