Creating a black filter for a custom ShaderEffect

假如想象 提交于 2019-12-13 19:36:52

问题


I am using this code example to adjust brightness and contrast on a BitmapImage for my WPF app. The relevant bit of HLSL code is this:

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;
}

What I would like to do is add something to filter out low intensity pixels- the idea is that I want to say that any part of the image (I'm just guessing I have to do this per pixel) is below a certain threshold, make it black. I'm trying to filter out greys a low intensity stuff to make the lighter parts pop out more (this is a greyscale image). I would then use a slider to adjust that threshold.

I just have no idea if this is a filter or what, was was hoping it's just a simple mod to the code above. Total n00b to HLSL.


回答1:


Try this:

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;
}



回答2:


Here's an alternate approach to @Ed version.

This takes an input of any color and replaces the original color with black.

/// <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;
}

Example is from one of the sample shaders included in Shazzam . Note, the /// comments are custom tags, used in the Shazzam Shader Editor



来源:https://stackoverflow.com/questions/21034898/creating-a-black-filter-for-a-custom-shadereffect

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!