Switching between lightmaps and custom shadergraphs for polyspatial lighting node

Hello,

When getting lightmaps to work we stumbled across this:

Does anyone have a good automated way to switch between the Lit Shader and a Custom Shadergraph that uses the Polyspatial lighting node without having to manually switch between every object and reassign textures?

We have 296+ static objects and this would be extremely tedious to change each one by hand and reassign the textures.

Any insight would be greatly appreciated!

Thank you!

We don’t include a script to do this, but one possibility is using a scene processor to replace the shaders right before builds/entering play mode. It might look something like this:

#if UNITY_EDITOR

using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LightmapMaterialSwapper : IProcessSceneWithReport
{
    public int callbackOrder => 0;

    public void OnProcessScene(Scene scene, BuildReport report)
    {
        var lightmapShader = Shader.Find("Shader Graphs/Lightmap");
        foreach (var renderer in Object.FindObjectsByType<MeshRenderer>(FindObjectsSortMode.None))
        {
            if (GameObjectUtility.GetStaticEditorFlags(renderer.gameObject).HasFlag(StaticEditorFlags.ContributeGI))
                renderer.material.shader = lightmapShader;
        }
    }
}

#endif

This assumes that the properties are the same between the original shader that you store in the scene (e.g., URP/Lit) and the lightmap shader graph that uses PolySpatial Lighting. For instance, you might want BaseColor and EmissionColor properties.

1 Like