我有透明的圆圈,我希望能够通过着色器显示它的一小部分,例如从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
这将为您提供跨越角度-22.5和角度+ 22.5度之间的扇区。但是,它不会为您提供非常用户友好的参数。