WPF, canvas, visual, shader fx effects

LB 1 Reputation point
2020-12-10T10:59:16.173+00:00

In a WPF application, I create a system.windows.media.effect to allow me change the color of a visual as red instead of the original color:
46895-immagine.png

here is a example ^^^^^^^^^^^^^

it's a shader .fx effect that, when applyed to the visual object, it change any pixel that is NOT white as RED.
So , in this case, only black appears Red.

I notice that the first visual (a rectangle) added to the canvas is correctly rendered as i need, here it's:

46896-immagine.png

if i add anothe visuals, the red pixel effect is applyed to the portion of another visuals too:

46925-immagine.png

47001-immagine.png

the question is: Why ??

the effect is appyed to only ONE object of the 3 object that are in the canvas...

How to render all the 3 rectangle as the first one ?
Anybody can have a advise ?

Thanks!

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,781 questions
{count} votes

2 answers

Sort by: Most helpful
  1. LB 1 Reputation point
    2020-12-11T07:21:38.4+00:00

    I have a shader SelectionEffect.fx effect like this:

    sampler2D Input1 : register(S0);

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

    if ((color.r != 1.0f) && (color.g != 1.0f) && (color.b != 1.0f)) {
        color.r = 1.0;
        color.g = 0.0;
        color.b = 0.0;
        //color.a = 0.9;
    }
    
    return color;
    

    }

    Compiled as SelectionEffect.ps and added his .cs class file, of course.

    It's used to change the color of the object to red only, when I select it (activate a property according in the object), instead of assigned a tempoaraneally red color to the object.
    I would to use this solution that is faster, i have 50-100 thousand objects.

    So,to assign this effect at the object that i have already added to canvas is only:

    ((System.Windows.Shapes.Path)shape.Visual).Effect = new MyShaders.SelectionEffect();

    to remove it:

    ((System.Windows.Shapes.Path)shape.Visual).Effect = null;

    Please note that in the example I'm applyed this "effect" to one object pro time, so, it's intended that i would change color of the only object that have "effect" applyed in that moment and not to the anothers.

    Using:

    ((System.Windows.Shapes.Path)shape.Visual).Stroke = new SolidColorBrush(Colors.Red);

    is slow.


  2. LB 1 Reputation point
    2020-12-16T08:50:42.887+00:00

    I try to explaine it as simple as i can:

    I use the "Path" Object added to a canvas:

    https://learn.microsoft.com/it-it/dotnet/api/system.windows.shapes.path?view=net-5.0

    to this object, i apply a effect (https://learn.microsoft.com/it-it/dotnet/api/system.windows.uielement.effect?view=net-5.0#System_Windows_UIElement_Effect):

    mypathobject.effect = new MyShaders.SelectionEffect();

    the effect is create by me using fxc compiler. the code is:

    sampler2D Input1 : register(S0);

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

     if ((color.r != 1.0f) && (color.g != 1.0f) && (color.b != 1.0f)) {  
         color.r = 1.0;  
         color.g = 0.0;  
         color.b = 0.0;  
         //color.a = 0.9;  
     }  
     return color;  
    

    this effect is applied when i select the object, so it become red only that object (this is what i need).

    the problem is the different rendering of the red effect on the 3 added objects to canvas, as you can see on images.

    is this clear ?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.