3 Point Align

Hello,

in an AR-Application I would like to align loaded scanned realworld meshes, with the current AR View of the real world.

The user should cklick 3 points on the scanned meshes (source), afterwards users should click 3 corresponding points in realworld (target).
Therefore I have 2 sets, each with 3x Vector3 and want them to be used to calculate the transform to align the scanned meshes on realworld accordingly.

I tried several things but can not get right mathematic to get the desired reuslt:

-take each set of three points as a “plane”, get those plane normals and use Quternion.LookRotation makes the “planes” parallel, but not in correct rotation (tried flipped or not flipped in various combinations)

private void StartAlign(List<Vector3> src, List<Vector3> trg)
{
        PlaneSrc = new Plane(src[0], src[1], src[2]);
        PlaneTrg = new Plane(trg[0], trg[1], trg[2]);

	Object_Scanned.transform.rotation = Quaternion.LookRotation(PlaneTrg.flipped.normal, PlaneSrc.normal);
}

-get the matrix4x4 for each set, calculate and attach, but none of my mathematic tries brought up what i need (below just one example fpr matrix4x4):

public void Align3Point(GameObject Objects_Scanned, GameObject to_target, List<Vector3> align_points, List<Vector3> target_points)
{
        Matrix4x4 sourcePlane = MatrixFrom3Points(align_points[0], align_points[1], align_points[2]);
        Matrix4x4 targetPlane = MatrixFrom3Points(target_points[0], target_points[1], target_points[2]);

        alignPlane = Matrix4x4.Rotate(Quaternion.Lerp(sourcePlane.rotation, targetPlane.rotation, 360f));

        Objects_Scanned.transform.rotation = Quaternion.LookRotation(alignPlane.GetColumn(2), alignPlane.GetColumn(1));
}

public Matrix4x4 MatrixFrom3Points(Vector3 p0, Vector3 p1, Vector3 p2)
{
        Vector3 x = p1 - p0;
        x.Normalize();
        Vector3 y = p2 - p0;
        Vector3 z = Vector3.Cross(x, y);
        z.Normalize();
        y = Vector3.Cross(z, x);
        y.Normalize();

        return new Matrix4x4(new Vector4(x.x, x.y, x.z, 0),
                             new Vector4(y.x, y.y, y.z, 0),
                             new Vector4(z.x, z.y, z.z, 0),
                             new Vector4(p0.x, p0.y, p0.z, 1));
}

Can someone help me with the mathematic behind a 3 point align please?

Thank you so much

found a solution that does it for my purpose:

private static Plane PlaneSrc, PlaneTrg;
private static GameObject src_G0, trg_G0
public static void Align3Point(GameObject go, Vector3 src0, Vector3 src1, Vector3 src2, Vector3 trg0, Vector3 trg1, Vector3 trg2, float drawDuration = 0f)
{
	PlaneSrc = new Plane(src0, src1, src2);
	PlaneTrg = new Plane(trg0, trg1, trg2);
    
	src_G0 = new GameObject();
	src_G0.transform.parent = go.transform;
	src_G0.transform.position = src0;
	trg_G0 = new GameObject();
	trg_G0.transform.position = trg0;

 	go.transform.rotation *= Quaternion.FromToRotation(PlaneSrc.normal, PlaneTrg.normal);
	go.transform.position += (trg_G0.transform.position - src_G0.transform.position);

	GameObject.Destroy(src_G0);
	GameObject.Destroy(trg_G0);
}

Hope it helps other too.