Skip to content

Instantly share code, notes, and snippets.

@cabbibo
Created July 10, 2018 22:57
Show Gist options
  • Save cabbibo/854dfd00bafe32f9acfb2d4037da70fa to your computer and use it in GitHub Desktop.
Save cabbibo/854dfd00bafe32f9acfb2d4037da70fa to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.iOS;
public class BlendShapeEventSystem : MonoBehaviour {
bool shapeEnabled = false;
Dictionary<string, float> currentBlendShapes;
Dictionary<string, float> oldBlendShapes;
private List<FaceEvent> faceEvents = new List<FaceEvent>();
// Use this for initialization
void Start () {
UnityARSessionNativeInterface.ARFaceAnchorAddedEvent += FaceAdded;
UnityARSessionNativeInterface.ARFaceAnchorUpdatedEvent += FaceUpdated;
UnityARSessionNativeInterface.ARFaceAnchorRemovedEvent += FaceRemoved;
}
void OnGUI()
{
}
void FaceAdded (ARFaceAnchor anchorData)
{
shapeEnabled = true;
currentBlendShapes = anchorData.blendShapes;
oldBlendShapes = currentBlendShapes;
}
void FaceUpdated (ARFaceAnchor anchorData)
{
oldBlendShapes = currentBlendShapes;
currentBlendShapes = anchorData.blendShapes;
for( int i = 0; i < faceEvents.Count; i++ ){
FaceEvent faceEvent = faceEvents[i];
float oldVal = oldBlendShapes[faceEvent.type];
float newVal = currentBlendShapes[faceEvent.type];
print((oldVal-newVal)*10000);
if( faceEvent.triggerDirection == 1 ){
//print("y1");
if( oldVal < faceEvent.triggerValue && newVal >= faceEvent.triggerValue ){
print("Y");
faceEvent.FaceFire();
}
}
if( faceEvent.triggerDirection == -1 ){
if( oldVal >= faceEvent.triggerValue && newVal < faceEvent.triggerValue ){
print("Y2");
faceEvent.FaceFire();
}
}
}
}
public void AddFaceEvent( FaceEvent faceEvent ){
Debug.Log("ADDED");
faceEvents.Add( faceEvent );
}
public void RemoveFaceEvent( FaceEvent faceEvent ){
Debug.Log("REMOVED");
faceEvents.Remove( faceEvent );
}
void FaceRemoved (ARFaceAnchor anchorData)
{
shapeEnabled = false;
}
// Update is called once per frame
void Update () {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment