Если я использую следующее
for(int i = 255; i > 0; i--)
{
Color transparentBlack = new Color(0, 0, 0, i);
}
У меня есть эффект от объекта, использующего этот цвет для рисования при переходе от черного к светло-серому, а затем невидимого, когда значение альфа уменьшается до нуля. Однако, если я начну со значения белого:
new Color(255, 255, 255, i);
Объекты никогда не становятся невидимыми и остаются только белыми. Я также заметил, что если я использую значение, которое немного светлее черного (скажем, 50, 50, 50), рисунок переходит от Темного, к невидимому, к Белому.
Полагаю, я просто не понимаю, как работает альфа-смешение, но есть ли способ заставить белый цвет исчезнуть до полупрозрачности?
Редактировать: Фон, на котором я рисую, - это Color.CornflowerBlue (100,149,237,255)
Редактировать: Пример программы XNA, воспроизводящей объяснение. Использовать; создайте новый проект XNA Game Studio 4.0 - Windows Game (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);
}
}
}
Редактировать: Это изображение, которое рисует этот код:
Редактировать: Один из комментариев касается того, что это текстовая «особенность» окон. Я использовал текст в качестве примера, чтобы демонстрационная программа была маленькой; тестирование с изображением дает тот же результат.
Чтобы добавить квадрат в демонстрационную программу; создайте квадратное белое изображение PNG и добавьте его в каталог содержимого (используйте значения по умолчанию для конвейера содержимого). Затем добавьте это в класс:
Texture2D tWhiteBox;
Добавьте это к методу загрузки:
tWhiteBox = Content.Load<Texture2D>("whitebox");
Затем в розыгрыше добавьте следующие остальные операторы розыгрыша:
v2.Y = v2.Y + 50;
spriteBatch.Draw(tWhiteBox, v2, WhiteTransparent);