Last active
June 7, 2022 15:58
-
-
Save ale2x72/66b01bab34deb13bdd3dbd07412b0b10 to your computer and use it in GitHub Desktop.
Upgrade of ReadAssemblage.cs..... now with Quaternions! Imports Rhiono OXYZ plane data in Unity and orients prefabs according to type and corresponding orientation plane
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 System; | |
using System.IO; | |
using UnityEngine; | |
public class ReadAssemblage : MonoBehaviour | |
{ | |
public bool debugMode; | |
[Range(0.5f, 5)] | |
public float axisLength; | |
[Header("AssemblyObjects")] | |
public GameObject[] AssemblyObjects; | |
public string fileName; | |
GameObject[] assemblage; | |
Vector3[] forwardVecs; | |
void Start() | |
{ | |
fileName = Application.dataPath + "\\" + fileName; | |
string[] data = File.ReadAllLines(fileName); | |
GameObject Collector = new GameObject("Assemblage"); | |
Collector.transform.position = Vector3.zero; | |
Collector.transform.rotation = Quaternion.identity; | |
assemblage = new GameObject[data.Length]; | |
forwardVecs = new Vector3[data.Length]; | |
for (int i = 0; i < data.Length; i++) | |
{ | |
// planetype structure: 0=type, 1=origin, 2=x, 3=y, 4=z | |
// separate type from plane OXYZ data | |
string[] planeType = data[i].Split(';'); | |
int type = Convert.ToInt32(planeType[0]); | |
string[][] vectors = new string[4][]; | |
for (int j = 1; j < planeType.Length; j++) | |
{ | |
vectors[j - 1] = planeType[j].Split(','); | |
} | |
Vector3 planeO, planeX, planeY, planeZ; | |
planeO = new Vector3(Convert.ToSingle(vectors[0][0]), Convert.ToSingle(vectors[0][1]), Convert.ToSingle(vectors[0][2])); | |
planeX = new Vector3(Convert.ToSingle(vectors[1][0]), Convert.ToSingle(vectors[1][1]), Convert.ToSingle(vectors[1][2])); | |
planeY = new Vector3(Convert.ToSingle(vectors[2][0]), Convert.ToSingle(vectors[2][1]), Convert.ToSingle(vectors[2][2])); | |
planeZ = new Vector3(Convert.ToSingle(vectors[3][0]), Convert.ToSingle(vectors[3][1]), Convert.ToSingle(vectors[3][2])); | |
Vector3 origin = ToLeftHanded(planeO); | |
Vector3 fwVector = -ToLeftHanded(planeY); | |
Vector3 upVector = ToLeftHanded(planeZ); | |
Vector3 rightVector = -ToLeftHanded(planeX); | |
// this is for alignment error checking | |
forwardVecs[i] = fwVector; | |
Quaternion rotated = QuaternionFromVectors(rightVector, upVector, fwVector); | |
// instantiate GameObject | |
assemblage[i] = Instantiate(AssemblyObjects[type], origin, rotated); | |
// assign to parent | |
assemblage[i].transform.parent = Collector.transform; | |
float newDotProduct = Vector3.Dot(assemblage[i].transform.forward, forwardVecs[i]); | |
// check for outliers | |
if (1 - newDotProduct > 0.001) | |
Debug.Log(assemblage[i].name + " - angle: " + Mathf.Acos(newDotProduct)); | |
// _____________ Method without Quaternions _____________ | |
//Vector3 origin = ToLeftHanded(planeO); | |
//Vector3 fwVector = -ToLeftHanded(planeY); | |
//Vector3 upVector = ToLeftHanded(planeZ); | |
//Vector3 rightVector = -ToLeftHanded(planeX); | |
//forwardVecs[i] = fwVector; | |
//Quaternion rot = Quaternion.identity; | |
//assemblage[i] = Instantiate(AssemblyObjects[type], origin, rot); | |
//assemblage[i].name = AssemblyObjects[type].name + "_" + i; | |
//// align with up vector first | |
//assemblage[i].transform.up = upVector; | |
//// compute angle between current forward and target forward | |
//float angle = Vector3.Angle(assemblage[i].transform.forward, forwardVecs[i]); | |
//// rotate aling up vector | |
//assemblage[i].transform.Rotate(0, angle, 0); | |
//float newAngle = Vector3.Angle(assemblage[i].transform.forward, forwardVecs[i]); | |
//// check if the rotation direction was wrong - in case rotate back twice | |
//if (newAngle > 0.5) | |
// assemblage[i].transform.Rotate(0, -2 * angle, 0); | |
//// assign to parent | |
//assemblage[i].transform.parent = Collector.transform; | |
//float newDotProduct = Vector3.Dot(assemblage[i].transform.forward, forwardVecs[i]); | |
// check for outliers | |
//if (1 - newDotProduct > 0.001) | |
//Debug.Log(assemblage[i].name + " - angle: " + angle + " - newAngle: " + newAngle); | |
} | |
} | |
// Note: Rhino right-handed XYZ orientation is translated in Unity's Left-Handed XZY | |
// to match how .OBJ are imported with the "Remap Z to OBJ Y" option when exporting from Rhino | |
// to account for the right-to-left hand transition, x and y coordinates become negative, | |
// and y and z are swapped | |
Vector3 ToLeftHanded(Vector3 vector) | |
{ | |
return new Vector3(-vector.x, vector.z, -vector.y); | |
} | |
// code from Martin Baker | |
// https://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/ | |
Quaternion QuaternionFromVectors(Vector3 XAxis, Vector3 YAxis, Vector3 ZAxis) | |
{ | |
float m00, m01, m02, m10, m11, m12, m20, m21, m22; | |
float qx, qy, qz, qw; | |
// build the plane rotation matrix (mRC - R - row index, C - column index) | |
m00 = XAxis.x; | |
m10 = XAxis.y; | |
m20 = XAxis.z; | |
m01 = YAxis.x; | |
m11 = YAxis.y; | |
m21 = YAxis.z; | |
m02 = ZAxis.x; | |
m12 = ZAxis.y; | |
m22 = ZAxis.z; | |
float tr = m00 + m11 + m22; | |
float S; | |
if (tr > 0) | |
{ | |
S = Mathf.Sqrt(tr + 1.0f) * 2; // S=4*qw | |
qw = 0.25f * S; | |
qx = (m21 - m12) / S; | |
qy = (m02 - m20) / S; | |
qz = (m10 - m01) / S; | |
} | |
else if ((m00 > m11) & (m00 > m22)) | |
{ | |
S = Mathf.Sqrt(1.0f + m00 - m11 - m22) * 2; // S=4*qx | |
qw = (m21 - m12) / S; | |
qx = 0.25f * S; | |
qy = (m01 + m10) / S; | |
qz = (m02 + m20) / S; | |
} | |
else if (m11 > m22) | |
{ | |
S = Mathf.Sqrt(1.0f + m11 - m00 - m22) * 2; // S=4*qy | |
qw = (m02 - m20) / S; | |
qx = (m01 + m10) / S; | |
qy = 0.25f * S; | |
qz = (m12 + m21) / S; | |
} | |
else | |
{ | |
S = Mathf.Sqrt(1.0f + m22 - m00 - m11) * 2; // S=4*qz | |
qw = (m10 - m01) / S; | |
qx = (m02 + m20) / S; | |
qy = (m12 + m21) / S; | |
qz = 0.25f * S; | |
} | |
return new Quaternion(qx, qy, qz, qw); | |
} | |
void DrawTransform(Transform t, float len) | |
{ | |
Debug.DrawLine(t.position, t.position + t.forward * len, Color.cyan); | |
Debug.DrawLine(t.position, t.position + t.up * len, Color.green); | |
Debug.DrawLine(t.position, t.position + t.right * len, Color.red); | |
} | |
void DrawVector(Vector3 anchor, Vector3 vector, float len, Color col) | |
{ | |
Debug.DrawLine(anchor, anchor + vector * len, col); | |
} | |
void Update() | |
{ | |
if (debugMode) | |
for (int i = 0; i < assemblage.Length; i++) | |
{ | |
DrawTransform(assemblage[i].transform, axisLength); | |
DrawVector(assemblage[i].transform.position, forwardVecs[i], 10, Color.black); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment