Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

DrawLine from Object to Cube's vertices

hugoassisjhugoassisj Member
edited May 2020 in Programming

So, I'm trying to draw lines from an object position to other object vertices, using the following code:

  public Transform light;
  public Transform cube;
  Mesh mesh;
  Vector3[] vertices;
  void Update()
  {
    mesh = cube.GetComponent<MeshFilter>().mesh;
    vertices = mesh.vertices;
    for (var i = 0; i < vertices.Length; i++)
    {
      Debug.DrawLine(light.transform.position, cube.transform.position + transform.TransformPoint(vertices[i]), UnityEngine.Color.cyan);
    }
  }


This is giving me the following result:


So the lines do not match the vertices positions and I can't figure out why.

Can someone give me some help? Why are the points in wrong positions?

Thank you

Answers

  • MrSlugMrSlug Member
    edited May 2020

    Hey @hugoassisj,

    This is tricky one, I've not played with vertices too much however I've edited your script and I've managed to get the DrawLine function slightly closer than your example by calculating the offset. However, the lines don't quite perfectly touch each vertex which I'm guessing you want it to. Perhaps someone will reply and explain why? I'll keep testing this and let you know if I can solve it.

    public Transform light;
        public Transform cube;
        Mesh mesh;
        Vector3[] vertices;
    
    
        void Update() {
            mesh = cube.GetComponent<MeshFilter>().mesh;
            vertices = mesh.vertices;
            foreach( Vector3 vertice in vertices ){
                Vector3 offset = light.transform.position - cube.transform.position;
                Debug.DrawLine( light.transform.position, transform.TransformPoint( vertice ) - offset, UnityEngine.Color.cyan );
            }
        }
    

    Good luck!

    Cheers,

    Slug

  • Thank you!

    The whole idea is that I wanna know the points defining the shadow in the back wall. That's why I'm trying to manually ray-casting from the light source to the vertices of the object, so I can later extend those lines to the back plane to match the vertices of the shadow on the wall and use some convex hull algorithm to draw the shadow outline manually.

    Maybe there is another approach to this. Maybe the shadow borders are already defined somewhere else.

    This was me trying to figure it out the hard way, I think.

    If someone could put light on this, I appreciate it.

Sign In or Register to comment.