2014-01-10 47 views
1

我使用this code example來調整我的WPF應用程序的BitmapImage的亮度和對比度。的HLSL代碼中的相關位是這樣的:爲自定義ShaderEffect創建黑色濾鏡Shader效果

sampler2D input : register(s0); 
float brightness : register(c0); 
float contrast : register(c1); 

float4 main(float2 uv : TEXCOORD) : COLOR 
{ 
    float4 color = tex2D(input, uv); 
    float4 result = color; 
    result = color + brightness; 
    result = result * (1.0+contrast)/1.0; 

    return result; 
} 

我想要做的就是添加一些東西過濾掉低強度pixels-的想法是,我想說的是,圖像的任何部分(我只是猜測我必須這樣做每像素)低於一定的閾值,使它黑。我試圖過濾灰度低強度的東西,使更輕的部分彈出更多(這是一個灰度圖像)。然後我會使用滑塊來調整該閾值。

我只是不知道,如果這是一個過濾器或什麼,是希望它只是一個簡單的模塊到上面的代碼。總數nb到HLSL。

回答

1

試試這個:

sampler2D input : register(s0); 
float threshold : register(c0); 
float4 blankColor : register(c1); 

float4 main(float2 uv : TEXCOORD) : COLOR 
{ 
    float4 color = tex2D(input, uv); 
    float intensity = (color.r + color.g + color.b)/3; 

    float4 result; 
    if (intensity < threshold) 
    { 
     result = blankColor; 
    } 
    else 
    { 
     result = color; 
    } 

    return result; 
} 
+0

函數名稱不應該是main(),而不是threshold()? –

+0

@WaltRitscher:是的,我的錯誤。 –

3

這裏的另一種方法來@Ed版本。

這需要輸入任何顏色並用黑色替換原來的顏色。

/// <class>AdjustToBlack</class> 
/// <description>An effect that makes pixels of a particular color black.</description> 

sampler2D inputSampler : register(S0); 


/// <summary>The color that becomes black.</summary> 
/// <defaultValue>Green</defaultValue> 
float4 ColorKey : register(C0); 

/// <summary>The tolerance in color differences.</summary> 
/// <minValue>0</minValue> 
/// <maxValue>1</maxValue> 
/// <defaultValue>0.3</defaultValue> 
float Tolerance : register(C1); 

float4 main(float2 uv : TEXCOORD) : COLOR 
{ 
    float4 color = tex2D(inputSampler, uv); 

    if (all(abs(color.rgb - ColorKey.rgb) < Tolerance)) { 
     color.rgb = 0; 
    } 

    return color; 
} 

示例來自Shazzam中包含的示例着色器之一。請注意,///註釋是自定義標記,用於Shazzam Shader Editor

+0

+1喜歡它比我的好 –