#Switching to 'Shader.SetGlobalTexture()' to a selected Shader setter

1 messages · Page 1 of 1 (latest)

regal plank
#

Hi,

I’m working on a legacy Unity project originally created in 2016, and I need to add a multiplayer component to it. It’s a custom implementation—not using a traditional client-server setup. I just send inputs and positions between players.

However, I’m currently stuck on an issue related to shader management.

In this project, I instantiate multiple spray guns, each with a Paint.cs script. This script creates and applies a Paint.shader that sets global shader textures like this:

  • SEE SetPaintShaderSettings() below *

The problem is that Shader.SetGlobalTexture applies globally, so all spray guns end up sharing the same textures, which is not what I want. I’d like each spray gun to use its own set of textures/floats/colors — similar to how we can do it with materials using mat.SetTexture("_MainTex", texture);.

Additionally, the shader is applied to the camera using:

  • SEE SetCameraReplacementShader() below *

However, I couldn’t find any way to retrieve the shader assigned to a camera via SetReplacementShader(Shader shader, string replacementTag), since there’s no getter available for that.

Do you have any ideas on how to assign unique shader parameters per spray gun, or work around the global texture limitation?
To be honest, I’m still a beginner with shaders and textures, so things are a bit unclear to me 😕

Thanks in advance!

#

This is only a part of the code :

    private RenderTexture m_paintRenderTex;
    private RenderTexture m_paintTex;
    private RenderTexture m_depthRenderTex;
    private RenderTexture m_dropletsRenderTex;
    private RenderTexture m_backgroundRenderTex;
    private Camera m_depthCamera;
    private Camera m_dropletsCamera;
    private Camera m_paintCamera;
    private Camera m_backgroundCamera;
    private Shader m_paintShader;
    private Shader m_depthShader;

private void Awake()
{
    #region Configure paint camera
    m_paintCamera = gameObject.GetComponent<Camera>();
    m_paintShader = Shader.Find("FPIR/Rendering/Paint");
    // Set paint shader as replacement shader for paint camera
    SetCameraReplacementShader(m_paintCamera, m_paintShader);
    
    // Set paint render texture as paint camera target
    m_paintCamera.targetTexture = m_paintRenderTex;
    
    m_paintCamera.cullingMask = 1 << m_paintableObjectsLayer.LayerIndex;
    #endregion

    #region Configure depth camera
    Transform depth = transform.Find("Depth");

    if (depth != null)
    {
        m_depthCamera = depth.GetComponent<Camera>();

        if (m_depthCamera != null)
        {
            m_depthCamera.targetTexture = m_depthRenderTex;
            m_depthShader = Shader.Find("FPIR/Rendering/DepthShader");
            SetCameraReplacementShader(m_depthCamera, m_depthShader);

            m_depthCamera.cullingMask = 1 << m_paintableObjectsLayer.LayerIndex;
        }
        else
        {
            Debug.LogError("Depth camera not found");
        }
    }
    else
    {
        Debug.LogError("Depth camera not found");
    }
    #endregion

        #region Set paint shader settings
        Debug.Log($"[Paint.cs] AWAKE: Setting paint shader settings with path: {this.transform.parent?.parent?.parent?.name}/{this.transform.parent?.parent?.name}/{this.transform.parent?.name}/{this.transform.name}");
        SetPaintShaderSettings();
        #endregion