Skip to content

Commit ade7e76

Browse files
added 'controllermodelready'-event + controller-specific-anchors demo
1 parent fb67c01 commit ade7e76

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

examples/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ <h2>Examples</h2>
150150
<li><a href="showcase/shopping/">Shopping</a></li>
151151
<li><a href="showcase/spheres-and-fog/">Spheres and Fog</a></li>
152152
<li><a href="showcase/wikipedia/">Wikipedia</a></li>
153+
<li><a href="showcase/controller-specific-anchors">Controller-specific anchors</a></li>
153154
<li><a href="boilerplate/hello-world/">Hello World</a></li>
154155
<li><a href="boilerplate/ar-hello-world/">AR Hello World</a></li>
155156
<li><a href="boilerplate/panorama/">360&deg; Image</a></li>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>multi input anchor</title>
6+
<meta name="description" content="Mixins">
7+
<script src="../../../dist/aframe-master.js"></script>
8+
<script src="./wearable.js"></script>
9+
</head>
10+
<body>
11+
<a-scene >
12+
<a-assets>
13+
<img id="highlight1" src="../../assets/img/radial-highlight.png">
14+
</a-assets>
15+
16+
<a-image position="0 -.2 -5" src="#highlight1" rotation="-90 0 0" scale="30 30 30"></a-image>
17+
18+
19+
<a-text value="Switch between hands/controllers\n\nfor controller-specific\n\nbehaviour & placement" align="center" position="0 2 -4" color="black"></a-text>
20+
<a-sky color="#CCC"></a-sky>
21+
22+
<a-entity wearable="el: #leftHand; class: mymenu;" position="0 1.5 -0.4" >
23+
<a-plane color="blue" position="0 0 0" width="0.065" height="0.01" animation="property: scale; to: 0.2 1.0 1.0; loop: true; dur: 2000; delay: 0"></a-plane>
24+
<a-plane color="cyan" position="0 -0.01 0" width="0.065" height="0.01" animation="property: scale; to: 0.2 1.0 1.0; loop: true; dur: 2000; delay: 500"></a-plane>
25+
<a-plane color="magenta" position="0 -0.02 0" width="0.065" height="0.01" animation="property: scale; to: 0.2 1.0 1.0; loop: true; dur: 2000; delay:1000"></a-plane>
26+
<a-text id="debug" color="blue" position="0 0.02 0" scale="0.14 0.14 0.14" value="hello" align="center"></a-text>
27+
</a-entity>
28+
29+
<a-entity id="rightHand" laser-controls="hand: right" hand-tracking-controls="hand: right"></a-entity>
30+
31+
<a-entity id="leftHand" laser-controls="hand: left" hand-tracking-controls="hand: left">
32+
<!-- customized per-controller placeholders -->
33+
<a-entity class="mymenu hand-tracking-controls" position="0 -0.03 -0.08" scale="0.5 0.5 0.5" rotation="90 0 180"></a-entity>
34+
<a-entity class="mymenu meta-touch-controls" position="0.01 0.02 -0.07" scale="0.7 0.7 0.7" rotation="-90 -10 0"></a-entity>
35+
<!-- default placeholder (used when none of the above applies) -->
36+
<a-entity class="mymenu"><a-entity>
37+
</a-entity>
38+
39+
</a-scene>
40+
</body>
41+
</html>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
*Usage:
3+
* <a-entity wearable="el: #leftHand; class: foo"></a-entity>
4+
* <a-entity id="leftHand">
5+
* <a-entity class="foo" position="0 0 0" rotation="0 0 0" scale="0 0 0"></a-entity>
6+
* <a-entity>
7+
*
8+
* NOTE: you can hint different position/rotation-offsets by adding controller-specific entities to the target:
9+
*
10+
* <a-entity id="leftHand">
11+
* <a-entity class="foo" position="0 0 0" rotation="0 0 0" scale="0 0 0"></a-entity>
12+
* <a-entity class="foo hand-tracking-controls" position="0 0 0" rotation="0 0 0" scale="0 0 0"></a-entity>
13+
* <a-entity>
14+
*
15+
* The controllernames are hinted by the 'controllermodelready' event:
16+
*
17+
* 'hand-tracking-controls',
18+
* 'meta-touch-controls',
19+
* 'valve-index-controls',
20+
* 'logitech-mx-ink-controls',
21+
* 'windows-motion-controls',
22+
* 'hp-mixed-reality-controls',
23+
* 'generic-tracked-controller-controls',
24+
* 'pico-controls',
25+
* (and more in the future)
26+
*/
27+
28+
AFRAME.registerComponent('wearable', {
29+
schema:{
30+
el: {type:"selector"},
31+
class: {type:"string"}
32+
},
33+
init: function(){
34+
if( !this.data.el ) return console.warn(`wearable.js: cannot find ${this.data.el}`)
35+
this.controllers = {}
36+
this.remember()
37+
this.data.el.addEventListener('controllermodelready', this.bindPlaceHolders.bind(this,["controllermodelready"]) )
38+
this.data.el.addEventListener('controllerconnected', this.bindPlaceHolders.bind(this,["controllerconnected"] ) )
39+
},
40+
41+
bindPlaceHolders: function(type,e){
42+
let controllerName = e.detail.name
43+
let selector = `.${this.data.class}.${controllerName}`
44+
let placeholder = this.data.el.querySelector(selector) || this.data.el.querySelector(`${this.data.class}`)
45+
if( !placeholder ) return console.warn("wearable.js: could not find placeholder to attach to")
46+
this.attachPlaceHolder(type,e,placeholder, controllerName)
47+
},
48+
49+
attachPlaceHolder: function(type, el, placeholder, controllerName){
50+
this.el.object3DMap = {} // unsync THREE <-> AFRAME entity
51+
placeholder.object3D.add( this.obj )
52+
if( controllerName != 'hand-tracking-controls' ){
53+
// re-add for controller-models which don't re-add children ('meta-touch-controls' e.g.)
54+
if( this.data.el.getObject3D("mesh") ){
55+
this.data.el.getObject3D("mesh").add(placeholder.object3D)
56+
}
57+
}
58+
// these are handled by the placeholder entity
59+
this.obj.position.set(0,0,0);
60+
this.obj.rotation.set(0,0,0);
61+
this.obj.scale.set(1,1,1);
62+
},
63+
64+
detach: function(){
65+
this.el.setObject3D( this.obj.uuid, this.obj )
66+
this.el.object3D.position.copy( this.position )
67+
this.el.object3D.rotation.copy( this.rotation )
68+
this.el.object3D.scale.copy( this.scale )
69+
},
70+
71+
remember: function(){
72+
this.obj = this.el.object3D
73+
this.position = this.el.object3D.position.clone()
74+
this.rotation = this.el.object3D.rotation.clone()
75+
this.scale = this.el.object3D.scale.clone()
76+
this.parent = this.el.object3D.parent
77+
}
78+
79+
})
80+

src/components/hand-tracking-controls.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,11 @@ export var Component = registerComponent('hand-tracking-controls', {
388388
this.updateModelMaterial();
389389
this.setupChildrenEntities();
390390
this.el.setObject3D('mesh', mesh);
391+
this.el.emit('controllermodelready', {
392+
name: 'hand-tracking-controls',
393+
model: this.data.model,
394+
rayOrigin: new THREE.Vector3(0, 0, 0)
395+
});
391396
},
392397

393398
setupChildrenEntities: function () {

0 commit comments

Comments
 (0)