Hello,
I’m making a class, that can create textures. Everything was great until I have tested the code on colors with different alpha values. I noticed a wierd behavior in how Monogame draws transparent textures, which is already explained in this thread.
However, I experimented a bit and found out, that a texture I made is drawn differently than the same texture made in photoshop. The texture is a square with color (0, 0, 255, 5).
Here are example pictures, top is the loaded texture, bottom is mine created through code.
On CornflowerBlue:
On white:
On black:
Here is code I used to create the pictures:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace color_test
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private Texture2D custom, loaded;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
public Texture2D CreateRectangle(int width, int height, Color color)
{
Color[] data = new Color[width * height];
Texture2D texture = new(GraphicsDevice, width, height);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
data[y * width + x] = color;
}
}
texture.SetData(data);
return texture;
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
loaded = Content.Load<Texture2D>("shadepng");
custom = CreateRectangle(200, 200, new Color(0, 0, 255, 5));
// TODO: use this.Content to load your game content here
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
_spriteBatch.Begin();
_spriteBatch.Draw(loaded, new Rectangle(50, 50, 200, 200), Color.White);
_spriteBatch.Draw(custom, new Rectangle(50, 250, 200, 200), Color.White);
_spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}
}
}
I really have no clue what to do, is something wrong with my code?