I’m trying to cache some item renders for my game during initialization for UI purposes. I’ve got the models rendering fine, but they seem to be extremely white and semi transparent.
See top left and hotbar.
Im not sure whether this is important or not, but I’m drawing everything to a RenderTarget2D every frame for post processing purposes.
This is my block preview renderer. The code is designed such that it returns a Texture2D containing the rendered item preview
static BasicEffect effect = null;
public static Texture2D Render(BlockModel itemModel, Matrix View, Matrix World)
{
if (effect == null)
{
effect = new BasicEffect(GameClient.Instance.GraphicsDevice);
}
//Create the projectionMatrix
Matrix projection = Matrix.CreateOrthographic(64, 64, 0.1f, 1024f);
//Initialise the render target
RenderTarget2D targetTexture = new RenderTarget2D(GameClient.Instance.GraphicsDevice, 64, 64, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8, 0, RenderTargetUsage.DiscardContents);
VoxelClient.Graphics.GraphicsDevice.BlendState = BlendState.Opaque;
VoxelClient.Graphics.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
GameClient.Instance.GraphicsDevice.SetRenderTarget(targetTexture);
GameClient.Instance.GraphicsDevice.Clear(Color.Transparent);
if (itemModel.voxels.Length > 0)
{
#region Mesh Construction
List<VertexPositionColorTexture> vertices = new List<VertexPositionColorTexture>();
//Construct vertices here. Not putting the code here because its not the issue
#endregion
//Prepare the effect
//rotation
effect.World = World;
//Camera
effect.View = View;
effect.Projection = projection;
//Texturing
effect.VertexColorEnabled = true;
effect.TextureEnabled = true;
effect.Texture = TextureManager.terrain;
//Double sided rendering
RasterizerState stat = new RasterizerState();
//stat.CullMode = CullMode.None;
VoxelClient.Graphics.GraphicsDevice.RasterizerState = stat;
//Point scaling
VoxelClient.Graphics.GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
//Draw the tris
foreach (var pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
VoxelClient.Graphics.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, vertices.ToArray(), 0, vertices.Count / 3);
}
}
GameClient.Instance.GraphicsDevice.SetRenderTarget(null);
//Make texture opaque
Texture2D opaqueTexture = new Texture2D(GameClient.Instance.GraphicsDevice, targetTexture.Width, targetTexture.Height);
Color[] pixelData = new Color[targetTexture.Width * targetTexture.Height];
targetTexture.GetData(pixelData);
//Make the texture opaque
for (int i = 0; i < pixelData.Length; i++)
{
Color col = pixelData[i];
//Console.WriteLine("A at (" + (i % opaqueTexture.Width) + ", " + (i / opaqueTexture.Width) + "): " + col.A);
if (col.A == 0)
{
//col.A = 255;
}
pixelData[i] = col;
}
opaqueTexture.SetData(pixelData);
return opaqueTexture;
}