示例#1
0
    EdgeInfo FindEdge(LightCastInfo minLightCast, LightCastInfo maxLightCast)
    {
        float   minAgle  = minLightCast.angle;
        float   maxAngle = maxLightCast.angle;
        Vector3 minPoint = Vector3.zero;
        Vector3 maxPoint = Vector3.zero;

        for (int i = 0; i < edgeResolveIterations; i++)
        {
            float         angle        = (minAgle + maxAngle) / 2;
            LightCastInfo newLightCast = LightCast(angle);

            bool edgeDistanceThresholdExceeded = Mathf.Abs(minLightCast.dist - newLightCast.dist) > edgeDistanceThreshold;
            if (newLightCast.hit == minLightCast.hit && !edgeDistanceThresholdExceeded)
            {
                minAgle  = angle;
                minPoint = newLightCast.point;
            }
            else
            {
                maxAngle = angle;
                maxPoint = newLightCast.point;
            }
        }
        return(new EdgeInfo(minPoint, maxPoint));
    }
示例#2
0
    void DrawLightField()
    {
        int            stepCount     = Mathf.RoundToInt(lightAngle * meshResolution);
        float          stepAngleSize = lightAngle / stepCount;
        List <Vector3> lightPoints   = new List <Vector3>();
        LightCastInfo  oldLightCast  = new LightCastInfo();

        for (int i = 0; i <= stepCount; i++)
        {
            float         angle        = transform.eulerAngles.z - lightAngle / 2 + stepAngleSize * i;
            LightCastInfo newLightCast = LightCast(angle);

            if (i > 0)
            {
                bool edgeDistanceThresholdExceeded = Mathf.Abs(oldLightCast.dist - newLightCast.dist) > edgeDistanceThreshold;
                if (oldLightCast.hit != newLightCast.hit || oldLightCast.hit && newLightCast.hit && edgeDistanceThresholdExceeded)
                {
                    EdgeInfo edge = FindEdge(oldLightCast, newLightCast);
                    if (edge.pointA != Vector3.zero)
                    {
                        lightPoints.Add(edge.pointA);
                    }
                    if (edge.pointB != Vector3.zero)
                    {
                        lightPoints.Add(edge.pointB);
                    }
                }
            }

            lightPoints.Add(newLightCast.point);
            oldLightCast = newLightCast;
        }
        int vertexCount = lightPoints.Count + 1;

        Vector3[] vertecies = new Vector3[vertexCount];
        Vector2[] uvs       = new Vector2[vertecies.Length];
        int[]     triangles = new int[(vertexCount - 2) * 3];

        vertecies[0] = Vector3.zero;
        for (int i = 0; i < vertexCount - 1; i++)
        {
            var vert = transform.InverseTransformPoint(lightPoints[i]) * lightMultiplier;

            vertecies[i + 1] = vert;
            if (i < vertexCount - 2)
            {
                triangles[i * 3]     = 0;
                triangles[i * 3 + 1] = i + 1;
                triangles[i * 3 + 2] = i + 2;
            }
        }
        for (int i = 0; i < uvs.Length; i++)
        {
            uvs[i] = new Vector2(0.5f + (vertecies[i].x) / (2 * (lightRadius * lightMultiplier)), 0.5f + (vertecies[i].y) / (2 * (lightRadius * lightMultiplier)));
        }

        lightMesh.Clear();
        lightMesh.vertices  = vertecies;
        lightMesh.triangles = triangles;
        lightMesh.uv        = uvs;
        lightMesh.RecalculateNormals();
    }