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 浏览: 156
这是一个Unity shader,用于实现基于颜色键的透明效果。在Properties中,定义了一个主纹理_MainTex、一个透明色_TransparentColourKey和一个透明度容差_TransparencyTolerance。在SubShader中,定义了一个Pass,其中使用了vertex shader函数vert和fragment shader函数frag实现渲染效果。在frag函数中,首先获取该像素点的颜色,然后计算该颜色与透明色之间的差异,如果差异小于设定的容差,就将该像素点设为透明,否则返回原始颜色。
相关问题
将下面的shader属性由transparent修改为opaque Shader "Custom/Texture Splatting" { Properties { _MainTex ("Splat Map", 2D) = "white" {} [NoScaleOffset] _Texture1 ("Texture 1", 2D) = "white" {} [NoScaleOffset] _Texture2 ("Texture 2", 2D) = "white" {} [NoScaleOffset] _Texture3 ("Texture 3", 2D) = "white" {} [NoScaleOffset] _Texture4 ("Texture 4", 2D) = "white" {} } SubShader { Pass { CGPROGRAM #pragma vertex MyVertexProgram #pragma fragment MyFragmentProgram #include "UnityCG.cginc" sampler2D _MainTex; float4 _MainTex_ST; sampler2D _Texture1, _Texture2, _Texture3, _Texture4; struct VertexData { float4 position : POSITION; float2 uv : TEXCOORD0; }; struct Interpolators { float4 position : SV_POSITION; float2 uv : TEXCOORD0; float2 uvSplat : TEXCOORD1; }; Interpolators MyVertexProgram (VertexData v) { Interpolators i; i.position = UnityObjectToClipPos(v.position); i.uv = TRANSFORM_TEX(v.uv, _MainTex); i.uvSplat = v.uv; return i; } float4 MyFragmentProgram (Interpolators i) : SV_TARGET { float4 splat = tex2D(_MainTex, i.uvSplat); return tex2D(_Texture1, i.uv) * splat.r + tex2D(_Texture2, i.uv) * splat.g + tex2D(_Texture3, i.uv) * splat.b + tex2D(_Texture4, i.uv) * (1 - splat.r - splat.g - splat.b); } ENDCG } } }
将下面的代码中的 SubShader 中的 Pass 的 shader 属性由 "Transparent" 修改为 "Opaque" 即可:
```
Shader "Custom/Texture Splatting" {
Properties {
_MainTex ("Splat Map", 2D) = "white" {}
[NoScaleOffset] _Texture1 ("Texture 1", 2D) = "white" {}
[NoScaleOffset] _Texture2 ("Texture 2", 2D) = "white" {}
[NoScaleOffset] _Texture3 ("Texture 3", 2D) = "white" {}
[NoScaleOffset] _Texture4 ("Texture 4", 2D) = "white" {}
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex MyVertexProgram
#pragma fragment MyFragmentProgram
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _Texture1, _Texture2, _Texture3, _Texture4;
struct VertexData {
float4 position : POSITION;
float2 uv : TEXCOORD0;
};
struct Interpolators {
float4 position : SV_POSITION;
float2 uv : TEXCOORD0;
float2 uvSplat : TEXCOORD1;
};
Interpolators MyVertexProgram (VertexData v) {
Interpolators i;
i.position = UnityObjectToClipPos(v.position);
i.uv = TRANSFORM_TEX(v.uv, _MainTex);
i.uvSplat = v.uv;
return i;
}
float4 MyFragmentProgram (Interpolators i) : SV_TARGET {
float4 splat = tex2D(_MainTex, i.uvSplat);
return
tex2D(_Texture1, i.uv) * splat.r +
tex2D(_Texture2, i.uv) * splat.g +
tex2D(_Texture3, i.uv) * splat.b +
tex2D(_Texture4, i.uv) * (1 - splat.r - splat.g - splat.b);
}
ENDCG
}
// 将下面的 shader 属性由 "Transparent" 修改为 "Opaque"
Tags { "RenderType"="Opaque" }
}
}
```
阅读全文
相关推荐
















