-
Notifications
You must be signed in to change notification settings - Fork 1.7k
WebXR Image Tracking #2574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
willeastcott
merged 11 commits into
playcanvas:master
from
Maksims:webxr-image-tracking
Dec 30, 2020
Merged
WebXR Image Tracking #2574
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
abff53e
WebXR Image Tracking
Maksims 41a111e
remove unecessary event handler from image tracking
Maksims 9d33919
events docs
Maksims b400b07
Merge branch 'master' into webxr-image-tracking
willeastcott dc1f9ca
Add missing closing brace
willeastcott b00ec09
Edit docs
willeastcott 68ac9ed
Edit docs
willeastcott a96e3f5
Spelling mistake
willeastcott 0f6d7fa
merge
Maksims 5bb01bd
merge
Maksims f4f9b2c
improve docs
Maksims File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| import { EventHandler } from '../core/event-handler.js'; | ||
| import { XrTrackedImage } from './xr-tracked-image.js'; | ||
|
|
||
| /** | ||
| * @class | ||
| * @name pc.XrImageTracking | ||
| * @classdesc Image Tracking provides the ability to track real world images by provided image samples and their estimate sizes. | ||
| * @description Image Tracking provides the ability to track real world images by provided image samples and their estimate sizes. | ||
| * @param {pc.XrManager} manager - WebXR Manager. | ||
| * @property {boolean} supported True if Image Tracking is supported. | ||
| * @property {boolean} available True if Image Tracking is available. This property will be false if no images were provided for the AR session or there was an error processing the provided images. | ||
| * @property {pc.XrTrackedImage[]} images List of {@link pc.XrTrackedImage} that contain tracking information. | ||
| */ | ||
| function XrImageTracking(manager) { | ||
| EventHandler.call(this); | ||
|
|
||
| this._manager = manager; | ||
| this._supported = !! window.XRImageTrackingResult; | ||
| this._available = false; | ||
|
|
||
| this._images = []; | ||
|
|
||
| if (this._supported) { | ||
| this._manager.on('start', this._onSessionStart, this); | ||
| this._manager.on('end', this._onSessionEnd, this); | ||
| } | ||
| } | ||
| XrImageTracking.prototype = Object.create(EventHandler.prototype); | ||
| XrImageTracking.prototype.constructor = XrImageTracking; | ||
|
|
||
| /** | ||
| * @event | ||
| * @name pc.XrImageTracking#error | ||
| * @param {Error} error - Error object related to a failure of image tracking. | ||
| * @description Fired when the XR session is started, but image tracking failed to process the provided images. | ||
| */ | ||
|
|
||
| /** | ||
| * @function | ||
| * @name pc.XrImageTracking#add | ||
| * @description Add an image for image tracking. A width can also be provided to help the underlying system estimate the appropriate transformation. Modifying the tracked images list is only possible before an AR session is started. | ||
| * @param {object} image - Image that is matching real world image as close as possible. Resolution of images should be at least 300x300. High resolution does NOT improve tracking performance. Color of image is irelevant, so greyscale images can be used. Images with too many geometric features or repeating patterns will reduce tracking stability. | ||
| * @param {number} width - Width (in meters) of image in the real world. Providing this value as close to the real value will improve tracking quality. | ||
| * @returns {pc.XrTrackedImage} Tracked image object that will contain tracking information. | ||
| * @example | ||
| * // image with width of 20cm (0.2m) | ||
| * app.xr.imageTracking.add(bookCoverImg, 0.2); | ||
| */ | ||
| XrImageTracking.prototype.add = function (image, width) { | ||
| if (! this._supported || this._manager.active) return null; | ||
|
|
||
| var trackedImage = new XrTrackedImage(image, width); | ||
| this._images.push(trackedImage); | ||
| return trackedImage; | ||
| }; | ||
|
|
||
| /** | ||
| * @function | ||
| * @name pc.XrImageTracking#remove | ||
| * @description Remove an image from image tracking. | ||
| * @param {pc.XrTrackedImage} trackedImage - Tracked image to be removed. Modifying the tracked images list is only possible before an AR session is started. | ||
| */ | ||
| XrImageTracking.prototype.remove = function (trackedImage) { | ||
| if (this._manager.active) return; | ||
|
|
||
| var ind = this._images.indexOf(trackedImage); | ||
| if (ind !== -1) { | ||
| trackedImage.destroy(); | ||
| this._images.splice(ind, 1); | ||
| } | ||
| }; | ||
|
|
||
| XrImageTracking.prototype._onSessionStart = function () { | ||
| var self = this; | ||
|
|
||
| this._manager.session.getTrackedImageScores().then(function (images) { | ||
| self._available = true; | ||
|
|
||
| for (var i = 0; i < images.length; i++) { | ||
| self._images[i]._trackable = images[i] === 'trackable'; | ||
| } | ||
| }).catch(function (err) { | ||
| self._available = false; | ||
| self.fire('error', err); | ||
| }); | ||
| }; | ||
|
|
||
| XrImageTracking.prototype._onSessionEnd = function () { | ||
| this._available = false; | ||
|
|
||
| for (var i = 0; i < this._images.length; i++) { | ||
| this._images[i]._pose = null; | ||
| this._images[i]._measuredWidth = 0; | ||
|
|
||
| if (this._images[i]._tracking) { | ||
| this._images[i]._tracking = false; | ||
| this._images[i].fire('untracked'); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| XrImageTracking.prototype.prepareImages = function (callback) { | ||
| if (this._images.length) { | ||
| Promise.all(this._images.map(function (trackedImage) { | ||
| return trackedImage.prepare(); | ||
| })).then(function (bitmaps) { | ||
| callback(null, bitmaps); | ||
| }).catch(function (err) { | ||
| callback(err, null); | ||
| }); | ||
| } else { | ||
| callback(null, null); | ||
| } | ||
| }; | ||
|
|
||
| XrImageTracking.prototype.update = function (frame) { | ||
| if (! this._available) return; | ||
|
|
||
| var results = frame.getImageTrackingResults(); | ||
| var index = { }; | ||
| var i; | ||
|
|
||
| for (i = 0; i < results.length; i++) { | ||
| index[results[i].index] = results[i]; | ||
|
|
||
| var trackedImage = this._images[results[i].index]; | ||
| trackedImage._emulated = results[i].trackingState === 'emulated'; | ||
| trackedImage._measuredWidth = results[i].measuredWidthInMeters; | ||
| trackedImage._dirtyTransform = true; | ||
| trackedImage._pose = frame.getPose(results[i].imageSpace, this._manager._referenceSpace); | ||
| } | ||
|
|
||
| for (i = 0; i < this._images.length; i++) { | ||
| if (this._images[i]._tracking && ! index[i]) { | ||
| this._images[i]._tracking = false; | ||
| this._images[i].fire('untracked'); | ||
| } else if (! this._images[i]._tracking && index[i]) { | ||
| this._images[i]._tracking = true; | ||
| this._images[i].fire('tracked'); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| Object.defineProperty(XrImageTracking.prototype, 'supported', { | ||
| get: function () { | ||
| return this._supported; | ||
| } | ||
| }); | ||
|
|
||
| Object.defineProperty(XrImageTracking.prototype, 'available', { | ||
| get: function () { | ||
| return this._available; | ||
| } | ||
| }); | ||
|
|
||
| Object.defineProperty(XrImageTracking.prototype, 'images', { | ||
| get: function () { | ||
| return this._images; | ||
| } | ||
| }); | ||
|
|
||
| export { XrImageTracking }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.