3
\$\begingroup\$

I'm currently working on an AR application that is able to measure volume. The functionality is required to draw a box is in the gif attached to this. I have tried to generate instantiating 3 points in a horizontal plane and then calculate the 4th point by vector calculations by this function.

enter image description here

Vector3 GetFourthPointPosition(Vector3 p1Transform, Vector3 p2Transform, Vector3 p3Transform)
{
    var OA = p1Transform;
    var OB = p2Transform;
    var OC = p3Transform;
    var OD = OC + OA - OB;
    GetFourthPoint(OA, OB, OC, OD);
    return OD;
}

Then, after that, I'm enabling a vertical plane and disable the horizontal plane to avoid adding points on the same axis. The last point is to calculate the height. After that, I'm instantiating 3 other points to generate the point view.

private void GetCompleteCube(Vector3 p1Transformation, Vector3 p2Transformation, Vector3 p3Transformation, Vector3 p4Transformation)
    {
        var height = Vector3.Distance(p3Transformation, p4Transformation);
        var length = Vector3.Distance(p1Transformation, p2Transformation);
        var width = Vector3.Distance(p2Transformation, p3Transformation);
        var invPointTransform = clone.transform.position;


        var p5Transformation = new Vector3(p1Transformation.x, p1Transformation.y + height, p1Transformation.z);
        var p6Transformation = new Vector3(p2Transformation.x, p1Transformation.y + height, p1Transformation.z);
        var p8Transformation = new Vector3(invPointTransform.x, invPointTransform.y + height, invPointTransform.z);


        InstantiatePoints(p1Transformation, p1Transformation, p3Transformation, p4Transformation, p5Transformation, p6Transformation, invPointTransform, p8Transformation);
    }

private void DrawCubeLineRendere(Vector3 p1Transformation, Vector3 p2Transformation, Vector3 p3Transformation, Vector3 p4Transformation, Vector3 p5Transformation, Vector3 p6Transformation, Vector3 p7Transformation, Vector3 p8Transformation)
{
        Vector3[] vertexPositions = new Vector3[] { p1Transformation, p2Transformation, p3Transformation, p7Transformation, p4Transformation, p5Transformation, p6Transformation, p8Transformation };
        lineRenderer.positionCount = vertexPositions.Length;
        lineRenderer.SetPositions(vertexPositions);
}

I'm tried using a line renderer t0 visualize the cube but the outcome is very odd. Does not lining to form a cuboid. it looks like this.

enter image description here

Can anybody help me to figure out what I have done wrong?

\$\endgroup\$
4
  • \$\begingroup\$ What is this function doing? GetFourthPoint(OA, OB, OC, OD); and your image of your line renderer - besides the all over the place points - is a continious line, you have to at least tell them which other point they have to connect, each corner connects to three dots, not only 2. \$\endgroup\$
    – Zibelas
    Sep 16, 2021 at 14:16
  • \$\begingroup\$ GetFourthPoint(OA, OB, OC, OD) function is Calculating the 4th point using the vertices of first clicked 03 points using Vector methods. \$\endgroup\$ Sep 16, 2021 at 14:21
  • \$\begingroup\$ But var OD = OC + OA - OB returns you already the forth point. \$\endgroup\$
    – Zibelas
    Sep 16, 2021 at 14:22
  • \$\begingroup\$ my mistake, that methods call for instantiate the 4th GameObject and then drawing the line using line renderer for the first 4 points on the ground. That's working. \$\endgroup\$ Sep 16, 2021 at 14:25

1 Answer 1

2
\$\begingroup\$

Instead of using a single line renderer, this uses 4 (you can go back for to 1 if you feel like it). Input are the first three points and the height (which you can as well later calculate/ generate on the fly).

The forth point is generated by simply subtracting the provided first three points.

Next we are generating with the cross product a normal vector and just use the corner pieces + the new normal vector + height to generate the other 4 points.

private void SetLineRenderer(LineRenderer lineRenderer, Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4) {
        Vector3[] vertexPositions = { p1, p2, p3, p4};
        lineRenderer.positionCount = 4;
        lineRenderer.loop = true;
        lineRenderer.SetPositions(vertexPositions);
    }

public void DrawCube(Transform p1, Transform p2, Transform p3, float height) {
        p4.position = p3.position + p1.position - p2.position;
        Vector3 crossProduct = Vector3.Cross(p2.position - p1.position, p4.position - p1.position).normalized * height;
        p5.position = p1.position - crossProduct;
        p6.position = p2.position - crossProduct;
        p7.position = p3.position - crossProduct;
        p8.position = p4.position - crossProduct;
        SetLineRenderer(l1, p1.position, p2.position, p3.position, p4.position);
        SetLineRenderer(l2, p5.position, p6.position, p7.position, p8.position);
        SetLineRenderer(l3, p1.position, p4.position, p8.position, p5.position);
        SetLineRenderer(l4, p2.position, p3.position, p7.position, p6.position);
    }

The circles are just for visual debugging if the math is right. If you want to draw in a single line renderer, you could pass the points p1, p2, p6, p2, p3, p7, p3, p4, p8, p4, p1, p5, p6, p7, p8. Loop does not need to be true but would not make a difference.

enter image description here

\$\endgroup\$
4
  • \$\begingroup\$ thank you for the answer, how should I pass l1,l2,l3,l4? When I create them as LineRenderer l1 = new LineRenderer(); It returns a null exception. \$\endgroup\$ Sep 16, 2021 at 18:45
  • 1
    \$\begingroup\$ I assigned them over the editor, on the dots. If you want to create them via code, it is something along the lines of gameObject.AddComponent<LineRendeter>(). You could draw it as well with only a single line renderer but a few lines would be drawn twice too. \$\endgroup\$
    – Zibelas
    Sep 16, 2021 at 19:24
  • \$\begingroup\$ It works, thank u. Do you know how to add that drawing animation? \$\endgroup\$ Sep 17, 2021 at 3:56
  • \$\begingroup\$ Is the drawing animation needed when you know already all parameters or created when the user is actually interacting with the cube? But a simple lerp should do the trick. \$\endgroup\$
    – Zibelas
    Sep 17, 2021 at 7:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .