-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.js
104 lines (86 loc) · 2.74 KB
/
main.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import "./styles/style.css";
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
class ThreeJSTemplate {
constructor() {
this.initScene();
this.initCamera();
this.initRenderer();
this.initLights();
this.initMesh();
this.initControls();
this.addEventListeners();
this.animate();
}
initScene() {
this.scene = new THREE.Scene();
this.clock = new THREE.Clock();
}
initCamera() {
this.sizes = {
width: window.innerWidth,
height: window.innerHeight,
};
this.camera = new THREE.PerspectiveCamera(
75,
this.sizes.width / this.sizes.height,
0.1,
100
);
this.camera.position.z = 3;
this.scene.add(this.camera);
}
initRenderer() {
this.canvas = document.querySelector("canvas.webgl");
this.renderer = new THREE.WebGLRenderer({
canvas: this.canvas,
alpha: true,
});
this.renderer.setSize(this.sizes.width, this.sizes.height);
this.renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
initLights() {
const ambientLight = new THREE.AmbientLight(0x404040);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
const hemisphereLight = new THREE.HemisphereLight(0x7444ff, 0xff00bb, 0.5);
const pointLight = new THREE.PointLight(0x7444ff, 1, 100);
pointLight.position.set(0, 3, 4);
this.scene.add(ambientLight, directionalLight, hemisphereLight, pointLight);
}
initMesh() {
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshStandardMaterial({ color: "#7444ff" });
this.mesh = new THREE.Mesh(geometry, material);
this.scene.add(this.mesh);
}
initControls() {
this.controls = new OrbitControls(this.camera, this.canvas);
this.controls.enableDamping = true;
}
addEventListeners() {
window.addEventListener("resize", () => this.onResize());
}
onResize() {
this.sizes.width = window.innerWidth;
this.sizes.height = window.innerHeight;
this.camera.aspect = this.sizes.width / this.sizes.height;
this.camera.updateProjectionMatrix();
this.renderer.setSize(this.sizes.width, this.sizes.height);
this.renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
animate() {
const elapsedTime = this.clock.getElapsedTime();
// Animate mesh
this.mesh.rotation.x = elapsedTime * 0.5;
this.mesh.rotation.y = elapsedTime * 0.5;
this.mesh.rotation.z = elapsedTime * 0.5;
// Update controls
this.controls.update();
// Render
this.renderer.render(this.scene, this.camera);
// Call animate again on the next frame
window.requestAnimationFrame(() => this.animate());
}
}
// Initialize the template
new ThreeJSTemplate();