- Home /
Texture filter mode problem
How do I set point sampling when magnifying a texture.
The following script (attached to Plane object) seems to use linear sampling, even though the filterMode is set to point:
using UnityEngine;
using System.Collections;
public class CreateSimpleTexture : MonoBehaviour {
void Start () {
Texture2D texture = new Texture2D(30,30,TextureFormat.ARGB32,false);
Color[] colors = new Color[texture.width*texture.height];
for (int x=0;x<texture.width;x++){
for (int y=0;y<texture.height;y++){
if (y%2==0 && x%2==0){
colors[y*texture.width+x] = new Color(1,0,0,1);
} else {
colors[y*texture.width+x] = new Color(1,1,1,1);
}
}
}
texture.filterMode = FilterMode.Point;
texture.wrapMode = TextureWrapMode.Clamp;
texture.SetPixels(colors);
texture.Apply();
GetComponent<MeshRenderer>().sharedMaterial.mainTexture = texture;
}
}
Result:
(I'm running OS/X - I don't know if this problem is platform related)
Answer by Mortennobel · Apr 20, 2012 at 01:51 PM
I found the problem.
Unity transforms rescales my texture into power of two. In that process bilinear sampling af used.
Answer by Kryptos · Apr 20, 2012 at 01:52 PM
Try modifying the mipmap bias to sharpen the texture:
texture.mipMapBias = -0.5F;
edit: I think that you can disable the linear sampling with this method:
// mipmap, linear
Texture2D texture = new Texture2D(30, 30, TextureFormat.ARGB32, false, false);
I doesn't help. I think the solution is to always use a power of two texture and then scale it to fit my non-power of two needs.
Your answer
Follow this Question
Related Questions
Update Texture type to GUI during runtime 0 Answers
Updating Sprite texture leaves red outline 1 Answer
Combine Array of Sprites to Form One Sprite 0 Answers
Offset detail texture in c# 1 Answer
Grass Texture 2 Answers