-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
125 lines (88 loc) · 3.12 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"use strict";
// Import only what you need, to help your bundler optimize final code size using tree shaking
// see https://developer.mozilla.org/en-US/docs/Glossary/Tree_shaking)
import {
PerspectiveCamera,
Scene,
WebGLRenderer,
BoxGeometry,
Mesh,
MeshNormalMaterial,
AmbientLight,
Clock
} from 'three';
// If you prefer to import the whole library, with the THREE prefix, use the following line instead:
// import * as THREE from 'three'
// NOTE: three/addons alias is supported by Rollup: you can use it interchangeably with three/examples/jsm/
// Importing Ammo can be tricky.
// Vite supports webassembly: https://vitejs.dev/guide/features.html#webassembly
// so in theory this should work:
//
// import ammoinit from 'three/addons/libs/ammo.wasm.js?init';
// ammoinit().then((AmmoLib) => {
// Ammo = AmmoLib.exports.Ammo()
// })
//
// But the Ammo lib bundled with the THREE js examples does not seem to export modules properly.
// A solution is to treat this library as a standalone file and copy it using 'vite-plugin-static-copy'.
// See vite.config.js
//
// Consider using alternatives like Oimo or cannon-es
import {
OrbitControls
} from 'three/addons/controls/OrbitControls.js';
import {
GLTFLoader
} from 'three/addons/loaders/GLTFLoader.js';
// Example of hard link to official repo for data, if needed
// const MODEL_PATH = 'https://raw.githubusercontent.com/mrdoob/js/r148/examples/models/gltf/LeePerrySmith/LeePerrySmith.glb';
// INSERT CODE HERE
const scene = new Scene();
const aspect = window.innerWidth / window.innerHeight;
const camera = new PerspectiveCamera(75, aspect, 0.1, 1000);
const light = new AmbientLight(0xffffff, 1.0); // soft white light
scene.add(light);
const renderer = new WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
controls.listenToKeyEvents(window); // optional
const geometry = new BoxGeometry(1, 1, 1);
const material = new MeshNormalMaterial();
const cube = new Mesh(geometry, material);
scene.add(cube);
function loadData() {
new GLTFLoader()
.setPath('assets/models/')
.load('test.glb', gltfReader);
}
function gltfReader(gltf) {
let testModel = null;
testModel = gltf.scene;
if (testModel != null) {
console.log("Model loaded: " + testModel);
scene.add(gltf.scene);
} else {
console.log("Load FAILED. ");
}
}
//loadData();
camera.position.z = 3;
const clock = new Clock();
// Main loop
const animation = () => {
renderer.setAnimationLoop(animation); // requestAnimationFrame() replacement, compatible with XR
const delta = clock.getDelta();
const elapsed = clock.getElapsedTime();
// can be used in shaders: uniforms.u_time.value = elapsed;
cube.rotation.x = elapsed / 2;
cube.rotation.y = elapsed / 1;
renderer.render(scene, camera);
};
animation();
window.addEventListener('resize', onWindowResize, false);
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}