Skip to content

Commit

Permalink
Added several scripts, edited some stuff
Browse files Browse the repository at this point in the history
• Added screenshot script.
• Changed how original bone positions are referenced.
• Added basic script to compare points & calculate score.
• Added script to limit bone positions.
  • Loading branch information
DrSharky committed Oct 21, 2018
1 parent c1c2f93 commit 2e71535
Show file tree
Hide file tree
Showing 8 changed files with 317 additions and 23 deletions.
31 changes: 31 additions & 0 deletions Bones.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//Add this script to the armature.
public class Bones : MonoBehaviour
{
public static List<Vector3> originalPositions;

void Start ()
{
originalPositions = new List<Vector3>();
SaveOriginals();
}

void Update ()
{

}

void SaveOriginals()
{
int boneCount = transform.childCount - 1;
for (int i = 0; i < boneCount; i++)
{
Transform bone = transform.GetChild(i);
if (!bone.gameObject.name.Contains("Base"))
originalPositions.Add(bone.position);
}
}
}
43 changes: 34 additions & 9 deletions ClickHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ void Start()
EventManager.StartListening("ToggleDots", toggleDotListener);
}

//void Update()
//{
// if (Input.GetKeyDown(KeyCode.F))
// {
// EventManager.TriggerEvent("ToggleDots");
// }
//}

void OnMouseDown()
{
screenPoint = Camera.main.WorldToScreenPoint(transform.position);
Expand All @@ -37,21 +45,38 @@ void OnMouseDrag()
transform.position = curPosition;
}

// -- CREATED FOR DEBUG PURPOSES!!! -- DELETE LATER --
//private void OnMouseUp()
//{
// Vector3 screenPt = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
// Debug.Log("Screen Pos: " + screenPt);
// Debug.Log("World Pos: " + transform.position);
//}
// -- DEBUG ONLY!! -- DELETE AFTER USE --

void ToggleDot()
{
if (redDotInstance == null)
redDotInstance = Instantiate(redDot, transform);
else
Destroy(redDotInstance);

if (pointPosInstance == null)
{
screenPoint = Camera.main.WorldToScreenPoint(transform.position);
pointPosInstance = Instantiate(pointPos, screenPoint, new Quaternion());
pointPosInstance.GetComponent<Text>().text = "(" + transform.position.x + ", " + transform.position.y + ", " + transform.position.z + ")";
pointPosInstance.transform.parent = GameObject.Find("Canvas").transform;
}
//else
// Destroy(pointPosInstance);
//TogglePositionDisplay();
}

// -- CREATED FOR DEBUG PURPOSES!!! -- DELETE LATER --
void TogglePositionDisplay()
{
Vector3 screenPosition;
if (pointPosInstance == null)
{
screenPosition = Camera.main.WorldToScreenPoint(transform.position);
pointPosInstance = Instantiate(pointPos, screenPosition, new Quaternion());
pointPosInstance.GetComponent<Text>().text = "(" + transform.position.x + ", " + transform.position.y + ", " + transform.position.z + ")";
pointPosInstance.transform.parent = GameObject.Find("Canvas").transform;
}
else
Destroy(pointPosInstance);
}
// -- DEBUG ONLY!! -- DELETE AFTER USE --
}
13 changes: 10 additions & 3 deletions Clicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
using UnityEngine;

public class Clicker : MonoBehaviour
{
// Update is called once per frame
{
bool toggleOn = false;

void Update ()
{
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(0))
if ((Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(0)) && !toggleOn)
EventManager.TriggerEvent("ToggleDots");

if (Input.GetKeyDown(KeyCode.F))
{
EventManager.TriggerEvent("ToggleDots");
toggleOn = !toggleOn;
}
}
}
66 changes: 66 additions & 0 deletions PositionComparison.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class PositionComparison : MonoBehaviour
{
[SerializeField]
GameObject armature;

const float MAX_POINTS = 100.0f;
int boneCount = 0;
float pointTotal = 0.0f;

[SerializeField]
RefPositions refPositions;

List<Vector3> currentPositions;
Vector3[] selectedRef;

// Use this for initialization
void Start ()
{
//Use -1 to exclude the base bone.
boneCount = armature.transform.childCount - 1;
selectedRef = refPositions.vectorList[UnityEngine.Random.Range(0,1)].vectorSet;
Debug.Log(selectedRef);
}

// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.Return))
Compare();
}

void Compare ()
{
double xTotalDiff = 0;
double yTotalDiff = 0;
for (int i = 0; i < boneCount; i++)
{
Vector3 refVector = selectedRef[i];
double xFinal = Math.Round(Math.Abs(armature.transform.GetChild(i).position.x), 1);
double yFinal = Math.Round(Math.Abs(armature.transform.GetChild(i).position.y), 1);

double xRef = Mathf.Abs(selectedRef[i].x);
double yRef = Mathf.Abs(selectedRef[i].y);

xTotalDiff += Math.Abs(xFinal - xRef);
yTotalDiff += Math.Abs(yFinal - yRef);
}

//Grade factor is 0.125, received from formula below. works
//for pumpkin, not sure about other models...
Debug.Log("X Total Diff: " + xTotalDiff);
Debug.Log("Y Total Diff: " + yTotalDiff);
double boneFloat = boneCount;
double combinedDiff = (xTotalDiff + yTotalDiff) * (1 / (boneFloat / (100 / (boneFloat * 2))));
Debug.Log("Combined Diff: " + combinedDiff);
double scoreFinal = Math.Round((MAX_POINTS - combinedDiff), 1);
if (scoreFinal < 0)
scoreFinal = 0;
Debug.Log("Score: " + scoreFinal);
}
}
70 changes: 70 additions & 0 deletions PositionLimiter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PositionLimiter : MonoBehaviour
{
GameObject armature;
int boneCount;
[SerializeField]
int distanceLimit = 20;

void Start()
{
armature = transform.parent.gameObject;
boneCount = armature.transform.childCount-1;
}

void Update()
{
if (Input.GetKeyDown(KeyCode.D))
ResetDefaults();
}

void OnMouseUp()
{
CheckPositionLimits();
}

//Function to check that the x & y coordinates of each bone
//don't go past a limit of +/- (distanceLimit) units from the original position.
void CheckPositionLimits()
{
int boneIndex = transform.GetSiblingIndex();
float xBone = Mathf.Abs(transform.position.x);
float xOriginal = Mathf.Abs(Bones.originalPositions[boneIndex].x);
float yBone = Mathf.Abs(transform.position.y);
float yOriginal = Mathf.Abs(Bones.originalPositions[boneIndex].y);

float xDiff = Mathf.Abs(xBone - xOriginal);
float yDiff = Mathf.Abs(yBone - yOriginal);

if (xDiff > 20)
{
if (transform.position.x < Bones.originalPositions[boneIndex].x)
transform.position = new Vector3(Bones.originalPositions[boneIndex].x - distanceLimit,
transform.position.y, transform.position.z);
else
transform.position = new Vector3(Bones.originalPositions[boneIndex].x + distanceLimit,
transform.position.y, transform.position.z);
}

if (yDiff > 20)
{
if (transform.position.y < Bones.originalPositions[boneIndex].y)
transform.position = new Vector3(transform.position.x,
Bones.originalPositions[boneIndex].y - distanceLimit, transform.position.z);
else
transform.position = new Vector3(transform.position.x,
Bones.originalPositions[boneIndex].y + distanceLimit, transform.position.z);
}
}

void ResetDefaults()
{
for (int i = 0; i < Bones.originalPositions.Count; i++)
{
armature.transform.GetChild(i).position = Bones.originalPositions[i];
}
}
}
40 changes: 29 additions & 11 deletions PositionSerialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
using UnityEngine;
using System.IO;

//This is a debug script, full version will capture image of screen instead.
//Needed coordinates to save some interesting faces to use as -
// - reference images in the actual game.
public class PositionSerialize : MonoBehaviour
{
[SerializeField]
Expand All @@ -11,37 +14,52 @@ public class PositionSerialize : MonoBehaviour
string directory = "Exported Positions/";
int lastFileIndex = 1;
string modelName;
int boneCount;

void Start ()
{
modelName = armature.transform.parent.gameObject.name;
filePath = directory + modelName + "/" + lastFileIndex + ".txt";
}

void Update ()
filePath = directory + modelName + lastFileIndex + ".txt";
boneCount = armature.transform.childCount;

////If file already exists, find last file count & set filePath to last file.
if (File.Exists(filePath))
{
string[] files = Directory.GetFiles(directory);

string lastFile = files[files.Length - 1].Split('.')[0];
lastFileIndex = int.Parse(lastFile.Substring(lastFile.Length - 1));
filePath = directory + modelName + lastFileIndex + ".txt";
}
}

void Update ()
{
if (Input.GetKeyDown(KeyCode.V))
WritePositions();
else if (Input.GetKeyDown(KeyCode.G))
ReadPositions();
else
{
if (Input.GetKeyDown(KeyCode.G))
ReadPositions();
}
}

void WritePositions()
{
//If file already exists, write to another file & add new count at end of file name.
//If file already exists, find last file count & add increased count at end of file name.
if (File.Exists(filePath))
{
string[] files = Directory.GetFiles(directory);

string lastFile = files[files.Length - 1].Split('.')[0];
lastFileIndex = int.Parse(lastFile.Substring(lastFile.Length - 1));
lastFileIndex++;
filePath = directory + modelName + "/" + lastFileIndex + ".txt";
filePath = directory + modelName + lastFileIndex + ".txt";
}

StreamWriter writer = new StreamWriter(filePath, true);

for (int i = 0; i < armature.transform.childCount; i++)
for (int i = 0; i < boneCount; i++)
{
if (!armature.transform.GetChild(i).gameObject.name.Contains("Base"))
writer.WriteLine(armature.transform.GetChild(i).position);
Expand All @@ -54,7 +72,7 @@ void ReadPositions()
StreamReader reader = new StreamReader(filePath);

//childCount-1 because base bone is excluded.
for(int i = 0; i < armature.transform.childCount-1; i++)
for(int i = 0; i < boneCount-1; i++)
{
Vector3 readPos = new Vector3();
string[] stringCoordinates = reader.ReadLine().Split('(', ',', ')');
Expand All @@ -65,7 +83,7 @@ void ReadPositions()
armature.transform.GetChild(i).position = readPos;

//DEBUG - SHOW READ POSITIONS
//Debug.Log(readPos);
Debug.Log(readPos);
}
reader.Close();
}
Expand Down
15 changes: 15 additions & 0 deletions RefPositions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class RefPositions
{
[System.Serializable]
public struct vectorData
{
public Vector3[] vectorSet;
}

public vectorData[] vectorList = new vectorData[20];
}
Loading

0 comments on commit 2e71535

Please sign in to comment.