Shader "Custom/ChromakeyTransparent" { Properties{ _MainTex("Base (RGB)", 2D) = "white" {} _TransparentColourKey("Transparent Colour Key", Color) = (0,0,0,1) _TransparencyTolerance("Transparency Tolerance", Float) = 0.01 } SubShader{ Pass{ Tags{ "RenderType" = "Opaque" } LOD 200 CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct a2v { float4 pos : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(a2v input) { v2f output; output.pos = UnityObjectToClipPos(input.pos); output.uv = input.uv; return output; } sampler2D _MainTex; float3 _TransparentColourKey; float _TransparencyTolerance; float4 frag(v2f input) : SV_Target { // What is the colour that *would* be rendered here? float4 colour = tex2D(_MainTex, input.uv); // Calculate the different in each component from the chosen transparency colour float deltaR = abs(colour.r - _TransparentColourKey.r); float deltaG = abs(colour.g - _TransparentColourKey.g); float deltaB = abs(colour.b - _TransparentColourKey.b); // If colour is within tolerance, write a transparent pixel if (deltaR < _TransparencyTolerance && deltaG < _TransparencyTolerance && deltaB < _TransparencyTolerance) { return float4(0.0f, 0.0f, 0.0f, 0.0f); } // Otherwise, return the regular colour return colour; } ENDCG } } }
时间: 2024-02-10 20:29:43 浏览: 143
vertex-shader-program-matlab.rar_SHADER_matlab vertex_color
这是一个Unity shader,用于实现基于颜色键的透明效果。在Properties中,定义了一个主纹理_MainTex、一个透明色_TransparentColourKey和一个透明度容差_TransparencyTolerance。在SubShader中,定义了一个Pass,其中使用了vertex shader函数vert和fragment shader函数frag实现渲染效果。在frag函数中,首先获取该像素点的颜色,然后计算该颜色与透明色之间的差异,如果差异小于设定的容差,就将该像素点设为透明,否则返回原始颜色。
阅读全文