是否存在使白色变得不可见的alpha值?

时间:2010-10-03 11:34:24

标签: c# xna windows-phone-7

如果我使用以下

for(int i = 255; i > 0; i--)
{
    Color transparentBlack = new Color(0, 0, 0, i);
}

我有使用此颜色的对象的效果,从黑色到浅灰色绘制,然后在alpha值变为零时不可见。但是,如果我以白色值开头:

new Color(255, 255, 255, i);

物体永远不会变得不可见,只会保持白色。我也注意到,如果我使用的颜色比黑色(比如50,50,50)稍微浅一些,那么绘图就会从黑暗变为隐形,变为白色。

我认为我只是不明白alpha混合混合是如何工作的,但有没有办法让白色渐变成半透明?

编辑:我正在绘制的背景是Color.CornflowerBlue(100,149,237,255)

编辑:示例XNA程序再现说明。使用;创建一个新的XNA Game Studio 4.0项目 - Windows游戏(4.0),称之为AlphaBlendTest - &在内容项目中添加一个新的SpriteFont并将其命名为testfont

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace AlphaBlendTest
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;


    SpriteFont font;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
        // TODO: Add your initialization logic here

        base.Initialize();
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        font = Content.Load<SpriteFont>("testfont");
    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        // TODO: Add your update logic here

        base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();

        //spriteBatch.Draw(tR, new Rectangle(100, 100, 100, 100), Color.Red);

        Vector2 v2 = new Vector2(0, 0);
        spriteBatch.DrawString(font, "Test - White", v2, Color.White);

        v2.Y = v2.Y + 50;

        spriteBatch.DrawString(font, "Test - Black", v2, Color.Black);

        v2.Y = v2.Y + 50;

        Color BlackTransparent = new Color(0, 0, 0, 0);
        spriteBatch.DrawString(font, "Test - Black Transparent", v2, BlackTransparent);

        v2.Y = v2.Y + 50;
        Color WhiteTransparent = new Color(255, 255, 255, 0);
        spriteBatch.DrawString(font, "Test - White Transparent", v2, WhiteTransparent);

        spriteBatch.End();

        base.Draw(gameTime);
    }
}
}

编辑:这是此代码绘制的图像: alt text

编辑:其中一条评论涉及这是一个仅与文本相关的Windows“功能”。我以文本为例来保持演示程序的小巧;使用图像进行测试会得到相同的结果。 alt text

将方框添加到演示程序中;创建一个方形白色PNG图像并将其添加到内容目录(使用内容管道的默认值)。然后将其添加到类:

Texture2D tWhiteBox;

将此添加到加载方法中:

tWhiteBox = Content.Load<Texture2D>("whitebox");

然后在绘图中添加以下其他绘图语句:

v2.Y = v2.Y + 50;
spriteBatch.Draw(tWhiteBox, v2, WhiteTransparent);

4 个答案:

答案 0 :(得分:11)

灰色是您在XNA 4.0中获得的(如果您尝试仅调整Alpha通道而不调整RGB通道,则为全白色)。使用预乘alpha进行透明度(默认情况下)。如果您正在寻找旧的非预先XNA 3.1行为,请参阅Shawn Hargreaves的这篇文章:http://blogs.msdn.com/b/shawnhar/archive/2010/04/08/premultiplied-alpha-in-xna-game-studio-4-0.aspx。在帖子的底部,它告诉你如何做到这一点。

我建议在这之前阅读所有关于预乘alpha的帖子(虽然你可以使用靠近页面顶部左侧的“博客索引”链接) - pre-mul真的很多更好,更符合逻辑。

答案 1 :(得分:2)

这是一个历史限制,至少在Windows上是这样。像DrawTextEx这样的GDI原语不支持透明度。他们只会忽略alpha通道。这种限制是通过GDI +和Milcore等图形库解决的。但是只有一个TrueType渲染器,它的工作太过重要,不容易被替换。您的屏幕截图显示它在常规Windows窗口中运行,因此限制很可能适用。不确定控制台上是否有所不同。

可能的解决方法是使用类似于GraphicsPath.AddString()的函数。但是,您将失去ClearType抗锯齿效果。不太确定XNA中的问题。我也不清楚它是否足以建议相当于GraphicsPath。

答案 2 :(得分:1)

我认为它与SpriteBatch的Alpha bleding设置有关。

默认为BlendState.AplhaBlend。它有公式BackgroundColor *(1-Alpha)+ ForegroundColor。这可以解释不同的黑色和白色的行为。

尝试使用BlendState.NonPremultiplied的不同spriteBatch.Begin方法来获得所需的结果。这将导致:BackgroundColor *(1-Alpha)+ ForegroundColor * Aplha,使白色完全透明。

答案 3 :(得分:0)

如果黑色变为“浅灰色”,当它是半透明的时,表明你有一个白色背景......此时,顶部的完全透明的白色仍然是白色。

尝试将白色物体用于不同颜色的背景 - 红色,比如说 - 应该从白色(alpha = 255)到粉红色(alpha = 127)再到红色(alpha = 0)。