【问题标题】:How to transparent Unity3D custom shader?如何透明 Unity3D 自定义着色器?
【发布时间】:2017-01-07 19:11:38
【问题描述】:

我有一个自定义着色器,可以在对象内部渲染纹理(例如:球体)。但是我不能让它透明,因为 Unity 说该着色器中没有添加 _Color 属性。

我不是 Unity Shader 方面的专家,所以需要一些帮助来制作我现有的着色器。这是我的着色器代码。

Shader "InsideVisible" 
{
    Properties 
    {
        _MainTex ("Base (RGB)", 2D) = "white" {}
    }

    SubShader 
    {
        Tags { "RenderType"="Opaque" }
        Cull front
        LOD 100

        Pass 
        {  
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata_t {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
            };

            struct v2f {
                float4 vertex : SV_POSITION;
                half2 texcoord : TEXCOORD0;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert (appdata_t v)
            {
                v2f o;

                o.vertex     = mul(UNITY_MATRIX_MVP, v.vertex);
                v.texcoord.x = 1 - v.texcoord.x;                
                o.texcoord   = TRANSFORM_TEX(v.texcoord, _MainTex);

                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.texcoord);
                return col;
            }

            ENDCG
        }
    }
}

【问题讨论】:

  • 发布的着色器在 Unity 中应用时似乎可以正常工作。您是否尝试通过脚本或其他方式访问 _Color 属性?
  • 是的。我试图通过脚本使其透明。普通的标准着色器很容易变得透明,但这个自定义着色器不是。否则这个着色器工作得很好。

标签: unity3d textures shader


【解决方案1】:

您的脚本可能正在尝试访问名为 _Color 的属性来设置其 alpha 值,但此着色器上没有 _Color 属性,因此脚本失败。

此外,着色器未设置为渲染透明度,因此我们需要修复它。

着色器属性有点像您在 C# 类上公开的公共属性:外部世界可以操纵它们来改变着色器的渲染方式。

在片段中,我们将图像颜色乘以_Color 属性,以便它可以控制色调和透明度。

为了让你的着色器渲染透明,你还需要在 SubShader 和 pragma 中添加几行代码。

Shader "InsideVisible" 
{
    Properties 
    {
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _Color ("Color (RGBA)", Color) = (1, 1, 1, 1) // add _Color property
    }

    SubShader 
    {
        Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
        ZWrite Off
        Blend SrcAlpha OneMinusSrcAlpha
        Cull front 
        LOD 100

        Pass 
        {
            CGPROGRAM

            #pragma vertex vert alpha
            #pragma fragment frag alpha

            #include "UnityCG.cginc"

            struct appdata_t 
            {
                float4 vertex   : POSITION;
                float2 texcoord : TEXCOORD0;
            };

            struct v2f 
            {
                float4 vertex  : SV_POSITION;
                half2 texcoord : TEXCOORD0;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float4 _Color;

            v2f vert (appdata_t v)
            {
                v2f o;

                o.vertex     = mul(UNITY_MATRIX_MVP, v.vertex);
                v.texcoord.x = 1 - v.texcoord.x;
                o.texcoord   = TRANSFORM_TEX(v.texcoord, _MainTex);

                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.texcoord) * _Color; // multiply by _Color
                return col;
            }

            ENDCG
        }
    }
}

【讨论】:

  • 现在可以了。非常感谢!这是我 10 天以来一直在寻找的正确的东西!
  • 谢谢。我发现在我的情况下不需要“剔除前端”,因为这消除了实际的阴影。
猜你喜欢
  • 2023-04-03
  • 1970-01-01
  • 2013-10-19
  • 1970-01-01
  • 1970-01-01
  • 2015-09-11
  • 2021-06-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多