Example #1
0
    private void DrawShadow()
    {
        RaycastHit2D[] hits = null;
        _source.GetHits(ref hits);
        if (hits.Length == 0)
        {
            return;
        }

        int vertCount  = hits.Length * 4;
        int indexCount = hits.Length * 6;

        Vector3[] verts   = new Vector3[vertCount];
        int[]     indices = new int[indexCount];
        var       uv      = new Vector2[verts.Length];

        int vi = 0;
        int ii = 0;

        for (int i = 0; i < hits.Length; i++)
        {
            // find edges
            var collider = hits[i].collider as CircleCollider2D;
            _source.GetShadowPositions(ref verts, vi, collider);

            // make two tris
            indices[ii]     = vi;
            indices[ii + 1] = vi + 1;
            indices[ii + 2] = vi + 2;
            indices[ii + 3] = vi + 1;
            indices[ii + 4] = vi + 2;
            indices[ii + 5] = vi + 3;

            vi += 4;
            ii += 6;
        }

        _mesh.Clear();
        _mesh.vertices  = verts;
        _mesh.triangles = indices;
        _mesh.RecalculateNormals();
        _mesh.RecalculateBounds();
        var size = _source.range * 2;

        for (int i = 0; i < verts.Length; i++)
        {
            var tv = (Vector2)verts[i] - _source.Position;

            uv[i].x = tv.x / size;
            uv[i].y = tv.y / size;
        }
        _mesh.uv = uv;
    }