Cilo / src /camera.js
Afredbilli's picture
Create src/camera.js
137d252 verified
Raw History Blame Contribute Delete
11.3 kB
import * as THREE from "three";
// =============================================
// CAMERA CONFIGURATION
// =============================================
const CAMERA_CONFIG = {
distance: 7,
height: 3.2,
lookHeight: 1.5,
minDistance: 3,
maxDistance: 12,
minPitch: -0.15,
maxPitch: 0.8,
rotationSpeed: 0.004,
zoomSpeed: 0.8,
positionSmoothing: 8,
rotationSmoothing: 10,
fov: 65,
near: 0.1,
far: 250
};
// =============================================
// THIRD-PERSON CAMERA
// =============================================
export function createCamera(scene, renderer) {
const camera = new THREE.PerspectiveCamera(
CAMERA_CONFIG.fov,
window.innerWidth / window.innerHeight,
CAMERA_CONFIG.near,
CAMERA_CONFIG.far
);
camera.position.set(0, 5, 9);
const target = new THREE.Vector3(0, 1, 0);
const desiredPosition = new THREE.Vector3();
const desiredTarget = new THREE.Vector3();
const cameraDirection = new THREE.Vector3();
const spherical = new THREE.Spherical(
CAMERA_CONFIG.distance,
Math.PI / 2.8,
0
);
let yaw = 0;
let pitch = 0.18;
let distance = CAMERA_CONFIG.distance;
let activeCharacter = null;
let isDragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
let disposed = false;
// =========================================
// CHARACTER TARGET
// =========================================
function getCharacterPosition(character) {
if (!character) {
return new THREE.Vector3(0, 0, 0);
}
// Character controller objects may expose
// their own position directly.
if (character.root) {
return character.root.position;
}
if (character.position) {
return character.position;
}
return new THREE.Vector3(0, 0, 0);
}
function updateDesiredPosition(character) {
const characterPosition =
getCharacterPosition(character);
desiredTarget.set(
characterPosition.x,
characterPosition.y + CAMERA_CONFIG.lookHeight,
characterPosition.z
);
const horizontalDistance =
distance * Math.cos(pitch);
desiredPosition.set(
characterPosition.x +
Math.sin(yaw) * horizontalDistance,
characterPosition.y +
CAMERA_CONFIG.height +
distance * Math.sin(pitch),
characterPosition.z +
Math.cos(yaw) * horizontalDistance
);
}
// =========================================
// MOUSE CAMERA ROTATION
// =========================================
function handlePointerDown(event) {
if (event.button !== 2) {
return;
}
isDragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture?.(
event.pointerId
);
}
function handlePointerMove(event) {
if (!isDragging) {
return;
}
const deltaX =
event.clientX - previousPointerX;
const deltaY =
event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
yaw -= deltaX * CAMERA_CONFIG.rotationSpeed;
pitch -= deltaY * CAMERA_CONFIG.rotationSpeed;
pitch = THREE.MathUtils.clamp(
pitch,
CAMERA_CONFIG.minPitch,
CAMERA_CONFIG.maxPitch
);
}
function handlePointerUp() {
isDragging = false;
}
function handleContextMenu(event) {
event.preventDefault();
}
// =========================================
// MOUSE WHEEL ZOOM
// =========================================
function handleWheel(event) {
distance +=
Math.sign(event.deltaY) *
CAMERA_CONFIG.zoomSpeed;
distance = THREE.MathUtils.clamp(
distance,
CAMERA_CONFIG.minDistance,
CAMERA_CONFIG.maxDistance
);
}
// =========================================
// TOUCH CAMERA ROTATION
// =========================================
function handleTouchStart(event) {
if (event.touches.length !== 1) {
return;
}
// Reserve the lower-left area for movement
// controls. Camera rotation starts elsewhere.
const touch = event.touches[0];
if (touch.clientX < window.innerWidth * 0.4) {
return;
}
isDragging = true;
previousPointerX = touch.clientX;
previousPointerY = touch.clientY;
}
function handleTouchMove(event) {
if (!isDragging || event.touches.length !== 1) {
return;
}
const touch = event.touches[0];
const deltaX =
touch.clientX - previousPointerX;
const deltaY =
touch.clientY - previousPointerY;
previousPointerX = touch.clientX;
previousPointerY = touch.clientY;
yaw -= deltaX * CAMERA_CONFIG.rotationSpeed;
pitch -= deltaY * CAMERA_CONFIG.rotationSpeed;
pitch = THREE.MathUtils.clamp(
pitch,
CAMERA_CONFIG.minPitch,
CAMERA_CONFIG.maxPitch
);
}
function handleTouchEnd() {
isDragging = false;
}
// =========================================
// CAMERA COLLISION
// =========================================
const raycaster = new THREE.Raycaster();
const cameraOffset = new THREE.Vector3();
function preventCameraClipping(character) {
if (!character || !character.root) {
return;
}
const origin = desiredTarget;
cameraOffset.subVectors(
desiredPosition,
origin
);
const desiredDistance = cameraOffset.length();
if (desiredDistance <= 0) {
return;
}
cameraOffset.normalize();
raycaster.set(origin, cameraOffset);
raycaster.far = desiredDistance;
// Future improvement:
// Raycast against dedicated collision geometry
// rather than every decorative scene object.
const hits = raycaster.intersectObjects(
scene.children,
true
);
for (const hit of hits) {
if (hit.object === character.root) {
continue;
}
if (
hit.object.isMesh &&
hit.distance > 0.2
) {
const safeDistance = Math.max(
CAMERA_CONFIG.minDistance,
hit.distance - 0.3
);
desiredPosition.copy(origin).addScaledVector(
cameraOffset,
Math.min(desiredDistance, safeDistance)
);
break;
}
}
}
// =========================================
// EVENT REGISTRATION
// =========================================
const element = renderer.domElement;
element.addEventListener(
"pointerdown",
handlePointerDown
);
element.addEventListener(
"pointermove",
handlePointerMove
);
element.addEventListener(
"pointerup",
handlePointerUp
);
element.addEventListener(
"pointercancel",
handlePointerUp
);
element.addEventListener(
"contextmenu",
handleContextMenu
);
element.addEventListener(
"wheel",
handleWheel,
{ passive: true }
);
element.addEventListener(
"touchstart",
handleTouchStart,
{ passive: true }
);
element.addEventListener(
"touchmove",
handleTouchMove,
{ passive: true }
);
element.addEventListener(
"touchend",
handleTouchEnd,
{ passive: true }
);
element.addEventListener(
"touchcancel",
handleTouchEnd,
{ passive: true }
);
// =========================================
// PUBLIC CAMERA API
// =========================================
function update(delta, character) {
if (disposed) {
return;
}
activeCharacter = character;
updateDesiredPosition(activeCharacter);
// Avoid making the camera jump when the game
// briefly drops frames.
const positionAlpha =
1 - Math.exp(
-CAMERA_CONFIG.positionSmoothing * delta
);
const rotationAlpha =
1 - Math.exp(
-CAMERA_CONFIG.rotationSmoothing * delta
);
preventCameraClipping(activeCharacter);
camera.position.lerp(
desiredPosition,
positionAlpha
);
target.lerp(
desiredTarget,
rotationAlpha
);
camera.lookAt(target);
cameraDirection.subVectors(
target,
camera.position
).normalize();
}
function resize(width, height) {
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
function rotate(horizontal, vertical) {
yaw -= horizontal * CAMERA_CONFIG.rotationSpeed;
pitch -= vertical * CAMERA_CONFIG.rotationSpeed;
pitch = THREE.MathUtils.clamp(
pitch,
CAMERA_CONFIG.minPitch,
CAMERA_CONFIG.maxPitch
);
}
function zoom(amount) {
distance = THREE.MathUtils.clamp(
distance + amount,
CAMERA_CONFIG.minDistance,
CAMERA_CONFIG.maxDistance
);
}
function reset() {
yaw = 0;
pitch = 0.18;
distance = CAMERA_CONFIG.distance;
}
function dispose() {
disposed = true;
element.removeEventListener(
"pointerdown",
handlePointerDown
);
element.removeEventListener(
"pointermove",
handlePointerMove
);
element.removeEventListener(
"pointerup",
handlePointerUp
);
element.removeEventListener(
"pointercancel",
handlePointerUp
);
element.removeEventListener(
"contextmenu",
handleContextMenu
);
element.removeEventListener(
"wheel",
handleWheel
);
element.removeEventListener(
"touchstart",
handleTouchStart
);
element.removeEventListener(
"touchmove",
handleTouchMove
);
element.removeEventListener(
"touchend",
handleTouchEnd
);
element.removeEventListener(
"touchcancel",
handleTouchEnd
);
}
return {
camera,
update,
resize,
rotate,
zoom,
reset,
dispose
};
}