killapp/Assets/Shaders/CameraRotateShader.shader

84 lines
2.3 KiB
Plaintext
Raw Normal View History

2026-06-10 15:04:14 +08:00
Shader "Custom/CameraRotate"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Rotation ("Rotation", Float) = 0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
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;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _Rotation;
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
{
float2 uv = i.uv;
// 顺时针旋转90度
float angle = -1.57079633; // -90度转弧度
float s = sin(angle);
float c = cos(angle);
// 将UV中心移到(0.5, 0.5)
uv -= 0.5;
// 旋转
float2 rotatedUV;
rotatedUV.x = uv.x * c - uv.y * s;
rotatedUV.y = uv.x * s + uv.y * c;
// 移回中心
rotatedUV += 0.5;
// 从中心截取横向区域
// 旋转后的画面是竖向的,需要截取中间横向部分
float aspectRatio = 0.6; // 预览框宽高比 / 旋转后相机宽高比
float2 scale = float2(1.0, aspectRatio);
float2 offset = float2(0.0, (1.0 - aspectRatio) * 0.5);
rotatedUV = rotatedUV * scale + offset;
// 裁剪越界
if (rotatedUV.x < 0 || rotatedUV.x > 1 || rotatedUV.y < 0 || rotatedUV.y > 1)
return fixed4(0, 0, 0, 1);
fixed4 col = tex2D(_MainTex, rotatedUV);
return col;
}
ENDCG
}
}
}