-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
346 lines (317 loc) · 11 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
"use strict";
//init global variables
var CAMERA_RADIUS = 100;
var scene, controls, camera, renderer, raycaster, manager;
var INTERSECTED;
var currLevel, FOCUS;
var material, light;
var MODELS_AVAILABLE = false; //true if models inited
var mouse = new THREE.Vector2();
var DATA;
function createCube(x,y,z) {
var cube = new THREE.Mesh( new THREE.CubeGeometry( 1, 1, 1 ), material);
cube.position.set(x,y,z);
scene.add(cube);
}
//initialization
function init() {
scene = new THREE.Scene();
manager = new THREE.LoadingManager();
manager.onProgress = function ( item, loaded, total ) {
console.log( item, "Загружено ",loaded, "из ",total );
};
manager.onLoad = function()
{
console.log("fully loaded");
MODELS_AVAILABLE = true;
};
//camera = new THREE.OrthographicCamera(window.innerWidth / -2, window.innerWidth / 2,
// window.innerHeight / 2, window.innerHeight / -2, 1, 1000);
camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 5000);
//renderer
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setClearColor(0xbbbbbb);
renderer.setSize(window.innerWidth, window.innerHeight);
//add to HTML
document.querySelector("#webgl-wrapper").appendChild(renderer.domElement);
//controls
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.position0.set(0, 0, CAMERA_RADIUS); // default camera position for reset
controls.rotateSpeed = 0.4;
controls.enablePan = false;
controls.mouseButtons = {ORBIT: THREE.MOUSE.RIGHT, ZOOM: THREE.MOUSE.MIDDLE};
//controls.enableDamping = true;
//controls.dampingFactor = 0.25;
//controls.addEventListener('change', render);
// lights
scene.add(new THREE.AmbientLight(0xcccccc, 0.6));
light = new THREE.DirectionalLight(0xbbbbbb, 0.7);
light.position.set(30, 10, 20);
scene.add(light);
light = new THREE.DirectionalLight(0xbbbbbb, 0.7);
light.position.set(-30, -10, -20);
scene.add(light);
material = new THREE.MeshPhongMaterial({color: 0x9eafab, shading: THREE.FlatShading});
loadModels();
raycaster = new THREE.Raycaster();
//
// loader.load('./3d/test.json', function(geometry) {
// cube = new THREE.Mesh(geometry, material);
// scene.add(cube);
// render();
// });
document.addEventListener('mousemove', onDocumentMouseMove, false);
document.addEventListener('click', selectPart);
window.addEventListener('resize', onWindowResize, false);
}
function render() {
renderer.render(scene, camera);
}
// responsible for user input
function animate() {
requestAnimationFrame(animate);
checkIntersections();
render();
//controls.update();
}
//UTILITIES
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
render();
}
function onDocumentMouseMove(event) {
event.preventDefault();
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = -( event.clientY / window.innerHeight ) * 2 + 1;
}
function loadModels() {
//read json and load 3d models
//TODO: Maybe make it async and remove double for loop
let parts = SHIP_DATA.shipParts;
let loader = new THREE.JSONLoader(manager);
for (let level = 0; level < parts.length; level++) {
let levelParts = parts[level];
for (let i = 0; i < levelParts.length; i++) {
let part = levelParts[i];
loader.load('./3d/' + part.model, function (geometry, materials) {
//TODO: Fix to use all materials, not just first
var material = new THREE.MultiMaterial(materials);
var model = new THREE.Mesh(geometry, material);
model.matrixAutoUpdate = false;
//user data
model.userData.level = level;
model.userData.color = model.currentHex;
model.userData.name = part.name;
model.userData.description = part.description;
//target coors
model.userData.tx = part.tx;
model.userData.ty = part.ty;
model.userData.tz = part.tz;
scene.add(model);
//console.log(level, i);
SHIP_DATA.shipParts[level][i].id = model.id;
});
}
}
}
//Sets ship info, levels, camera position etc
function resetData() {
//TODO: rename to resetAll or smth and refactor to utilize control's reset
resetLevel();
resetFOCUS();
controls.reset();
setVisibilityLevel(SHIP_DATA.shipParts.length);
}
//resets info about level
function resetLevel() {
var name = SHIP_DATA.shipName;
var description = SHIP_DATA.shipDescription;
document.getElementById('info-name').innerText = name;
document.getElementById('info-description').innerText = description;
document.getElementById('level').innerText = "Выберите уровень";
currLevel = null;
}
//resets camera position from target and current focus, highlight and visibility
function resetFOCUS() {
//camera
// camera.position.set(0, 0, CAMERA_RADIUS);
// camera.lookAt(scene.position);
//highlight
if(FOCUS){
FOCUS.userData.color = 0x000000;
highlightPart(FOCUS, 0x000000);
}
FOCUS = null;
}
function checkIntersections() {
//TODO: Refactor
//camera.updateMatrixWorld();
raycaster.setFromCamera(mouse, camera);
var intersects = raycaster.intersectObjects(scene.children);
if (intersects.length > 0) {
if (INTERSECTED !== intersects[0].object) {
//if was intersected, set intersected color back to normal
if (INTERSECTED) {
highlightPartLevel(INTERSECTED, null);
//INTERSECTED.material.emissive.setHex(INTERSECTED.currentHex);
}
INTERSECTED = intersects[0].object;
// highlight single part only if there is currently selected level
if (currLevel !== null) {
if(INTERSECTED.userData.level === currLevel){
if(FOCUS === INTERSECTED){
highlightPart(INTERSECTED, 0x00ff00);
} else{
highlightPart(INTERSECTED, 0xff0000);
}
}
} else {
highlightPartLevel(INTERSECTED, 0xff0000);
}
}
} else {
if (INTERSECTED) {
//console.log(INTERSECTED);
//INTERSECTED.material.emissive.setHex(INTERSECTED.userData.color);
highlightPartLevel(INTERSECTED, null);
}
INTERSECTED = null;
}
}
function setCameraTop() {
//set to topdown view
var tx,ty,tz;
if(FOCUS){
tx = FOCUS.userData.tx;
ty = FOCUS.userData.ty;
tz = FOCUS.userData.tz;
}
camera.position.set(tx || 0.00, CAMERA_RADIUS, tz || 0.00);
controls.update();
}
function setCameraSide() {
//while camera not 90 degrees, move it etc
var tx,ty,tz;
if(FOCUS){
tx = FOCUS.userData.tx;
ty = FOCUS.userData.ty;
tz = FOCUS.userData.tz;
}
camera.position.set(tx||0.0, ty||0.0, CAMERA_RADIUS);
controls.update();
}
//DESCRIPTIONS
function setPartDescription() {
document.getElementById("info-name").innerText = INTERSECTED.userData.name;
document.getElementById("info-description").innerText = INTERSECTED.userData.description;
}
function setLevelInfo(level) {
document.getElementById('level').innerText = "Уровень: " + (level+1);
}
function setLevelDescription() {
//if there is no FOCUS, get hovered detail level description
}
//MODEL HIGHLIGHTS
function highlightPartLevel(part, color) {
//to select color
let level = part.userData.level;
let levelParts = SHIP_DATA.shipParts[level];
for (var i = 0; i < levelParts.length; i++) {
let part = levelParts[i];
let object = scene.getObjectById(part.id);
if (color === null){
highlightPart(object, object.userData.color);
} else{
highlightPart(object, color);
}
}
}
function highlightPart(part, color) {
//TODO:Refactor higlight to another function
//part.currentHex = INTERSECTED.material.emissive.getHex();
//if selected current level and part is on it
let materials = part.material.materials;
for (let i = 0; i < materials.length; i++) {
let mat = materials[i];
mat.emissive.setHex(color);
}
//part.material.emissive.setHex(color);
}
//MAIN CONTROLS
function searchPart() {
//set currently selected parts to empty list
//get current search text, loop through models and if they ok add them to selectHighlighted
}
function setTarget(object) {
//get object info from data and set camera target to it.
//target position
var tx = object.userData.tx;
var ty = object.userData.ty;
var tz = object.userData.tz;
// object position
var x = object.position.x;
var y = object.position.y;
var z = object.position.z;
// console.log(tx, ty, tz);
// //TODO: not very safe to use it
controls.target.set(tx||x, ty||y, tz||z);
// setCameraSide();
controls.update()
}
function setVisibilityLevel(desiredLevel) {
//set visibile only that is below and including desiredLevel variable
if(MODELS_AVAILABLE){
var parts = SHIP_DATA.shipParts;
for(var i = 0; i < parts.length; i++){
let level = parts[i];
for (var j =0; j<level.length; j++){
let id = parts[i][j].id;
if( i<= desiredLevel){
scene.getObjectById(id).visible = true;
} else{
scene.getObjectById(id).visible = false;
}
}
}
}
else{console.log("Models are not yet available");}
}
function selectPart(part) {
//set FOCUS to detail, set its description
if (INTERSECTED) {
if (currLevel !== null) {
if(currLevel === INTERSECTED.userData.level){
if(FOCUS){
//highlightPart(FOCUS, 0x000000);
FOCUS.userData.color = 0x000000;
highlightPart(FOCUS, FOCUS.userData.color);
}
FOCUS = INTERSECTED;
FOCUS.userData.color = 0x004400;
highlightPart(FOCUS, 0x00ff00);
setPartDescription();
setTarget(FOCUS);
}
} else {
currLevel = INTERSECTED.userData.level;
setVisibilityLevel(currLevel);
setLevelInfo(currLevel);
highlightPartLevel(INTERSECTED, 0x000000);
highlightPart(INTERSECTED, 0xff0000);
}
}else {
if(currLevel !== null){
resetLevel();
resetFOCUS();
setTarget(scene);
setVisibilityLevel(SHIP_DATA.shipParts.length);
}
}
}
//STARTING LOOP
init();
resetData();
animate();