-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
401 lines (347 loc) · 16.3 KB
/
index.html
File metadata and controls
401 lines (347 loc) · 16.3 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cool Unlock</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tensorflow/4.7.0/tf.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/speech-commands"></script>
<style>
body { margin: 0; }
canvas { display: block; }
#orientationValues {
position: absolute;
top: 10px;
left: 10px;
color: white;
font-size: 20px;
background: rgba(0, 0, 0, 0.7);
padding: 10px;
border-radius: 5px;
pointer-events: none;
}
#voiceStatus {
position: absolute;
top: 10px;
right: 10px;
color: white;
font-size: 20px;
background: rgba(0, 0, 0, 0.7);
padding: 10px;
border-radius: 5px;
pointer-events: none;
}
</style>
<script type="module">
import * as THREE from 'https://cdn.jsdelivr.net/gh/mrdoob/three.js@r128/build/three.module.js';
import { PointerLockControls } from 'https://cdn.jsdelivr.net/gh/mrdoob/three.js@r128/examples/jsm/controls/PointerLockControls.js';
import { OBJLoader } from 'https://cdn.jsdelivr.net/gh/mrdoob/three.js@r128/examples/jsm/loaders/OBJLoader.js';
import { GLTFLoader } from 'https://cdn.jsdelivr.net/gh/mrdoob/three.js@r128/examples/jsm/loaders/GLTFLoader.js';
// Voice recognition setup
let recognizer;
let selectedSpecialCat = false;
async function loadMetadata() {
const response = await fetch('./models/tm-my-audio-model/metadata.json');
return await response.json();
}
async function createModel() {
try {
const metadata = await loadMetadata();
const recognizer = speechCommands.create(
"BROWSER_FFT",
undefined,
'./models/tm-my-audio-model/model.json',
metadata
);
await recognizer.ensureModelLoaded();
return recognizer;
} catch (error) {
console.error('Error creating model:', error);
throw error;
}
}
async function initVoiceRecognition() {
try {
recognizer = await createModel();
const classLabels = recognizer.wordLabels();
const voiceStatus = document.getElementById('voiceStatus');
recognizer.listen(result => {
const scores = result.scores;
const maxScore = Math.max(...scores);
const maxScoreIndex = scores.indexOf(maxScore);
const detectedLabel = classLabels[maxScoreIndex];
// voiceStatus.textContent = detectedLabel === 'psps' ? 'psps detected!' : 'listening...';
if (detectedLabel === 'psps' && selectedSpecialCat) {
window.location.href = 'unlocked.html';
}
}, {
includeSpectrogram: true,
probabilityThreshold: 0.99,
invokeCallbackOnNoiseAndUnknown: true,
overlapFactor: 0.5
});
} catch (error) {
console.error('Error in voice recognition init:', error);
}
}
// Three.js scene setup
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x000000); // Set black background initially
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.appendChild(renderer.domElement);
// Create voice status display
const voiceStatusDiv = document.createElement('div');
voiceStatusDiv.id = 'voiceStatus';
// voiceStatusDiv.textContent = 'Initializing voice...';
// document.body.appendChild(voiceStatusDiv);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const textureLoader = new THREE.TextureLoader();
// Load Japanese Room
const gltfLoader = new GLTFLoader();
gltfLoader.load(
'assets/japanese-style-room/room.glb',
function (gltf) {
const room = gltf.scene;
room.traverse((child) => {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
// Map your texture files to specific materials or meshes
if (child.name.includes('floor') || child.name.includes('ground')) {
const floorTexture = textureLoader.load('assets/japanese-style-room/textures/internal_ground_ao_texture.jpeg');
child.material.map = floorTexture;
}
// Add more texture mappings as needed
}
});
room.position.set(0, 0, -2);
scene.add(room);
},
undefined,
function (error) {
console.error('Error loading room:', error);
}
);
// Lighting setup for indoor scene
const ambientLight = new THREE.AmbientLight(0xffffff, 0.2);
scene.add(ambientLight);
const mainLight = new THREE.DirectionalLight(0xffffff, 0.2);
mainLight.position.set(5, 5, 5);
mainLight.castShadow = true;
scene.add(mainLight);
const pointLight1 = new THREE.PointLight(0xffd1b3, 0.1); // Warm light
pointLight1.position.set(0, 3, 0);
scene.add(pointLight1);
// Camera setup
const userHeight = 7; // Average human height in meters
camera.position.set(0, userHeight, 5);
// Cat setup remains largely the same, but adjust initial positions
const catObjects = [];
const loader = new OBJLoader();
const numCats = 20;
const radius = 7; // Adjusted for room size
const cats = [];
// Define cat types
const catTypes = [
{ model: 'assets/3d/fishcat.obj', texture: 'assets/3d/fishcat.png', sound: 'assets/audio/meow2.mp3' },
{ model: 'assets/3d/collarcat.obj', texture: 'assets/3d/collarcat.png', sound: 'assets/audio/meow1.wav' },
{ model: 'assets/3d/orangecat.obj', texture: 'assets/3d/orangecat.png', sound: 'assets/audio/meow3.ogg' },
{ model: 'assets/3d/siamesecat.obj', texture: 'assets/3d/siamesecat.png', sound: 'assets/audio/meow4.wav' },
{ model: 'assets/3d/fishcat.obj', texture: 'assets/3d/fishcat.png', sound: 'assets/audio/meow1.wav' },
{ model: 'assets/3d/collarcat.obj', texture: 'assets/3d/collarcat.png', sound: 'assets/audio/meow2.mp3' },
{ model: 'assets/3d/orangecat.obj', texture: 'assets/3d/orangecat.png', sound: 'assets/audio/meow4.wav' },
{ model: 'assets/3d/siamesecat.obj', texture: 'assets/3d/siamesecat.png', sound: 'assets/audio/meow3.ogg' },
{ model: 'assets/3d/fishcat.obj', texture: 'assets/3d/fishcat.png', sound: 'assets/audio/meow3.ogg' },
{ model: 'assets/3d/collarcat.obj', texture: 'assets/3d/collarcat.png', sound: 'assets/audio/meow4.wav' },
{ model: 'assets/3d/orangecat.obj', texture: 'assets/3d/orangecat.png', sound: 'assets/audio/meow1.wav' },
{ model: 'assets/3d/siamesecat.obj', texture: 'assets/3d/siamesecat.png', sound: 'assets/audio/meow2.mp3' },
{ model: 'assets/3d/fishcat.obj', texture: 'assets/3d/fishcat.png', sound: 'assets/audio/meow4.wav' },
{ model: 'assets/3d/collarcat.obj', texture: 'assets/3d/collarcat.png', sound: 'assets/audio/meow3.ogg' },
{ model: 'assets/3d/orangecat.obj', texture: 'assets/3d/orangecat.png', sound: 'assets/audio/meow2.mp3' },
{ model: 'assets/3d/siamesecat.obj', texture: 'assets/3d/siamesecat.png', sound: 'assets/audio/meow1.wav' },
];
// Fixed cat assignments - each number represents the index of catTypes
const fixedCatTypes = [
0, // ID 0 - fishcat
2, // ID 1 - orangecat (special cat)
1, // ID 2 - collarcat
3, // ID 3 - siamesecat
4, // ID 4 - fishcat
5, // ID 5 - collarcat
6, // ID 6 - orangecat
7, // ID 7 - siamesecat
8, // ID 8 - fishcat
9, // ID 9 - collarcat
10, // ID 10 - orangecat
11, // ID 11 - siamesecat
0, // ID 12 - fishcat
1, // ID 13 - collarcat
10, // ID 14 - orangecat
3, // ID 15 - siamesecat
4, // ID 16 - fishcat
5, // ID 17 - collarcat
6, // ID 18 - orangecat
7 // ID 19 - siamesecat
];
// Adjust cat positions to be appropriate for room size
for (let i = 0; i < numCats; i++) {
const angle = (i / numCats) * Math.PI * 2;
const x = radius * Math.cos(angle);
const z = 5 + radius * Math.sin(angle);
const catType = catTypes[fixedCatTypes[i]];
cats.push({
id: i,
model: catType.model,
texture: catType.texture,
sound: catType.sound,
position: [x, 1, z] // Adjusted Y position to be on room floor
});
}
const jumpHeight = 1;
const jumpDuration = 1000;
cats.forEach(cat => {
const texture = textureLoader.load(cat.texture, () => {
loader.load(cat.model, (object) => {
object.traverse((child) => {
if (child.isMesh) {
child.material.map = texture;
child.material.needsUpdate = true;
child.material.color.multiplyScalar(0.9);
}
});
object.position.set(...cat.position);
object.scale.set(5, 5, 5);
const dx = 0 - cat.position[0];
const dz = 5 - cat.position[2];
object.rotation.y = Math.atan2(dx, dz);
object.userData = {
id: cat.id,
isJumping: false,
jumpStartTime: 0,
initialY: cat.position[1],
sound: new Audio(cat.sound)
};
catObjects.push(object);
scene.add(object);
});
});
});
const skyTexture = textureLoader.load('assets/3d/sky.jpg');
const skyMaterial = new THREE.MeshBasicMaterial({ map: skyTexture, side: THREE.BackSide });
const skyGeometry = new THREE.SphereGeometry(500, 32, 32);
const skyDome = new THREE.Mesh(skyGeometry, skyMaterial);
scene.add(skyDome);
const controls = new PointerLockControls(camera, document.body);
scene.add(controls.getObject());
const light = new THREE.HemisphereLight(0xffffff, 0x444444);
light.position.set(0, 200, 0);
scene.add(light);
function onPointerDown(event) {
pointer.x = (event.clientX / window.innerWidth) * 2 - 1;
pointer.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const intersects = raycaster.intersectObjects(catObjects, true);
if (intersects.length > 0) {
let catObject = intersects[0].object;
while (catObject.parent && !catObjects.includes(catObject)) {
catObject = catObject.parent;
}
if (catObjects.includes(catObject)) {
// Check if it's the special cat (ID 1)
selectedSpecialCat = catObject.userData.id === 1;
if (selectedSpecialCat) {
// voiceStatusDiv.textContent = 'Special cat selected! Say "psps"';
} else {
// voiceStatusDiv.textContent = 'Listening...';
}
if (!catObject.userData.isJumping) {
catObject.userData.isJumping = true;
catObject.userData.jumpStartTime = Date.now();
catObject.userData.sound.currentTime = 0;
catObject.userData.sound.play();
}
}
}
}
function animateCats() {
const currentTime = Date.now();
catObjects.forEach(cat => {
if (cat.userData.isJumping) {
const elapsed = currentTime - cat.userData.jumpStartTime;
const progress = elapsed / jumpDuration;
if (progress >= 1) {
cat.position.y = cat.userData.initialY;
cat.userData.isJumping = false;
} else {
const jumpProgress = Math.sin(progress * Math.PI);
cat.position.y = cat.userData.initialY + jumpHeight * jumpProgress;
}
}
});
}
window.addEventListener('pointerdown', onPointerDown);
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
const orientationDiv = document.createElement('div');
orientationDiv.id = 'orientationValues';
document.body.appendChild(orientationDiv);
let smoothedAlpha = 0;
let smoothedBeta = 0;
let prevAlpha = null;
let accumulatedAlpha = 0;
function getShortestRotation(from, to) {
const diff = ((to - from + 180) % 360) - 180;
return diff < -180 ? diff + 360 : diff;
}
const onDeviceOrientation = (event) => {
const { alpha, beta } = event;
if (prevAlpha !== null) {
const alphaDiff = getShortestRotation(prevAlpha, alpha);
accumulatedAlpha += alphaDiff;
} else {
accumulatedAlpha = alpha;
}
prevAlpha = alpha;
smoothedAlpha = THREE.MathUtils.lerp(smoothedAlpha, accumulatedAlpha, 0.1);
smoothedBeta = THREE.MathUtils.lerp(smoothedBeta, beta, 0.1);
const alphaRad = THREE.MathUtils.degToRad(smoothedAlpha - 90);
const betaRad = THREE.MathUtils.degToRad(smoothedBeta - 90);
const quaternionYaw = new THREE.Quaternion();
quaternionYaw.setFromAxisAngle(new THREE.Vector3(0, 1, 0), alphaRad);
const quaternionPitch = new THREE.Quaternion();
quaternionPitch.setFromAxisAngle(new THREE.Vector3(1, 0, 0), betaRad);
camera.quaternion.copy(quaternionYaw).multiply(quaternionPitch);
// Get current time and date
const now = new Date();
const timeString = now.toLocaleTimeString();
const dateString = now.toLocaleDateString();
orientationDiv.innerHTML = `
${timeString}<br>
${dateString}<br>
8° ⛅️
`;
};
// Initialize voice recognition when the page loads
window.addEventListener('load', () => {
initVoiceRecognition();
});
window.addEventListener('deviceorientation', onDeviceOrientation, true);
const animate = function () {
requestAnimationFrame(animate);
animateCats();
renderer.render(scene, camera);
};
animate();
</script>
</head>
<body>
</body>
</html>