Created
February 12, 2024 16:30
-
-
Save ababilinski/ca7fed02d5085c94469d55a9c634d2de to your computer and use it in GitHub Desktop.
Web Camera Shader With Rotate Property
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Shader to rotate an image from a Unity WebCameraTexture. | |
Shader "Custom/Web Camera Shader" | |
{ | |
Properties | |
{ | |
_MainTex ( "Main Texture", 2D ) = "white" {} | |
_RotationDegrees ( "Rotation Degrees", Float) = 0 | |
} | |
SubShader | |
{ | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
uniform sampler2D _MainTex; | |
#include "UnityCG.cginc" | |
struct vertexInput | |
{ | |
float4 vertex : POSITION; | |
float4 texcoord : TEXCOORD0; | |
}; | |
struct vertexOutput | |
{ | |
float4 pos : SV_POSITION; | |
half2 uv : TEXCOORD0; | |
}; | |
float _RotationDegrees; | |
vertexOutput vert(vertexInput v) | |
{ | |
const float Deg2Rad = (UNITY_PI * 2.0) / 360.0; | |
float rotationRadians = _RotationDegrees * Deg2Rad; | |
vertexOutput o; | |
v.texcoord.xy -=0.5; | |
float s = sin (rotationRadians); | |
float c = cos (rotationRadians); | |
float2x2 rotationMatrix = float2x2( c, -s, s, c); | |
rotationMatrix *=0.5; | |
rotationMatrix +=0.5; | |
rotationMatrix = (rotationMatrix * 2)-1; | |
v.texcoord.xy = mul ( v.texcoord.xy, rotationMatrix ); | |
v.texcoord.xy += 0.5; | |
o.pos = UnityObjectToClipPos(v.vertex); | |
o.uv = v.texcoord; | |
return o; | |
} | |
float4 frag(vertexOutput i) : SV_Target | |
{ | |
return tex2D( _MainTex, i.uv ); | |
} | |
ENDCG | |
} | |
} | |
Fallback "Diffuse" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to get the rotated data