forked from urish/web-ar-experiment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
184 lines (166 loc) · 8.13 KB
/
index.html
File metadata and controls
184 lines (166 loc) · 8.13 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebAR Animation Experiment</title>
<script src="https://aframe.io/releases/0.7.1/aframe.min.js"></script>
<script src="https://cdn.rawgit.com/donmccurdy/aframe-extras/cfe5f316/dist/aframe-extras.js"></script>
<script src="https://rawgit.com/chenzlabs/three.ar.js/0f645a5/dist/three.ar.min.js"></script>
<script src="https://rawgit.com/chenzlabs/aframe-ar/0cf8d5a/dist/aframe-ar.min.js"></script>
<script>
AFRAME.registerComponent('shadow-material', {
init: function () {
this.material = this.el.getOrCreateObject3D('mesh').material = new THREE.ShadowMaterial();
this.material.opacity = 0.3;
}
});
</script>
<!-- Prevent touch causing flicker on iOS. -->
<style> * { -webkit-tap-highlight-color: rgba(0,0,0,0); } </style>
</head>
<body>
<a-scene ar>
<!-- Specify raycaster="objects:none" to avoid intersections with A-Frame entities like the fox. -->
<a-camera user-height="0" cursor="fuse:false" ar-raycaster raycaster="objects:none"></a-camera>
<a-entity id="walker" position="0 0 -5" visible="false">
<a-entity gltf-model="models/fox.glb" scale="0.5 0.5 0.5" id="fox" animation-mixer="clip:Fox_Idle" shadow="cast: true">
</a-entity>
<a-plane width="0.4" height="0.4" position="0 0 0" rotation="-90 0 0" color="white" shadow="receive: true" shadow-material></a-plane>
</a-entity>
<a-entity id="walker2" position="0 0 -5" visible="false">
<a-entity gltf-model="models/dino.glb" id="dino" scale="0.5 0.5 0.5" animation-mixer="clip:Idle" shadow="cast: true">
</a-entity>
<a-plane width="0.9" height="0.9" position="0 0 0" rotation="-90 0 0" color="white" shadow="receive: true" shadow-material></a-plane>
</a-entity>
<a-sphere id="intersection" visible="false" radius="0.02" color="red"></a-sphere>
</a-scene>
<script>
let scene = document.querySelector('a-scene');
let walker;
let fox;
let idleAnimation;
let sitting = false;
function activateFox() {
walker = document.querySelector('#walker');
fox = document.querySelector('#fox');
idleAnimation = 'Fox_Idle';
walkAnimation = 'Fox_Run';
}
function activateDino() {
walker = document.querySelector('#walker2');
fox = document.querySelector('#dino');
idleAnimation = 'Idle';
walkAnimation = 'Walk';
}
function playAnimation(animation) {
if (fox === document.querySelector('#dino')) {
sitting = animation.startsWith('Sit') && animation !== 'Sit_Up';
fox.setAttribute('animation-mixer', { clip: animation });
const listener = () => {
fox.setAttribute('animation-mixer', { clip: sitting ? 'Sit_Idle' : idleAnimation });
fox.removeEventListener('animation-loop', listener);
};
fox.addEventListener('animation-loop', listener);
}
}
function onceSceneLoaded() {
let raycaster = document.querySelector('[ar-raycaster]');
let marker = document.querySelector('#intersection');
activateFox();
raycaster.addEventListener('raycaster-intersection', function (event) {
const intersection = event.detail.intersections[0]; //.find(i => i.object.type === 'Scene');
if (intersection) {
marker.setAttribute('position', intersection.point);
marker.setAttribute('visible', true);
marker.setAttribute('color', 'green');
}
});
raycaster.addEventListener('raycaster-intersection-cleared', function (event) {
marker.setAttribute('color', 'red');
});
const { stringify } = AFRAME.utils.coordinates;
let lastEvent = null;
raycaster.addEventListener('click', function () {
const currentEvent = new Date().getTime();
if (lastEvent && (currentEvent - lastEvent < 1000)) {
return;
}
lastEvent = currentEvent;
const targetPosition = raycaster.components.cursor.intersection.point;
if (!walker.getAttribute('visible')) {
walker.setAttribute('visible', true);
walker.setAttribute('position', stringify(targetPosition));
} else {
const currentPosition = walker.object3D.position;
const distance = currentPosition.distanceTo(targetPosition);
walker.object3D.lookAt(targetPosition);
fox.setAttribute('animation-mixer', { clip: walkAnimation });
const animation = document.createElement('a-animation');
animation.setAttribute('attribute', 'position');
animation.setAttribute('to', stringify(targetPosition));
animation.setAttribute('dur', distance * 3000);
animation.setAttribute('easing', 'linear');
animation.addEventListener('animationend',
() => fox.setAttribute('animation-mixer', { clip: idleAnimation }));
marker.setAttribute('color', 'blue');
setTimeout(() => marker.setAttribute('color', 'green'), 350);
walker.appendChild(animation);
}
});
}
function onCommand(results) {
recognition.start();
for (const result of results) {
for (const { transcript } of Array.from(result)) {
if (transcript.toLowerCase().includes('dino') || transcript.toLowerCase().includes('dina')) {
activateDino();
}
if (transcript.toLowerCase().includes('fox')) {
activateFox();
}
const animations = sitting ? {
'Stand up': 'Sit_Up',
'Eat': 'Sit_Eat',
'Chew': 'Sit_Chew',
'Look left': 'Sit_Head_Left',
'Look right': 'Sit_Head_Right',
} : {
'Look left': 'Head_Left',
'Look right': 'Head_Right',
'Chew': 'Chew',
'Drink': 'Drink',
'Jump': 'Jump',
'Head': 'Attack1',
'Attack': 'Attack2',
'Sit Down': 'Sit',
'Eat': 'Eat2',
'Run': 'Run',
'Play dead': 'Death',
};
for (const animationKey of Object.keys(animations)) {
if (transcript.toLowerCase().includes(animationKey.toLowerCase())) {
playAnimation(animations[animationKey]);
return;
}
}
if (transcript.toLowerCase().includes('random')) {
const animationKey = Object.keys(animations)[Math.floor(Math.random() * Object.keys(animations).length)];
playAnimation(animations[animationKey]);
return;
}
}
}
}
try {
recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.maxAlternatives = 5;
recognition.onresult = ({results}) => onCommand(results);
recognition.onerror = () => setTimeout(() => recognition.start(), 500);
recognition.start();
} catch (err) { console.error(err.message); }
scene.addEventListener('loaded', onceSceneLoaded);
</script>
</body>
</html>