Using Post Processing Shaders

I have a Grayscale shader that works when applied to material. I would like to apply it to what the camera does.

I found a tutorial talking about void OnRenderImage (source RenderTexture, destination RenderTexture)

but I could not get him to work with mine.

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? Thank you very much!

+4
source share
1 answer

You need to add a vertex shader because there is no vertex shader by default. It just copies the data from the 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 can add ZTest Always after Pass { .

This is clearly a mistake, but Unity3d support says they left it that way (which is the default ZTest) so as not to change the behavior of the shaders. I hope they review this and make ZTest Always default for Graphics.Blit .

+2
source

Source: https://habr.com/ru/post/1442089/


All Articles