This code has been frustrating, I’m trying to make a spinning cube and I keep getting errors. My code in camera.js and scene.js:
import * as THREE from "three";
export function createCamera(gameWindow) {
const DEG2RAD = Math.PI / 180;
const LEFT_MOUSE_BUTTON = 0;
const MIDDLE_MOUSE_BUTTON = 1;
const RIGHT_MOUSE_BUTTON = 2;
const MIN_CAMERA_RADIUS = 2;
const MAX_CAMERA_RADIUS = 10;
const MIN_CAMERA_ELEVATION = 30;
const MAX_CAMERA_ELEVATION = 90;
const ROTATION_SENSITIVITY = 0.5;
const ZOOM_SENSITIVITY = 0.02;
const PAN_SENSITIVITY = -0.01;
const Y_AXIS = new THREE.Vector3(0, 1, 0);
const camera = new THREE.PerspectiveCamera(75, gameWindow.offsetWidth / gameWindow.offsetHeight, 0.1, 1000);
let cameraOrigin = new THREE.Vector3();
let cameraRadius = 4;
let cameraAzimuth = 0;
let cameraElevation = 0;
let isLeftMouseDown = false;
let isRightMouseDown = false;
let isMiddleMouseDown = false;
let prevMouseX = 0;
let prevMouseY = 0;
updateCameraPosition();
function onMouseDown(event) {
if (event.button === LEFT_MOUSE_BUTTON) {
isLeftMouseDown = true;
}
if (event.button === MIDDLE_MOUSE_BUTTON) {
isMiddleMouseDown = true;
}
if (event.button === RIGHT_MOUSE_BUTTON) {
isRightMouseDown = true;
}
}
function onMouseUp(event) {
if (event.button === LEFT_MOUSE_BUTTON) {
isLeftMouseDown = false;
}
if (event.button === MIDDLE_MOUSE_BUTTON) {
isMiddleMouseDown = false;
}
if (event.button === RIGHT_MOUSE_BUTTON) {
isRightMouseDown = false;
}
}
function MouseDown(event) {
camera.onMouseDown(event);
}
function MouseUp(event) {
camera.onMouseUp(event);
}
function MouseMove(event) {
camera.onMouseDown(event);
}
function onMouseMove(event) {
const deltaX = (event.clientX - prevMouseX);
const deltaY = (event.clientY - prevMouseY);
//Rotation
if (isLeftMouseDown) {
cameraAzimuth += -(deltaX * ROTATION_SENSITIVITY);
cameraElevation += (deltaY * ROTATION_SENSITIVITY);
cameraElevation = Math.min(MAX_CAMERA_ELEVATION, Math.max(MIN_CAMERA_ELEVATION, cameraElevation));
updateCameraPosition();
}
//Panning
if (isMiddleMouseDown) {
const forward = new THREE.Vector3(0, 0, 1).applyAxisAngle(Y_AXIS, cameraAzimuth * DEG2RAD);
const left = new THREE.Vector3(1, 0, 0).applyAxisAngle(Y_AXIS, cameraAzimuth * DEG2RAD);
cameraOrigin.add(forward.multiplyScalar(PAN_SENSITIVITY * deltaY));
cameraOrigin.add(left.multiplyScalar(PAN_SENSITIVITY * deltaX));
updateCameraPosition();
}
//Zooming
if (isRightMouseDown) {
cameraRadius += deltaY * ZOOM_SENSITIVITY;
cameraRadius = Math.min(MAX_CAMERA_RADIUS, Math.max(MIN_CAMERA_RADIUS, cameraRadius));
updateCameraPosition();
}
prevMouseX = event.clientX;
prevMouseY = event.clientY;
}
function updateCameraPosition() {
camera.position.x = cameraRadius * Math.sin(cameraAzimuth * DEG2RAD) * (Math.cos(cameraElevation * DEG2RAD));
camera.position.y = cameraRadius * Math.sin(cameraElevation * DEG2RAD);
camera.position.z = cameraRadius * Math.cos(cameraAzimuth * DEG2RAD) * (Math.cos(cameraElevation * DEG2RAD));
camera.add(cameraOrigin);
camera.lookAt(cameraOrigin);
camera.updateMatrix();
}
return {
camera,
onMouseDown,
onMouseUp,
onMouseMove
}
}
//scene.js
import * as THREE from "https://unpkg.com/three/build/three.module.js";
import { createCamera } from "./camera.js"
export function createScene() {
const gameWindow = document.getElementById("render-target");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x777777);
const camera = createCamera(gameWindow);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(gameWindow.offsetWidth, gameWindow.offsetHeight);
gameWindow.appendChild(renderer.domElement);
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshBasicMaterial({ color: 0xff0000 })
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
let meshes = [];
function initialize(city) {
}
function draw() {
renderer.render(scene, camera.camera);
}
function start() {
renderer.setAnimationLoop(draw);
}
function stop() {
renderer.setAnimationLoop(null);
}
return {
start,
stop,
}
}
I tried to run it and expected a pannable, zoomable, and rotatable cube yet it gave me the error in the title. I instead got a cube stuck in place and an error, I don’t get why it’s giving me the error and where it’s giving me the error. I’m confused and am not that good with three.js.