Skip to main content
added 192 characters in body
Source Link
Zibelas
  • 5.1k
  • 2
  • 15
  • 26

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

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.

enter image description here

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

Source Link
Zibelas
  • 5.1k
  • 2
  • 15
  • 26

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.

enter image description here