示例#1
0
    /// <summary>
    /// The fastest form of visibility updates -- radius-based, no line of sights checks.
    /// 最快的可见性更新形-基于半径,不进行视线检查。
    /// 向buffer1的r填充可见性
    /// </summary>
    void RevealUsingRadius(IFOWRevealer r, float worldToTex)
    {
        // Position relative to the fog of war
        //相对于战争迷雾的位置
        Vector3 pos    = (r.GetPosition() - mOrigin) * worldToTex;  //纹理上的坐标
        float   radius = r.GetRadius() * worldToTex - radiusOffset; //纹理上的半径

        // Coordinates we'll be dealing with
        //我们将要处理的坐标
        //RoundToInt返回四舍五入到最接近的整数的f
        int xmin = Mathf.RoundToInt(pos.x - radius);
        int ymin = Mathf.RoundToInt(pos.z - radius);
        int xmax = Mathf.RoundToInt(pos.x + radius);
        int ymax = Mathf.RoundToInt(pos.z + radius);

        int cx = Mathf.RoundToInt(pos.x);
        int cy = Mathf.RoundToInt(pos.z);

        cx = Mathf.Clamp(cx, 0, textureSize - 1);
        cy = Mathf.Clamp(cy, 0, textureSize - 1);

        int radiusSqr = Mathf.RoundToInt(radius * radius);

        for (int y = ymin; y < ymax; ++y)
        {
            if (y > -1 && y < textureSize)
            {
                int yw = y * textureSize;

                for (int x = xmin; x < xmax; ++x)
                {
                    if (x > -1 && x < textureSize)
                    {
                        int xd   = x - cx;
                        int yd   = y - cy;
                        int dist = xd * xd + yd * yd;

                        // Reveal this pixel
                        if (dist < radiusSqr)
                        {
                            mBuffer1[x + yw].r = 255;
                        }
                    }
                }
            }
        }
    }