Selaa lähdekoodia

Update plugins: add pointer-based input

customisations
alemart 10 kuukautta sitten
vanhempi
commit
4647feeac5
3 muutettua tiedostoa jossa 123 lisäystä ja 6 poistoa
  1. 81
    2
      plugins/aframe-with-encantar.js
  2. 21
    2
      plugins/babylon-with-encantar.js
  3. 21
    2
      plugins/three-with-encantar.js

+ 81
- 2
plugins/aframe-with-encantar.js Näytä tiedosto

@@ -39,6 +39,19 @@ const ARUtils = () => ({
39 39
         return null;
40 40
     },
41 41
 
42
+    getTrackablePointers(frame)
43
+    {
44
+        if(frame === null)
45
+            return [];
46
+
47
+        for(const result of frame.results) {
48
+            if(result.tracker.type == 'pointer-tracker')
49
+                return result.trackables;
50
+        }
51
+
52
+        return [];
53
+    },
54
+
42 55
 });
43 56
 
44 57
 /* ========================================================================= */
@@ -52,8 +65,10 @@ AFRAME.registerSystem('ar', {
52 65
     // data;
53 66
     // schema;
54 67
 
55
-    session: null,
56
-    frame: null,
68
+    session: /** @type {Session | null} */ (null),
69
+    frame: /** @type {Frame | null} */ (null),
70
+    pointers: /** @type {TrackablePointer[]} */ ([]),
71
+
57 72
     _utils: ARUtils(),
58 73
     _started: false,
59 74
     _components: [],
@@ -95,6 +110,14 @@ AFRAME.registerSystem('ar', {
95 110
     {
96 111
         const scene = this.el;
97 112
 
113
+        // read trackable pointers
114
+        this.pointers.length = 0;
115
+        if(this.frame) {
116
+            const newPointers = this._utils.getTrackablePointers(this.frame);
117
+            if(newPointers.length > 0)
118
+                this.pointers.push.apply(this.pointers, newPointers);
119
+        }
120
+
98 121
         // we take control of the rendering
99 122
         scene.renderer.setAnimationLoop(null);
100 123
 
@@ -696,6 +719,35 @@ AFRAME.registerPrimitive('ar-canvas-source', {
696 719
     }
697 720
 });
698 721
 
722
+/**
723
+ * AR Pointer Source
724
+ */
725
+AFRAME.registerComponent('ar-pointer-source', ARComponent({
726
+
727
+    schema: {
728
+    },
729
+
730
+    validate()
731
+    {
732
+        if(!this.el.parentNode.getAttribute('ar-sources'))
733
+            throw new Error('ar-pointer-source must be a direct child of ar-sources');
734
+    },
735
+
736
+    source()
737
+    {
738
+        return AR.Source.Pointer();
739
+    },
740
+
741
+}));
742
+
743
+AFRAME.registerPrimitive('ar-pointer-source', {
744
+    defaultComponents: {
745
+        'ar-pointer-source': {}
746
+    },
747
+    mappings: {
748
+    }
749
+});
750
+
699 751
 /* ========================================================================= */
700 752
 
701 753
 /**
@@ -827,6 +879,33 @@ AFRAME.registerPrimitive('ar-reference-image', {
827 879
     }
828 880
 });
829 881
 
882
+/**
883
+ * AR Pointer Tracker
884
+ */
885
+AFRAME.registerComponent('ar-pointer-tracker', ARComponent({
886
+
887
+    schema: {
888
+    },
889
+
890
+    validate()
891
+    {
892
+        if(!this.el.parentNode.getAttribute('ar-trackers'))
893
+            throw new Error('ar-pointer-tracker must be a direct child of ar-trackers');
894
+    },
895
+
896
+    tracker()
897
+    {
898
+        return AR.Tracker.Pointer();
899
+    },
900
+
901
+}));
902
+
903
+AFRAME.registerPrimitive('ar-pointer-tracker', {
904
+    defaultComponents: {
905
+        'ar-pointer-tracker': {}
906
+    }
907
+});
908
+
830 909
 /* ========================================================================= */
831 910
 
832 911
 /**

+ 21
- 2
plugins/babylon-with-encantar.js Näytä tiedosto

@@ -83,6 +83,16 @@ class ARSystem
83 83
     }
84 84
 
85 85
     /**
86
+     * Pointer-based input in the current frame (touch, mouse, pen...)
87
+     * You need a PointerTracker in your session in order to use these
88
+     * @returns {TrackablePointer[]}
89
+     */
90
+    get pointers()
91
+    {
92
+        return this._pointers;
93
+    }
94
+
95
+    /**
86 96
      * The root is a node that is automatically aligned to the physical scene.
87 97
      * Objects of your virtual scene should be descendants of this node.
88 98
      * @returns {BABYLON.TransformNode}
@@ -126,6 +136,7 @@ class ARSystem
126 136
     {
127 137
         this._session = null;
128 138
         this._frame = null;
139
+        this._pointers = [];
129 140
         this._origin = null;
130 141
         this._root = null;
131 142
         this._scene = null;
@@ -162,6 +173,9 @@ function encantar(demo)
162 173
 
163 174
     function mix(frame)
164 175
     {
176
+        let found = false;
177
+        ar._pointers.length = 0;
178
+
165 179
         for(const result of frame.results) {
166 180
             if(result.tracker.type == 'image-tracker') {
167 181
                 if(result.trackables.length > 0) {
@@ -173,12 +187,17 @@ function encantar(demo)
173 187
                     align(projectionMatrix, viewMatrix, modelMatrix);
174 188
                     ar._origin.setEnabled(true);
175 189
 
176
-                    return;
190
+                    found = true;
177 191
                 }
178 192
             }
193
+            else if(result.tracker.type == 'pointer-tracker') {
194
+                if(result.trackables.length > 0)
195
+                    ar._pointers.push.apply(ar._pointers, result.trackables);
196
+            }
179 197
         }
180 198
 
181
-        ar._origin.setEnabled(false);
199
+        if(!found)
200
+            ar._origin.setEnabled(false);
182 201
     }
183 202
 
184 203
     function align(projectionMatrix, viewMatrix, modelMatrix)

+ 21
- 2
plugins/three-with-encantar.js Näytä tiedosto

@@ -83,6 +83,16 @@ class ARSystem
83 83
     }
84 84
 
85 85
     /**
86
+     * Pointer-based input in the current frame (touch, mouse, pen...)
87
+     * You need a PointerTracker in your session in order to use these
88
+     * @returns {TrackablePointer[]}
89
+     */
90
+    get pointers()
91
+    {
92
+        return this._pointers;
93
+    }
94
+
95
+    /**
86 96
      * The root is a node that is automatically aligned to the physical scene.
87 97
      * Objects of your virtual scene should be descendants of this node.
88 98
      * @returns {THREE.Group}
@@ -126,6 +136,7 @@ class ARSystem
126 136
     {
127 137
         this._session = null;
128 138
         this._frame = null;
139
+        this._pointers = [];
129 140
         this._origin = null;
130 141
         this._root = null;
131 142
         this._scene = null;
@@ -156,6 +167,9 @@ function encantar(demo)
156 167
 
157 168
     function mix(frame)
158 169
     {
170
+        let found = false;
171
+        ar._pointers.length = 0;
172
+
159 173
         for(const result of frame.results) {
160 174
             if(result.tracker.type == 'image-tracker') {
161 175
                 if(result.trackables.length > 0) {
@@ -167,12 +181,17 @@ function encantar(demo)
167 181
                     align(projectionMatrix, viewMatrixInverse, modelMatrix);
168 182
                     ar._origin.visible = true;
169 183
 
170
-                    return;
184
+                    found = true;
171 185
                 }
172 186
             }
187
+            else if(result.tracker.type == 'pointer-tracker') {
188
+                if(result.trackables.length > 0)
189
+                    ar._pointers.push.apply(ar._pointers, result.trackables);
190
+            }
173 191
         }
174 192
 
175
-        ar._origin.visible = false;
193
+        if(!found)
194
+            ar._origin.visible = false;
176 195
     }
177 196
 
178 197
     function align(projectionMatrix, viewMatrixInverse, modelMatrix)

Loading…
Peruuta
Tallenna