-
Notifications
You must be signed in to change notification settings - Fork 2
/
highlightItem.html
70 lines (65 loc) · 2.1 KB
/
highlightItem.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>HighLightItem</title>
<script src="https://aframe.io/releases/1.0.4/aframe.min.js"></script>
</head>
<body>
<script>
AFRAME.registerComponent("highlighter", {
init: function () {
let el = this.el;
//this runs when 3D model is loaded will store the original material color
el.addEventListener("model-loaded", function () {
console.log("model loaded");
let mesh = el.getObject3D("mesh");
this.oldMat = [];
mesh.traverse((node) => {
if (node.material) {
let oldColor = node.material.color;
this.oldMat.push(oldColor);
}
});
});
//this runs when cursor leaves the model returning it to original color
el.addEventListener("mouseleave", function () {
let mesh = el.getObject3D("mesh");
let i = 0;
mesh.traverse((node) => {
if (node.material) {
node.material.color = this.oldMat[i++];
node.material.needsUpdate = true;
}
});
});
//this runs when cursor enters the model giving it a highlighted color
el.addEventListener("mouseenter", function () {
let mesh = el.getObject3D("mesh");
mesh.traverse((node) => {
if (node.material) {
node.material.color = new THREE.Color(0xff0000);
node.material.needsUpdate = true;
}
});
});
},
});
</script>
<a-scene>
<a-camera>
<a-cursor raycaster="objects:a-gltf-model"></a-cursor>
</a-camera>
<!-- highlighter is our custom component -->
<a-gltf-model
highlighter
id="one"
src="model/fallguy.glb"
scale="0.005 0.005 0.005"
position="0 1.5 -3"
>
</a-gltf-model>
</a-scene>
</body>
</html>