旋转图片

作者:追风剑情 发布于:2024-4-9 19:01 分类:Shader

1、工程截图

111.png

2、Shader

  1. Shader "Custom/RotationUV"
  2. {
  3. Properties
  4. {
  5. _MainTex ("Texture", 2D) = "white" {}
  6. //旋转速度
  7. _Speed ("Speed", float) = 1.0
  8. }
  9. SubShader
  10. {
  11. Tags { "RenderType"="Transparent" "Queue" = "Transparent" "IgnoreProjector" = "True" "PreviewType" = "Plane" }
  12. LOD 100
  13.  
  14. //Alpha混合
  15. Blend SrcAlpha OneMinusSrcAlpha
  16.  
  17. Pass
  18. {
  19. CGPROGRAM
  20. #pragma vertex vert
  21. #pragma fragment frag
  22.  
  23. #include "UnityCG.cginc"
  24.  
  25. struct appdata
  26. {
  27. float4 vertex : POSITION;
  28. float2 uv : TEXCOORD0;
  29. };
  30.  
  31. struct v2f
  32. {
  33. float2 uv : TEXCOORD0;
  34. float4 vertex : SV_POSITION;
  35. };
  36.  
  37. sampler2D _MainTex;
  38. float4 _MainTex_ST;
  39. float _Speed;
  40.  
  41. v2f vert (appdata v)
  42. {
  43. v2f o;
  44. o.vertex = UnityObjectToClipPos(v.vertex);
  45. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  46. return o;
  47. }
  48.  
  49. fixed4 frag (v2f i) : SV_Target
  50. {
  51. float t = _Time.y * _Speed;
  52. /*-- 算法原理:通过旋转坐标基向量来旋转uv --*/
  53. fixed2 center = fixed2(0.5, 0.5);//坐标原点
  54. fixed2 v = i.uv - center;
  55. //乘以旋转矩阵
  56. fixed x = v.x * cos(t) + v.y * sin(t);
  57. fixed y = v.x * cos(t + UNITY_HALF_PI) + v.y * sin(t + UNITY_HALF_PI);
  58. //旋转后的uv
  59. fixed2 uv = fixed2(x, y);
  60. uv += center;
  61. //--End
  62.  
  63. fixed4 col = tex2D(_MainTex, uv);
  64. return col;
  65. }
  66. ENDCG
  67. }
  68. }
  69. }


运行效果
11111114.gif

标签: Shader

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号