-
Notifications
You must be signed in to change notification settings - Fork 0
/
initialize.js
59 lines (49 loc) · 1.29 KB
/
initialize.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
import {
Scene,
PerspectiveCamera,
DirectionalLight,
WebGLRenderer,
} from "three";
export function initialize() {
const scene = createScene();
const camera = createCamera();
scene.add(camera);
const renderer = initializeRenderer();
initializeWindowResize({ renderer, camera });
return { scene, camera, renderer };
}
function createScene() {
const scene = new Scene();
const directionalLight = new DirectionalLight(0xffffff, 1);
directionalLight.position.set(0, 1, 1);
scene.add(directionalLight);
return scene;
}
function createCamera() {
const camera = new PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
0.1,
2000,
);
camera.position.y = 10;
return camera;
}
function initializeRenderer() {
const renderer = new WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
document.getElementById("render-target").appendChild(renderer.domElement);
return renderer;
}
function initializeWindowResize({ renderer, camera }) {
document.addEventListener(
"resize",
() => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
},
false,
);
}