0

I have this (part of the) Shader file:

            sampler2D _MainTex;
            float4 _MainTex_ST;
            sampler2D _HighLumiTex;
            float4 _HighLumiTex_ST;

            v2f vert (appdata v)
            { [untouched from default] }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);
                fixed4 highLumi = tex2D(_HighLumiTex, i.uv);
                return col + highLumi;
            }

And this (part of the) script:

    public Shader highLumiShader; // Working bloom shader
    public Shader blurShader; // Working gaussian blur shader
    public Shader compoShader;
    private Material highLumiMat;
    private Material blurMat;
    private Material compoMat;

    private void Awake()
    {
        highLumiMat = new Material(highLumiShader);
        blurMat = new Material(blurShader);
    }

    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        RenderTexture highLumiTex =
            RenderTexture.GetTemporary(source.width, 
            source.height, 0, source.format);
        RenderTexture blurTex =
            RenderTexture.GetTemporary(source.width, 
            source.height, 0, source.format);

        Graphics.Blit(source, highLumiTex, highLumiMat);
        Graphics.Blit(highLumiTex, blurTex, blurMat);

        compoMat.SetTexture("_HighLumiTex", blurTex); // This is the line that is giving an error.
        Graphics.Blit(source, destination, compoMat);

        RenderTexture.ReleaseTemporary(blurTex);
        RenderTexture.ReleaseTemporary(highLumiTex);
    }

I don't see why compoMat.SetTexture("_HighLumiTex", blurTex) wouldn't work, seeing as _HighLumiTex does exist in the compoShader file. Why would it be "object reference is not set to an instance of an object"?

I've tried changing the order, and the declaration in the other shaders but it seems like the only issue is the "SetTexture" line.

1 Answer 1

1

Unity shader files need a bit more metadata to define which properties are exposed and how.

Essentially, you need to define the Shader and Properties blocks in the shader source file before you get to the CG stuff.

See here for more info: https://docs.unity3d.com/Manual/SL-Properties.html

Sign up to request clarification or add additional context in comments.

5 Comments

I should mention that that's not the whole shader file! Just the bits I added / edited. The rest of the file is untouched from default as well.
What "default"? Also, since Unity requires the Properties section to make properties available to the CPU code, you should include that in your question as well.
As in exactly the way it gets created when you do Right Click -> Create -> Shader -> Unlit. So the rest of the file is untouched.
Well, the other issue could be that you're trying to set a texture on a variable (compoMat) that doesn't appear to have ever been initialized (like the others were in Awake). So, that's probably what's null.
I see it now, you're right, that was the issue. The guide I was following said "you can ignore that" but it seems like you absolutely cannot. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.