transparent mode does not work at alpha=255

Hi, i’m trying to implement a fade for my surfaces, when alpha=255, i want the object to be fully opaque, when the alpha<255, i want the object to fade. however, when i use transparent or fade mode, the alpha=255 case looks weird (when using Transparent/Diffuse shader)

see below:
LEFT: transparent or fade mode, alpha=255, shader = Transparent/Diffuse
RIGHT: any mode, alpha=255, shader = VertexLit
bottom: transparent or fade mode, alpha=50, shader=Transparent/Diffuse

i would like to be able to fade the object, while also maintaining full opacity at alpha=255, which i have been unable to do so far.

here is my code:

  public void SetTransparent(string objectString, byte alpha)
    {
        GameObject pial = GameObject.Find(objectString);
        MeshRenderer[] rendererArray = pial.GetComponentsInChildren<MeshRenderer>();
        MeshRenderer renderer = rendererArray[0];
        Material material = renderer.material;
        //material.SetFloat("_Mode", 4f);

        material.SetOverrideTag("RenderType", "Fade");

        Shader shade = null; 
        shade = Shader.Find("Transparent/Diffuse");

        material.shader = shade; 

        Color32 col = renderer.material.GetColor("_Color");
        col.a = alpha;
        col.r = 195;
        col.g = 127;
        col.b = 80;
        renderer.material.SetColor("_Color", col);

        
        material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
        material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
        material.SetInt("_ZWrite", 0);
        material.DisableKeyword("_ALPHATEST_ON");
        material.EnableKeyword("_ALPHABLEND_ON");
        material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
        material.renderQueue = 3000;
        
        

    }

Transparency is a huge problem when you deal with just a single object. For transparency to work correctly things need to be drawn back to front. Unity does sort transparent objects based on their location (pivot) back to front. However the individual polygons within a single mesh are never sorted and usually rendered in the order they appear in the triangles array. Any transparent shader usually does not write to the depth buffer but it does perform a zTest (since opaque shaders are rendered first front to back to avoid overdraw).

If an object has a quite low alpha value the slight error due to wrong drawing order is almost neglectable. However the more opaque it gets the more visible the difference will be. At full opacity you can get very strange results as triangles which might be further away can overdraw those which are closer. Depending on the requirements you could use a stipple shader which basically renders the whole thing opaque with proper depth writing but simply omits more and more fragments (pixels) the more transparent the object should be.

So actually using alpha blending doesn’t work if the object has self occluding triangles. You would need to sort your triangles based on the distance from the camera. If your camera view or the object rotation can change this would have to be redone each time you do so. This may be impractical for large meshes… Though it could possibly be sped up by using some space partitioning.

So i don’t think anybody has an easy solution for this problem. This issue is as old as computer graphics and still does not have an easy solution. More information can be found here.

If you just want to have the object being transparent without seeing other triangles from the same object you can use a two pass approach where you first just render the object into the depth buffer without writing color information and the second pass does actually render the transparent object. That way only the visible surface projection is visible but no triangles which are occluded. So only other objects which are behind the object you’re rendering can be seen.