Texture2d load from disk - set filtering
I'm new to Unity so might be doing something wrong but I'm using the following function to load an external image but the problem is, I cannot seem to set filtering or wrap mode - it always displays jaggy and wrapped.
public static Texture2D LoadImg(string filePath)
{
//load image from disk as Texture2d
Texture2D tex = null;
byte[] fileData;
if (File.Exists(filePath))
{
fileData = File.ReadAllBytes(filePath);
tex = new Texture2D(2048, 2048, TextureFormat.RGB24, false);
tex.LoadImage(fileData);
tex.filterMode = FilterMode.Trilinear;
tex.wrapMode = TextureWrapMode.Clamp;
tex.anisoLevel = 16;
tex.Apply(); //needed?
}
return tex;
}
//call the external image loader and apply it to gameObject
myMats[0].mainTexture = LoadImg(mypath + "/01.jpg");
I want to display the external jpeg image as high quality as possible in game. Any ideas?
@robdeja
Just use following code to load Texture
Texture2D textureName;
void Start ()
{
textureName = (Texture2D)Resources.Load("Resources/yourTextureName");
}
renderer.material.mainTexture = textureName;
Thanks for the reply but I cannot do this (as far as I know). I am loading an external image not packaged with the game, it's a file on the android file system (photos).
Update I have had some success in changing the following to allow it to generate mipmaps
tex = new Texture2D(2048, 2048, TextureFormat.RGB24, true);
Filtering now seems to work and the texture is not aliasing which is good, but it is now slightly blurry.
I've played with mip$$anonymous$$apBias but it's not increasing sharpness. $$anonymous$$y original texture is 2048px but it looks like it's much lower when shown in game - I'm always at a fixed distance from these textures (pretty close) so using mipmapping is a bit redundant in my case, but it does help with the texture filtering.
Any suggestions?
@robdeja Did you try TextureFormat.RGBA32 ?? Try it if not tried it.
Your answer
Follow this Question
Related Questions
TextureWrapMode repeats despite UV being 1 0 Answers
SetPixels edge pixels 0 Answers
Unity seems to be in 2D, but I want it in 3D 1 Answer
Mix Shaders Question -- Multiple Textures, Recolorable? 1 Answer