Created
July 10, 2013 01:24
-
-
Save tboydar/5962773 to your computer and use it in GitHub Desktop.
unity ska
This file contains 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
using UnityEngine; | |
using System.Collections; | |
using Iisu; | |
using IisuUnity; | |
using System; | |
using System.Collections.Generic; | |
using SKA; | |
using SKA.Geometry; | |
public class ska_skelton : | |
MonoBehaviour | |
{ | |
public IisuInputProvider InputProvider; | |
public GameObject JointPrefab; | |
public Transform Pelvis; | |
public Transform Waist; | |
public Transform Collar; | |
public Transform Neck; | |
public Transform Head; | |
public Transform RightShoulder; | |
public Transform RightElbow; | |
public Transform RightWrist; | |
public Transform RightHand; | |
public Transform RightHip; | |
public Transform RightKnee; | |
public Transform RightAnkle; | |
public Transform RightFoot; | |
public Transform LeftShoulder; | |
public Transform LeftElbow; | |
public Transform LeftWrist; | |
public Transform LeftHand; | |
public Transform LeftHip; | |
public Transform LeftKnee; | |
public Transform LeftAnkle; | |
public Transform LeftFoot; | |
private Vector3[] positions; | |
private float[] confidence; | |
Frame frame ; | |
CoordinateSystem sourceCoordinateSystem; | |
CoordinateSystem targetCoordinateSystem; | |
Retargeter retargeter; | |
SettingsUserJoint settingsJoints; | |
SettingsUserFrame settingsTargetFrame; | |
ICharacter character; | |
//Defines a map between Retargeting joint and the joint | |
//contained in the user rig(UserRetargetingRig.dae, see below) | |
JointMapper jointMapper; | |
// Use this for initialization | |
private Transform[] allJoints; | |
private Transform[] allJoints2; | |
public void Awake() { | |
allJoints = new Transform[21]; | |
allJoints2= new Transform[21]; | |
int index = 0; | |
Debug.Log("in Awake()"); | |
allJoints[0] = Pelvis; | |
allJoints[1] = Waist; | |
allJoints[2] = Collar; | |
allJoints[3] = Neck; | |
allJoints[4] = Head; | |
allJoints[5] = LeftShoulder; | |
allJoints[6] = LeftElbow; | |
allJoints[7] = LeftWrist; | |
allJoints[8] = LeftHand; | |
allJoints[9] = RightShoulder; | |
allJoints[10] = RightElbow; | |
allJoints[11] = RightWrist; | |
allJoints[12] = RightHand; | |
allJoints[13] = LeftKnee; | |
allJoints[14] = LeftHip; | |
allJoints[15] = LeftAnkle; | |
allJoints[16] = LeftFoot; | |
allJoints[17] = RightHip; | |
allJoints[18] = RightKnee; | |
allJoints[19] = RightAnkle; | |
allJoints[20] = RightFoot; | |
for (int i=0; i<allJoints.Length; ++i) | |
{ | |
GameObject tempobj = ((GameObject)Instantiate(JointPrefab, new Vector3(0, -10, 0), JointPrefab.transform.rotation)); | |
//tempobj.transform.localScale = Vector3.one * 0.05f; | |
//tempobj.transform.localScale = Vector3.one * 0.15f; | |
//tempobj.transform.localScale = Vector3.one *0.15; | |
tempobj.name = "ska_"+i; | |
allJoints2[i] = tempobj.transform; | |
allJoints2[i].parent = transform; | |
// allJoints[i].rotation = UnityEngine.Quaternion.identity; | |
} | |
Debug.Log("index:"+index); | |
try | |
{ | |
// Console.WriteLine("Softkinetic - Retargeting Sample"); | |
// Console.WriteLine("Starting Retargeting process using Iisu skeleton data"); | |
//UnityEngine.Quaternion unityQuaternion = new UnityEngine.Quaternion(skaQuaternion.x, skaQuaternion.y, skaQuaternion.z, skaQuaternion.w); | |
//SKA.Geometry.Quaternion skaQuaternion = new SKA.Geometry.Quaternion(); | |
//UnityEngine.Quaternion unityQuaternion = new UnityEngine.Quaternion(skaQuaternion.x, skaQuaternion.y, skaQuaternion.z, skaQuaternion.w); | |
frame = new Frame(new SKA.Geometry.Quaternion(0.0f, 0.0f, 0.0f, 1.0f), new Position()); | |
//Instantiates the Retargeting Engine | |
sourceCoordinateSystem = new CoordinateSystem(DistanceUnit.SKA_METERS, Handedness.SKA_RIGHT_HANDED, frame); | |
targetCoordinateSystem = new CoordinateSystem(DistanceUnit.SKA_METERS, Handedness.SKA_RIGHT_HANDED, frame); | |
retargeter = new Retargeter(sourceCoordinateSystem, targetCoordinateSystem); | |
//Register to logging event. | |
Retargeter.OnLoggingDelegate OnLogEvent = new Retargeter.OnLoggingDelegate(Utility.OnLogEvent); | |
retargeter.registerLogEventListener(OnLogEvent); | |
//Defines a map between Retargeting joint and the joint | |
//contained in the user rig(UserRetargetingRig.dae, see below) | |
JointMapper jointMapper = new JointMapper(); | |
jointMapper[Keypoint.SKA_PELVIS]="Pelvis"; | |
jointMapper[Keypoint.SKA_SPINE_1]="Spine1"; | |
jointMapper[Keypoint.SKA_SPINE_2] = "Spine2"; | |
jointMapper[Keypoint.SKA_HEAD]="Head"; | |
jointMapper[Keypoint.SKA_NECK]="Neck"; | |
jointMapper[Keypoint.SKA_RIGHT_SHOULDER]="RightShoulder"; | |
jointMapper[Keypoint.SKA_RIGHT_ELBOW]="RightElbow"; | |
jointMapper[Keypoint.SKA_RIGHT_WRIST]="RightWrist"; | |
jointMapper[Keypoint.SKA_RIGHT_FINGERS]="RightFingers"; | |
jointMapper[Keypoint.SKA_RIGHT_HIP]="RightHip"; | |
jointMapper[Keypoint.SKA_RIGHT_KNEE]="RightKnee"; | |
jointMapper[Keypoint.SKA_RIGHT_ANKLE]="RightAnkle"; | |
jointMapper[Keypoint.SKA_RIGHT_TOES]="RightToes"; | |
jointMapper[Keypoint.SKA_LEFT_SHOULDER]="LeftShoulder"; | |
jointMapper[Keypoint.SKA_LEFT_ELBOW]="LeftElbow"; | |
jointMapper[Keypoint.SKA_LEFT_WRIST]="LeftWrist"; | |
jointMapper[Keypoint.SKA_LEFT_FINGERS]="LeftFingers"; | |
jointMapper[Keypoint.SKA_LEFT_HIP]="LeftHip"; | |
jointMapper[Keypoint.SKA_LEFT_KNEE]="LeftKnee"; | |
jointMapper[Keypoint.SKA_LEFT_ANKLE]="LeftAnkle"; | |
jointMapper[Keypoint.SKA_LEFT_TOES]="LeftToes"; | |
//Creates a user character. | |
settingsJoints = new SettingsUserJoint("Pelvis", jointMapper); | |
settingsTargetFrame = new SettingsUserFrame(CanonicAxis.SKA_Y_REVERSE_AXIS, new Position()); | |
character = retargeter.createCharacter("", settingsJoints, settingsTargetFrame); | |
//Debug.Log("Retargeting results: "); | |
character.retarget(); | |
String[] jointNames = character.targetJointNamesOrder; | |
int index2 = 0; | |
foreach (ScaledFrame f in character.targetFrames) | |
{ | |
Debug.Log(jointNames[index2]); | |
if ( allJoints[index2]!=null) | |
allJoints[index2].position = new Vector3(f.position.x, f.position.y, f.position.z); | |
//Debug.Log("Joint Name : {0}"+ jointNames[index]); | |
// Debug.Log("Position : [x={0} , y={1} , z={2} ]"+ f.position.x+"_"+ f.position.y+"_"+ f.position.z); | |
//Debug.Log("Orientation : [x={0} , y={1} , z={2} ,w={3} ]"+ f.quaternion.x+"_"+ f.quaternion.y+"_"+ f.quaternion.z+"_"+ f.quaternion.w); | |
//Debug.Log("Scale : {0}"+ f.scale); | |
++index2; | |
} | |
Debug.Log("index:"+index); | |
//Releases the acquired frame from iisu | |
// iisu.releaseFrame(); | |
} | |
catch (System.Exception e) | |
{ | |
Debug.Log(e); | |
} | |
} | |
void Start () | |
{ | |
} | |
// Update is called once per frame | |
void Update () | |
{ | |
positions = InputProvider.SkeletonPositions;//_skeleton | |
confidence = InputProvider.SkeletonConfidence; | |
try { | |
SourcePose sourcePose=Utility.Converter( positions ); | |
if (!sourcePose.IsComplete) | |
{ | |
Debug.Log("{0} are missing in the sourcePose object !"+ sourcePose.CountMissingKeypoints); | |
return; | |
} | |
character.setSourceKeypoints(sourcePose.toArray()); | |
character.retarget(); | |
String[] jointNames = character.targetJointNamesOrder; | |
int index2 = 0; | |
foreach (ScaledFrame f in character.targetFrames) | |
{ | |
// Debug.Log(jointNames[index2]); | |
if ( allJoints[index2]!=null) { | |
allJoints[index2].position = new Vector3(f.position.x, f.position.y, f.position.z); | |
//UnityEngine.Quaternion unityQuaternion = new UnityEngine.Quaternion(skaQuaternion.x, skaQuaternion.y, skaQuaternion.z, skaQuaternion.w); | |
if (index2 >= 9 && index2 <=12) | |
allJoints[index2].rotation = new UnityEngine.Quaternion(f.quaternion.x, f.quaternion.y, f.quaternion.z, f.quaternion.w); | |
} | |
//Debug.Log("Orientation : [x={0} , y={1} , z={2} ,w={3} ]"+ f.quaternion.x+"_"+ f.quaternion.y+"_"+ f.quaternion.z+"_"+ f.quaternion.w); | |
allJoints2[index2].position = new Vector3(f.position.x, f.position.y, f.position.z); | |
allJoints2[index2].rotation = new UnityEngine.Quaternion(f.quaternion.x, f.quaternion.y, f.quaternion.z, f.quaternion.w); | |
//Debug.Log("Joint Name : {0}"+ jointNames[index]); | |
// Debug.Log("Position : [x={0} , y={1} , z={2} ]"+ f.position.x+"_"+ f.position.y+"_"+ f.position.z); | |
//Debug.Log("Orientation : [x={0} , y={1} , z={2} ,w={3} ]"+ f.quaternion.x+"_"+ f.quaternion.y+"_"+ f.quaternion.z+"_"+ f.quaternion.w); | |
//Debug.Log("Scale : {0}"+ f.scale); | |
++index2; | |
} | |
} | |
catch (System.Exception e) | |
{ | |
Debug.Log(e); | |
} | |
} | |
} | |
public class Utility | |
{ | |
/// <summary> | |
/// Helper used to convert the Iisu keypoint structure to | |
/// RetargetingNet keypoints structure. | |
/// </summary> | |
/// <param name="keypoints">iisu keypoint</param> | |
/// <returns>RetargetingNet SourcePose</returns> | |
public static SourcePose Converter(Vector3[] keypoints ) | |
{ | |
if (keypoints.Length != SourcePose.DefaultLength) | |
throw new System.Exception("Iisu should have" + SourcePose.DefaultLength + "keypoints (actual" + keypoints.Length + " )"); | |
SourcePose sourcePose=new SourcePose(); | |
int index = 0; | |
foreach (Vector3 keypoint in keypoints) | |
{ | |
Position position=new Position(keypoint.x, keypoint.y, keypoint.z); | |
sourcePose[index] = position; | |
++index; | |
} | |
return sourcePose; | |
} | |
public static void OnLogEvent(String message) | |
{ | |
Debug.Log("Retargeting Log Event : " + message); | |
} | |
public static float degreeToRadian(float val) | |
{ | |
return (float)(Math.PI / 180) * val; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment