浏览代码

Update plugins: add pointer-based input

customisations
alemart 10 个月前
父节点
当前提交
4647feeac5
共有 3 个文件被更改,包括 123 次插入6 次删除
  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 查看文件

39
         return null;
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
     // data;
65
     // data;
53
     // schema;
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
     _utils: ARUtils(),
72
     _utils: ARUtils(),
58
     _started: false,
73
     _started: false,
59
     _components: [],
74
     _components: [],
95
     {
110
     {
96
         const scene = this.el;
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
         // we take control of the rendering
121
         // we take control of the rendering
99
         scene.renderer.setAnimationLoop(null);
122
         scene.renderer.setAnimationLoop(null);
100
 
123
 
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
     }
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 查看文件

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
      * The root is a node that is automatically aligned to the physical scene.
96
      * The root is a node that is automatically aligned to the physical scene.
87
      * Objects of your virtual scene should be descendants of this node.
97
      * Objects of your virtual scene should be descendants of this node.
88
      * @returns {BABYLON.TransformNode}
98
      * @returns {BABYLON.TransformNode}
126
     {
136
     {
127
         this._session = null;
137
         this._session = null;
128
         this._frame = null;
138
         this._frame = null;
139
+        this._pointers = [];
129
         this._origin = null;
140
         this._origin = null;
130
         this._root = null;
141
         this._root = null;
131
         this._scene = null;
142
         this._scene = null;
162
 
173
 
163
     function mix(frame)
174
     function mix(frame)
164
     {
175
     {
176
+        let found = false;
177
+        ar._pointers.length = 0;
178
+
165
         for(const result of frame.results) {
179
         for(const result of frame.results) {
166
             if(result.tracker.type == 'image-tracker') {
180
             if(result.tracker.type == 'image-tracker') {
167
                 if(result.trackables.length > 0) {
181
                 if(result.trackables.length > 0) {
173
                     align(projectionMatrix, viewMatrix, modelMatrix);
187
                     align(projectionMatrix, viewMatrix, modelMatrix);
174
                     ar._origin.setEnabled(true);
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
     function align(projectionMatrix, viewMatrix, modelMatrix)
203
     function align(projectionMatrix, viewMatrix, modelMatrix)

+ 21
- 2
plugins/three-with-encantar.js 查看文件

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
      * The root is a node that is automatically aligned to the physical scene.
96
      * The root is a node that is automatically aligned to the physical scene.
87
      * Objects of your virtual scene should be descendants of this node.
97
      * Objects of your virtual scene should be descendants of this node.
88
      * @returns {THREE.Group}
98
      * @returns {THREE.Group}
126
     {
136
     {
127
         this._session = null;
137
         this._session = null;
128
         this._frame = null;
138
         this._frame = null;
139
+        this._pointers = [];
129
         this._origin = null;
140
         this._origin = null;
130
         this._root = null;
141
         this._root = null;
131
         this._scene = null;
142
         this._scene = null;
156
 
167
 
157
     function mix(frame)
168
     function mix(frame)
158
     {
169
     {
170
+        let found = false;
171
+        ar._pointers.length = 0;
172
+
159
         for(const result of frame.results) {
173
         for(const result of frame.results) {
160
             if(result.tracker.type == 'image-tracker') {
174
             if(result.tracker.type == 'image-tracker') {
161
                 if(result.trackables.length > 0) {
175
                 if(result.trackables.length > 0) {
167
                     align(projectionMatrix, viewMatrixInverse, modelMatrix);
181
                     align(projectionMatrix, viewMatrixInverse, modelMatrix);
168
                     ar._origin.visible = true;
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
     function align(projectionMatrix, viewMatrixInverse, modelMatrix)
197
     function align(projectionMatrix, viewMatrixInverse, modelMatrix)

正在加载...
取消
保存