Note that there are some explanatory texts on larger screens.

plurals
  1. POTexture2D is turning black
    text
    copied!<p>I have a <code>Texture2D</code> that I'm loading from the Content Pipeline. That's working fine, but as soon as I try to use <code>SetData</code> on a completely different <code>Texture2D</code> all of the textures in my game go completely black:</p> <p><img src="https://i.stack.imgur.com/yZGvy.png" alt="Normal"></p> <p><img src="https://i.stack.imgur.com/8OOP3.png" alt="Black"></p> <p>This is in my HUDMeter class, the class that I want to be just red</p> <pre><code>Texture2D colorGrad = Content.Load&lt;Texture2D&gt;(GradientAsset); Color[,] pixels = new Color[colorGrad.Width, colorGrad.Height]; Color[] pixels1D = new Color[colorGrad.Width * colorGrad.Height]; pixels = GetRedChannel(colorGrad); pixels1D = Color2DToColor1D(pixels, colorGrad.Width); System.Diagnostics.Debug.WriteLine(pixels[32,32]); Gradient = colorGrad; Gradient.SetData&lt;Color&gt;(pixels1D); </code></pre> <p>These are using Riemers tutorial</p> <pre><code>protected Color[,] GetRedChannel(Texture2D texture) { Color[,] pixels = TextureTo2DArray(texture); Color[,] output = new Color[texture.Width, texture.Height]; for (int x = 0; x &lt; texture.Width; x++) { for (int y = 0; y &lt; texture.Height; y++) { output[x,y] = new Color(pixels[x,y].G, 0, 0); } } return output; } protected Color[,] TextureTo2DArray(Texture2D texture) { Color[] colors1D = new Color[texture.Width * texture.Height]; texture.GetData(colors1D); Color[,] colors2D = new Color[texture.Width, texture.Height]; for (int x = 0; x &lt; texture.Width; x++) for (int y = 0; y &lt; texture.Height; y++) colors2D[x, y] = colors1D[x + y * texture.Width]; return colors2D; } private Color[] Color2DToColor1D (Color[,] colors, int width) { Color[] output = new Color[colors.Length]; for (int x = 0; x &lt; width; x++) { for (int y = 0; y &lt; colors.Length / width; y++) { output[x + y * width] = colors[x % width, y % (colors.Length/width)]; } } return output; } </code></pre> <p>And here is the code to draw the sprites, this works fine and is how I always draw sprites:</p> <pre><code>batch.Draw(meter.Gradient, new Vector2(X, Y), Color.White); </code></pre> <p>Update:</p> <p>I've actually found that the sprites that don't use the same file are not black. Does Texture2D.SetData&lt;>() actually change the file itself? what is the use of that?</p> <p>Update:</p> <p>I just tried to use the Alpha as well as RGB and it's working. I'm thinking that there's something wrong with one of the conversion methods.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload