-
Notifications
You must be signed in to change notification settings - Fork 37
/
arindex.html
197 lines (162 loc) · 6.93 KB
/
arindex.html
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
<!DOCTYPE html>
<html>
<head>
<title>ar aframe.city</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.7.3/firebase.js"></script>
<script src="https://aframe.io/releases/0.6.1/aframe.min.js"></script>
<script src="https://rawgit.com/google-ar/three.ar.js/master/dist/three.ar.js"></script>
<script src="https://rawgit.com/chenzlabs/aframe-ar/master/dist/aframe-ar.js"></script>
<script src="lib/dist/aframe-city-builder.js"></script>
<script src="http://192.168.1.15:1337/vorlon.js"></script>
</head>
<body>
<a-scene id="my-scene" ar>
<!-- Use AR raycaster, and set raycaster to ignore all A-Frame objects like logos. -->
<a-entity ar-raycaster raycaster="objects:none"></a-entity>
<!-- Declare a ring to use as the raycaster intersection mark. -->
<a-ring id="mark" rotation="-90 0 0" radius-inner="0.01" radius-outer="0.02"></a-ring>
<!-- City -->
<a-entity id="city-holder" position="100000 0 0">
<a-entity id="tableGrid" position="0 0 0" gridhelper="size:10; divisions:20"> </a-entity>
<a-entity id="city" position="-0.25 -0.5 -0.25">
<!-- Grid -->
<!-- <a-entity id="tableGrid" position="0.25 0.5 0.25" gridhelper="size:10; divisions:20"> </a-entity> -->
</a-entity>
</a-entity>
<a-entity
id="title"
position="3.5 1 -0.5"
geometry="primitive: plane; width: 4; height: 1.4"
material="color: black; opacity: 0.6"
text__appname="color: yellow; align: center; font: exo2bold; wrap-count: 12; z-offset: 0.01; baseline: bottom;
value: aframe.city"
text__cityname="color: white; align: center; font: exo2semibold; wrap-count: 24; z-offset: 0.01; baseline: top;
value: #NewCity"
></a-entity>
</a-entity>
</a-scene>
<script type="text/javascript">
// Initialize Firebase
var config = {
apiKey: "AIzaSyBcPajwJo7PyqEjQ70GuvQyIXGOE8xW7Cs",
authDomain: "test-b0c62.firebaseapp.com",
databaseURL: "https://test-b0c62.firebaseio.com",
storageBucket: "test-b0c62.appspot.com",
messagingSenderId: "832504680709"
};
firebase.initializeApp(config);
// Get a reference to the database service
var database = firebase.database();
function loadCityData(cityNameCamel) {
console.log("trying to load city:" + cityNameCamel);
// Erase exiting city (if any)
var cityEl = document.getElementById("city");
while (cityEl.firstChild) {
cityEl.removeChild(cityEl.firstChild);
}
firebase.database().ref('cities/' + cityNameCamel).once('value').then(function(snapshot) {
var cityJSON = snapshot.val().cityJSON;
console.log(cityJSON);
toDOM(cityJSON);
document.getElementById("title").setAttribute("text__cityname", "value", "#" + cityNameCamel)
document.title = "aframe.city#" + cityNameCamel;
});
}
// on load - load the city -- TODO - this should be on a city loader component on the scene entity instead
setTimeout(function(){
// IS THERE A HASH?
var cityName = location.hash.substring(1);
console.log("hash check: " + cityName);
if (cityName) {
loadCityData(cityName);
};
}, 1500);
function appendObject(id, file, scale, position, rotation, scale) {
console.log(position);
$('<a-entity />', {
id: id,
class: 'city object children',
position: position, // doesn't seem to do anything
scale: scale,
rotation: rotation,
file: file,
"obj-model": "obj: url(assets/obj/" + file + ".obj); mtl: url(assets/obj/" + file + ".mtl)",
// src: '#' + file + '-obj',
// mtl: '#' + file + '-mtl',
appendTo : $('#city')
});
document.getElementById(id).setAttribute("position", position); // workaround - this does set position
}
function toDOM(jsonInput) {
// this assumes it is being run first -- it will start placing objects with id 0 and set objectcount to the number of loaded objects
for(var i = 0; i < jsonInput.length; i++) {
var obj = jsonInput[i];
console.log("Adding entity #" + obj.id);
appendObject(obj.id, obj.file, obj.scale, obj.position, obj.rotation, obj.scale)
}
objectCount = i;
console.log("objectCount:" + objectCount);
}
function loadFileButton() {
$.getJSON($("#fileInput")[0].value, function(json) {
toDOM(json);
});
}
function loadButton() {
console.log("load button clicked");
function isJsonString(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
jsonInput = $("#jsonInput")[0].value;
console.log(jsonInput);
if (isJsonString(jsonInput)) {
toDOM(JSON.parse(jsonInput));
} else {
console.log("oops not valid json");
}
}
function locationHashChanged() {
// check if valid hash
// if yes, then clear old city // load new city with that hash
var cityName = location.hash.substring(1);
console.log("hash changed to: " + cityName);
if (cityName) {
loadCityData(cityName);
};
}
window.onhashchange = locationHashChanged;
window.addEventListener('click', function () {
// NEW: Move the city element to where the mark is
var mark = document.querySelector('#mark');
var el = document.querySelector('#city-holder');
// AFRAME.scenes[0].appendChild(el);
// el.innerHTML = content;
// el.setAttribute('scale', '0.1, 0.1, 0.1');
el.setAttribute('position', mark.getAttribute('position'));
// rotate the city to "face" the camera
// return object3D.lookAt(new THREE.Vector3(target.x, target.y, target.z));
// https://github.com/ngokevin/kframe/blob/master/components/look-at/index.js
});
var raycaster = document.querySelector('[ar-raycaster]');
var mark = document.querySelector('#mark');
raycaster.addEventListener('raycaster-intersection', function (evt) {
// Turn the mark green and move it to the intersection point.
mark.setAttribute('color', 'green');
// FIXME: lerp position
mark.setAttribute('position', evt.detail.intersections[0].point);
});
raycaster.addEventListener('raycaster-intersection-cleared', function (evt) {
// Turn the mark red. FIXME: lerp position
mark.setAttribute('color', 'red');
});
</script>
</body>
</html>