Unity3d 为什么平面在Unity 3D中渲染2次?

Unity3d 为什么平面在Unity 3D中渲染2次?,unity3d,3d,rendering,shader,unity5,Unity3d,3d,Rendering,Shader,Unity5,首先,我想对你们说声抱歉,我不能在每一行之后马上上传图片,因为我的声誉不够(一个问题中甚至不能发布超过2张图片) 以下是所有图像的驱动链接: 我正在用Unity3D和FogOfWar库制作一个游戏。它使用的是带有以下组件的平面:(请参见驱动链接,谢谢) 启动时将重新创建网格过滤器: halfPlaneSize = (float)planeSize * 0.5f; nodeSize = 1f / (float)nodeResolution; vertCount = ((in

首先,我想对你们说声抱歉,我不能在每一行之后马上上传图片,因为我的声誉不够(一个问题中甚至不能发布超过2张图片)

以下是所有图像的驱动链接:

我正在用Unity3D和FogOfWar库制作一个游戏。它使用的是带有以下组件的平面:(请参见驱动链接,谢谢) 启动时将重新创建网格过滤器:

    halfPlaneSize = (float)planeSize * 0.5f;
    nodeSize = 1f / (float)nodeResolution;
    vertCount = ((int)(planeSize * nodeResolution)) + 1;

    mesh = GetComponent<MeshFilter>().mesh;
    mesh.Clear();

    Vector3 offset = new Vector3(-halfPlaneSize, 0, -halfPlaneSize);
    Vector3[] vs = this.vs = new Vector3[vertCount * vertCount];
    Vector2[] uvs = this.uvs = new Vector2[vs.Length];
    revealedArea = new bool[vs.Length];

    int[] tris = new int[(vertCount - 1) * (vertCount - 1) * 6];

    for (int r = 0; r < vertCount; ++r)
    {
        for (int c = 0; c < vertCount; ++c)
        {
            int i = r + (vertCount * c);

            vs[i] = offset + new Vector3(r * nodeSize, 0f, c * nodeSize);

            if (terrain != null)
            {
                vs[i].y = terrain.SampleHeight(transform.TransformPoint(new Vector3(vs[i].x, 0, vs[i].z)));
            }
        }
    }

    if (useFadingOrBlockyShader)
    {
        for (int i = 0; i < vs.Length; ++i)
        {
            uvs[i] = new Vector2(-100f, -100f);
        }
    }
    else
    {
        for (int i = 0; i < vs.Length; ++i)
        {
            uvs[i] = new Vector2(fogInstantDarkness, fogInstantDarkness); // 1f
        }
    }

    int t = 0;

    for (int y = 0; y < vertCount - 1; y++)
    {
        for (int x = 0; x < vertCount - 1; x++)
        {
            tris[t] = (y * vertCount) + x;
            tris[t + 1] = ((y + 1) * vertCount) + x;
            tris[t + 2] = (y * vertCount) + x + 1;

            tris[t + 3] = ((y + 1) * vertCount) + x;
            tris[t + 4] = ((y + 1) * vertCount) + x + 1;
            tris[t + 5] = (y * vertCount) + x + 1;
            t += 6;
        }
    }

    mesh.vertices = vs;
    mesh.uv = uvs;
    mesh.triangles = tris;
    mesh.RecalculateNormals();
我注意到,一旦我激活了revealer,一切都像下图一样正常:(请参见驱动链接,谢谢)

在那之后,仅仅一眨眼,它似乎又被渲染了,它变得更糟,像这样:(请参阅驱动链接,谢谢)

我不知道Unity是如何处理的。你能告诉我为什么会发生这种情况吗?如果有办法让它保持在第一状态(良好状态),那就太好了。如果您需要更多信息,请告诉我,然后我会立即更新

谢谢你的阅读

以下是驱动链接:

请尽量提供答案,以最大化您的问题被回答的机会。请尽量提供答案,以最大化您的问题被回答的机会。
void Update()
{
    frameCount += 1;
    UpdateFog = frameCount >= updateEveryNFrames || UpdateFog || ClearAll;
    if (frameCount >= updateEveryNFrames)
    {
        UpdateFogForVisibilityChecker = true;
    }
    else
    {
        UpdateFogForVisibilityChecker = false;
    }
    if (ClearAll)
    {
        for (int i = 0; i < uvs.Length; ++i)
        {
            uvs[i].x = fogInstantDarkness;
        }

        ClearAll = false;
    }

    if (UpdateFog)
    {
        if (useFadingOrBlockyShader) // I don't enable this
        {
            revealValue = Time.time;
        }
        else
        {
            //If we're updating fog, make it all black //old
            for (int i = 0; i < uvs.Length; ++i)
            {
                if (revealedArea[i]) // if that area was revealed before, make it less dark
                {
                    uvs[i].x = fogLessDarkness;
                }
                else // if that area wasn't revealed before, make it black
                {
                    uvs[i].x = fogInstantDarkness;
                }
            }
        }
    }

    GetComponent<Renderer>().material.SetFloat("_WorldTime", revealValue);
}
public void Reveal(Vector3 pos, float radius)
{
    pos = transform.InverseTransformPoint(pos);

    int r = Mathf.RoundToInt(((planeSize / 2f) + pos.x) * nodeResolution);
    int c = Mathf.RoundToInt(((planeSize / 2f) + pos.z) * nodeResolution);
    int center = r + (vertCount * c);

    if (r >= 0 && r < vertCount && c >= 0 && c < vertCount && center < uvs.Length)
    {
        radius *= nodeResolution;

        int row = Mathf.Clamp(r - (int)radius, 0, int.MaxValue);
        int col = Mathf.Clamp(c - (int)radius, 0, int.MaxValue);

        int row_end = Mathf.Clamp(r + (int)radius, 0, vertCount);
        int col_end = Mathf.Clamp(c + (int)radius, 0, vertCount);

        int row_mid = (row+row_end)/2;

        int col_mid = (col+col_end)/2;

        float sqr = radius * radius;

        Vector3 v = Vector3.zero;
        Vector3 ct = new Vector3(r, c);

        for (; row < row_end; ++row)
        {
            col = Mathf.Clamp(c - (int)radius, 0, int.MaxValue);

            for (; col < col_end; ++col)
            {
                v = new Vector3(row, col);

                float sqr_to_target = (ct - v).sqrMagnitude;
                if (sqr_to_target < sqr)
                {
                    float radius_per = sqr_to_target/sqr;                       
                    radius_per*=radius_per;
                    int tmp = row + (vertCount * col);
                    uvs[tmp].x*= radius_per;

                    revealedArea[tmp] = true; //reveal that area
                }
            }
        }
    }
}
Shader "Mobile/Fog of War (Instant - Faded)" {  
Properties {  }  
Subshader {
Tags { "Queue"="Overlay-50" "RenderType"="Transparent" }

Pass {  
    Cull Back 
    Lighting Off 
    ZWrite Off
    ZTest Always
    Blend Zero OneMinusSrcAlpha
    Fog { Mode Off }

    CGPROGRAM

    #pragma vertex vert_img
    #pragma fragment frag

    struct appdata_img {
        float4 vertex : POSITION;
        half2 texcoord : TEXCOORD0;
    };

    struct v2f_img {
        float4 pos : SV_POSITION;
        half2 uv : TEXCOORD0;
    };

    v2f_img vert_img( appdata_img v )
    {
        v2f_img o;

        o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
        o.uv = v.texcoord;

        return o;
    }

    float4 frag (v2f_img i) : COLOR
    {   
        return float4(1, 1, 1, i.uv.x);
    }

    ENDCG
}
}
}