Skip to content

Commit

Permalink
feat: Zoom XY Shader ( Fixes #199 )
Browse files Browse the repository at this point in the history
  • Loading branch information
StartAutomating authored and StartAutomating committed Feb 15, 2024
1 parent c008830 commit b63146e
Showing 1 changed file with 197 additions and 0 deletions.
197 changes: 197 additions & 0 deletions Commands/Shaders/Get-OBSZoomXYShader.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
function Get-OBSZoomXYShader {

[Alias('Set-OBSZoomXYShader','Add-OBSZoomXYShader')]
param(
# Set the center_x_percent of OBSZoomXYShader
[Alias('center_x_percent')]
[ComponentModel.DefaultBindingProperty('center_x_percent')]
[Int32]
$CenterXPercent,
# Set the center_y_percent of OBSZoomXYShader
[Alias('center_y_percent')]
[ComponentModel.DefaultBindingProperty('center_y_percent')]
[Int32]
$CenterYPercent,
# Set the x_power of OBSZoomXYShader
[Alias('x_power')]
[ComponentModel.DefaultBindingProperty('x_power')]
[Single]
$XPower,
# Set the y_power of OBSZoomXYShader
[Alias('y_power')]
[ComponentModel.DefaultBindingProperty('y_power')]
[Single]
$YPower,
# The name of the source. This must be provided when adding an item for the first time
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('SceneItemName')]
[String]
$SourceName,
# The name of the filter. If this is not provided, this will default to the shader name.
[Parameter(ValueFromPipelineByPropertyName)]
[String]
$FilterName,
# The inline value of the shader. This will normally be provided as a default parameter, based off of the name.
[Alias('ShaderContent')]
[String]
$ShaderText,
# If set, will force the recreation of a shader that already exists
[Management.Automation.SwitchParameter]
$Force,
# If set, will pass thru the commands that would be sent to OBS (these can be sent at any time with Send-OBS)
[Management.Automation.SwitchParameter]
$PassThru,
# If set, will not wait for a response from OBS (this will be faster, but will not return anything)
[Management.Automation.SwitchParameter]
$NoResponse,
# If set, use the shader elapsed time, instead of the OBS system elapsed time
[ComponentModel.DefaultBindingProperty('use_shader_elapsed_time')]
[Management.Automation.SwitchParameter]
$UseShaderTime
)


process {
$shaderName = 'Zoom_XY'
$ShaderNoun = 'OBSZoomXYShader'
if (-not $psBoundParameters['ShaderText']) {
$psBoundParameters['ShaderText'] = $ShaderText = '
// Zoom XY Shader
// A simple twist on the Zoom Shader in https://github.com/exeldro/obs-shaderfilter/
// The allow for an independent Horizontal and Vertical Zoom.
uniform int center_x_percent<
string label = "center x percent";
string widget_type = "slider";
int minimum = 0;
int maximum = 100;
int step = 1;
> = 50;
uniform int center_y_percent<
string label = "center y percent";
string widget_type = "slider";
int minimum = 0;
int maximum = 100;
int step = 1;
> = 50;
uniform float x_power<
string label = "x power";
string widget_type = "slider";
float minimum = 0;
float maximum = 20.0;
float step = 0.001;
> = 1;
uniform float y_power<
string label = "y power";
string widget_type = "slider";
float minimum = 0;
float maximum = 20.0;
float step = 0.001;
> = 1;
float4 mainImage(VertData v_in) : TARGET
{
float2 center_pos = float2(center_x_percent * .01, center_y_percent * .01);
float2 uv = v_in.uv;
uv.x = (v_in.uv.x - center_pos.x) * x_power + center_pos.x;
uv.y = (v_in.uv.y - center_pos.y) * y_power + center_pos.y;
return image.Sample(textureSampler, uv);
}
'
}
$MyVerb, $myNoun = $MyInvocation.InvocationName -split '-',2
if (-not $myNoun) {
$myNoun = $myVerb
$myVerb = 'Get'
}
switch -regex ($myVerb) {
Get {
$FilterNamePattern = "(?>$(
if ($FilterName) {
[Regex]::Escape($FilterName)
}
else {
[Regex]::Escape($ShaderNoun -replace '^OBS' -replace 'Shader$'),[Regex]::Escape($shaderName) -join '|'
}
))"
if ($SourceName) {
Get-OBSInput |
Where-Object InputName -eq $SourceName |
Get-OBSSourceFilterList |
Where-Object FilterName -Match $FilterNamePattern
} else {
$obs.Inputs |
Get-OBSSourceFilterList |
Where-Object FilterName -Match $FilterNamePattern
}
}
'Remove' {
if ($SourceName) {
Get-OBSInput |
Where-Object InputName -eq $SourceName |
Get-OBSSourceFilterList |
Where-Object FilterName -Match $FilterNamePattern |
Remove-OBSSourceFilter
}
}
'(?>Add|Set)' {
$ShaderSettings = [Ordered]@{}
:nextParameter foreach ($parameterMetadata in $MyInvocation.MyCommand.Parameters[@($psBoundParameters.Keys)]) {
foreach ($parameterAttribute in $parameterMetadata.Attributes) {
if ($parameterAttribute -isnot [ComponentModel.DefaultBindingPropertyAttribute]) { continue }
$ShaderSettings[$parameterAttribute.Name] = $PSBoundParameters[$parameterMetadata.Name]
if ($ShaderSettings[$parameterAttribute.Name] -is [switch]) {
$ShaderSettings[$parameterAttribute.Name] = $ShaderSettings[$parameterAttribute.Name] -as [bool]
}
continue nextParameter
}
}

if (-not $PSBoundParameters['FilterName']) {
$filterName = $PSBoundParameters['FilterName'] = $shaderName
}

$ShaderFilterSplat = [Ordered]@{
ShaderSetting = $ShaderSettings
FilterName = $FilterName
SourceName = $SourceName
}

foreach ($CarryOnParameter in "PassThru", "NoResponse","Force") {
if ($PSBoundParameters.ContainsKey($CarryOnParameter)) {
$ShaderFilterSplat[$CarryOnParameter] = $PSBoundParameters[$CarryOnParameter]
}
}

if (-not $script:CachedShaderFilesFromCommand) {
$script:CachedShaderFilesFromCommand = @{}
}

if ($Home -and -not $script:CachedShaderFilesFromCommand[$shaderName]) {
$MyObsPowerShellPath = Join-Path $home ".obs-powershell"
$ThisShaderPath = Join-Path $MyObsPowerShellPath "$shaderName.shader"
$shaderText | Set-Content -LiteralPath $ThisShaderPath
$script:CachedShaderFilesFromCommand[$shaderName] = Get-Item -LiteralPath $ThisShaderPath
}
if ($script:CachedShaderFilesFromCommand[$shaderName]) {
$ShaderFilterSplat.ShaderFile = $script:CachedShaderFilesFromCommand[$shaderName].FullName
} else {
$ShaderFilterSplat.ShaderText = $shaderText
}

if ($myVerb -eq 'Add') {
Add-OBSShaderFilter @ShaderFilterSplat
} else {
Set-OBSShaderFilter @ShaderFilterSplat
}
}
}

}


}

0 comments on commit b63146e

Please sign in to comment.