一个全屏 Three.js 应用程序的模板和工具
Boilerplate and utils for a fullscreen three.js app It is inspired from mattdesl's threejs-app, but it was rewritten and simplified using ES6 syntax rather than node, making it easier to read and well commented, so it can be easily customized to fit your needs.
All the three.js boilerplate code is tucked away in a file, the exported WebGLApp is easily configurable from the outside, for example you can enable postprocessing, orbit controls, a gui, FPS stats, Detect GPU, and use the save screenshot or record mp4 functionality. It also has built-in support for cannon-es. [Read more]
A scalable three.js component structure where each component is a class which extends THREE.Group, so you can add any object to it. The class also has update, resize, and pointer hooks. [Read more]
An asset manager which handles the preloading of .gltf models, images, audios, videos and can be easily extended to support other files. It also automatically uploads a texture to the GPU, loads cube env maps or parses equirectangular projection images. [Read more]
global window.DEBUG flag which is true when the url contains ?debug as a query parameter. So you can enable debug mode both locally and in production. [Read more]
GPU tiering info using detect-gpu [Read more]
Modern and customizable development tools such as ⚡️esbuild⚡️, eslint, and prettier.
Beautiful console output:
Once you installed the dependencies running yarn, these are the available commands:
yarn start starts a server locallyyarn start --https starts an HTTPS server locallyyarn build builds the project for production, ready to be deployed from the build/ folderAll the build tools logic is in the package.json and esbuild.js.
import WebGLApp from './utils/WebGLApp'
const webgl = new WebGLApp({ ...options })The WebGLApp class contains all the code needed for three.js to run a scene, it is always the same so it makes sense to hide it in a standalone file and don't think about it.
You can see an example configuration here:
You can pass the class the options you would pass to the THREE.WebGLRenderer, and also some more options:
| Option | Default | Description |
|---|---|---|
background |
'#111' |
The background of the scene. |
backgroundAlpha |
1 | The transparency of the background. |
maxPixelRatio |
2 | You can clamp the pixelRatio. Often the pixelRatio is clamped for performance reasons. |
width |
window.innerWidth |
The canvas width. |
height |
window.innerHeight |
The canvas height. |
orthographic |
false | Use an OrthographicCamera instead of the default PerspectiveCamera. |
cameraPosition |
new Vector3(0, 0, 4) |
Set the initial camera position. The camera will always look at [0, 0, 0]. |
fov |
45 | The field of view of the PerspectiveCamera. It is ignored if the option orthographic is true. |
frustumSize |
3 | Defines the size of the OrthographicCamera frustum. It is ignored if the option orthographic is false. |
near |
0.01 | The camera near plane. |
far |
100 | The camera far plane. |
postprocessing |
false | Enable the postprocessing library. The composer gets exposed as webgl.composer. |
xr |
false | Enable three.js WebXR mode. The update function now will have a xrframe object passed as a third parameter. |
showFps |
false | Show the stats.js fps counter. |
orbitControls |
undefined | Set this to true to enable OrbitControls. You can also pass an object of OrbitControls properties to set. |
gui |
undefined | Wether or not to initialize the gui using lil-gui. Exposed ad webgl.gui. |
guiClosed |
false | Set this to true to initialize the gui panel closed. |
world |
undefined | Accepts an instance of the cannon-es world (new CANNON.World()). Exposed as webgl.world. |
showWorldWireframes |
false | Set this to true to show the wireframes of every body in the world. Uses cannon-es-debugger. |
The webgl instance will contain all the three.js elements such as webgl.scene, webgl.renderer, webgl.camera or webgl.canvas. It also exposes some useful properties and methods:
Wether or not the user is currently dragging. It is true between the onPointerDown and onPointerUp events.
Set this property to change the cursor style of the canvas. For example you can use it to display the pointer cursor on some objects:
onPointerMove(event, { x, y }) {
// raycast and get the intersecting mesh
const intersectingMesh = getIntersectingMesh([x, y], this, this.webgl)
if (intersectingMesh) {
this.webgl.cursor = 'pointer'
} else {
this.webgl.cursor = null
}
}Save a screenshot of the application as a png.
| Option | Default | Description |
|---|---|---|
width |
window.innerWidth |
The width of the screenshot. |
height |
window.innerHeight |
The height of the screenshot. |
fileName |
'Screenshot' |
The name the .png file will have. |
Start the recording of a video using mp4-wasm.
| Option | Default | Description |
|---|---|---|
width |
window.innerWidth |
The width of the video. |
height |
window.innerHeight |
The height of the video. |
fileName |
'Recording' |
The name the .mp4 file will have. |
| ...others | Other options that you can pass to the createWebCodecsEncoder() method of mp4-wasm. |
Stop the recording and download the video. It returns a promise that is resolved once the processing is done.
Subscribe to the update requestAnimationFrame without having to create a component. If needed you can later unsubscribe the function with webgl.offUpdate(function).
| Parameter | Description |
|---|---|
dt |
The seconds elapsed from the latest frame, in a 60fps application it's 0.016s (aka 16ms) |
time |
The time in seconds elapsed from when the animation loop starts |
Subscribe a function to the pointerdown event on the canvas without having to create a component. If needed you can later unsubscribe the function with webgl.offPointerDown(function).
| Parameter | Description |
|---|---|
event |
The native event. |
position |
An object containing the x and the y position from the top left of the canvas. |
Subs
暂无开放 Issues,或尚未同步最近议题。