为什么我的精灵(自定义 CG 着色器)没有绘制在 Unity rendertexture 上

Why aren't my sprites (custom CG shader) drawn on a Unity 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"
}

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

#pragma surface surf BlinnPhongTransparent keepalpha // <= here

而且我发现了 "layers" 的其他问题,也许你知道这个:第一颗行星渲染在第二颗行星后面,但坐标首先最接近相机。

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

在着色器中:

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