-
Notifications
You must be signed in to change notification settings - Fork 38
/
box.ts
47 lines (40 loc) · 1.11 KB
/
box.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import * as THREE from "three";
import type { Base } from "../base/base";
import { Component } from "../components/component";
export interface BoxConfig {
width: number;
height: number;
depth: number;
position: THREE.Vector3;
material: THREE.Material;
}
/**
* A cute box mesh that we can see everywhere
*/
class Box extends Component {
mesh: THREE.Mesh;
constructor(base: Base, config: Partial<BoxConfig> = {}) {
super(base);
const {
width = 0.2,
height = 0.2,
depth = 0.2,
position = new THREE.Vector3(0, 0, 0),
material = new THREE.MeshBasicMaterial({
color: new THREE.Color("#ffffff"),
}),
} = config;
const geometry = new THREE.BoxGeometry(width, height, depth);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(position);
this.mesh = mesh;
}
addExisting(): void {
this.container.add(this.mesh);
}
spin(time: number, axis: "x" | "y" | "z" = "y", speed = 1) {
const mesh = this.mesh;
mesh.rotation[axis] = (time / 1000) * speed;
}
}
export { Box };