User Tag List

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 16

Thread: Pixel Shader Help

  1. #1
    No Products Registered

    Join Date
    Jul 2006
    Location
    Texas
    Posts
    1,225
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pixel Shader Help

    Can anyone explain how to make a pixel shader? I know a little bit of c++.

    I just need one that can replace every color in the active object with the color specified while leaving the alpha channel untouched.

    EDIT: And I can't use replace color because then every instance of an object has the same color.

  2. #2
    Clickteam Clickteam

    Join Date
    Jun 2006
    Location
    France
    Posts
    13,185
    Mentioned
    140 Post(s)
    Tagged
    2 Thread(s)

    Re: Pixel Shader Help

    To do that :

    1. Go to the Effects folder, copy for example BlendExample.fx and BlendExample.xml to ReplaceColor.fx and ReplaceColor.xml.

    2. Edit ReplaceColor.xml with notepad. This file contains the description of your effect. Change the title, the description. This .xml file also allows you to add parameters to your effect. These parameters will be displayed in the property window when you select your effect in the list. In your effect you need 2 parameters, the color to replace and the new color that will replace it :

    Edit the parameter block : rename the parameter to "Color to replace", rename the "code" (variable name) to fOldColor, replace <type>float</type> by <type>int_float4</type> (format of a color parameter), replace <property>edit</property> by <property>COLOR</property> and then replace <value>1.0</value> by <value>0</value> (black).

    And then duplicate the parameter block and rename the second parameter to "New Color" and its code name to fNewColor.

    3. Edit ReplaceColor.fx with notepad.

    First you have to declare the parameters : remove float fBlend; and add :

    float4 fOlcColor;
    float4 fNewColor;

    Now you have to modify the pixel shader function itself, ps_main. This function is called for each pixel. The In parameter contains the position of the pixel. To get the color of the pixel at the current position, use tex2D(Tex0, In.Texture). This returns a float4 value. For example :

    float4 clr = tex2D(Tex0, In.Texture);

    You have different ways to access the rgba components, for example clr.r, clr.a, clr.rgb, etc. Some operators work on all the components you have specified, for example when you do clr1.rgb + clr2.rgb, it will do 3 additions, one per component.

    And then when you have calculated the pixel color, just put it to Out.Color and then return Out.

    So a replace color effect should be something like :

    Code:
    PS_OUTPUT ps_main( in PS_INPUT In )
    {
        PS_OUTPUT Out;
        float4 clr = tex2D(Tex0, In.Texture);
    	if ( clr.r == fOldColor.r && clr.g == fOldColor.g && clr.b == fOldColor.b )
    	{
    	{
    		Out.Color.rgb = fNewColor.rgb;
    		Out.Color.a = clr.a;
    	}
    	else
    		Out.Color = clr;
        return Out;
    }
    However that doesn't work... Well, that works for some colors, not for all. Why? Because the color parameter in MMF2 is a set of 4 integers between 0 and 255, and the shader uses a set of 4 floats between 0 and 1.0, and it seems that converting an integer between 0 and 255 to a floating point value in C++ doesn't give the exact same value as the floats used by the shader... So you have to test if the difference between both colors is less than a certain value, like this :

    Code:
    PS_OUTPUT ps_main( in PS_INPUT In )
    {
        PS_OUTPUT Out;
        float4 clr = tex2D(Tex0, In.Texture);
    
    	float4 diff = clr - fOldColor;
    	if ( diff.r < 0 )
    		diff.r = -diff.r;
    	if ( diff.g < 0 )
    		diff.g = -diff.g;
    	if ( diff.b < 0 )
    		diff.b = -diff.b;
    
    	if ( diff.r < 0.004f && diff.g < 0.004f && diff.b < 0.004f )
    	{
    		Out.Color.rgb = fNewColor.rgb;
    		Out.Color.a = clr.a;
    	}
    	else
    		Out.Color = clr;
    
        return Out;
    }
    4. Finally you have to specify the number of minimum pixel shader version required by your effect, in the PixelShader = compile ps_x_y ps_main(); line. Replace x_y by the version number (1_1, 1_2, 1_3, 1_4, 2_0, 3_0, etc). Basic effects obly require 1_1, this effect requires 2_0.

    That gives :

    Code:
    // Effect technique
    technique tech_main
    {
        pass P0
        {
            // shaders
            VertexShader = NULL;
            PixelShader  = compile ps_2_0 ps_main();
        }  
    }
    PS : I'm not at all an expert in pixel shaders so maybe there are better ways to do this effect and/or maybe make it so that it requires a lower version of the pixel shaders. This is just an example.

    PPS : in the current beta version (1f) of MMF2 there is no action / expression to set / get parameter values at runtime. This will be fixed in the next beta version.

  3. #3
    No Products Registered

    Join Date
    Jul 2006
    Posts
    2,289
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)

    Re: Pixel Shader Help

    It seems that Sphax has done some extensive work in this direction, so maybe we will hear more from him about doing this.

  4. #4
    Clickteam Clickteam
    Anders's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, ┼rhus
    Posts
    3,455
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)

    Re: Pixel Shader Help

    Only knowing GLSL (OpenGL Shader Language) I don't know if this is correct, but I believe it is possible to compare the current pixel like this: (.rgb returns a float3 vector which can be compared to the other color in one shot)

    Code:
    PS_OUTPUT ps_main( in PS_INPUT In )
    {
        PS_OUTPUT Out;
        float4 clr = tex2D(Tex0, In.Texture);
        if ( clr.rgb == fOldColor.rgb ){
            Out.Color.rgb = fNewColor.rgb;
            Out.Color.a = clr.a;
        }
        else
            Out.Color = clr;
        return Out;
    }

  5. #5
    Clickteam Clickteam

    Join Date
    Jun 2006
    Location
    France
    Posts
    13,185
    Mentioned
    140 Post(s)
    Tagged
    2 Thread(s)

    Re: Pixel Shader Help

    The problem is that the float values converted by MMF2 from color values are not strictly identical to the float values of the colors returned by tex2D, there is a little difference so the == operator doesn't work. Do you see what I mean?

  6. #6
    Forum Moderator Fusion 2.5 DeveloperAndroid Export ModuleHTML5 Export ModuleiOS Export ModuleSWF Export ModuleXNA Export Module
    Sphax's Avatar
    Join Date
    Jun 2006
    Location
    Paris, France
    Posts
    4,454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Pixel Shader Help

    I've already done a lot of pixel shaders and some for replacing one or more colors by another from a parameter (and all of them support an error range).
    I'll release them when a new version of MMF2 HWA will handle the parameters of the pixel shaders through event editor.

    To see what pixel shaders I've already created (more have been created since the posted example), have a look to this post.

  7. #7
    No Products Registered

    Join Date
    Jul 2006
    Location
    Texas
    Posts
    1,225
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Pixel Shader Help

    Thanks Yves! What threw me off was that in the 'blendexample.xml' (something like that) it said 'parameters (float or int), so I didn't know you could use a float4. I also had no idea what tex2d was.

    Thanks for clearing that up!

  8. #8
    No Products Registered

    Join Date
    Jul 2006
    Location
    Texas
    Posts
    1,225
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Pixel Shader Help

    Alright! I got it to work!

    I think you misunderstood what I meant earlier by replace color. I should have said something like 'wipe with color'. What it does is it replaces EVERY color with the color specified, and leaves the alpha value untouched.

  9. #9
    Clickteam Clickteam
    Anders's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, ┼rhus
    Posts
    3,455
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)

    Re: Pixel Shader Help

    Ah yeah, floats were never meant to be compared. Also the interpolated colors which comes from reading neighboring pixels when it uses linear interpolation could also be a problem, even with an error margin. At some point the pixel will be out of the error margin and will not change color but still appear as a faint border around the colored areas that were replaced. I think it's hard to get around that.

  10. #10
    No Products Registered

    Join Date
    Jul 2006
    Location
    Texas
    Posts
    1,225
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Pixel Shader Help

    Well... what I do to get a 'team color on the unit' effect is I have each 'unit' really made up of 2 objects. The first object is the object itself, but it is transparent where there should be team color. Then, I have another object beneath it that is wiped solid with the team color (so therefore, the brightness of the color is determined by the transparency of the object above).

    I was wanting an ink effect that could do that (the wipe color thing) because the 'replace color' action replaces the color for all instances of an object.

    And I'm curious about what Yves said earlier (about the versions of pixel shaders). What's the difference between 2_0, and 1_1 (and all the others)? Mine works with 1_1.

Page 1 of 2 1 2 LastLast

Similar Threads

  1. Tilemap Pixel Shader (aka "texture atlas" shader)
    By MuddyMole in forum Hardware Accelerated Runtime
    Replies: 3
    Last Post: 10th May 2013, 09:21 PM
  2. Can any one get the pixel shader to wok?
    By SEELE in forum Multimedia Fusion 2 - Technical Support
    Replies: 4
    Last Post: 11th August 2008, 02:28 PM
  3. Sub-Pixel Positioning Pixel Shader
    By LazyCoder in forum Hardware Accelerated Runtime
    Replies: 15
    Last Post: 16th March 2008, 12:24 PM
  4. HWA Pixel Shader - Skew
    By LazyCoder in forum File Archive
    Replies: 2
    Last Post: 9th February 2008, 09:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •