2012-10-25 42 views
4

我有一个灰度着色器,适用于材质时工作。 我想将它应用于相机呈现的内容。应用后处理着色器

我发现了一些教程谈到 无效OnRenderImage(渲染纹理源,渲染纹理目的地)

,但我无法使其与我的工作。

着色器:

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" 
} 

和连接到相机的脚本:

using UnityEngine; 
using System.Collections; 

public class Grayscale : ImageEffectBase { 

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

有什么建议? 非常感谢!

+0

统一标签用于Microsoft Unity。请不要滥用它。 –

回答

2

您必须添加顶点着色器,因为没有默认顶点着色器。这只是从顶点缓冲区复制数据。

#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; 
} 

和还你JAVE Pass {后添加ZTest Always

这显然是一个bug,但Unity3d支持说他们这样离开它(默认使用ZTest)而不是改变着色器的行为。我希望他们会重新考虑这个,并且默认使用ZTest Always作为Graphics.Blit

相关问题