只显示部分纹理
Show only part of texture
我有透明圆圈,我想通过着色器只显示它的一小部分,例如从 30 度到 90 度,我的想法是从片段中获取他的原点并将其传递给顶点通过这样做 像素而不是顶点使它更“干净”。
这就是我所拥有的,任何人都可以帮助我弄清楚如何做到这一点?
Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
Shader "Unlit/PipeShader"
{
Properties
{
_Color ("Main Color (A=Opacity)", Color) = (1,1,1,1)
_MainTex ("Base (A=Opacity)", 2D) = "" {}
_Transparency("Transparency", Range(0.0,0.5)) = 0.25
}
SubShader
{
Tags
{
"Queue"="Transparent"
"RenderType"="Transparent"
"IgnoreProjector" = "true"
}
Zwrite Off
Blend SrcAlpha OneMinusSrcAlpha
Cull Off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float4 worldSpacePos : TEXCOORD0;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _Color;
float _Transparency;
float4 setAxisAngle (float3 axis, float rad) {
rad = rad * 0.5;
float s = sin(rad);
return float4(s * axis[0], s * axis[1], s * axis[2], cos(rad));
}
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float4 objectOrigin = mul(unity_ObjectToWorld, float4(0.0,0.0,0.0,1.0));
i.worldSpacePos = mul(unity_ObjectToWorld,i.vertex);
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv) * _Color;
col.a = _Transparency;
return col;
}
ENDCG
}
}
}
最简单的方法是选择一个指向范围中心的轴,然后获取从 UV 坐标到 UV 中心的轴,并获取它们之间的点积:
float2 a = float2(cos(_Angle), sin(_Angle)); // Note that _Angle is in radians!
float2 b = normalize(i.uv - (float2)0.5)
half d = dot(a, b);
// dot(a, b) gets the cosine of the angle between a and b
half maxAngle = 0.5 // 45 degrees range
half mask = d > maxAngle ? 1 : 0;
col.a *= mask;
return col
这将为您提供一个介于 angle-22.5 和 angle+22.5 度之间的扇区。不过,它不会为您提供非常人性化的参数。
我有透明圆圈,我想通过着色器只显示它的一小部分,例如从 30 度到 90 度,我的想法是从片段中获取他的原点并将其传递给顶点通过这样做 像素而不是顶点使它更“干净”。
这就是我所拥有的,任何人都可以帮助我弄清楚如何做到这一点?
Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
Shader "Unlit/PipeShader"
{
Properties
{
_Color ("Main Color (A=Opacity)", Color) = (1,1,1,1)
_MainTex ("Base (A=Opacity)", 2D) = "" {}
_Transparency("Transparency", Range(0.0,0.5)) = 0.25
}
SubShader
{
Tags
{
"Queue"="Transparent"
"RenderType"="Transparent"
"IgnoreProjector" = "true"
}
Zwrite Off
Blend SrcAlpha OneMinusSrcAlpha
Cull Off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float4 worldSpacePos : TEXCOORD0;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _Color;
float _Transparency;
float4 setAxisAngle (float3 axis, float rad) {
rad = rad * 0.5;
float s = sin(rad);
return float4(s * axis[0], s * axis[1], s * axis[2], cos(rad));
}
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float4 objectOrigin = mul(unity_ObjectToWorld, float4(0.0,0.0,0.0,1.0));
i.worldSpacePos = mul(unity_ObjectToWorld,i.vertex);
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv) * _Color;
col.a = _Transparency;
return col;
}
ENDCG
}
}
}
最简单的方法是选择一个指向范围中心的轴,然后获取从 UV 坐标到 UV 中心的轴,并获取它们之间的点积:
float2 a = float2(cos(_Angle), sin(_Angle)); // Note that _Angle is in radians!
float2 b = normalize(i.uv - (float2)0.5)
half d = dot(a, b);
// dot(a, b) gets the cosine of the angle between a and b
half maxAngle = 0.5 // 45 degrees range
half mask = d > maxAngle ? 1 : 0;
col.a *= mask;
return col
这将为您提供一个介于 angle-22.5 和 angle+22.5 度之间的扇区。不过,它不会为您提供非常人性化的参数。