using UnityEngine;
using System.Collections.Generic;
using System.IO;
using System.Globalization;
public class ArbitraryToUnityCoordinateTransformer : MonoBehaviour
{
public string csvFileName = "3lüveri.csv";
// Known corresponding points
public Vector3 unityPoint1;
public Vector3 arbitraryPoint1;
public Vector3 unityPoint2;
public Vector3 arbitraryPoint2;
Yes, there is an error in your code related to how you're calculating the transformation matrix, specifically the translation vector. In your CalculateTransformMatrix() method, you're computing the translation as:
This calculation is incorrect for the transformation you're aiming to achieve. The translation vector should represent the vector you need to add to your arbitrary point to get the Unity point. Therefore, the correct calculation should be:
Desired Transformation: You want to map arbitraryPoint1 to unityPoint1. That means when you apply your transformation to arbitraryPoint1, it should result in unityPoint1.
Translation Vector: The translation vector should be the difference between the target point (unityPoint1) and the source point (arbitraryPoint1).
Mathematical Representation: Translation vector T = UnityPoint1 - ArbitraryPoint1
By using arbitraryPoint1 - unityPoint1, you're effectively moving in the wrong direction, which will misalign your entire coordinate transformation.
Corrected CalculateTransformMatrix() Method:
csharp
void CalculateTransformMatrix()
{
// Corrected translation calculation
Vector3 translation = unityPoint1 - arbitraryPoint1;
Debug.Log($"Translation: {translation}");
Vector3 arbitraryDelta = arbitraryPoint2 - arbitraryPoint1;
Vector3 unityDelta = unityPoint2 - unityPoint1;
Debug.Log($"Arbitrary Delta: {arbitraryDelta}, Unity Delta: {unityDelta}");
float scaleX = 1f, scaleY = 1f, scaleZ = 1f;
if (Mathf.Abs(arbitraryDelta.x) > Mathf.Epsilon)
scaleX = unityDelta.x / arbitraryDelta.x;
if (Mathf.Abs(arbitraryDelta.y) > Mathf.Epsilon)
scaleY = unityDelta.y / arbitraryDelta.y;
if (Mathf.Abs(arbitraryDelta.z) > Mathf.Epsilon)
scaleZ = unityDelta.z / arbitraryDelta.z;
Debug.Log($"Scale Factors: {scaleX}, {scaleY}, {scaleZ}");
Quaternion rotation = Quaternion.FromToRotation(arbitraryDelta.normalized, unityDelta.normalized);
Debug.Log($"Rotation: {rotation}");
// Note: The order of operations in TRS is Scale, then Rotation, then Translation
transformMatrix = Matrix4x4.TRS(Vector3.zero, rotation, new Vector3(scaleX, scaleY, scaleZ));
transformMatrix.SetColumn(3, new Vector4(translation.x, translation.y, translation.z, 1));
Debug.Log($"Transform Matrix: {transformMatrix}");
}
Explanation:
Translation Adjustment: Changed the translation calculation to unityPoint1 - arbitraryPoint1.
Constructing the Matrix: When using Matrix4x4.TRS, the translation (position) parameter applies the translation after scaling and rotation, matching the order of transformations in Unity (Scale ➔ Rotate ➔ Translate). However, if you supply the translation vector directly to TRS, it may not combine correctly due to the rotation and scaling affecting the translation component.
To resolve this, you can set the translation manually after constructing the matrix with zero translation:
csharp
transformMatrix = Matrix4x4.TRS(Vector3.zero, rotation, new Vector3(scaleX, scaleY, scaleZ));
transformMatrix.SetColumn(3, new Vector4(translation.x, translation.y, translation.z, 1));
Additional Considerations:
Order of Transformations: Be cautious with the order in which transformations are applied. In the TRS matrix, scaling is applied first, then rotation, then translation. Ensure this order aligns with your intended transformations.
Sufficient Data Points: Using only two points may not be enough to uniquely determine a 3D transformation that includes scaling, rotation, and translation, especially if the points are colinear. For a more accurate transformation, consider using at least three non-colinear point correspondences.
Scaling Factors: Verify that scaling factors are computed correctly and make sense in your context. Non-uniform scaling (different scaling factors for x, y, z) can lead to distortions if not intended.
Data Parsing: Ensure your CSV parsing correctly handles decimal separators and does not inadvertently split decimal numbers when splitting the line by commas. If your CSV uses commas as decimal separators, you need a more robust CSV parsing method, possibly using a CSV parsing library that can handle different locales.
Revised Data Parsing (if needed):
If your CSV uses commas as decimal separators and semicolons as field separators, adjust your parsing accordingly:
csharp
string[] values = line.Split(';');
if (values.Length >= 3)
{
string latStr = values[0].Trim();
string lonStr = values[1].Trim();
string typeStr = values[2].Trim();
// Replace decimal commas with dots
latStr = latStr.Replace(',', '.');
lonStr = lonStr.Replace(',', '.');
if (float.TryParse(latStr, NumberStyles.Float, CultureInfo.InvariantCulture, out float lat) &&
float.TryParse(lonStr, NumberStyles.Float, CultureInfo.InvariantCulture, out float lon))
{
arbitraryPoints.Add(new Vector3(lat, 0, lon));
}
else
{
Debug.LogWarning($"Invalid coordinate format on line {i + 1}: Latitude: {latStr}, Longitude: {lonStr}");
}
}
Conclusion:
Correcting the translation calculation should fix the primary issue in your coordinate transformation. Ensure that all transformations (scale, rotation, translation) are applied in the correct order and that they match the mathematical operations required to map your arbitrary coordinate system to Unity's coordinate system.