├── .gitignore ├── Assets ├── FreeTrackInput.meta ├── FreeTrackInput │ ├── Assets.meta │ └── Assets │ │ ├── Materials.meta │ │ ├── Materials │ │ ├── cube.mat │ │ ├── cube.mat.meta │ │ ├── texture.mat │ │ └── texture.mat.meta │ │ ├── grid.jpg │ │ └── grid.jpg.meta ├── MainScene.unity ├── MainScene.unity.meta ├── Scripts.meta └── Scripts │ ├── FaceTrackMovment.cs │ ├── FaceTrackMovment.cs.meta │ ├── UDPReceive.cs │ └── UDPReceive.cs.meta ├── ProjectSettings ├── AudioManager.asset ├── ClusterInputManager.asset ├── DynamicsManager.asset ├── EditorBuildSettings.asset ├── EditorSettings.asset ├── GraphicsSettings.asset ├── InputManager.asset ├── NavMeshAreas.asset ├── NetworkManager.asset ├── Physics2DSettings.asset ├── ProjectSettings.asset ├── ProjectVersion.txt ├── QualitySettings.asset ├── TagManager.asset ├── TimeManager.asset ├── UnityAdsSettings.asset └── UnityConnectSettings.asset └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | Library 2 | Temp 3 | *.csproj 4 | *.sln 5 | *.pidb 6 | *.userprefs 7 | *.suo -------------------------------------------------------------------------------- /Assets/FreeTrackInput.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: fcaf9227bb157584aa3b231225efe3f1 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /Assets/FreeTrackInput/Assets.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 88380c86186f1a2458cb7ab5fb5b4a8f 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /Assets/FreeTrackInput/Assets/Materials.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: cb0022176a3244a488ee38e6c40e05d8 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /Assets/FreeTrackInput/Assets/Materials/cube.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcteys/unityFaceTracking/6c0a268db52874a969b390386a85fa3b0e7f8f23/Assets/FreeTrackInput/Assets/Materials/cube.mat -------------------------------------------------------------------------------- /Assets/FreeTrackInput/Assets/Materials/cube.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2e2c8846c4a76a941ba4035efe186eae 3 | NativeFormatImporter: 4 | userData: 5 | assetBundleName: 6 | assetBundleVariant: 7 | -------------------------------------------------------------------------------- /Assets/FreeTrackInput/Assets/Materials/texture.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcteys/unityFaceTracking/6c0a268db52874a969b390386a85fa3b0e7f8f23/Assets/FreeTrackInput/Assets/Materials/texture.mat -------------------------------------------------------------------------------- /Assets/FreeTrackInput/Assets/Materials/texture.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9826498b6c4ece04cba53adba1254691 3 | NativeFormatImporter: 4 | userData: 5 | assetBundleName: 6 | assetBundleVariant: 7 | -------------------------------------------------------------------------------- /Assets/FreeTrackInput/Assets/grid.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcteys/unityFaceTracking/6c0a268db52874a969b390386a85fa3b0e7f8f23/Assets/FreeTrackInput/Assets/grid.jpg -------------------------------------------------------------------------------- /Assets/FreeTrackInput/Assets/grid.jpg.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d91a59c7d7969bc4fb848505686ee308 3 | TextureImporter: 4 | fileIDToRecycleName: {} 5 | serializedVersion: 2 6 | mipmaps: 7 | mipMapMode: 0 8 | enableMipMap: 1 9 | linearTexture: 0 10 | correctGamma: 0 11 | fadeOut: 0 12 | borderMipMap: 0 13 | mipMapFadeDistanceStart: 1 14 | mipMapFadeDistanceEnd: 3 15 | bumpmap: 16 | convertToNormalMap: 0 17 | externalNormalMap: 0 18 | heightScale: 0.25 19 | normalMapFilter: 0 20 | isReadable: 0 21 | grayScaleToAlpha: 0 22 | generateCubemap: 0 23 | cubemapConvolution: 0 24 | cubemapConvolutionSteps: 7 25 | cubemapConvolutionExponent: 1.5 26 | seamlessCubemap: 0 27 | textureFormat: -1 28 | maxTextureSize: 1024 29 | textureSettings: 30 | filterMode: -1 31 | aniso: 2 32 | mipBias: -1 33 | wrapMode: -1 34 | nPOTScale: 1 35 | lightmap: 0 36 | rGBM: 0 37 | compressionQuality: 50 38 | allowsAlphaSplitting: 0 39 | spriteMode: 0 40 | spriteExtrude: 1 41 | spriteMeshType: 1 42 | alignment: 0 43 | spritePivot: {x: 0.5, y: 0.5} 44 | spriteBorder: {x: 0, y: 0, z: 0, w: 0} 45 | spritePixelsToUnits: 100 46 | alphaIsTransparency: 0 47 | textureType: -1 48 | buildTargetSettings: [] 49 | spriteSheet: 50 | sprites: [] 51 | outline: [] 52 | spritePackingTag: 53 | userData: 54 | assetBundleName: 55 | assetBundleVariant: 56 | -------------------------------------------------------------------------------- /Assets/MainScene.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcteys/unityFaceTracking/6c0a268db52874a969b390386a85fa3b0e7f8f23/Assets/MainScene.unity -------------------------------------------------------------------------------- /Assets/MainScene.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 993d7772d3da04e4eb8e031e3f525889 3 | DefaultImporter: 4 | userData: 5 | assetBundleName: 6 | assetBundleVariant: 7 | -------------------------------------------------------------------------------- /Assets/Scripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 34ea96c0ef365404fbc3cf396f519677 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /Assets/Scripts/FaceTrackMovment.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class FaceTrackMovment : MonoBehaviour { 5 | 6 | [SerializeField] 7 | UDPReceive receiver = null; 8 | 9 | Transform startPos = null; 10 | 11 | [SerializeField] 12 | float reductionPositionFactor = 1; 13 | 14 | [SerializeField] 15 | float reductionRotationFactor = 1; 16 | 17 | 18 | void Start () 19 | { 20 | startPos = transform; 21 | } 22 | 23 | void Update () 24 | { 25 | transform.position = new Vector3(receiver.xPos * reductionPositionFactor, receiver.yPos * reductionPositionFactor, -receiver.zPos * reductionPositionFactor ); 26 | transform.rotation = Quaternion.Euler(-receiver.pitch * reductionRotationFactor, -receiver.yaw * reductionRotationFactor, receiver.roll * reductionRotationFactor); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Assets/Scripts/FaceTrackMovment.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3991a7d9f4c67524cbc57cbf932a2446 3 | timeCreated: 1452016387 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/Scripts/UDPReceive.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | using System; 5 | using System.Text; 6 | using System.Net; 7 | using System.Net.Sockets; 8 | using System.Threading; 9 | 10 | public class UDPReceive: MonoBehaviour { 11 | 12 | // receiving Thread 13 | Thread receiveThread; 14 | 15 | // udpclient object 16 | UdpClient client; 17 | 18 | 19 | public int port; // define > init 20 | 21 | // infos 22 | public string lastReceivedUDPPacket = ""; 23 | public string allReceivedUDPPackets = ""; // clean up this from time to time! 24 | 25 | // FaceAPI 26 | public float xPos; 27 | public float yPos; 28 | public float zPos; 29 | public float pitch; 30 | public float yaw; 31 | public float roll; 32 | 33 | // start from shell 34 | private static void Main() { 35 | 36 | UDPReceive receiveObj = new UDPReceive(); 37 | receiveObj.init(); 38 | string text = ""; 39 | 40 | do { 41 | text = Console.ReadLine(); 42 | } 43 | while (!text.Equals("exit")); 44 | } 45 | 46 | // start from unity3d 47 | public void Start() { 48 | init(); 49 | } 50 | 51 | public string getLastPacket() { 52 | return lastReceivedUDPPacket; 53 | } 54 | 55 | 56 | // init 57 | private void init() { 58 | 59 | xPos = 0; 60 | yPos = 0; 61 | zPos = 0; 62 | pitch = 0; 63 | yaw = 0; 64 | roll = 0; 65 | 66 | 67 | print("UDPSend.init()"); 68 | // define port 69 | port = 5550; 70 | 71 | // status 72 | print("Sending to 127.0.0.1 : " + port); 73 | print("Test-Sending to this Port: nc -u 127.0.0.1 " + port + ""); 74 | 75 | 76 | receiveThread = new Thread( 77 | new ThreadStart(ReceiveData)); 78 | receiveThread.IsBackground = true; 79 | receiveThread.Start(); 80 | } 81 | 82 | // receive thread 83 | private void ReceiveData() { 84 | 85 | client = new UdpClient(port); 86 | while (true) { 87 | try { 88 | 89 | IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0); 90 | byte[] data = client.Receive(ref anyIP); 91 | 92 | 93 | 94 | for (int i = 0; i < 6; i++) { 95 | double datum = System.BitConverter.ToDouble(data, i * 8); 96 | var j = i+1; 97 | switch (j) 98 | { 99 | case 1: { 100 | xPos = (float)datum; 101 | break; 102 | } case 2: { 103 | pitch = (float)datum; 104 | 105 | break; 106 | } case 3: { 107 | zPos = (float)datum; 108 | break; 109 | }case 4: { 110 | yaw = (float)datum; 111 | 112 | break; 113 | } case 5: { 114 | yPos = (float)datum; // 115 | 116 | break; 117 | } case 6: { 118 | roll = (float)datum; 119 | break; 120 | } 121 | } 122 | } 123 | 124 | // .... 125 | // allReceivedUDPPackets=allReceivedUDPPackets+text; 126 | } catch (Exception err) { 127 | print(err.ToString()); 128 | } 129 | } 130 | } 131 | 132 | 133 | // getLatestUDPPacket 134 | // cleans up the rest 135 | public string getLatestUDPPacket() { 136 | allReceivedUDPPackets = ""; 137 | return lastReceivedUDPPacket; 138 | } 139 | void OnDisable() { 140 | if (receiveThread != null) 141 | receiveThread.Abort(); 142 | client.Close(); 143 | } 144 | void OnApplicationQuit() { 145 | if (receiveThread != null) 146 | receiveThread.Abort(); 147 | client.Close(); 148 | } 149 | } -------------------------------------------------------------------------------- /Assets/Scripts/UDPReceive.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 94a9aba41c2de1e4997e5222ea091cca 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | assetBundleName: 10 | assetBundleVariant: 11 | -------------------------------------------------------------------------------- /ProjectSettings/AudioManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcteys/unityFaceTracking/6c0a268db52874a969b390386a85fa3b0e7f8f23/ProjectSettings/AudioManager.asset -------------------------------------------------------------------------------- /ProjectSettings/ClusterInputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcteys/unityFaceTracking/6c0a268db52874a969b390386a85fa3b0e7f8f23/ProjectSettings/ClusterInputManager.asset -------------------------------------------------------------------------------- /ProjectSettings/DynamicsManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcteys/unityFaceTracking/6c0a268db52874a969b390386a85fa3b0e7f8f23/ProjectSettings/DynamicsManager.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorBuildSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcteys/unityFaceTracking/6c0a268db52874a969b390386a85fa3b0e7f8f23/ProjectSettings/EditorBuildSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcteys/unityFaceTracking/6c0a268db52874a969b390386a85fa3b0e7f8f23/ProjectSettings/EditorSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/GraphicsSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcteys/unityFaceTracking/6c0a268db52874a969b390386a85fa3b0e7f8f23/ProjectSettings/GraphicsSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/InputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcteys/unityFaceTracking/6c0a268db52874a969b390386a85fa3b0e7f8f23/ProjectSettings/InputManager.asset -------------------------------------------------------------------------------- /ProjectSettings/NavMeshAreas.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcteys/unityFaceTracking/6c0a268db52874a969b390386a85fa3b0e7f8f23/ProjectSettings/NavMeshAreas.asset -------------------------------------------------------------------------------- /ProjectSettings/NetworkManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcteys/unityFaceTracking/6c0a268db52874a969b390386a85fa3b0e7f8f23/ProjectSettings/NetworkManager.asset -------------------------------------------------------------------------------- /ProjectSettings/Physics2DSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcteys/unityFaceTracking/6c0a268db52874a969b390386a85fa3b0e7f8f23/ProjectSettings/Physics2DSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/ProjectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcteys/unityFaceTracking/6c0a268db52874a969b390386a85fa3b0e7f8f23/ProjectSettings/ProjectSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/ProjectVersion.txt: -------------------------------------------------------------------------------- 1 | m_EditorVersion: 5.3.1f1 2 | m_StandardAssetsVersion: 0 3 | -------------------------------------------------------------------------------- /ProjectSettings/QualitySettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcteys/unityFaceTracking/6c0a268db52874a969b390386a85fa3b0e7f8f23/ProjectSettings/QualitySettings.asset -------------------------------------------------------------------------------- /ProjectSettings/TagManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcteys/unityFaceTracking/6c0a268db52874a969b390386a85fa3b0e7f8f23/ProjectSettings/TagManager.asset -------------------------------------------------------------------------------- /ProjectSettings/TimeManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcteys/unityFaceTracking/6c0a268db52874a969b390386a85fa3b0e7f8f23/ProjectSettings/TimeManager.asset -------------------------------------------------------------------------------- /ProjectSettings/UnityAdsSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcteys/unityFaceTracking/6c0a268db52874a969b390386a85fa3b0e7f8f23/ProjectSettings/UnityAdsSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/UnityConnectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcteys/unityFaceTracking/6c0a268db52874a969b390386a85fa3b0e7f8f23/ProjectSettings/UnityConnectSettings.asset -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Unity Face Tracking 2 | ================= 3 | 4 | Unity face tracking is an easy way to use head position in your interactive projects. 5 | 6 | [View Demo](https://t.co/3t7QAAwRQu) (6s vine video) 7 | 8 | 9 | Requirements 10 | ---------- 11 | To use Unity Face Tracking, you need to download and install [OpenTrack](https://github.com/opentrack/opentrack). This software is used to capture the head via your webcam. 12 | This project works with Unity free and pro. 13 | 14 | Getting Started 15 | -------- 16 | 17 | 18 | ####Opentrack 19 | 20 | 1. Install and launch the software 21 | 2. Select `FaceAPI` in Tracker Source. 22 | 3. Use `Flight Gear` as Game Protocol (use the port 5550, by default) 23 | 4. Press Start 24 | 25 | ####Unity 26 | 27 | 1. Open Unity project 28 | 2. Run the game. You are done ! 29 | 30 | ###The code 31 | 32 | 33 | 34 | 35 | How does it works 36 | ------------------ 37 | [FaceAPI](http://www.seeingmachines.com/product/faceapi/) is a powerfull real time 2D tracker. This software track the head-position in 3D providing X,Y,Z position and orientation coordinates per frame of video. 38 | Opentrack allow us to send this via the [UDP](http://fr.wikipedia.org/wiki/User_Datagram_Protocol) protocol, on the local server. The head position data is stored in the ram. With unity 3D, the script UDPRecieve read the bytes, and decode it from hexa to float. 39 | 40 | The UDPRecieve `c#` script is located in the Standard Assets folder, to be compiled before the `javascript` script. By doing this, we can make a gap between `c#` and `javascript`. 41 | 42 | ### To do 43 | 44 | * Complete documentation 45 | 46 | Credits 47 | ------- 48 | * [Stanisław Halik](https://github.com/sthalik) (Opentrack team) for his help with byte decoding. 49 | --------------------------------------------------------------------------------