add plane orientation for looking from above/below

This commit is contained in:
Teriuihi 2025-06-22 17:32:32 +02:00
parent 4c31a91bb4
commit 3e98e1a498

View File

@ -12,7 +12,8 @@ export class IntersectionPlaneService {
private intersectionPlane!: THREE.Mesh;
private planePosition: number = 8; // Position in 1/16th of a block
constructor(private rendererService: RendererService) {}
constructor(private rendererService: RendererService) {
}
/**
* Creates the intersection plane and adds it to the scene
@ -43,7 +44,14 @@ export class IntersectionPlaneService {
// Convert from 1/16th block to Three.js units
const position = (this.planePosition / 16) - 0.5; // Center at 0
// Apply position based on the plane's current rotation
// Check if the plane is rotated vertically (looking from above/below)
if (Math.abs(this.intersectionPlane.rotation.x) > 0.1) {
// For vertical orientation, adjust Y position
this.intersectionPlane.position.y = position * (this.intersectionPlane.rotation.x > 0 ? -1 : 1);
// Reset x and z positions
this.intersectionPlane.position.x = 0;
this.intersectionPlane.position.z = 0;
} else {
const rotation = this.intersectionPlane.rotation.y;
if (Math.abs(rotation) < 0.1 || Math.abs(rotation - Math.PI) < 0.1) {
@ -60,6 +68,7 @@ export class IntersectionPlaneService {
this.intersectionPlane.position.z = 0;
}
}
}
/**
* Updates the plane orientation based on camera position
@ -67,6 +76,29 @@ export class IntersectionPlaneService {
updatePlaneOrientation(camera: THREE.Camera): void {
if (!this.intersectionPlane) return;
// Check if camera is looking from above or below first
const verticalAngle = Math.atan2(
camera.position.y,
Math.sqrt(camera.position.x * camera.position.x + camera.position.z * camera.position.z)
);
// Threshold angle for considering the camera to be above/below (about 45 degrees)
const verticalThreshold = Math.PI / 4;
if (verticalAngle > verticalThreshold) {
// Camera is above
this.intersectionPlane.rotation.x = -Math.PI / 2;
this.intersectionPlane.rotation.y = 0;
this.updatePlaneMaterial(0xAA0000);
} else if (verticalAngle < -verticalThreshold) {
// Camera is below
this.intersectionPlane.rotation.x = Math.PI / 2;
this.intersectionPlane.rotation.y = 0;
this.updatePlaneMaterial(0xAA0000);
} else {
// Reset rotation.x as we're now in the horizontal plane
this.intersectionPlane.rotation.x = 0;
// Calculate the angle between camera and player (in the XZ plane)
const cameraAngle = Math.atan2(
camera.position.x,
@ -90,6 +122,7 @@ export class IntersectionPlaneService {
this.intersectionPlane.rotation.y = -Math.PI / 2; // Camera on left
this.updatePlaneMaterial(0x0000AA);
}
}
// Update position after rotation change
this.updatePlanePosition(this.planePosition);