Setup Development Environment (ok)
https://sbcode.net/threejs/webpack-dev-server/
C:\Users\Administrator\Desktop\three\package.json
{
"name": "three",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"dev": "webpack serve --config ./src/client/webpack.dev.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"devDependencies": {
"@types/three": "^0.160.0",
"three": "^0.160.1",
"ts-loader": "^9.5.1",
"typescript": "^5.3.3",
"webpack": "^5.89.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^4.15.1"
}
}
C:\Users\Administrator\Desktop\three\src\client\webpack.dev.js
const { merge } = require('webpack-merge')
const common = require('./webpack.common.js')
const path = require('path')
module.exports = merge(common, {
mode: 'development',
devtool: 'eval-source-map',
devServer: {
static: {
directory: path.join(__dirname, '../../dist/client'),
},
hot: true,
},
})
C:\Users\Administrator\Desktop\three\src\client\webpack.common.js
const path = require('path')
module.exports = {
entry: './src/client/client.ts',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, '../../dist/client'),
},
}
C:\Users\Administrator\Desktop\three\src\client\tsconfig.json
{
"compilerOptions": {
"target": "ES6",
"moduleResolution": "node",
"strict": true
},
"include": ["**/*.ts"]
}
C:\Users\Administrator\Desktop\three\src\client\client.ts
import * as THREE from 'three';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 2;
const renderer = new THREE.WebGLRenderer()
renderer.setSize(window.innerWidth, window.innerHeight)
document.body.appendChild(renderer.domElement)
const geometry = new THREE.BoxGeometry()
const material = new THREE.MeshBasicMaterial({
color: 0x00ff00,
wireframe: true,
})
const cube = new THREE.Mesh(geometry, material)
scene.add(cube)
window.addEventListener('resize', onWindowResize, false)
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
renderer.setSize(window.innerWidth, window.innerHeight)
render()
}
function animate() {
requestAnimationFrame(animate)
cube.rotation.x += 0.01
cube.rotation.y += 0.01
render()
}
function render() {
renderer.render(scene, camera)
}
animate()
http://localhost:8080