-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
156 lines (130 loc) · 5.32 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebXR Experience with Full Features</title>
<script src="https://cdn.jsdelivr.net/npm/three@0.155.0/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.155.0/examples/js/controls/VRButton.js"></script>
</head>
<body style="margin: 0; overflow: hidden;">
<script>
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x87ceeb);
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 1.6, 3);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.xr.enabled = true;
document.body.appendChild(renderer.domElement);
document.body.appendChild(VRButton.createButton(renderer));
const listener = new THREE.AudioListener();
camera.add(listener);
// Audio setup
const hoverSound = new THREE.Audio(listener);
const grabSound = new THREE.Audio(listener);
const audioLoader = new THREE.AudioLoader();
audioLoader.load('hover.mp3', buffer => hoverSound.setBuffer(buffer));
audioLoader.load('grab.mp3', buffer => grabSound.setBuffer(buffer));
const groundGeometry = new THREE.PlaneGeometry(100, 100);
const groundMaterial = new THREE.MeshStandardMaterial({ color: 0x008800 });
const ground = new THREE.Mesh(groundGeometry, groundMaterial);
ground.rotation.x = -Math.PI / 2;
scene.add(ground);
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(5, 10, 7.5);
scene.add(directionalLight);
const boxGeometry = new THREE.BoxGeometry(0.5, 0.5, 0.5);
const boxMaterial = new THREE.MeshStandardMaterial({ color: 0xff0000 });
const box = new THREE.Mesh(boxGeometry, boxMaterial);
box.position.set(0, 0.25, -1);
box.castShadow = true;
box.userData.isGrabbable = true;
box.userData.originalColor = boxMaterial.color.clone();
scene.add(box);
// Grabbing and Hover Setup
const controllers = [];
const tempMatrix = new THREE.Matrix4();
const raycaster = new THREE.Raycaster();
for (let i = 0; i < 2; i++) {
const controller = renderer.xr.getController(i);
scene.add(controller);
controllers.push(controller);
controller.addEventListener('selectstart', onGrabStart);
controller.addEventListener('selectend', onGrabEnd);
}
let grabbedObject = null;
let hoveredObject = null;
function onGrabStart(event) {
const controller = event.target;
if (hoveredObject) {
grabbedObject = hoveredObject;
grabbedObject.userData.originalParent = grabbedObject.parent;
grabbedObject.attach(controller);
// Play grab sound
grabSound.play();
resetVisualIndicator(grabbedObject);
// Trigger haptics
triggerHaptics(controller, 0.5, 100);
}
}
function onGrabEnd(event) {
const controller = event.target;
if (grabbedObject) {
grabbedObject.userData.originalParent.attach(grabbedObject);
grabbedObject = null;
}
}
function applyVisualIndicator(object) {
if (object && object.userData.isGrabbable) {
object.material.color.set(0x00ff00);
object.scale.set(1.1, 1.1, 1.1);
// Play hover sound
if (!hoverSound.isPlaying) hoverSound.play();
}
}
function resetVisualIndicator(object) {
if (object && object.userData.isGrabbable) {
object.material.color.copy(object.userData.originalColor);
object.scale.set(1, 1, 1);
}
}
function triggerHaptics(controller, intensity, duration) {
const gamepad = controller.inputSource?.gamepad;
if (gamepad && gamepad.hapticActuators && gamepad.hapticActuators[0]) {
const actuator = gamepad.hapticActuators[0];
actuator.pulse(intensity, duration);
}
}
function checkHoverState() {
controllers.forEach(controller => {
tempMatrix.identity().extractRotation(controller.matrixWorld);
raycaster.ray.origin.setFromMatrixPosition(controller.matrixWorld);
raycaster.ray.direction.set(0, 0, -1).applyMatrix4(tempMatrix);
const intersects = raycaster.intersectObjects(scene.children);
if (intersects.length > 0 && intersects[0].object.userData.isGrabbable) {
if (hoveredObject !== intersects[0].object) {
if (hoveredObject) resetVisualIndicator(hoveredObject);
hoveredObject = intersects[0].object;
applyVisualIndicator(hoveredObject);
}
} else {
if (hoveredObject) {
resetVisualIndicator(hoveredObject);
hoveredObject = null;
}
}
});
}
const animate = () => {
renderer.setAnimationLoop(() => {
checkHoverState();
renderer.render(scene, camera);
});
};
animate();
</script>
</body>
</html>