为什么我的精灵(自定义CG着色器)不是在Unity渲染上绘制的

时间:2017-07-17 09:13:53

标签: unity3d shader alpha render-to-texture cg

我有一个自定义的法线贴图+镜面精灵着色器,效果很好。但是,当我尝试绘制一个rendertexture时,我的精灵是不可见的。我猜他们的alpha设置为0,因为使用Unity的默认着色器的其他精灵被绘制好了。

我该如何解决这个问题?

Shader "SG/_ShipSurfaceShader" {
Properties {
    _MainTex ("Main texture", 2D) = "white" {}
    _NormalTex ("Normal map", 2D) = "bump" {}
    _SpecColor ("Specular colour", Color) = (1,1,1,1)
    _SpecPower ("Specular power", Range (0.0,2.0)) = 0.5
}
SubShader {
    Tags { "Queue"="Transparent" }
    Blend SrcAlpha OneMinusSrcAlpha
    LOD 200

    CGPROGRAM
    #pragma surface surf BlinnPhongTransparent alpha

    inline fixed4 LightingBlinnPhongTransparent (SurfaceOutput s, fixed3 lightDir, half3 viewDir, fixed atten) {
        half3 h = normalize (lightDir + viewDir);
        fixed diff = max (0, dot (s.Normal, lightDir));
        float nh = max (0, dot (s.Normal, h));
        float spec = pow (nh, s.Specular*32.0) * s.Gloss;
        fixed4 c;
        c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * _SpecColor.rgb * spec) * (atten);
        c.a = s.Alpha;
        return c;
    }

    sampler2D _MainTex;
    sampler2D _NormalTex;
    half _SpecPower;

    struct Input {
        float2 uv_MainTex;
    };

    void surf (Input IN, inout SurfaceOutput o) {
        fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
        o.Albedo = c.rgb;
        o.Alpha = c.a;
        o.Normal = UnpackNormal(tex2D (_NormalTex, IN.uv_MainTex));
        o.Specular = _SpecPower;
        o.Gloss = 1;
    }
    ENDCG
} 
FallBack "Diffuse"
}

1 个答案:

答案 0 :(得分:1)

编辑:所以,我发现了问题!

#pragma surface surf BlinnPhongTransparent keepalpha // <= here

我发现了“图层”的其他问题,也许你知道这个:第一个行星在第二个行星后面渲染,但是在最接近相机的坐标中。

RT_lowres = new RenderTexture(width_rt, height_rt, 1, RenderTextureFormat.ARGB32);// if 0 bad "layers"

一些提示:您可以在着色器中使用全局变量来提高性能!洛尔

void Start(){
    //....
    lowresCam.targetTexture = RT_lowres;
    Shader.SetGlobalTexture("_LowResTexture", RT_lowres);
}

void OnRenderImage(RenderTexture src, RenderTexture dest){
    Graphics.Blit(src, dest, compositionMaterial);
}

在Shader中:

Properties
{
    _MainTex ("Texture", 2D) = "white" {}
}
//.....
sampler2D _MainTex;
sampler2D _LowResTexture;
fixed4 frag (v2f i) : SV_Target
{
    //...
    fixed4 lowres = tex2D(_LowResTexture, i.uv);
    //...
}