下面代码实现了Center作为旋转中心点和挖洞中心点,Offset是偏移文章来源地址https://www.toymoban.com/news/detail-408980.html
Shader "Custom/Spin" {
Properties{
_MainTex("Texture", 2D) = "white" {}
_Speed("Speed", Range(0.1, 100)) = 1.0
_Center("Center", Vector) = (0.5, 0.5, 0.0, 0.0)
_Opacity("Opacity", Range(0, 1)) = 1.0
_TransparentLength("TransparentLength", Range(0, 1)) = 0.2
_Offset("Offset", Vector) = (0.5, 0.5, 0.0, 0.0)
}
SubShader{
Tags {"Queue" = "Transparent" "RenderType" = "Opaque"}
LOD 100
Pass {
Blend SrcAlpha OneMinusSrcAlpha // 启用混合模式
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;
};
sampler2D _MainTex;
float2 _Center;
float _Speed;
float _Opacity;
float _TransparentLength;
float2 _Offset;
v2f vert(appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag(v2f i) : SV_Target{
float2 center = _Center;
float2 uv = i.uv - center;
float angle = _Time.y * _Speed;
float s = sin(angle);
float c = cos(angle);
float2 offset = _Offset;
float2 rotatedUV = float2((uv.x + offset.x) * c - (uv.y + offset.y) * s, (uv.x + offset.x) * s + (uv.y + offset.y) * c);
float dist = length(uv);
if (dist < _TransparentLength) {
discard;
}
fixed4 col = tex2D(_MainTex, rotatedUV + center);
col.a *= _Opacity;
return col;
}
/*fixed4 frag(v2f i) : SV_Target{
float2 center = _Center;
float2 uv = i.uv - center;
float angle = _Time.y * _Speed;
float s = sin(angle);
float c = cos(angle);
float2 rotatedUV = float2(uv.x * c - uv.y * s, uv.x * s + uv.y * c);
float dist = length(uv);
if (dist < _TransparentLength) {
discard;
}
fixed4 col = tex2D(_MainTex, rotatedUV + center);
col.a *= _Opacity;
return col;
}*/
//fixed4 frag(v2f i) : SV_Target {
// float2 center = _Center;
// float2 uv = i.uv - center;
// float angle = _Time.y * _Speed;
// float s = sin(angle);
// float c = cos(angle);
// float2 rotatedUV = float2(uv.x * c - uv.y * s, uv.x * s + uv.y * c);
// fixed4 col = tex2D(_MainTex, rotatedUV + center); // 采样纹理
// col.a *= _Opacity; // 乘以不透明度
// return col;
//}
ENDCG
}
}
FallBack "Diffuse"
}
文章来源:https://www.toymoban.com/news/detail-408980.html
到了这里,关于使用shader实现一个图片旋转、某个点挖洞、偏移的功能的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!