u
This commit is contained in:
@@ -26,6 +26,12 @@
|
||||
<p style="font-size: 11px; color: #aaa; margin: 5px 0;">In FPV: WASD to move, mouse to look, SPACE to jump, SHIFT to crouch, F or ESC to exit</p>
|
||||
<button id="print-btn" style="background: #4CAF50; color: white; border: none; padding: 8px 12px; border-radius: 4px; cursor: pointer; font-size: 12px;">🖨️ Print</button>
|
||||
</div>
|
||||
|
||||
<!-- FPV Crosshair -->
|
||||
<div id="fpv-crosshair" style="display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); pointer-events: none; z-index: 1000;">
|
||||
<div style="position: absolute; width: 20px; height: 2px; background: rgba(255, 255, 255, 0.8); top: -1px; left: -10px; border: 1px solid rgba(0, 0, 0, 0.5);"></div>
|
||||
<div style="position: absolute; width: 2px; height: 20px; background: rgba(255, 255, 255, 0.8); top: -10px; left: -1px; border: 1px solid rgba(0, 0, 0, 0.5);"></div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
68
src/index.js
68
src/index.js
@@ -24,7 +24,9 @@ let fpvControls = {
|
||||
rotation: { x: 0, y: 0 },
|
||||
verticalVelocity: 0,
|
||||
isJumping: false,
|
||||
isCrouching: false
|
||||
isCrouching: false,
|
||||
currentHeight: 0,
|
||||
targetHeight: 0
|
||||
};
|
||||
let modelBounds = null;
|
||||
let groundLevel = 0;
|
||||
@@ -952,19 +954,29 @@ function enterFPVMode() {
|
||||
// Disable orbit controls
|
||||
controls.enabled = false;
|
||||
|
||||
// Position camera at eye height on the ground level (center of model in X/Z)
|
||||
const centerX = modelBounds.center.x;
|
||||
const centerZ = modelBounds.center.z;
|
||||
camera.position.set(centerX, eyeHeight, centerZ);
|
||||
// Keep current camera position instead of forcing to model center
|
||||
// Just ensure the height is reasonable for walking
|
||||
fpvControls.currentHeight = Math.max(camera.position.y, eyeHeight);
|
||||
fpvControls.targetHeight = fpvControls.currentHeight;
|
||||
camera.position.y = fpvControls.currentHeight;
|
||||
|
||||
// Reset rotation and movement
|
||||
fpvControls.rotation.x = 0;
|
||||
fpvControls.rotation.y = 0;
|
||||
// Calculate rotation based on current camera orientation
|
||||
const euler = new THREE.Euler().setFromQuaternion(camera.quaternion, 'YXZ');
|
||||
fpvControls.rotation.x = euler.x;
|
||||
fpvControls.rotation.y = euler.y;
|
||||
|
||||
// Reset movement state
|
||||
fpvControls.velocity.set(0, 0, 0);
|
||||
fpvControls.verticalVelocity = 0;
|
||||
fpvControls.isJumping = false;
|
||||
fpvControls.isCrouching = false;
|
||||
|
||||
// Show crosshair
|
||||
const crosshair = document.getElementById('fpv-crosshair');
|
||||
if (crosshair) {
|
||||
crosshair.style.display = 'block';
|
||||
}
|
||||
|
||||
// Update button text
|
||||
const fpvBtn = document.getElementById('fpv-btn');
|
||||
if (fpvBtn) {
|
||||
@@ -993,6 +1005,12 @@ function exitFPVMode() {
|
||||
// Re-enable orbit controls
|
||||
controls.enabled = true;
|
||||
|
||||
// Hide crosshair
|
||||
const crosshair = document.getElementById('fpv-crosshair');
|
||||
if (crosshair) {
|
||||
crosshair.style.display = 'none';
|
||||
}
|
||||
|
||||
// Update button text
|
||||
const fpvBtn = document.getElementById('fpv-btn');
|
||||
if (fpvBtn) {
|
||||
@@ -1010,6 +1028,8 @@ function exitFPVMode() {
|
||||
fpvControls.verticalVelocity = 0;
|
||||
fpvControls.isJumping = false;
|
||||
fpvControls.isCrouching = false;
|
||||
fpvControls.currentHeight = eyeHeight;
|
||||
fpvControls.targetHeight = eyeHeight;
|
||||
|
||||
console.log('Orbit controls restored');
|
||||
}, 50);
|
||||
@@ -1020,9 +1040,10 @@ function updateFPVMovement() {
|
||||
|
||||
const delta = 0.016; // Approximate 60fps
|
||||
const moveSpeed = 100.0; // Units per second (1000x faster)
|
||||
const jumpSpeed = 120.0; // Jump initial velocity (4x higher)
|
||||
const gravity = 120.0; // Gravity strength (4x faster)
|
||||
const crouchHeight = eyeHeight * 0.6; // Crouch height (60% of normal eye height)
|
||||
const jumpSpeed = 220.0; // Jump initial velocity (4x higher)
|
||||
const gravity = 420.0; // Gravity strength (4x faster)
|
||||
const crouchHeight = eyeHeight * 0.5; // Crouch height (60% of normal eye height)
|
||||
const crouchSpeed = 100.0; // Speed of crouching transition
|
||||
|
||||
// Reset direction
|
||||
fpvControls.direction.set(0, 0, 0);
|
||||
@@ -1057,23 +1078,36 @@ function updateFPVMovement() {
|
||||
fpvControls.canJump = false;
|
||||
}
|
||||
|
||||
// Update target height based on crouching state
|
||||
fpvControls.targetHeight = fpvControls.isCrouching ? crouchHeight : eyeHeight;
|
||||
|
||||
// Apply gravity and vertical movement
|
||||
if (fpvControls.isJumping) {
|
||||
fpvControls.verticalVelocity -= gravity * delta;
|
||||
camera.position.y += fpvControls.verticalVelocity * delta;
|
||||
|
||||
// Check if landed back on ground
|
||||
const targetHeight = fpvControls.isCrouching ? crouchHeight : eyeHeight;
|
||||
if (camera.position.y <= targetHeight) {
|
||||
camera.position.y = targetHeight;
|
||||
if (camera.position.y <= fpvControls.targetHeight) {
|
||||
camera.position.y = fpvControls.targetHeight;
|
||||
fpvControls.currentHeight = fpvControls.targetHeight;
|
||||
fpvControls.verticalVelocity = 0;
|
||||
fpvControls.isJumping = false;
|
||||
fpvControls.canJump = true;
|
||||
}
|
||||
} else {
|
||||
// Handle crouching when not jumping
|
||||
const targetHeight = fpvControls.isCrouching ? crouchHeight : eyeHeight;
|
||||
camera.position.y = targetHeight;
|
||||
// Smooth crouching transition when not jumping
|
||||
if (Math.abs(fpvControls.currentHeight - fpvControls.targetHeight) > 0.01) {
|
||||
const direction = fpvControls.targetHeight > fpvControls.currentHeight ? 1 : -1;
|
||||
fpvControls.currentHeight += direction * crouchSpeed * delta;
|
||||
|
||||
// Clamp to target
|
||||
if (direction > 0 && fpvControls.currentHeight >= fpvControls.targetHeight) {
|
||||
fpvControls.currentHeight = fpvControls.targetHeight;
|
||||
} else if (direction < 0 && fpvControls.currentHeight <= fpvControls.targetHeight) {
|
||||
fpvControls.currentHeight = fpvControls.targetHeight;
|
||||
}
|
||||
}
|
||||
camera.position.y = fpvControls.currentHeight;
|
||||
}
|
||||
|
||||
// Apply rotation to camera
|
||||
|
||||
Reference in New Issue
Block a user