-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloTalkMap.html
More file actions
370 lines (318 loc) · 11.1 KB
/
HelloTalkMap.html
File metadata and controls
370 lines (318 loc) · 11.1 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Google Street View with WSAD Controls</title>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCZyX7wlZ8OYbboF82yqPSXoPuSaJ-nECk&libraries=places&callback=initMap" async defer></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/loaders/GLTFLoader.js"></script>
<style>
/* Layout styles */
body {
margin: 0;
display: flex;
flex-direction: column;
height: 100vh;
}
#search-bar {
display: flex;
padding: 10px;
background-color: #f8f8f8;
border-bottom: 1px solid #ddd;
}
#search-input {
flex: 1;
padding: 10px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 4px;
}
#search-button {
padding: 10px 20px;
margin-left: 10px;
font-size: 16px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
#search-button:hover {
background-color: #0056b3;
}
#map-container {
display: flex;
flex: 1;
}
#map {
width: 50%;
height: 100%;
}
#street-view {
width: 50%;
height: 100%;
position: relative;
}
/* WSAD Controls */
.wsad-controls {
position: absolute;
bottom: 20px;
left: 20px;
display: grid;
grid-template-columns: 25px 25px 25px; /* 50% of original size */
gap: 2.5px; /* 50% of original gap */
z-index: 1000;
}
.wsad-controls button {
width: 25px; /* 50% of original size */
height: 25px; /* 50% of original size */
background-color: #ffffff;
border: 2px solid #cccccc;
border-radius: 5px;
font-size: 9px; /* 50% of original size */
font-weight: bold;
cursor: pointer;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}
.wsad-controls button:hover {
background-color: #f0f0f0;
}
.wsad-controls button:active {
background-color: #e0e0e0;
}
.wsad-controls .spacer {
visibility: hidden;
}
#avatar-container {
position: absolute;
top: 0%;
left: 50%;
width: 50%;
height: 100%;
/*transform: translate(-50%, -50%);*/
z-index: 100;
pointer-events: none; /* Make sure the container does not block interactions */
}
.gm-style-cc, .gmnoprint, .gm-bundled-control {
display: none !important;
}
#size-slider-container {
position: absolute;
bottom: 20px;
right: 20px;
z-index: 1000;
display: flex;
align-items: center;
}
#size-slider {
width: 150px;
}
</style>
</head>
<body>
<!-- Search Bar -->
<div id="search-bar">
<input id="search-input" type="text" placeholder="Search for a location..." />
<button id="search-button">Search</button>
</div>
<!-- Map and Street View Container -->
<div id="map-container">
<div id="map"></div>
<div id="street-view">
<!-- WSAD Controls -->
<div class="wsad-controls">
<div class="spacer"></div>
<button id="up">W</button>
<div class="spacer"></div>
<button id="left">A</button>
<button id="down">S</button>
<button id="right">D</button>
</div>
</div>
</div>
<div id="avatar-container"></div>
<div id="size-slider-container">
<label for="size-slider">Avatar Size:</label>
<input type="range" id="size-slider" min="0.2" max="2" step="0.1" value="0.5">
</div>
<script>
let map, panorama, geocoder, placesService, avatarModel, mixer, camera;
function initMap() {
// Initialize the map
const initialLocation = { lat: 37.7749, lng: -122.4194 }; // San Francisco, CA
map = new google.maps.Map(document.getElementById("map"), {
center: initialLocation,
zoom: 14,
});
// Initialize the Street View panorama
panorama = new google.maps.StreetViewPanorama(document.getElementById("street-view"), {
position: initialLocation,
pov: {
heading: 0,
pitch: 0,
},
zoom: 1,
disableDefaultUI: true,
showRoadLabels: false,
});
// Link the map and Street View
map.setStreetView(panorama);
// Initialize the geocoder and places service
geocoder = new google.maps.Geocoder();
placesService = new google.maps.places.PlacesService(map);
// Add event listeners for WSAD buttons
document.getElementById("up").addEventListener("click", () => adjustView("up"));
document.getElementById("down").addEventListener("click", () => adjustView("down"));
document.getElementById("left").addEventListener("click", () => adjustView("left"));
document.getElementById("right").addEventListener("click", () => adjustView("right"));
// Add event listener for the search button
document.getElementById("search-button").addEventListener("click", searchLocation);
// Add event listeners for WASD keys
document.addEventListener("keydown", handleKeyDown);
// Add event listener for mouse wheel to zoom in/out on the Street View panorama
panorama.addListener('pov_changed', handlePovEvent);
}
function adjustView(direction) {
if (!panorama) return;
// Get the current POV (point of view)
const pov = panorama.getPov();
// Adjust heading and pitch based on the direction
switch (direction) {
case "up":
pov.pitch = Math.min(pov.pitch + 5, 90); // Move up (max pitch: 90)
break;
case "down":
pov.pitch = Math.max(pov.pitch - 5, -90); // Move down (min pitch: -90)
break;
case "left":
//pov.heading = (pov.heading - 5 + 360) % 360; // Rotate left
break;
case "right":
//pov.heading = (pov.heading + 5) % 360; // Rotate right
break;
default:
return; // Ignore other inputs
}
// Update the panorama's POV
panorama.setPov(pov);
}
function searchLocation() {
const query = document.getElementById("search-input").value;
if (!query) return;
// Use the Places API to search for the location
placesService.textSearch({ query }, (results, status) => {
if (status === google.maps.places.PlacesServiceStatus.OK && results[0]) {
const location = results[0].geometry.location;
// Update the map and Street View to the new location
map.setCenter(location);
panorama.setPosition(location);
} else {
alert("Location not found. Please try again.");
}
});
}
function initAvatar() {
const container = document.getElementById('avatar-container'); // Attach to avatar-container
const scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(75, container.clientWidth / container.clientHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }); // Enable alpha for transparency
renderer.setSize(container.clientWidth, container.clientHeight);
renderer.setClearColor(0x000000, 0); // Set background to transparent
container.appendChild(renderer.domElement);
const light = new THREE.AmbientLight(0x404040);
scene.add(light);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(1, 1, 1).normalize();
scene.add(directionalLight);
const loader = new THREE.GLTFLoader();
const cachedGLB = localStorage.getItem('cachedGLB');
if (cachedGLB) {
const arrayBuffer = new Uint8Array(JSON.parse(cachedGLB)).buffer;
loader.parse(arrayBuffer, '', function (gltf) {
avatarModel = gltf.scene;
scene.add(avatarModel);
avatarModel.position.set(0, -1, 0);
avatarModel.scale.set(0.5, 0.5, 0.5);
mixer = new THREE.AnimationMixer(avatarModel);
const action = mixer.clipAction(gltf.animations[0]);
action.play();
});
} else {
loader.load('https://api.keepwork.com/ts-storage/siteFiles/44128/raw#lixizhi_stand.glb', function (gltf) {
avatarModel = gltf.scene;
scene.add(avatarModel);
avatarModel.position.set(0, -1, 0);
avatarModel.scale.set(0.5, 0.5, 0.5);
mixer = new THREE.AnimationMixer(avatarModel);
const action = mixer.clipAction(gltf.animations[0]);
action.play();
// Cache the GLB file
const glbData = new Uint8Array(gltf.parser.extensions['KHR_binary_glTF'].body);
localStorage.setItem('cachedGLB', JSON.stringify(Array.from(glbData)));
}, undefined, function (error) {
console.error(error);
});
}
camera.position.z = 2;
function animate() {
requestAnimationFrame(animate);
if (mixer) mixer.update(0.01); // Update the animation mixer
renderer.render(scene, camera);
}
animate();
// Resize renderer on window resize
window.addEventListener('resize', () => {
camera.aspect = container.clientWidth / container.clientHeight;
camera.updateProjectionMatrix();
renderer.setSize(container.clientWidth, container.clientHeight);
});
// Ensure the avatar canvas does not capture key or mouse input
renderer.domElement.style.pointerEvents = 'none';
const sizeSlider = document.getElementById('size-slider');
sizeSlider.addEventListener('input', () => {
const scale = parseFloat(sizeSlider.value);
if (avatarModel) {
avatarModel.scale.set(scale, scale, scale);
}
});
}
// Add event listener for mouse wheel to zoom in/out on the map
//document.getElementById('street-view').addEventListener('wheel', handleMouseWheel);
function handleKeyDown(event) {
if (!avatarModel || !camera) return;
switch (event.key) {
case "w":
//avatarModel.position.z -= 0.1; // Move forward
break;
case "s":
//avatarModel.position.z += 0.1; // Move backward
break;
case "a":
avatarModel.rotation.y += 0.1; // Rotate left
break;
case "d":
avatarModel.rotation.y -= 0.1; // Rotate right
break;
default:
return;
}
}
function handlePovEvent(event) {
if (!camera) return;
// Get the current zoom level of the Street View panorama
const zoomLevel = panorama.getZoom();
// Get the current pitch value of the Street View panorama
const pitch = panorama.getPov().pitch;
// Adjust the camera's distance to the object based on the zoom level
camera.position.z = 5 / (zoomLevel*zoomLevel); // Adjust the divisor to control the zoom sensitivity
// Adjust the camera's pitch based on the panorama's current pitch value
camera.rotation.x = THREE.Math.degToRad(pitch);
}
window.onload = function() {
initMap();
initAvatar();
};
</script>
</body>
</html>