5

I have a Grayscale shader that work when applied to a material. I'd like to apply it to what the camera renders.

I found some tutorial speaking about void OnRenderImage (RenderTexture source, RenderTexture destination)

but I wasn't able to make it work with mine.

The shader :

Shader "Custom/Grayscale" {

SubShader {
    Pass{

        CGPROGRAM

        #pragma exclude_renderers gles
        #pragma fragment frag

        #include "UnityCG.cginc"

        uniform sampler2D _MainTex;

        fixed4 frag(v2f_img i) : COLOR{
            fixed4 currentPixel = tex2D(_MainTex, i.uv);

            fixed grayscale = Luminance(currentPixel.rgb);
            fixed4 output = currentPixel;

            output.rgb = grayscale;
            output.a = currentPixel.a;

            //output.rgb = 0;

            return output;
        }

        ENDCG
    }

}

FallBack "VertexLit"
}

And the script attached to the camera :

using UnityEngine;
using System.Collections;

public class Grayscale : ImageEffectBase {

void OnRenderImage (RenderTexture source, RenderTexture destination) {
    Graphics.Blit (source, destination, material);
}
}

Any suggestions ? Many thanks !

1
  • The unity tag is for Microsoft Unity. Please don't misuse it.
    – Lex Li
    Oct 31, 2012 at 7:47

1 Answer 1

3

You have to add a vertex shader because there's no default vertex shader. This one just copies data from vertex buffer.

#pragma vertex vert
v2f_img vert (appdata_base v) {
    v2f_img o;
    o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    o.uv = v.texcoord;
    return o;
}

AND ALSO you jave to add ZTest Always after Pass {.

This is clearly a bug but Unity3d support says that they left it this way (that default ZTest is used) not to alter shaders behavior. I hope they'll reconsider this and make that ZTest Always is used by default for Graphics.Blit.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.