本トピックスは、一部の Humanoid モデル(Standard Assets の "Ethan" など)には対応していません。(2017/08/25)
UnityChan には対応しました。こちらをご覧下さい。(2017/09/09)
目次へ

File -> New Scene
File -> Save Scene as ... -> Sample02.unity
Projectウィンドウのアセットを右クリック -> Create -> Folder -> 名前を"Models"に変更する
ここでは 「 MakeHuman: Unity5 で使用する人型モデルを作成する 」 で作成したデータ makehuman2.zip を用います。
上記のzipファイルを展開すると、exports/の下に AsianBoy.fbx, AsianGirl.fbx, AfricanBoy.fbx, AfricanGirl.fbx, CaucasianBoy.fbx, CaucasianGirl.fbx と textures/ があるはずです。 これを Assets/Models/ の下にimportします。

[注意]上の操作は
Assets -> Import New Asset... -> AsianBody.fbxから行なってもよいのですが、これだとAsianBoy.fbx に必要なtextureが 自動ではimportされず、モデルが真っ白になってしまいます。 この場合は Assets/Models/Materials/に生成された白いMaterialに対応する Textureを手動でimportしなくてはいけません。

図では AsianBoy.fbx をHumanoid に変換する例を示しています。 6個のfbx全てについて Humanoid に変換する作業を行って下さい。




上部のメニューから「Assets」-> 「Create」 -> 「C# Script」 -> ファイル名は Sample02
| Sample02.cs |
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sample02 : MonoBehaviour {
public GameObject humanoid;
public bool mirror = true;
public bool move = true;
NtUnity.Kinect nt;
NtUnity.HumanoidSkeleton hs;
void Start () {
nt = new NtUnity.Kinect();
hs = new NtUnity.HumanoidSkeleton(humanoid);
}
void Update () {
nt.setRGB();
nt.setSkeleton();
nt.setFace();
nt.imshowBlack();
int n = nt.getSkeleton();
if (n > 0) {
hs.set(nt,0,mirror,move);
}
}
void OnApplicationQuit() {
nt.stopKinect();
}
}
|


人間の顔の向きがHumanoidの顔の向きに反映されます。
[注意] 骨格の認識状態を表示するためにDLL内でOpenCVのウィンドウを生成しています。 後から生成されたこのウィンドウにフォーカスがあるときは (= Unityのウィンドウにフォーカスがない場合は) Unityの画面は変化しないので注意して下さい。 Unityのウィンドウの上部をクリックしてUnityのウィンドウにフォーカスが ある状態で動作を試して下さい。
Runtime Video UnityDLL02.mp4

上記のzipファイルには必ずしも最新の "NtKinectDLL.dll" と "NtUnity.cs" が含まれていない場合があるので、 こちらから最新版をダウンロードして 差し替えてお使い下さい。
6人までの Humanoid を同時に動かすプロジェクトを作成します。
File -> Save Scence as -> Sample02b.unity
Hierarchy には Humanoid が合計で 6 体存在するようになりました。 それぞれの Position や Rotation をそれぞれ次のように設定します。 Position の Z 座標を -10 として、骨格が認識されていないときは Main Camera に写らなくしているだけです。
| Model Name | Position | Rotation | ||||
|---|---|---|---|---|---|---|
| X | Y | Z | X | Y | Z | |
| AsianBoy | 0 | 0 | -10 | 0 | 0 | 0 |
| AsianGirl | 0 | 0 | -10 | 0 | 0 | 0 |
| AfricanBoy | 0 | 0 | -10 | 0 | 0 | 0 |
| AfricanGirl | 0 | 0 | -10 | 0 | 0 | 0 |
| CaucasianBoy | 0 | 0 | -10 | 0 | 0 | 0 |
| CaucasianGirl | 0 | 0 | -10 | 0 | 0 | 0 |

| Sample02b.cs |
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sample02b : MonoBehaviour {
public GameObject[] humanoid = new GameObject[] {null, null, null, null, null, null};
public bool[] mirror = new bool[] {true, true, true, true, true, true};
public bool[] move = new bool[] {true, true, true, true, true, true};
NtUnity.Kinect nt;
NtUnity.HumanoidSkeleton[] hs = new NtUnity.HumanoidSkeleton[] {null, null, null, null, null, null};
void Start () {
nt = new NtUnity.Kinect();
for (int i=0; i< humanoid.Length; i++) {
if (humanoid[i] != null) {
hs[i] = new NtUnity.HumanoidSkeleton(humanoid[i]);
}
}
}
void Update () {
nt.setRGB();
nt.setSkeleton();
nt.setFace();
nt.imshowBlack();
for (int i=0; i<nt.skeleton.Count; i++) {
if (hs[i] != null) {
hs[i].set(nt,i,mirror[i],move[i]);
} else {
humanoid[i].transform.position = new Vector3(0,0,-10);
}
}
}
void OnApplicationQuit() {
nt.stopKinect();
}
}
|



File -> Save Scenes

上記のzipファイルには必ずしも最新の "NtKinectDLL.dll" と "NtUnity.cs" が含まれていない場合があるので、 こちらから最新版をダウンロードして 差し替えてお使い下さい。