Si j'utilise les éléments suivantsY a-t-il une valeur alpha qui rend le blanc invisible?
for(int i = 255; i > 0; i--)
{
Color transparentBlack = new Color(0, 0, 0, i);
}
Je l'effet de l'objet en utilisant cette couleur pour dessiner avec allant du noir à un gris clair et invisible lorsque la valeur alpha va à zéro. Cependant, si je commence par une valeur blanche:
new Color(255, 255, 255, i);
Les objets ne deviennent jamais invisibles et restent uniquement blancs. J'ai aussi remarqué que si j'utilise une valeur un peu plus claire que le noir (disons 50, 50, 50) le dessin va de Dark, à invisible, à White.
Je suppose que je ne comprends pas comment fonctionne le mélange alpha, mais y a-t-il un moyen de faire passer la couleur blanche à la translucidité?
Edit: Le fond je dessine sur est Color.CornflowerBlue (100.149.237.255)
Edit: Exemple de programme XNA reproduisant l'explication. Utiliser; créer un nouveau projet XNA Game Studio 4.0 - Windows Game (4.0), appelez AlphaBlendTest - & Dans le projet de contenu ajouter un nouveau SpriteFont et appelez-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);
}
}
}
Edit: Ceci est l'image de ce code dessine: Editer: L'un des commentaires est concerné par le fait qu'il s'agit d'une «fonctionnalité» liée au texte de Windows. J'ai utilisé le texte comme exemple pour garder le programme de démonstration petit; tester avec une image donne le même résultat.
Pour ajouter la case carrée dans le programme de démonstration; Créez une image PNG carrée blanche et ajoutez-la au répertoire de contenu (utilisez les valeurs par défaut pour le pipeline de contenu). Puis ajouter à la classe:
Texture2D tWhiteBox;
Ajouter ceci dans la méthode de charge:
tWhiteBox = Content.Load<Texture2D>("whitebox");
ensuite au tirage au sort ajouter ce qui suit ci-dessous les autres déclarations draw:
v2.Y = v2.Y + 50;
spriteBatch.Draw(tWhiteBox, v2, WhiteTransparent);
Maintenant que vous avez de meilleures réponses, je suis tenté de supprimer le mien ... mais juste pour vérifier, est-ce que cela arrive avec des objets * autres que du texte *? –
Oui; J'ai simplement utilisé Draw String dans l'exemple pour ne pas avoir besoin de télécharger une texture pour le programme de test simple ;-) Pour tester cela, j'ai utilisé une image qui est juste une boîte blanche et ensuite dessiner, en utilisant les différentes couleurs l'instruction draw changera alors sa couleur. –