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

Making part of a texture transparent

Last post 11/18/2009 2:30 PM by laarsp. 9 replies.
  • 11/15/2009 10:44 AM

    Making part of a texture transparent

    I'm trying to make some kind of point'n'click adventure game in 2D. When the character walks behind a wall I want him to disappear.... I figured I'd use some kind of masking technique like this:



    How would I do this? Or does anyone else know a better way?
  • 11/15/2009 11:01 AM In reply to

    Re: Making part of a texture transparent

    If you draw your scene top to bottom (sorting by Y coordinates), this will happen automatically. You haven't shown us how the whole game works, so that technique might not cut it. In that case, you might want to have layers too.
    return;
  • 11/15/2009 3:34 PM In reply to

    Re: Making part of a texture transparent

    Ah, well you see the wall is part of the background, not a separate sprite :)
  • 11/15/2009 3:40 PM In reply to

    Re: Making part of a texture transparent

    Then crop it out and place it right on top of itself as an object. When you are done doing that, think of all the other features like this you will need in your game, and make sure your scene setup and rendering code will support it.

    It's also possible to save the depth information if the scenes are 3d renderings, and use that to render your scene, so the player will not show up behind the wall.

    You could also write code just to mask out that one section of the game as a special case, but it will be more trouble than it's worth, and you will have special cases all over your code.

    If you just use my first technique, and place it as a foreground layer, or an object with everything drawn sorted by the y value, you will be able to go back and have a front layer everywhere in your scenes for free.
    return;
  • 11/15/2009 6:17 PM In reply to

    Re: Making part of a texture transparent

    Well I'd rather skip the hassle of cropping everything out and loading as a separate image :\ I tried using a shader that looks like this:

    sampler TextureSampler : register(s0);
    sampler OverlaySampler : register(s1);

    float2 ScenePosition;

    float4 PixelShaderFunction(float4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0
    {
        // Look up the texture color.
        float4 tex = tex2D(TextureSampler, texCoord);
            
        // Look up the overlay texture color
        float4 overlayColor = tex2D(OverlaySampler, ScenePosition+ texCoord);
        
        // Change the alpha of the source texture
        tex.a -= overlayColor.a;
        
        return tex * 2;
    }

    technique Technique1
    {
        pass Pass1
        {
            PixelShader = compile ps_2_0 PixelShaderFunction();
        }
    }

    Where I set ScenePosition to the xy of the object in the scene. But this doesnt seem to work very well... The overlay gets scaled down somehow :\\\\\

  • 11/15/2009 11:28 PM In reply to

    Re: Making part of a texture transparent

    Could be that you and Daaark mean different things when you say "crop it out". You seem to mean "crop it out in an image processing tool, save as an additional image, add it to your project, then load this as an asset (Texture2D) for drawing in the game". Which would indeed be cumbersome. But I think what Daaark meant was to do the cropping "in game". With that method, you only have the single Texture2D of the background image, no additional images for the "cropped" parts:
    First you draw the full background image to the background.
    Second you draw the character.
    Third, you draw those parts of the background image again which are actually supposed to be in the foreground. Use the SpriteBatch.Draw(...) overload which takes a source rectangle as input. I.e. draw the same background Texture2D, but specify as a source rect only the part that is supposed to be in the foreground. Repeat this with all "foreground parts" in the background image.

    Doc
    Urgently needed: Reviews in German for Kuchibi and Golden Tangram (No more reviews in English needed :-)

    Twitter - My Game Trailers - www.spyn-doctor.de - Games: Your Doodles Are Bugged! - Kuchibi - Golden Tangram

    Useful for peer reviews and testing your own game: My little "evil" checklist for peer review stress testing
  • 11/16/2009 1:28 AM In reply to

    Re: Making part of a texture transparent

    Maybe I forgot to mention that not all parts will be rectangular... doesnt it make it more difficult?
  • 11/16/2009 5:20 PM In reply to

    Re: Making part of a texture transparent

    Yep, that certainly rules out the simple source rectangle method.

    Doc
    Urgently needed: Reviews in German for Kuchibi and Golden Tangram (No more reviews in English needed :-)

    Twitter - My Game Trailers - www.spyn-doctor.de - Games: Your Doodles Are Bugged! - Kuchibi - Golden Tangram

    Useful for peer reviews and testing your own game: My little "evil" checklist for peer review stress testing
  • 11/17/2009 12:44 AM In reply to

    Re: Making part of a texture transparent

    laarsp:
    Maybe I forgot to mention that not all parts will be rectangular... doesnt it make it more difficult?
    No. Draw by Y order, and there is no problem. This is not complex at all. Just draw your level in order, and you can walk in front or behind your wall or any alpha blended object easily.
    return;
  • 11/18/2009 2:30 PM In reply to

    Re: Making part of a texture transparent

    I meant that they are not rectangular and still part of the background. But I guess I'll go with cutting every object that youre able to walk behind out of the background and load as a separate texture... even if it means a bit more work.
Page 1 of 1 (10 items) Previous Next