Okay, let's start building a basic 3D first-person shooter foundation using Three.js and Vite. This will set up the scene, basic controls, and a simple environment.
Next steps:
npm install
to install the dependencies (three
, vite
).npm run dev
to start the development server.This will give you a basic 3D environment with first-person movement controls (WASD for movement, mouse to look, Space to jump) and pointer lock. Click the screen to lock the cursor and enable controls. Press Esc
to unlock.
Okay, I'll modify the project to use ES modules directly from a CDN, removing the need for Vite, npm install
, or npm run dev
. You'll be able to run this directly by opening the index.html
file (though serving it locally via a simple HTTP server or using a live preview extension in your editor is often better for avoiding potential browser security restrictions with local file access).
Now you have index.html
, style.css
, and main.js
. The JavaScript code imports Three.js directly from a CDN using an importmap
defined in the HTML. You should be able to see the basic FPS setup running directly in the browser preview.
Okay, a black screen after clicking usually means the rendering isn't working as expected after the pointer lock activates. This could be due to camera position, renderer setup, or issues with the animation loop integration.
A common cause is the renderer or camera not having the correct dimensions initially. Let's ensure the size is set correctly right after initialization by calling the resize function once.
I'll add a call to onWindowResize()
at the end of the init
function.
This change ensures the renderer and camera aspect ratio are correctly set based on the window size right from the start. Try clicking "Click to play" again.