複数ターゲットの認識
このサンプルは、複数の画像を同時に認識してトラッキングする方法を示します。さらに、トラッキングされた画像間の距離、平行移動、回転の使い方を示します。
より理解を深めるために、画像認識と3Dモデルの拡張のトピックスを理解しておくことをお勧めします。
サンプルを確認するには、このページにある画像を使用してください。画像を印刷してさまざまなカードを切り取れば恐竜の相互作用を見ることができます。
var targetCollectionResource = new AR.TargetCollectionResource("assets/dinosaurs.wtc");
var tracker = new AR.ImageTracker(targetCollectionResource, {
// To enable simultaneous tracking of multiple targets 'maximumNumberOfConcurrentlyTrackableTargets' has to be set.
maximumNumberOfConcurrentlyTrackableTargets: 5, // A maximum of five images can be concurrently tracked.
//Disables extended range recognition. The reason for this is that extended range recognition requires more processing power and with multiple targets the SDK is trying to recognize targets until the maximumNumberOfConcurrentlyTrackableTargets is reached and it may slow down the tracking of already recognized targets.
extendedRangeRecognition: AR.CONST.IMAGE_RECOGNITION_RANGE_EXTENSION.OFF,
...
});
Drawables
を特定のImageTargets
に追加するために、ImageTrackable.addImageTargetCamDrawables
を使用できます。
new AR.ImageTrackable(tracker, "*", {
onImageRecognized: function (target) {
var model = new AR.Model("assets/models/" + target.name + ".wt3");
// Adds the model as augmentation for the currently recognized target.
this.addImageTargetCamDrawables(target, model);
}
...
});
ターゲット間の距離
このサンプルは、ターゲット間の距離を使用する方法を示します。
ターゲット間の距離を取得するには以下の2種類の方法のいずれかを使用します。
ImageTarget.getDistanceTo
:ImageTarget
から渡されたImageTarget
までの距離を返します。ImageTarget.onDistanceChanged
:ImageTarget
から現在トラッキングされている他のImageTarget
までの距離がしきい値を超えて変化されるたびにトリガされます。しきい値は、ImageTracker
(onDistanceChangedThreshold)のオプションで指定します。パラメータには、距離とこの距離が測定されたImageTarget
が含まれます。
var tracker = new AR.ImageTracker(targetCollectionResource, {
maximumNumberOfConcurrentlyTrackableTargets: 5,
onDistanceChangedThreshold: 10 // By setting this to 10, the distance between two targets has to change by 10mm before ImageTarget.onDistanceChanged is triggered again.
...
});
new AR.ImageTrackable(tracker, "*", {
onImageRecognized: function (target) {
var model = new AR.Model("assets/models/" + target.name + ".wt3");
var idleAnimation = new AR.ModelAnimation(model, "Idle");
idleAnimation.onFinish = idleAnimation.start;
idleAnimation.start();
this.addImageTargetCamDrawables(target, model);
var jumpAnimation = new AR.ModelAnimation(model, "Jump");
jumpAnimation.onFinish = jumpAnimation.start;
// Trigger callback whenever the distance between the currently recognized target and another target change by more than the threshold defined in the AR.ImageTracker.
target.onDistanceChanged = function (distance, otherTarget) {
// start the jumpAnimation when the distance between this and another target gets below 150 mm
if (distance < 150) {
jumpAnimation.onFinish = jumpAnimation.start;
idleAnimation.onFinish = function() {
jumpAnimation.start();
};
}
}
}
});
ターゲット間の変換
このサンプルは、ターゲット間の平行移動と回転を使用する方法を示します。
ターゲット間の平行移動を取得するには以下の2種類の方法のいずれかを使用します。
ImageTarget.getTranslationTo
:ImageTarget
から渡されたImageTarget
までの平行移動を返します。ImageTarget.onTranslationChanged
:ImageTarget
から現在トラッキングされている他のImageTarget
までの平行移動がImageTracker
(onTranslationChangedThreshold)のオプションで指定したしきい値を超えて変化されるたびにトリガされます。パラメータには、平行移動とこの平行移動が測定されたImageTarget
が含まれます。
ターゲット間の回転を取得するには以下の2種類の方法のいずれかを使用します。
ImageTarget.getRotationTo
:ImageTarget
から渡されたImageTarget
までの回転を返します。ImageTarget.onRotationChanged
:ImageTarget
から現在トラッキングされている他のImageTarget
までの回転がImageTracker
(onRotationChangedThreshold)のオプションで指定したしきい値を超えて変化されるたびにトリガされます。パラメータには、回転とこの回転が測定されたImageTarget
が含まれます。
メモ:変換と回転は、
onTranslation/-RotationChanged
コールバックがトリガーされたやgetTranslation/-Rotation
が呼び出されたImageTarget
の座標系に基づいています。つまり、imageTarget1からimageTarget2への平行移動は、imageTarget2からimageTarget1への平行移動と異なる場合があります。
var tracker = new AR.ImageTracker(targetCollectionResource, {
maximumNumberOfConcurrentlyTrackableTargets: 5,
onRotationThreshold: 10 // By setting this to 10, the distance between two targets has to change by 10mm before ImageTarget.onDistanceChanged is triggered again.
...
});
new AR.ImageTrackable(tracker, "*", {
onImageRecognized: function (target) {
var model = new AR.Model("assets/models/" + target.name + ".wt3");
var idleAnimation = new AR.ModelAnimation(model, "Idle");
idleAnimation.onFinish = idleAnimation.start;
idleAnimation.start();
this.addImageTargetCamDrawables(target, model);
// Trigger callback whenever the distance between the currently recognized target and another target change by more than the threshold defined in the AR.ImageTracker.
target.onRotationChanged = function (rotation, otherTarget) {
// If dinosaurs are facing each other and are within 150mm one should attack.
if (rotation.z > 170 && rotation.z < 190 && target.getDistanceTo(destinationTarget) < 150) {
... // attacking animations
}
}
}
});