Skip to content

Instantly share code, notes, and snippets.

@BOLL7708
Created November 20, 2022 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BOLL7708/6e802771923170b0d7dc5d6c18037646 to your computer and use it in GitHub Desktop.
Save BOLL7708/6e802771923170b0d7dc5d6c18037646 to your computer and use it in GitHub Desktop.
OBS Fisheye Shader (old)
// This is a shader I hacked together by various sources online to use with the OBS ShaderFilter plugin, which has since been retired.
uniform float _width = 1920;
uniform float _height = 1080;
uniform float _distort = 1.0;
uniform float _zoom = 3.0;
uniform float _horizontal = 0;
uniform float _vertical = 0;
uniform float _trim = 0;
uniform float _gradient = 0;
uniform float _alpha = 0.5;
float4 mainImage(VertData v_in) : TARGET
{
float PI = 3.1415926535;
float2 xy = float2(0,0);
float scaleX = 1.0;
float scaleY = 1.0;
// To make the input a square, for proper distortion
if(_width > _height) {
scaleY = 1.0/_width*_height;
} else {
scaleX = 1.0/_height*_width;
}
// It's fun to change the distort value
float zoom = _zoom * _distort;
float uvScaleX = 1.0 / (_distort * scaleX); // Apply scale to correct aspect ratio
float uvScaleY = 1.0 / (_distort * scaleY);
//
xy.x = (scaleX * v_in.uv.x - scaleX/2.0) * zoom;
xy.y = (scaleY * v_in.uv.y - scaleY/2.0) * zoom;
float2 uv;
float d = length(xy);
if (d < 1.0 - _trim)
{
float z = sqrt(1.0 - d * d);
float r = atan2(d, z) / PI;
float phi = atan2(xy.y, xy.x);
uv.x = r * cos(phi) + 0.5;
uv.y = r * sin(phi) + 0.5;
} else if(d < 1.0 - _trim + _gradient) {
float a = 1.0 - (d - 1.0 + _trim) / _gradient;
return float4(0,0,0,a*a*_alpha);
} else {
return float4(0,0,0,0);
}
// SIZE OF INPUT
uv.x = uv.x * uvScaleX - uvScaleX/2.0 + 0.5 - _horizontal;
uv.y = uv.y * uvScaleY - uvScaleY/2.0 + 0.5 - _vertical;
float4 c = image.Sample(textureSampler, uv);
if(c[3] == 0) return float4(0,0,0,0); // Transparent if no source is available
return c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment