-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
197 lines (160 loc) · 5.73 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// NOTE если будет в spa, нужно удалять event'ы и requestAnimationFrame иначе застакается
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import WebGL from 'three/addons/capabilities/WebGL.js';
import Stats from 'three/addons/libs/stats.module.js';
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader.js';
let camera, scene, renderer, stats, model, controls;
const viewSize = 1;
const container = document.body;
const loadingScreen = document.getElementById('loading')
const loadingScreenText = loadingScreen.querySelector('span')
const pgBarTotal = loadingScreen.querySelector('#progress-bar')
// const pgBarSize = loadingScreen.querySelector('#progress-bar-size')
if (WebGL.isWebGLAvailable()) {
init();
// задаем контрол для управления "скелетом" модели
const input = container.querySelector('input');
if (!input) throw new Error('input not found');
input.addEventListener('input', (e) => {
if (!model) return;
const value = +e.target.value
const doubleValue = value * 2
// 1
model.children[7].position.set(0, 0, doubleValue)
model.children[8].position.set(0, 0, doubleValue)
model.children[9].position.set(0, 0, doubleValue)
// 2
model.children[6].position.set(0, 0, value)
})
} else {
const warning = WebGL.getWebGLErrorMessage();
container.appendChild(warning);
}
function init() {
/**
* loaders
* onLoad fire after all loader was load and his onLoad callbacks fired
*/
const loadingManager = new THREE.LoadingManager();
const gltfLoader = new GLTFLoader(loadingManager);
const textureLoader = new THREE.TextureLoader(loadingManager)
const dracoLoader = new DRACOLoader(loadingManager);
dracoLoader.setDecoderPath('resources/draco/')
dracoLoader.setDecoderConfig({ type: 'js' })
gltfLoader.setDRACOLoader(dracoLoader)
loadingManager.onProgress = (url, loaded, total) => {
loadingScreenText.textContent = `Загружено ${loaded} из ${total} ресурсов`
pgBarTotal.value = Math.round((loaded / total) * 100);
console.log( 'Started loading file: ' + url );
}
loadingManager.onLoad = () => {
loadingScreen.style.display = 'none';
render();
}
loadingManager.onError = () => {
onError(`Resources \`${url}\` was not found or not load`)
}
/**
* camera
*/
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, viewSize);
// camera.position.set(0, 130, 50);
/**
* scene
*/
scene = new THREE.Scene();
/**
* background
*/
scene.background = new THREE.Color(0xf2f2f2);
/**
* model
*/
gltfLoader
.setPath('resources/')
.load('ski-colors.glb', (gltf) => {
model = gltf.scene
console.log('gltf.scene[0]', gltf.scene)
gltf.scene.traverse((object) => {
// fix semi-opacity on zoom
// search depthWrite on threejs docs for more info
if (object.material) {
object.material.depthWrite = true;
}
});
/**
* resize
*/
// controls.reset();
const box = new THREE.Box3().setFromObject(gltf.scene);
const size = box.getSize(new THREE.Vector3()).length();
const center = box.getCenter(new THREE.Vector3());
camera.near = size / 100;
camera.far = size * 100;
camera.updateProjectionMatrix();
camera.position.copy(center);
camera.position.x += size / 2.0;
camera.position.y += size / 5.0;
camera.position.z += size / 2.0;
camera.lookAt(center);
controls = new OrbitControls(camera, renderer.domElement)
controls.maxDistance = size * 10;
controls.update();
/**
* end resize
*/
scene.add(gltf.scene);
})
textureLoader
.load(
"resources/bg.jpg",
function (texture) {
console.log('texture', texture)
texture.mapping = THREE.EquirectangularReflectionMapping;
const pmremGenerator = new THREE.PMREMGenerator(renderer);
pmremGenerator.compileEquirectangularShader();
const envMap = pmremGenerator.fromEquirectangular(texture).texture;
// scene.background = envMap; //this loads the envMap for the background
scene.environment = envMap; //this loads the envMap for reflections and lighting
texture.dispose(); //we have envMap so we can erase the texture
pmremGenerator.dispose(); //we processed the image into envMap so we can stop this
});
/**
* only for loaders onProgress
*/
// function onLoaderProgress(idx) {
// return (xhr) => {
// if (xhr.loaded === xhr.total) {
// pgBarSize.children[idx].textContent = 'Инициализация'
// } else {
// pgBarSize.children[idx].textContent = `${(xhr.loaded / 1e6).toFixed(2)}мб / ${(xhr.total / 1e6).toFixed(2)}мб`
// }
// }
// }
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);
// const controls = new OrbitControls(camera, renderer.domElement);
// controls.target.set(0, 100, 0);
// controls.update();
window.addEventListener('resize', onWindowResize);
// stats
stats = new Stats();
container.appendChild(stats.dom);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function render() {
requestAnimationFrame(render);
renderer.render(scene, camera);
stats.update();
}
function onError(...args) {
console.error('ERROR:', ...args)
}