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>
|
<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>
|
<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>
|
</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>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
68
src/index.js
68
src/index.js
@@ -24,7 +24,9 @@ let fpvControls = {
|
|||||||
rotation: { x: 0, y: 0 },
|
rotation: { x: 0, y: 0 },
|
||||||
verticalVelocity: 0,
|
verticalVelocity: 0,
|
||||||
isJumping: false,
|
isJumping: false,
|
||||||
isCrouching: false
|
isCrouching: false,
|
||||||
|
currentHeight: 0,
|
||||||
|
targetHeight: 0
|
||||||
};
|
};
|
||||||
let modelBounds = null;
|
let modelBounds = null;
|
||||||
let groundLevel = 0;
|
let groundLevel = 0;
|
||||||
@@ -952,19 +954,29 @@ function enterFPVMode() {
|
|||||||
// Disable orbit controls
|
// Disable orbit controls
|
||||||
controls.enabled = false;
|
controls.enabled = false;
|
||||||
|
|
||||||
// Position camera at eye height on the ground level (center of model in X/Z)
|
// Keep current camera position instead of forcing to model center
|
||||||
const centerX = modelBounds.center.x;
|
// Just ensure the height is reasonable for walking
|
||||||
const centerZ = modelBounds.center.z;
|
fpvControls.currentHeight = Math.max(camera.position.y, eyeHeight);
|
||||||
camera.position.set(centerX, eyeHeight, centerZ);
|
fpvControls.targetHeight = fpvControls.currentHeight;
|
||||||
|
camera.position.y = fpvControls.currentHeight;
|
||||||
|
|
||||||
// Reset rotation and movement
|
// Calculate rotation based on current camera orientation
|
||||||
fpvControls.rotation.x = 0;
|
const euler = new THREE.Euler().setFromQuaternion(camera.quaternion, 'YXZ');
|
||||||
fpvControls.rotation.y = 0;
|
fpvControls.rotation.x = euler.x;
|
||||||
|
fpvControls.rotation.y = euler.y;
|
||||||
|
|
||||||
|
// Reset movement state
|
||||||
fpvControls.velocity.set(0, 0, 0);
|
fpvControls.velocity.set(0, 0, 0);
|
||||||
fpvControls.verticalVelocity = 0;
|
fpvControls.verticalVelocity = 0;
|
||||||
fpvControls.isJumping = false;
|
fpvControls.isJumping = false;
|
||||||
fpvControls.isCrouching = false;
|
fpvControls.isCrouching = false;
|
||||||
|
|
||||||
|
// Show crosshair
|
||||||
|
const crosshair = document.getElementById('fpv-crosshair');
|
||||||
|
if (crosshair) {
|
||||||
|
crosshair.style.display = 'block';
|
||||||
|
}
|
||||||
|
|
||||||
// Update button text
|
// Update button text
|
||||||
const fpvBtn = document.getElementById('fpv-btn');
|
const fpvBtn = document.getElementById('fpv-btn');
|
||||||
if (fpvBtn) {
|
if (fpvBtn) {
|
||||||
@@ -993,6 +1005,12 @@ function exitFPVMode() {
|
|||||||
// Re-enable orbit controls
|
// Re-enable orbit controls
|
||||||
controls.enabled = true;
|
controls.enabled = true;
|
||||||
|
|
||||||
|
// Hide crosshair
|
||||||
|
const crosshair = document.getElementById('fpv-crosshair');
|
||||||
|
if (crosshair) {
|
||||||
|
crosshair.style.display = 'none';
|
||||||
|
}
|
||||||
|
|
||||||
// Update button text
|
// Update button text
|
||||||
const fpvBtn = document.getElementById('fpv-btn');
|
const fpvBtn = document.getElementById('fpv-btn');
|
||||||
if (fpvBtn) {
|
if (fpvBtn) {
|
||||||
@@ -1010,6 +1028,8 @@ function exitFPVMode() {
|
|||||||
fpvControls.verticalVelocity = 0;
|
fpvControls.verticalVelocity = 0;
|
||||||
fpvControls.isJumping = false;
|
fpvControls.isJumping = false;
|
||||||
fpvControls.isCrouching = false;
|
fpvControls.isCrouching = false;
|
||||||
|
fpvControls.currentHeight = eyeHeight;
|
||||||
|
fpvControls.targetHeight = eyeHeight;
|
||||||
|
|
||||||
console.log('Orbit controls restored');
|
console.log('Orbit controls restored');
|
||||||
}, 50);
|
}, 50);
|
||||||
@@ -1020,9 +1040,10 @@ function updateFPVMovement() {
|
|||||||
|
|
||||||
const delta = 0.016; // Approximate 60fps
|
const delta = 0.016; // Approximate 60fps
|
||||||
const moveSpeed = 100.0; // Units per second (1000x faster)
|
const moveSpeed = 100.0; // Units per second (1000x faster)
|
||||||
const jumpSpeed = 120.0; // Jump initial velocity (4x higher)
|
const jumpSpeed = 220.0; // Jump initial velocity (4x higher)
|
||||||
const gravity = 120.0; // Gravity strength (4x faster)
|
const gravity = 420.0; // Gravity strength (4x faster)
|
||||||
const crouchHeight = eyeHeight * 0.6; // Crouch height (60% of normal eye height)
|
const crouchHeight = eyeHeight * 0.5; // Crouch height (60% of normal eye height)
|
||||||
|
const crouchSpeed = 100.0; // Speed of crouching transition
|
||||||
|
|
||||||
// Reset direction
|
// Reset direction
|
||||||
fpvControls.direction.set(0, 0, 0);
|
fpvControls.direction.set(0, 0, 0);
|
||||||
@@ -1057,23 +1078,36 @@ function updateFPVMovement() {
|
|||||||
fpvControls.canJump = false;
|
fpvControls.canJump = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update target height based on crouching state
|
||||||
|
fpvControls.targetHeight = fpvControls.isCrouching ? crouchHeight : eyeHeight;
|
||||||
|
|
||||||
// Apply gravity and vertical movement
|
// Apply gravity and vertical movement
|
||||||
if (fpvControls.isJumping) {
|
if (fpvControls.isJumping) {
|
||||||
fpvControls.verticalVelocity -= gravity * delta;
|
fpvControls.verticalVelocity -= gravity * delta;
|
||||||
camera.position.y += fpvControls.verticalVelocity * delta;
|
camera.position.y += fpvControls.verticalVelocity * delta;
|
||||||
|
|
||||||
// Check if landed back on ground
|
// Check if landed back on ground
|
||||||
const targetHeight = fpvControls.isCrouching ? crouchHeight : eyeHeight;
|
if (camera.position.y <= fpvControls.targetHeight) {
|
||||||
if (camera.position.y <= targetHeight) {
|
camera.position.y = fpvControls.targetHeight;
|
||||||
camera.position.y = targetHeight;
|
fpvControls.currentHeight = fpvControls.targetHeight;
|
||||||
fpvControls.verticalVelocity = 0;
|
fpvControls.verticalVelocity = 0;
|
||||||
fpvControls.isJumping = false;
|
fpvControls.isJumping = false;
|
||||||
fpvControls.canJump = true;
|
fpvControls.canJump = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Handle crouching when not jumping
|
// Smooth crouching transition when not jumping
|
||||||
const targetHeight = fpvControls.isCrouching ? crouchHeight : eyeHeight;
|
if (Math.abs(fpvControls.currentHeight - fpvControls.targetHeight) > 0.01) {
|
||||||
camera.position.y = targetHeight;
|
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
|
// Apply rotation to camera
|
||||||
|
|||||||
Reference in New Issue
Block a user