Refactor Camera and OceanLOD for improved movement and LOD management; enhance shader effects for better visual fidelity
This commit is contained in:
74
src/main.ts
74
src/main.ts
@@ -138,17 +138,15 @@ var lodStatsTimer = 0;
|
||||
/** Input states*/
|
||||
var mouseXVel = 0;
|
||||
var mouseYVel = 0;
|
||||
var keyboardRotationX = 0;
|
||||
var keyboardRotationY = 0;
|
||||
var keyboardZoom = 0;
|
||||
var keysPressed: Set<string> = new Set();
|
||||
/** Objects and states*/
|
||||
var camera: Camera;
|
||||
var oceanLOD: OceanLOD;
|
||||
var skybox: Skybox;
|
||||
var curRotX = Config.CAMERA_DEFAULT_ROT_X;
|
||||
var curRotY = Config.CAMERA_DEFAULT_ROT_Y;
|
||||
var wireframeMode = false;
|
||||
/** Camera movement speed */
|
||||
var moveSpeed = 0.15;
|
||||
var fastMoveSpeed = 0.4;
|
||||
/** Ocean shader settings */
|
||||
var waveHeight = 1.0;
|
||||
var waveSpeed = 1.0;
|
||||
@@ -190,9 +188,16 @@ function drawScene() {
|
||||
var projection = mat4.create();
|
||||
mat4.perspective(projection, Config.FOV, viewportWidth / viewportHeight, Config.NEAR_PLANE, Config.FAR_PLANE);
|
||||
|
||||
camera.setOffset(Config.CAMERA_DEFAULT_OFFSET + keyboardZoom);
|
||||
camera.setRotationX((curRotX += mouseYVel * Config.MOUSE_SENSITIVITY + keyboardRotationX));
|
||||
camera.setRotationY((curRotY += mouseXVel * Config.MOUSE_SENSITIVITY + keyboardRotationY));
|
||||
// Handle FPS camera movement
|
||||
handleCameraMovement();
|
||||
|
||||
// Apply mouse rotation
|
||||
if (mouseXVel !== 0 || mouseYVel !== 0) {
|
||||
camera.rotate(mouseXVel, mouseYVel);
|
||||
mouseXVel = 0;
|
||||
mouseYVel = 0;
|
||||
}
|
||||
|
||||
var view = camera.getViewMatrix();
|
||||
|
||||
// Draw skybox first with depth test disabled (always behind everything)
|
||||
@@ -245,28 +250,38 @@ function drawScene() {
|
||||
requestAnimationFrame(drawScene);
|
||||
}
|
||||
|
||||
/** Handle keyboard input for camera controls */
|
||||
function handleKeyboardInput() {
|
||||
keyboardRotationX = 0;
|
||||
keyboardRotationY = 0;
|
||||
/** Handle FPS camera movement */
|
||||
function handleCameraMovement() {
|
||||
const speed = keysPressed.has('Shift') ? fastMoveSpeed : moveSpeed;
|
||||
|
||||
if (keysPressed.has('w') || keysPressed.has('W') || keysPressed.has('ArrowUp')) {
|
||||
keyboardRotationX = Config.KEYBOARD_ROTATION_SPEED;
|
||||
// WASD for horizontal movement
|
||||
if (keysPressed.has('w') || keysPressed.has('W')) {
|
||||
camera.moveForward(speed);
|
||||
}
|
||||
if (keysPressed.has('s') || keysPressed.has('S') || keysPressed.has('ArrowDown')) {
|
||||
keyboardRotationX = -Config.KEYBOARD_ROTATION_SPEED;
|
||||
if (keysPressed.has('s') || keysPressed.has('S')) {
|
||||
camera.moveForward(-speed);
|
||||
}
|
||||
if (keysPressed.has('a') || keysPressed.has('A') || keysPressed.has('ArrowLeft')) {
|
||||
keyboardRotationY = Config.KEYBOARD_ROTATION_SPEED;
|
||||
if (keysPressed.has('a') || keysPressed.has('A')) {
|
||||
camera.moveRight(-speed);
|
||||
}
|
||||
if (keysPressed.has('d') || keysPressed.has('D') || keysPressed.has('ArrowRight')) {
|
||||
keyboardRotationY = -Config.KEYBOARD_ROTATION_SPEED;
|
||||
if (keysPressed.has('d') || keysPressed.has('D')) {
|
||||
camera.moveRight(speed);
|
||||
}
|
||||
if (keysPressed.has('q') || keysPressed.has('Q') || keysPressed.has('+')) {
|
||||
keyboardZoom -= Config.KEYBOARD_ZOOM_SPEED;
|
||||
|
||||
// Q/E for vertical movement
|
||||
if (keysPressed.has('q') || keysPressed.has('Q')) {
|
||||
camera.moveUp(-speed);
|
||||
}
|
||||
if (keysPressed.has('e') || keysPressed.has('E') || keysPressed.has('-')) {
|
||||
keyboardZoom += Config.KEYBOARD_ZOOM_SPEED;
|
||||
if (keysPressed.has('e') || keysPressed.has('E')) {
|
||||
camera.moveUp(speed);
|
||||
}
|
||||
|
||||
// Space to go up, Ctrl to go down
|
||||
if (keysPressed.has(' ')) {
|
||||
camera.moveUp(speed);
|
||||
}
|
||||
if (keysPressed.has('Control')) {
|
||||
camera.moveUp(-speed);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -313,19 +328,20 @@ function main() {
|
||||
// Keyboard controls
|
||||
window.addEventListener('keydown', (evt) => {
|
||||
keysPressed.add(evt.key);
|
||||
handleKeyboardInput();
|
||||
|
||||
// Reset camera on 'R' key
|
||||
if (evt.key === 'r' || evt.key === 'R') {
|
||||
curRotX = Config.CAMERA_DEFAULT_ROT_X;
|
||||
curRotY = Config.CAMERA_DEFAULT_ROT_Y;
|
||||
keyboardZoom = 0;
|
||||
camera = new Camera(); // Reset to initial position
|
||||
}
|
||||
|
||||
// Prevent default for space to avoid page scroll
|
||||
if (evt.key === ' ') {
|
||||
evt.preventDefault();
|
||||
}
|
||||
});
|
||||
|
||||
window.addEventListener('keyup', (evt) => {
|
||||
keysPressed.delete(evt.key);
|
||||
handleKeyboardInput();
|
||||
});
|
||||
|
||||
// Window resize handler
|
||||
|
||||
Reference in New Issue
Block a user