2012-10-25 40 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

相關問題