Skip to content

Instantly share code, notes, and snippets.

@ababilinski
Created February 12, 2024 16:30
Show Gist options
  • Save ababilinski/ca7fed02d5085c94469d55a9c634d2de to your computer and use it in GitHub Desktop.
Save ababilinski/ca7fed02d5085c94469d55a9c634d2de to your computer and use it in GitHub Desktop.
Web Camera Shader With Rotate Property
//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"
}
@ababilinski
Copy link
Author

How to get the rotated data

var webCameraMaterial = new Material(Shader.Find("Custom/Web Camera Shader"));
var webcam = new WebCamTexture();
webcam.Play();
webCameraMaterial.SetFloat("_RotationDegrees", -webcam.videoRotationAngle);

// if the image is rotated 90 degrees, make sure the texture is the correct size
var rotatedWebCamTexture = Mathf.Abs(webcam.videoRotationAngle) == 90?
                           new RenderTexture(webcam.height, webcam.width, 0):
                           new RenderTexture(webcam.width, webcam.height, 0);

//Fill the render texture with the rotated data                
Graphics.Blit(webcam, rotatedWebCamTexture, webCameraMaterial);
webcam.Stop();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment