2020-01-01 13:55:01 +01:00
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
|
|
namespace OpenSee {
|
|
|
|
|
|
|
|
|
|
|
|
public class OpenSeeShowPoints : MonoBehaviour {
|
|
|
|
|
|
public OpenSee openSee = null;
|
2020-02-16 15:58:47 +01:00
|
|
|
|
public int faceId = 0;
|
2020-01-01 13:55:01 +01:00
|
|
|
|
public bool show3DPoints = true;
|
|
|
|
|
|
public bool applyTranslation = false;
|
|
|
|
|
|
public bool applyRotation = false;
|
2020-02-15 13:09:33 +01:00
|
|
|
|
[Range(0, 1)]
|
2020-01-03 16:40:45 +01:00
|
|
|
|
public float minConfidence = 0.20f;
|
2020-03-08 13:50:53 +01:00
|
|
|
|
public bool showGaze = true;
|
|
|
|
|
|
public Material material;
|
2020-02-16 15:58:47 +01:00
|
|
|
|
private OpenSee.OpenSeeData openSeeData;
|
2020-01-01 13:55:01 +01:00
|
|
|
|
private GameObject[] gameObjects;
|
|
|
|
|
|
private GameObject centerBall;
|
|
|
|
|
|
private double updated = 0.0;
|
2020-03-08 13:50:53 +01:00
|
|
|
|
private int total = 70;
|
2020-01-01 13:55:01 +01:00
|
|
|
|
|
|
|
|
|
|
void Start () {
|
|
|
|
|
|
if (openSee == null) {
|
|
|
|
|
|
openSee = GetComponent<OpenSee>();
|
|
|
|
|
|
}
|
2020-02-24 00:44:17 +01:00
|
|
|
|
gameObjects = new GameObject[70];
|
2020-03-08 13:50:53 +01:00
|
|
|
|
if (!showGaze)
|
|
|
|
|
|
total = 66;
|
|
|
|
|
|
for (int i = 0; i < total; i++) {
|
2020-01-01 13:55:01 +01:00
|
|
|
|
gameObjects[i] = GameObject.CreatePrimitive(PrimitiveType.Sphere);
|
|
|
|
|
|
gameObjects[i].name = "Point " + (i + 1);
|
2020-03-08 13:50:53 +01:00
|
|
|
|
if (material != null)
|
|
|
|
|
|
gameObjects[i].GetComponent<Renderer>().material = material;
|
|
|
|
|
|
gameObjects[i].layer = gameObject.layer;
|
2020-01-01 13:55:01 +01:00
|
|
|
|
gameObjects[i].transform.SetParent(transform);
|
|
|
|
|
|
gameObjects[i].transform.localScale = new Vector3(0.025f, 0.025f, 0.025f);
|
2020-02-24 00:44:17 +01:00
|
|
|
|
if (i >= 68) {
|
|
|
|
|
|
GameObject cylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
|
2020-03-08 13:50:53 +01:00
|
|
|
|
if (material != null)
|
|
|
|
|
|
cylinder.GetComponent<Renderer>().material = material;
|
|
|
|
|
|
cylinder.layer = gameObject.layer;
|
2020-02-24 00:44:17 +01:00
|
|
|
|
cylinder.transform.SetParent(gameObjects[i].transform);
|
|
|
|
|
|
cylinder.transform.localEulerAngles = new Vector3(90f, 0f, 0f);
|
|
|
|
|
|
cylinder.transform.localPosition = new Vector3(0f, 0f, -4f);
|
|
|
|
|
|
cylinder.transform.localScale = new Vector3(1f, 4f, 1f);
|
|
|
|
|
|
}
|
2020-01-01 13:55:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
centerBall = GameObject.CreatePrimitive(PrimitiveType.Sphere);
|
|
|
|
|
|
centerBall.name = "Center";
|
|
|
|
|
|
centerBall.transform.SetParent(transform);
|
|
|
|
|
|
centerBall.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
|
|
|
|
|
|
}
|
2020-02-15 13:09:33 +01:00
|
|
|
|
|
2020-01-01 13:55:01 +01:00
|
|
|
|
void Update () {
|
|
|
|
|
|
if (!openSee)
|
|
|
|
|
|
return;
|
2020-02-16 15:58:47 +01:00
|
|
|
|
/*openSeeData = openSee.trackingData;
|
2020-01-01 13:55:01 +01:00
|
|
|
|
if (openSeeData == null || openSeeData.Length < 1)
|
2020-02-16 15:58:47 +01:00
|
|
|
|
return;*/
|
|
|
|
|
|
openSeeData = openSee.GetOpenSeeData(faceId);
|
2020-03-30 16:44:33 +02:00
|
|
|
|
if (openSeeData == null || (show3DPoints && openSeeData.fit3DError > openSee.maxFit3DError))
|
2020-01-01 13:55:01 +01:00
|
|
|
|
return;
|
2020-02-16 15:58:47 +01:00
|
|
|
|
if (openSeeData.time > updated) {
|
|
|
|
|
|
updated = openSeeData.time;
|
2020-01-01 13:55:01 +01:00
|
|
|
|
} else {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (show3DPoints) {
|
|
|
|
|
|
centerBall.gameObject.SetActive(false);
|
2020-03-08 13:50:53 +01:00
|
|
|
|
for (int i = 0; i < total; i++) {
|
2020-02-24 00:44:17 +01:00
|
|
|
|
if (openSeeData.got3DPoints && (i >= 68 || openSeeData.confidence[i] > minConfidence)) {
|
2020-01-01 13:55:01 +01:00
|
|
|
|
Renderer renderer = gameObjects[i].GetComponent<Renderer>();
|
2020-02-16 15:58:47 +01:00
|
|
|
|
Vector3 pt = openSeeData.points3D[i];
|
2020-02-09 01:25:55 +01:00
|
|
|
|
pt.x = -pt.x;
|
|
|
|
|
|
gameObjects[i].transform.localPosition = pt;
|
2020-02-24 00:44:17 +01:00
|
|
|
|
if (i < 68)
|
|
|
|
|
|
renderer.material.SetColor("_Color", Color.Lerp(Color.red, Color.green, openSeeData.confidence[i]));
|
|
|
|
|
|
else {
|
|
|
|
|
|
if (i == 68)
|
|
|
|
|
|
gameObjects[i].transform.localRotation = openSeeData.rightGaze;
|
|
|
|
|
|
else
|
|
|
|
|
|
gameObjects[i].transform.localRotation = openSeeData.leftGaze;
|
|
|
|
|
|
}
|
2020-01-01 13:55:01 +01:00
|
|
|
|
} else {
|
|
|
|
|
|
Renderer renderer = gameObjects[i].GetComponent<Renderer>();
|
|
|
|
|
|
renderer.material.SetColor("_Color", Color.cyan);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (applyTranslation) {
|
2020-02-16 15:58:47 +01:00
|
|
|
|
Vector3 v = openSeeData.translation;
|
2020-02-09 01:25:55 +01:00
|
|
|
|
v.x = -v.x;
|
|
|
|
|
|
v.z = -v.z;
|
|
|
|
|
|
transform.localPosition = v;
|
2020-01-01 13:55:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
if (applyRotation) {
|
2020-02-09 01:25:55 +01:00
|
|
|
|
Quaternion offset = Quaternion.Euler(0f, 0f, -90f);
|
2020-02-16 15:58:47 +01:00
|
|
|
|
Quaternion convertedQuaternion = new Quaternion(-openSeeData.rawQuaternion.y, -openSeeData.rawQuaternion.x, openSeeData.rawQuaternion.z, openSeeData.rawQuaternion.w) * offset;
|
2020-02-09 01:25:55 +01:00
|
|
|
|
transform.localRotation = convertedQuaternion;
|
2020-01-01 13:55:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
centerBall.gameObject.SetActive(false);
|
|
|
|
|
|
Vector3 center = new Vector3(0.0f, 0.0f, 0.0f);
|
|
|
|
|
|
float minX = 10000.0f;
|
|
|
|
|
|
float minY = 10000.0f;
|
|
|
|
|
|
float maxX = -1.0f;
|
|
|
|
|
|
float maxY = -1.0f;
|
|
|
|
|
|
for (int i = 0; i < 66; i++) {
|
2020-02-16 15:58:47 +01:00
|
|
|
|
float x = openSeeData.points[i].x;
|
|
|
|
|
|
float y = -openSeeData.points[i].y;
|
2020-01-01 13:55:01 +01:00
|
|
|
|
if (minX > x) {
|
|
|
|
|
|
minX = x;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (minY > y) {
|
|
|
|
|
|
minY = y;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (maxX < x) {
|
|
|
|
|
|
maxX = x;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (maxY < y) {
|
|
|
|
|
|
maxY = y;
|
|
|
|
|
|
}
|
|
|
|
|
|
center += new Vector3(x, y, 0.0f);
|
|
|
|
|
|
}
|
|
|
|
|
|
center = center / 66;
|
|
|
|
|
|
center = center - new Vector3(minX, minY, 0.0f);
|
|
|
|
|
|
center.x = center.x / (maxX - minX);
|
|
|
|
|
|
center.y = center.y / (maxX - minX);
|
|
|
|
|
|
center.z = 0.5f;
|
|
|
|
|
|
centerBall.transform.localPosition = center;
|
2020-02-15 13:09:33 +01:00
|
|
|
|
|
2020-01-01 13:55:01 +01:00
|
|
|
|
for (int i = 0; i < 66; i++) {
|
|
|
|
|
|
Renderer renderer = gameObjects[i].GetComponent<Renderer>();
|
2020-02-16 15:58:47 +01:00
|
|
|
|
renderer.material.SetColor("_Color", Color.Lerp(Color.red, Color.green, openSeeData.confidence[i]));
|
|
|
|
|
|
float x = openSeeData.points[i].x;
|
|
|
|
|
|
float y = -openSeeData.points[i].y;
|
2020-01-01 13:55:01 +01:00
|
|
|
|
Vector3 position = new Vector3(x, y, 0.0f);
|
|
|
|
|
|
position = position - new Vector3(minX, minY, 0.0f);
|
|
|
|
|
|
position.x = position.x / (maxX - minX);
|
|
|
|
|
|
position.y = position.y / (maxX - minX);
|
|
|
|
|
|
gameObjects[i].transform.localPosition = position;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|