User Tag List

Results 1 to 5 of 5

Thread: Interesting thing: float3 and implicit conversion

  1. #1
    No Products Registered

    Join Date
    Sep 2007
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Interesting thing: float3 and implicit conversion

    Hello,

    Here is an interesting thing I discovered while writing my shader. The following lines were reducing the image to have no color value, and only return luminosity, because it appears to have gone through an implicit narrowing convention:

    Code:
        float4 OldColor = tex2D( Tex0, Tex.xy);
    
        //Do Gradient
        if(useGrad)  OldColor.rgb = (OldColor.r + (Tex.y * 0.1),OldColor.g + (Tex.y * 0.1),OldColor.b + (Tex.y * 0.1));
    If you set a float4's .RGB value this way, this results in the image to be completely desaturated. So, If you want to make sure this does not occur, you must prefix the data structure with Float3 to explicitly tell the shader compiler that you are replacing the RGB components with the components of a float3 structure.

    Code:
        float4 OldColor = tex2D( Tex0, Tex.xy);
    
        //Do Gradient
        if(useGrad)  OldColor.rgb = float3(OldColor.r + (Tex.y * 0.1),OldColor.g + (Tex.y * 0.1),OldColor.b + (Tex.y * 0.1));
    In particularly large shaders (PS 3.0), this could be easily overlooked, and you might be wondering why your images are being desaturated! I hope that by posting this, others will avoid this problem in the future. Good luck, and happy shading :3

  2. #2
    Clicker Fusion 2.5 DeveloperSWF Export ModuleUnicode Add-on
    Looki's Avatar
    Join Date
    Aug 2006
    Location
    Karlsruhe, Germany
    Posts
    3,739
    Mentioned
    5 Post(s)
    Tagged
    1 Thread(s)

    Re: Interesting thing: float3 and implicit conversion

    Hm, I'm not sure where you got that first syntax from. I've never seen it anywhere and I didn't expect it to compile at all! But thanks for the hint.

    Anyway, instead of
    Code:
    OldColor.rgb = float3(OldColor.r + (Tex.y * 0.1),OldColor.g + (Tex.y * 0.1),OldColor.b + (Tex.y * 0.1));
    you could simply write:
    Code:
    OldColor.rgb += Tex.y*0.1;

  3. #3
    No Products Registered

    Join Date
    Sep 2007
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Interesting thing: float3 and implicit conversion

    Code:
    OldColor.rgb += Tex.y*0.1;
    does this really preserve each individual channel's colors? :0

    I tried it, and it works! Thanks for the tip.

  4. #4
    Clicker Multimedia Fusion 2 Developer

    Join Date
    Jun 2006
    Location
    Darlington, UK
    Posts
    3,298
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Interesting thing: float3 and implicit conversion

    If the syntax is the same as c++, using (1,2,3) means evaluate all three then only use the first (or last, I forget which). It's annoying as hell that it actually compiles.

  5. #5
    Clicker Fusion 2.5 DeveloperSWF Export ModuleUnicode Add-on
    Looki's Avatar
    Join Date
    Aug 2006
    Location
    Karlsruhe, Germany
    Posts
    3,739
    Mentioned
    5 Post(s)
    Tagged
    1 Thread(s)

    Re: Interesting thing: float3 and implicit conversion

    Incredible, I can't believe it actually compiles in C++. It will use the last element:

    int foo = (1,2,3);

    Sets foo to 3.

Similar Threads

  1. Implicit type convesion to float when using abs(), min(), max() etc?
    By netninja in forum Multimedia Fusion 2 - Technical Support
    Replies: 2
    Last Post: 11th May 2012, 04:24 AM
  2. Interesting thing when you save into Java
    By Czentnar in forum File Archive
    Replies: 3
    Last Post: 1st September 2008, 09:34 AM
  3. thing thing movement
    By Pritchy96 in forum Multimedia Fusion 2 - Technical Support
    Replies: 2
    Last Post: 3rd May 2008, 03:08 AM

Posting Permissions

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