0
\$\begingroup\$

It looks like depending on the camera view angle that there is kind of space in the connected points between the lines:

box

What I mean not connected or kind of pspaces in the connection points of the lines is marked here with blue circles:

box

The script:

using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine;

public class ShowMeshBounds : MonoBehaviour { public GameObject[] objectsToDraw; public GameObject prefabEffect; public Color color = Color.green;

private Vector3 v3FrontTopLeft;
private Vector3 v3FrontTopRight;
private Vector3 v3FrontBottomLeft;
private Vector3 v3FrontBottomRight;
private Vector3 v3BackTopLeft;
private Vector3 v3BackTopRight;
private Vector3 v3BackBottomLeft;
private Vector3 v3BackBottomRight;

private float counter = 0;
public bool animateLines;
public float speed = 1f;

private List<GameObject> allLines = new List<GameObject>();
private List<GameObject> instancesToMove = new List<GameObject>();
private Vector3 startPos;
private Vector3 endPos;

private void Start()
{
    DrawObjects();
}

private void DrawObjects()
{
    objectsToDraw = GameObject.FindGameObjectsWithTag("SpawnObject");

    for (int i = 0; i < objectsToDraw.Length; i++)
    {
        CalcPositons(objectsToDraw[i]);
        DrawBox();

        for (int x = 0; x < allLines.Count; x++)
        {

            allLines[x].transform.parent = objectsToDraw[i].transform;
        }
    }

    DuplicatePrefabEffects(1);

    StartCoroutine(moveStuff());
}

private void DuplicatePrefabEffects(int duplicationNumber)
{
    for (int i = 0; i < duplicationNumber; i++)
    {
        var go = Instantiate(prefabEffect);
        go.tag = "Duplicated Prefab";
        go.name = "Duplicated Prefab";
        instancesToMove.Add(go);
    }
}

void CalcPositons(GameObject objectToDrawTransform)
{
    Bounds bounds = objectToDrawTransform.GetComponent<MeshFilter>().sharedMesh.bounds;

    Vector3 v3Center = bounds.center;
    Vector3 v3Extents = bounds.extents;

    v3FrontTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z);  // Front top left corner
    v3FrontTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z);  // Front top right corner
    v3FrontBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z);  // Front bottom left corner
    v3FrontBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z);  // Front bottom right corner
    v3BackTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z);  // Back top left corner
    v3BackTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z);  // Back top right corner
    v3BackBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z);  // Back bottom left corner
    v3BackBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z);  // Back bottom right corner

    v3FrontTopLeft = objectToDrawTransform.transform.TransformPoint(v3FrontTopLeft);
    v3FrontTopRight = objectToDrawTransform.transform.TransformPoint(v3FrontTopRight);
    v3FrontBottomLeft = objectToDrawTransform.transform.TransformPoint(v3FrontBottomLeft);
    v3FrontBottomRight = objectToDrawTransform.transform.TransformPoint(v3FrontBottomRight);
    v3BackTopLeft = objectToDrawTransform.transform.TransformPoint(v3BackTopLeft);
    v3BackTopRight = objectToDrawTransform.transform.TransformPoint(v3BackTopRight);
    v3BackBottomLeft = objectToDrawTransform.transform.TransformPoint(v3BackBottomLeft);
    v3BackBottomRight = objectToDrawTransform.transform.TransformPoint(v3BackBottomRight);
}

void DrawBox()
{
    SpawnLineGenerator(v3FrontTopLeft, v3FrontTopRight, color);
    SpawnLineGenerator(v3FrontTopRight, v3FrontBottomRight, color);
    SpawnLineGenerator(v3FrontBottomRight, v3FrontBottomLeft, color);
    SpawnLineGenerator(v3FrontBottomLeft, v3FrontTopLeft, color);

    SpawnLineGenerator(v3BackTopLeft, v3BackTopRight, color);
    SpawnLineGenerator(v3BackTopRight, v3BackBottomRight, color);
    SpawnLineGenerator(v3BackBottomRight, v3BackBottomLeft, color);
    SpawnLineGenerator(v3BackBottomLeft, v3BackTopLeft, color);

    SpawnLineGenerator(v3FrontTopLeft, v3BackTopLeft, color);
    SpawnLineGenerator(v3FrontTopRight, v3BackTopRight, color);
    SpawnLineGenerator(v3FrontBottomRight, v3BackBottomRight, color);
    SpawnLineGenerator(v3FrontBottomLeft, v3BackBottomLeft, color);
}

void SpawnLineGenerator(Vector3 start, Vector3 end, Color color)
{
    GameObject myLine = new GameObject();

    myLine.tag = "FrameLine";
    myLine.name = "FrameLine";

    myLine.AddComponent<LineRenderer>();
    myLine.AddComponent<EndHolder>();
    myLine.GetComponent<EndHolder>().EndVector = end;
    LineRenderer lr = myLine.GetComponent<LineRenderer>();
    lr.material = new Material(Shader.Find("Particles/Alpha Blended Premultiply"));
    lr.startColor = color;
    lr.useWorldSpace = false;
    lr.endColor = color;
    lr.startWidth = 0.1f;//0.03f;
    lr.endWidth = 0.1f;//0.03f;
    lr.SetPosition(0, start);
    lr.SetPosition(1, start);

    allLines.Add(myLine);
}

IEnumerator moveStuff()
{
    for (int i = 0; i < allLines.Count; i++)
    {
        counter = 0;
        endPos = allLines[i].GetComponent<EndHolder>().EndVector;

        var dist = Vector3.Distance(endPos, startPos);
        while (counter < 25)//dist > 0f)
        {
            dist = Vector3.Distance(instancesToMove[0].transform.position, endPos);
            counter++;
            endPos = allLines[i].GetComponent<EndHolder>().EndVector;
            startPos = allLines[i].GetComponent<LineRenderer>().GetPosition(0);

            instancesToMove[0].transform.position =
                Vector3.MoveTowards(startPos, endPos, counter / 25f * speed);

            allLines[i].GetComponent<LineRenderer>().SetPosition(1, instancesToMove[0].transform.position);//tempPos);

            yield return null;
        }
    }
}

} What I want is to create animate the linerenderer lines and create smooth complete box

\$\endgroup\$
1
\$\begingroup\$

Because they're LineRenderers, not "BoxRenderers."

Each one's job is just to draw the one line you give it. It doesn't know anything about nearby lines being drawn by other objects, and it doesn't know you want it to connect to them.

You can get nicer corners by drawing multiple segments with a single LineRenderer - then it knows about both sides of a joint, and can connect them properly. But this won't work from all angles (cubes have 3-way corners, but the LineRenderer knows only about corners where 2 consecutive segments meet, not 3)

The simplest fix is to turn up the End Cap Vertices property on the LineRenderers. This will cap each end of a line with a semi-circle, which will help fill-in the joints where two lines meet. A value of 4 or 5 works pretty well, though you can go higher if you need it to look smooth when viewed from very close-up.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.