This repository has been archived by the owner on Jan 22, 2022. It is now read-only.
forked from Autodesk/3DViewerComponent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
three-d-viewer.es6.js
113 lines (96 loc) · 2.87 KB
/
three-d-viewer.es6.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
'use strict';
class ThreeDViewer {
get LOADING_ERROR() {
return 'Could not load file.';
}
beforeRegister() {
this.is = 'three-d-viewer';
this.properties = {
previews: {
type: Array,
observer: 'previewsChanged'
},
color: {
type: String,
value: 'cccccc',
observer: '_changeModelColor'
},
options: {
type: Object,
value: () => {
return {};
}
}
};
}
unloadObj() {
this.viewer.unloadObj();
this.isThreeDViewerReady = false;
}
showPreview(preview) {
return this.selectedPreview === preview;
}
showViewerArrows(previews) {
return previews && previews.length > 1;
}
showPreviousPreview() {
this.unloadObj();
this.selectedIndex = (this.selectedIndex - 1) % this.previews.length;
if (this.selectedIndex < 0) {
this.selectedIndex = this.previews.length - 1;
}
this.getPreviewPath(this.previews[this.selectedIndex]);
}
showNextPreview() {
this.unloadObj();
this.selectedIndex = (this.selectedIndex + 1) % this.previews.length;
this.getPreviewPath(this.previews[this.selectedIndex]);
}
renderModel(index) {
//index = index || 0;
this.viewer.render('#three-d-viewer');
this.viewer.resize();
this.viewer.loadObject(this.currentModelUrl, this.fileExtension, () => {
this.isThreeDViewerReady = true;
this.viewer.startRotating();
this.viewer.changeColor(this.color);
}, () => {
this.setLoadingFileErrorMessage();
this.viewer.stopRotating();
this.isThreeDViewerReady = false;
});
}
computeErrorClass(loadingMessage) {
return loadingMessage === this.LOADING_ERROR ? 'error' : '';
}
getPreviewPath(prev) {
this.set('loadingMessage', 'Loading...');
let preview = prev || this.previews[0];
this.selectedPreview = preview;
this.selectedIndex = this.previews.indexOf(preview);
this.fileExtension = preview.file_type;
this.currentModelUrl = preview.file_url;
setTimeout(() => {
this.renderModel();
});
}
setLoadingFileErrorMessage() {
this.set('loadingMessage', this.LOADING_ERROR);
}
previewsChanged() {
if (this.previews && this.previews.length > 0) {
this.isThreeDViewerReady = false;
this.getPreviewPath();
}
}
ready() {
this.viewer = new Viewer();
this.viewer.init();
}
_changeModelColor() {
if (this.viewer) {
this.viewer.changeColor(this.color);
}
}
}
Polymer(ThreeDViewer);