Manual cross fading?

Unity 2022.3.5f1 URP 14
Gosh, I hate having to make a forum post, but I can’t figure this out. I am trying to manually cross fade objects (no LODGroup component). Here is the code I am using:

using Unity.Mathematics;
using UnityEngine;

public class manual_cross_fade : MonoBehaviour
{
    public float4 fade;
    MeshRenderer renderer;
    MeshFilter filter;

    void Start()
    {
        renderer = gameObject.GetComponent<MeshRenderer>();
        filter = gameObject.GetComponent<MeshFilter>();

        renderer.sharedMaterial = Instantiate(renderer.sharedMaterial);
        renderer.sharedMaterial.EnableKeyword("LOD_FADE_CROSSFADE");
    }

    void Update()
    {
        renderer.sharedMaterial.SetVector("unity_LODFade", fade);
        // renderer.forceRenderingOff = false;

        // var cmd = CommandBufferPool.Get();
        // cmd.Clear();
        // cmd.DrawRenderer(renderer, mat);
        // Graphics.ExecuteCommandBuffer(cmd);
        // CommandBufferPool.Release(cmd);

        // Graphics.DrawMesh(filter.sharedMesh, Matrix4x4.identity, mat, 0);

        // var mpb = new MaterialPropertyBlock();
        // mpb.SetVector("unity_LODFade", math.float4(fade));
        // renderer.SetPropertyBlock(mpb);
    }
}

But none of those work. When I debug using “Frame Debugger” the “unity_LODFade” never changes. I think it is getting overridden down the line.

The other solution would be to reinvent the wheel and manually cross fade in the shader etc. But I don’t want to do that.

Or maybe built-in “unity_” prefixed vars aren’t meant to get modified? Is this a bug?

@hickv I wrote an article on how to override the LOD crossfading so you can control it with a shader float variable or the material colour’s alpha channel. Just scroll down to the section called Dissolving a Billboard.

1 Like

What you did works but it is quite hacky. I believe you are using custom shaders? I am using normal SG shaders, so ‘unwrapping’ all my shaders and rewiring #includes would be a pain.

I will most likely end up having to implement it myself into each shader unless someone comes up with a straightforward solution.