所以我对着色器编程非常陌生(基本上是从今天开始),我从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) * _AlphaOffset;
设置我要返回的像素的alpha。
谢谢!
答案 0 :(得分:0)
这里主要有两件事是错误的-首先,您已将RenderType设置为“ Opaque”,这有望使其呈现为非透明的。而是应将其设置为“透明”。其次,您需要指定一种混合模式,以确定该对象的颜色如何与已经渲染到缓冲区的颜色混合。从Unity manual开始融合:
混合SrcFactor DstFactor:配置并启用混合。的 生成的颜色乘以SrcFactor。颜色已经在 屏幕乘以DstFactor并将两者相加。
对于常规的alpha混合,请在您的着色器中添加以下语句:
- name: List sorting
hosts: localhost
gather_facts: no
vars:
MS2Packages:
- ms2-desert
- ms2-ctps
- ms2-apache
- ms2-w3gui
- ms2-provider
ProviderDebList:
- ms2-apache_1.6.1.8~20160324_amd64.deb
- ms2-ctps_1.6.1.8~20160324_amd64.deb
- ms2-desert_1.6.1.8~20160324_amd64.deb
- ms2-provider_1.6.1.8~20160324_amd64.deb
- ms2-w3gui_1.6.1.8+1~20160324_amd64.deb
tasks:
- name: Print package in the right order
debug:
msg: " - {{ ProviderDebList | map('regex_search', '.*'+order+'.*') | select('string') | list }}"
loop: "{{ MS2Packages }}"
loop_control:
loop_var: order
对于添加剂混合,它会产生类似发光的效果,请使用以下方法:
Blend SrcAlpha OneMinusSrcAlpha