-
Notifications
You must be signed in to change notification settings - Fork 2
/
echoAR.js
167 lines (137 loc) · 6.49 KB
/
echoAR.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
export class echoAR {
constructor(apiKey) {
this.models = new Map()
this.serverURL = "https://api.echo3d.com/query?key=" + apiKey
this.loader = new THREE.GLTFLoader()
}
transformations(id, data) {
// initializes model's transformations based on metadata in console
var keys = Object.keys(data.db)
var i
for (i=0; i<keys.length; i++) {
if (id.includes(data.db[keys[i]].id)) {
break
}
}
if(data.db[keys[i]].additionalData.direction=="right") {
this.models.get(id).direction = "right"
}
else if(data.db[keys[i]].additionalData.direction=="left") {
this.models.get(id).direction = "left"
}
if(data.db[keys[i]].additionalData.x!=null) {
this.models.get(id).hologram.position.x += Number(data.db[keys[i]].additionalData.x)
}
if(data.db[keys[i]].additionalData.y!=null) {
this.models.get(id).hologram.position.y += Number(data.db[keys[i]].additionalData.y)
}
if(data.db[keys[i]].additionalData.z!=null) {
this.models.get(id).hologram.position.z += Number(data.db[keys[i]].additionalData.z)
}
if(data.db[keys[i]].additionalData.xAngle!=null) {
this.models.get(id).hologram.rotation.x = Number(data.db[keys[i]].additionalData.xAngle)
}
if(data.db[keys[i]].additionalData.yAngle!=null) {
this.models.get(id).hologram.rotation.y = Number(data.db[keys[i]].additionalData.yAngle)
}
if(data.db[keys[i]].additionalData.zAngle!=null) {
this.models.get(id).hologram.rotation.z = Number(data.db[keys[i]].additionalData.zAngle)
}
if(data.db[keys[i]].additionalData.scale!=null) {
this.models.get(id).hologram.scale.x = Number(data.db[keys[i]].additionalData.scale)
this.models.get(id).hologram.scale.y = Number(data.db[keys[i]].additionalData.scale)
this.models.get(id).hologram.scale.z = Number(data.db[keys[i]].additionalData.scale)
}
// deal with image targets
if (data.db[keys[i]].target.type=="IMAGE_TARGET") {
this.models.get(id).imageTarget = data.db[keys[i]].target.filename
}
}
// Load the glb model at the requested point on the surface.
findObject(isAFrame, callback) {
var self = this
var data
$.getJSON(self.serverURL, function(data) {
var keys = Object.keys(data.db)
var i
var modelsLoaded = 0
for(i=0; i< keys.length; i++) {
var fileURL = self.serverURL + "&file=" + data.db[keys[i]].additionalData.glbHologramStorageID
if (data.db[keys[i]].additionalData.glbHologramStorageID==null) {
fileURL = self.serverURL + "&file=" + data.db[keys[i]].hologram.storageID
}
if (isAFrame) {
var newElement = document.createElement("a-entity")
newElement.setAttribute("gltf-model", fileURL)
console.log("URL " + fileURL)
self.models.set(data.db[keys[i]].id, {hologram: newElement, direction: null, url: fileURL, imageTarget: null})
self.transformationsAFrame(data.db[keys[i]].id, data)
modelsLoaded++
if (modelsLoaded == keys.length) {
callback(data)
}
}
else {
self.loadModels(fileURL, data.db[keys[i]].id, data, function(data) {
modelsLoaded++
if (modelsLoaded == keys.length) {
callback(data)
}
})
}
}
})
}
loadModels(fileURL, id, data, callback) {
console.log(fileURL)
this.loader.load(
fileURL, // resource URL.
(gltf) => {this.models.set(id, {hologram: gltf.scene, direction: null, url: fileURL, imageTarget: null}); this.transformations(id, data); callback(data) }, // loaded handler.
(xhr) => {console.log(`${(xhr.loaded / xhr.total * 100 )}% loaded`)}, // progress handler.
(error) => {console.log('An error happened: ' + error)}
)
}
transformationsAFrame(id, data) {
// initializes model's transformations based on metadata in console
var keys = Object.keys(data.db)
var i
for (i=0; i<keys.length; i++) {
if (id.includes(data.db[keys[i]].id)) {
break
}
}
if(data.db[keys[i]].additionalData.direction=="right") {
this.models.get(id).direction = "right"
}
else if(data.db[keys[i]].additionalData.direction=="left") {
this.models.get(id).direction = "left"
}
if(data.db[keys[i]].additionalData.x!=null) {
this.models.get(id).hologram.object3D.position.x += Number(data.db[keys[i]].additionalData.x)
}
if(data.db[keys[i]].additionalData.y!=null) {
this.models.get(id).hologram.object3D.position.y += Number(data.db[keys[i]].additionalData.y)
}
if(data.db[keys[i]].additionalData.z!=null) {
this.models.get(id).hologram.object3D.position.z += Number(data.db[keys[i]].additionalData.z)
}
if(data.db[keys[i]].additionalData.xAngle!=null) {
this.models.get(id).hologram.object3D.rotation.x = Number(data.db[keys[i]].additionalData.xAngle)
}
if(data.db[keys[i]].additionalData.yAngle!=null) {
this.models.get(id).hologram.object3D.rotation.y = Number(data.db[keys[i]].additionalData.yAngle)
}
if(data.db[keys[i]].additionalData.zAngle!=null) {
this.models.get(id).hologram.object3D.rotation.z = Number(data.db[keys[i]].additionalData.zAngle)
}
if(data.db[keys[i]].additionalData.scale!=null) {
this.models.get(id).hologram.object3D.scale.x = Number(data.db[keys[i]].additionalData.scale)
this.models.get(id).hologram.object3D.scale.y = Number(data.db[keys[i]].additionalData.scale)
this.models.get(id).hologram.object3D.scale.z = Number(data.db[keys[i]].additionalData.scale)
}
// deal with image targets
if (data.db[keys[i]].target.type=="IMAGE_TARGET") {
this.models.get(id).imageTarget = data.db[keys[i]].target.filename
}
}
}