Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity3d 我的unity着色器上的Alpha通道不工作_Unity3d_Shader - Fatal编程技术网

Unity3d 我的unity着色器上的Alpha通道不工作

Unity3d 我的unity着色器上的Alpha通道不工作,unity3d,shader,Unity3d,Shader,所以我对着色器编程非常陌生(基本上今天才开始),我从Youtube上的一个教程中获得了这段代码,它非常有用。它只是在纹理的边缘找到了像素,如果是的话,用普通颜色替换它。我希望能够设置我返回的颜色的透明度 但它似乎不起作用 Shader "Custom/OutlineShader" { Properties { _MainTex ("Texture", 2D) = "white" {} _Color("Color", Color) = (1, 1,

所以我对着色器编程非常陌生(基本上今天才开始),我从Youtube上的一个教程中获得了这段代码,它非常有用。它只是在纹理的边缘找到了像素,如果是的话,用普通颜色替换它。我希望能够设置我返回的颜色的透明度

但它似乎不起作用

Shader "Custom/OutlineShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Color("Color", Color) = (1, 1, 1, 1)
        _AlphaOffset("Transparency", Range(0,1)) = 1
    }
    SubShader
    {
        Tags{ "Queue"="Transparent" "RenderType"="Opaque"}
        Cull Off ZWrite Off ZTest Always

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag Lambert alpha

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };


            fixed4 _Color;
            sampler2D _MainTex;
            float4 _MainTexelSize;
            float _AlphaOffset;

            v2f vert (appdata v)
            {
                v2f OUT;
                OUT.vertex = UnityObjectToClipPos(v.vertex);
                OUT.uv = v.uv;

                return OUT;
            }

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

                col.rgb *= col.a;

                fixed4 outlineColor = _Color;

                // This is where I want the shader to be transparent or not based on the _AlphaOffset Value
                outlineColor.a *= ceil(col.a) * _AlphaOffset;

                // This a code just to check is the current pixel is on the edge of the texture
                fixed upAlpha = tex2D(_MainTex, i.uv + fixed2(0, _MainTexelSize.y)).a;
                fixed downAlpha = tex2D(_MainTex, i.uv - fixed2(0, _MainTexelSize.y)).a;
                fixed leftAlpha = tex2D(_MainTex, i.uv - fixed2(_MainTexelSize.x, 0)).a;
                fixed rightAlpha = tex2D(_MainTex, i.uv + fixed2(_MainTexelSize.x, 0)).a;

                // If it's on the edge, return the color (+ alpha) else, just return the same pixel
                return lerp(outlineColor, col, ceil(upAlpha * downAlpha * leftAlpha * rightAlpha));
            }
            ENDCG
        }
    }
}

我想要这条线
outlineColor.a*=ceil(col.a)*\u AlphaOffset
设置我返回的像素的alpha


谢谢

这里主要有两件事是错误的-首先,您将RenderType设置为“不透明”,这将使其呈现为不透明。这应该改为设置为“透明”。其次,需要指定混合模式,以确定此对象的颜色如何与已渲染到缓冲区的颜色混合。从“混合”部分开始:

混合因素:配置并启用混合。这个 生成的颜色将乘以系数。颜色已经亮了 屏幕乘以系数,二者相加

对于常规alpha混合,请在子阴影中添加以下语句:

Blend SrcAlpha OneMinusSrcAlpha
对于产生类似辉光效果的添加剂混合,请使用以下方法:

Blend One One

您正在执行ceil(col.a),alpha参数只能在0.0和1.0之间。这样做的目的是什么?你的rendertype是不透明的,为什么它会让你有一个透明的像素和一个不透明的rendertype?