- Home /
export normal texture at runtime
I am having a problem with the process of making a normal texture into a file at runtime.
This is fine for gamma settings, but for linear settings the normal image is exported in pink.
Changing to *.TGA format or changing the textureformat type does not solve the problem.
This is the code to make a texture into a file.
public void ExportTexture(Texture2D t)
{
Texture2D exTexture = new Texture2D(t.width, t.height, TextureFormat.RGBA32, false);
exTexture.SetPixels(t.GetPixels());
fileName = $"{t.name}.png";
exportName = textureFolder + "\\" + fileName;
System.IO.File.WriteAllBytes(exportName, exTexture.EncodeToPNG());
}
The original image is tif & textureformat type dxt5.
The exported image is attached.
Answer by Namey5 · Aug 03, 2020 at 09:04 AM
You'll need to manually mark the texture as linear (normal maps should be linear anyway, so your normal map creation might be wrong). Unity doesn't document this, but there is an additional parameter to the Texture2D constructor that lets you mark textures as linear;
Texture2D exTexture = new Texture2D(t.width, t.height, TextureFormat.RGBA32, false, true);
I would make sure that your source texture is also linear.
Thanks for the answer. I checked the parameters for the linear.
Answer by Helloezzi · Aug 05, 2020 at 06:16 AM
For those who have the same problem as me..
I modified the code in the way @Namey5 mentioned. in both code exporter and loader
I unchecked srgb in the original normal texture options. I saw this. -> https://docs.unity3d.com/Manual/LinearRendering-LinearTextures.html
This created the normal texture I wanted. And it was rendered as desired.