- Home /
Setpixels creates gradient texture instead of raw colored pixels
Hi!
I'm using Texture2D's setPixels() method to generate a texture. I want to turn my data like:
000000002222211
112222200011221
...
to an image like this
However my result looks like this (I've added a black frame to make it more visible):
I think it must be some kind of shader thing I have to change, it looks like when you say in openGL (it's just a pseudo something):
var topLeft = vec2(0, 0)
var topRight = vec2(0, 1)
var botLeft = vec2(1, 0)
var botRight = vec2(1, 1)
var p = Primitive.Quad(topLeft, topRight, botLeft, botRight)
p.setColors(Color.brown, Color.bown Color.white, Color.white)
Here's my Unity Code for generating the texture:
using UnityEngine;
using System.Collections;
public class MiniMap : MonoBehaviour {
public static int WHITE = 0;
public static int BROWN = 1;
public static int DEEP_BROWN = 2;
public SpriteRenderer myRenderer;
public Map map;
Texture2D texture;
Sprite sp;
Color c_WHITE = Color.white;
Color c_BROWN = new Color(204, 139, 0);
Color c_DEEP_BROWN = new Color(148, 101, 0);
void Start() {
texture = new Texture2D(30, 15);
}
void Update() {
int[][] minimapInfo = map.getMinimapInfo(0);
Color[] pixels = new Color[30*15];
Color color = Color.red;
for(int i = 0; i < 30; ++i) {
for(int j = 0; j < 15; ++j) {
if(WHITE == minimapInfo[i][j]) {
pixels[i*15 +j] = c_WHITE;
}
else if(BROWN == minimapInfo[i][j]) {
pixels[i*15 + j] = c_BROWN;
}
else if(DEEP_BROWN == minimapInfo[i][j]) {
pixels[i*15 + j] = c_DEEP_BROWN;
}
}
}
texture.SetPixels(pixels, 0);
// Apply all SetPixel calls
texture.Apply();
sp = Sprite.Create(texture, new Rect(0, 0, 0.5f, 1f), new Vector2(0.0f, 0.0f), 0.1f);
myRenderer.sprite = sp;
}
}
Answer by Bunny83 · May 08, 2015 at 11:06 AM
I almost never used the 2d stuff in Unity (at least the Sprite stuff). However the only thing i can think of which could cause this is that the second "Rect" parameter of Sprite.Create doesn't represent UV coordinates but pixel coordinates. So if this is true that means you only use 1 pixel in y and the next pixel is bleeding into the texture from above. Try using:
Sprite.Create(texture, new Rect(0, 0, 15f, 15f), new Vector2(0.0f, 0.0f), 0.1f);
thanks for the quick response mate, that was the problem, the reference doesn't say much about that property btw.
@klolololol: Yeah, i know the documentation for some classes / methods is just like a stub. However due to the fact that sprites use a lot per-pixel values (like the borders) and you got that result i just assumed it might be the problem ^^.
You might want to file a bug report in Unity (Help->Report a bug). You can choose "documentation" as category.
Your answer
Follow this Question
Related Questions
Fastest rendering of 2D world with lots of changes to screen every frame? 3 Answers
Drawing a solid circle onto texture 2 Answers
For different texture sizes, which is faster? SetPixel or SetPixels? 2 Answers
Best Method for Setting Mass Numbers of Pixels (SetPixel, SetPixels) 2 Answers
SetPixels32 called with invalid number if pixels in the array 1 Answer