代码之家  ›  专栏  ›  技术社区  ›  Sebastian Gray

有没有一个alpha值使白色变为不可见的?

  •  7
  • Sebastian Gray  · 技术社区  · 14 年前

    如果我用下面的

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

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

    物体永远不会变的看不见,只会保持白色。我还注意到,如果我使用一个比黑色稍浅的值(比如50,50,50),绘图会从黑色变为不可见,再变为白色。

    我想我只是不明白alpha混合是如何工作的,但是有没有办法让白色褪色为半透明呢?

    编辑:我画的背景是颜色:矢车菊蓝(100,149,237,255)

    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

    alt text

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

    Texture2D tWhiteBox;
    

    将此添加到load方法中:

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

    v2.Y = v2.Y + 50;
    spriteBatch.Draw(tWhiteBox, v2, WhiteTransparent);
    
    4 回复  |  直到 14 年前
        1
  •  11
  •   MikeBMcL    14 年前

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

    我建议在阅读之前先阅读他所有关于pre-multipled alpha的文章(你可以使用他页面顶部左侧的“Blog Index”链接)——pre-mul确实更好,逻辑上更一致。

        2
  •  2
  •   Hans Passant    14 年前

    一种可能的解决方法是使用类似GraphicsPath.AddString文件(). 但是,您将失去ClearType抗锯齿效果。不太确定这在XNA中是否重要。我也不太清楚这一点,不足以提出类似于斯帕思的说法。

        3
  •  1
  •   Euphoric    14 年前

    我想这和SpriteBatch的Alpha出血设置有关。

    默认值为混合状态.aplablend. 具有公式BackgroundColor*(1-Alpha)+ForegroundColor。这就解释了黑白不同的巴哈维奥语。

    尝试使用不同的雪碧。开始方法混合状态。非重复为了得到想要的结果。这将导致:BackgroundColor*(1-Alpha)+ForegroundColor*Aplha,使白色完全透明。

        4
  •  0
  •   Jon Skeet    14 年前

    如果黑色变成“浅灰色”时,它是半透明的,这表明你有一个白色的背景。。。在这一点上,一个完全透明的白色放在顶部仍然是白色。

    试着在不同颜色的背景下使用你的白色物体,比如说红色,这会使它从白色(alpha=255)到粉色(alpha=127)变成红色(alpha=0)。