Commands/Shaders/Get-OBSIntensityScopeShader.ps1
function Get-OBSIntensityScopeShader { [Alias('Set-OBSIntensityScopeShader','Add-OBSIntensityScopeShader')] param( # Set the gain of OBSIntensityScopeShader [ComponentModel.DefaultBindingProperty('gain')] [Single] $Gain, # Set the blend of OBSIntensityScopeShader [ComponentModel.DefaultBindingProperty('blend')] [Single] $Blend, # 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 = 'intensity-scope' $ShaderNoun = 'OBSIntensityScopeShader' if (-not $psBoundParameters['ShaderText']) { $psBoundParameters['ShaderText'] = $ShaderText = ' // Robin Green, Dec 2016 // Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. // https://www.shadertoy.com/view/XtcSRs adopted for OBS by Exeldro uniform float gain< string label = "Gain"; string widget_type = "slider"; float minimum = 0.01; float maximum = 1.00; float step = 0.01; > = 0.3; uniform float blend< string label = "Blend"; string widget_type = "slider"; float minimum = 0.00; float maximum = 1.00; float step = 0.01; > = 0.6; float4 mainImage(VertData v_in) : TARGET { float2 uv = v_in.uv; uv.y = 1.0 - uv.y; // calculate the intensity bucket for this pixel based on column height (padded at the top) const float max_value = 270.0; const float buckets = 512.0; float bucket_min = log( max_value * floor(uv.y * buckets) / buckets ); float bucket_max = log( max_value * floor((uv.y * buckets) + 1.0) / buckets ); // count the count the r,g,b and luma in this column that match the bucket float4 count = float4(0.0, 0.0, 0.0, 0.0); for( int i=0; i < 512; ++i ) { float j = float(i) / buckets; float4 pixel = image.Sample(textureSampler, float2(uv.x, j )) * 256.0; // calculate the Rec.709 luma for this pixel pixel.a = pixel.r * 0.2126 + pixel.g * 0.7152 + pixel.b * 0.0722; float4 logpixel = log(pixel); if( logpixel.r >= bucket_min && logpixel.r < bucket_max) count.r += 1.0; if( logpixel.g >= bucket_min && logpixel.g < bucket_max) count.g += 1.0; if( logpixel.b >= bucket_min && logpixel.b < bucket_max) count.b += 1.0; if( logpixel.a >= bucket_min && logpixel.a < bucket_max) count.a += 1.0; } // sum luma into RGB, tweak log intensity for readability count.rgb = log(count.rgb * (1.0-blend) + count.aaa * blend) * gain; // output return count; } ' } $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 } } } } } |