XNA Creators Club Online
Page 1 of 1 (9 items)
Sort Posts: Previous Next

ColorKey problem in WinForms

Last post 10/18/2009 10:32 PM by ERiba. 8 replies.
  • 10/16/2009 4:12 PM

    ColorKey problem in WinForms

    Hi all,
    I have a strange problem, my textures compiled with colorkeys are not transparent inside my scene editor which is a windows forms application.
    I am compiling them inside my scene editor not through Visual Studio but the XNB files are OK.
    Just tried loading the scene inside a game and the same textures are transparent.

    Here is a screenshot inside the editor: Editor
    And here the same scene loaded into a game:Loaded Scene

    Searched the forum and found some interesting posts that the colorkey based transparency is not working if the texture is loaded with Texture2D.FromFile without texture creation params.
    Although I have a few wrappers which load content from file if not found in the ContentManager but placing breakpoints assured me that the textures are indeed loaded through the content manager.

    That is very strange since both the game and the scene editor are using the same Xnb files without custom content pipeline processing.
    The nodes deserialized from Xml files use reflection to create the types but they work the same inside the editor as inside the game.
    Since I am even loading the saved scene in the editor the same execution path is taken as inside the game project.

    The only difference I could think of is the Windows forms environment where the same engine is used but the rendering is through a present:
     
    protected override void Draw(GameTime gameTime) 
                base.Draw(gameTime); 
     
                if (winControl != null
                    GraphicsDevice.Present(winControl.Handle); 

    Any ideas if the texture colorkey problem can occurs under other circumstances than loading the texture from file?


  • 10/16/2009 4:56 PM In reply to

    Re: ColorKey problem in WinForms

    Two likely possibilities:

    • Maybe you don't have colorkey enabled when you build the texture into .xnb format? (this is controlled by a content processor parameter)
    • Or maybe you don't have the alpha testing or alpha blend renderstates enabled when you draw it? (in which case the texture alpha channel will just be ignored)

    You could use single frame capture mode in PIX to debug this: that will show you both what renderstates are set and what data is in the alpha channel of your texture.
    XNA Framework Developer - blog - homepage
  • 10/16/2009 5:50 PM In reply to

    Re: ColorKey problem in WinForms

    As I said I was loading the same scene (and the scene consists not only of xml definitions but of Xnb files as well) inside a game project so the same xnb resources get rendered with transparency :-(
    I tried PIX already before but seems it cant't handle the Windows Forms environment: PIX
    Maybe it just requires some special setup which I am not aware of?

    Since it works well inside a game I cant debug it there. Actually I can live with that, it's just real so much user unfriendly not to show transparencies inside the editor.

    EDIT: errr will check the renderstates right now .-)))

    EDIT2: Nope both AlphaTest and AlphaBlend are enabled if I can trust the Visual Studio debugger in debug mode that is.
  • 10/16/2009 7:56 PM In reply to

    Re: ColorKey problem in WinForms

    That sure looks to me like alpha testing is disabled when you render in the editor.
    XNA Framework Developer - blog - homepage
  • 10/16/2009 8:03 PM In reply to

    Re: ColorKey problem in WinForms

    Ive checked in the debbuger the renderstates are ok before and after the rendering, did a check in the shader and even there are the states defined:
    technique TXnaTexturedLights 
        pass p0 
        {        
            vertexshader = compile vs_3_0 GenericVS();   
            pixelshader = compile ps_3_0 TxnaPS();   
            AlphaBlendEnable = true
            AlphaTestEnable = true
            ZWriteEnable = true
            ZEnable = true;                      
        } 

    Are the renderstates by any chance changeable by the Xna Framework, I mean are they changed without user code for any reason/condition which might be triggered here?
  • 10/16/2009 10:04 PM In reply to

    Re: ColorKey problem in WinForms

    Answer
    Reply Quote
    SpriteBatch changes many renderstates.

    In addition to AlphaTestEnable, you should look at what the alpha compare function and alpha reference value are set to.

    What format is your backbuffer? Many cards don't support alpha blend / test when using floating point formats or MRT.

    Does the debug DX runtime say anything useful?
    XNA Framework Developer - blog - homepage
  • 10/17/2009 12:07 AM In reply to

    Re: ColorKey problem in WinForms

    Shawn you are 100% right regarding the renderstates and I feel so dumb.

    In case anyone is interested here is what I did to find out whats wrong.
    To narrow the problem I created a scene with one single textured quad, same problem on winforms version and transparency on game version.
    As PIX refused to provide information on the winform version I started the game version to check if anything wrong is happening.
    Usually I only use the single frame capture but this time I decided to record a repleyable call stream.

    Inspecting the first 2 frames revealed that the quad is rendered without transparency and after the second frame with transparency...and then it hit me.
    There is one more difference between the winform version and the game version - I was using a SpriteBatch to render the FPS counter in one corner.
    Commented out the function and verified that the quad is rendered without the transparency :-/

    PIX showed that the alphatest was on but the compare function was set to always until the spritebatch kicked in and changed it to compare greater.
    Currently I decided to set a whole bunch of renderstates inside the SceneManager's Draw:
    #region renderstates 
                            //---------------------------------- 
                            //  Set most common RenderState's 
                            //---------------------------------- 
                            Game.GraphicsDevice.RenderState.AlphaBlendEnable = true
                            Game.GraphicsDevice.RenderState.AlphaBlendOperation = BlendFunction.Add; 
                            Game.GraphicsDevice.RenderState.AlphaDestinationBlend = Blend.Zero; 
                            Game.GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha; 
                            Game.GraphicsDevice.RenderState.AlphaTestEnable = true
                            Game.GraphicsDevice.RenderState.AlphaSourceBlend = Blend.One; 
                            Game.GraphicsDevice.RenderState.AlphaFunction = CompareFunction.Greater; 

    But I wonder if that is smart since it is a per frame task?


    I completely hate SpriteBatches and usually avoid them but who could resist not to use them for such trivial tasks as rendering the FPS stat?




  • 10/18/2009 9:23 PM In reply to

    Re: ColorKey problem in WinForms


    try this in the shader , and remove all from your c# code file , allways set renderstate down in the shader

    ALPHAREF = 0x0000000A;

    ALPHATESTENABLE = TRUE;
    ALPHAFUNC = GREATEREQUAL ; 
    ALPHABLENDENABLE = TRUE;
    BLENDOP = ADD; 
    DESTBLEND = DESTALPHA; 
    SRCBLEND = INVDESTALPHA; 
    BLENDOP = ADD;
    DESTBLEND = INVSRCALPHA;
    SRCBLEND = SRCALPHA;
    ZEnable = true
    ZWriteEnable = true;
    CullMode = NONE;



    and you be good to go

     

     

     

     

     

    follow the dev. on youtube channel

    The legend of the old spice , name rank and number soldier ... Join the Xna Developer Group at youtube , the place where you can show off your creations
  • 10/18/2009 10:32 PM In reply to

    Re: ColorKey problem in WinForms

    The issue is fixed already but thnx for the effort.
    BTW you have double settings for some states and completely disabled culling and I am just curious why is alpharef set to 10?
Page 1 of 1 (9 items) Previous Next