in my game I’m loading Textures using Texture2D.LoadRawTextureData and then animate them by moving the UV coordinates of the Texture.
Behaviour on Windows / Android
This works on Windows and Android but not in WebGL where the Texture allways behaves like the TextureWrapMode is set to clamp even though I have tested that the WrapMode of the texture is properly set to Reapeat in WebGL also.
Behaviour in WebGL
This only occurs when i load the Texture via Texture2D.LoadRawTextureData.
If I use Resources.Load for example the texture behaves correctly.
Is this a bug or am I missing something?
Code to Load the Texture:
using (MemoryStream stream = new MemoryStream(data))
{
byte[] temp = new byte[4];
stream.Read(temp, 0, 4);
int width = BitConverter.ToInt32(temp, 0);
stream.Read(temp, 0, 4);
int height = BitConverter.ToInt32(temp, 0);
temp = new byte[data.Length - 8];
stream.Read(temp, 0, temp.Length);
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false, false);
tex.LoadRawTextureData(temp);
tex.Apply();
return tex;
}
According to the GLES 2.0.24 $3.8.2: Non-power-of-two textures must have a wrap mode of CLAMP_TO_EDGE. Which means that repeat wrap mode can not be used in WebGL 1.0 if your texture is NPOT.
There are possible ways to workaround the issue:
a) Use a power-of-two texture.
b) Use a texture with mipmaps (new Texture2D(width, height, TextureFormat.RGB24, true, false))
Explanation: the engine should then implicitly create a power-of-two texture which will emulate non-power-of-two behavior (non-power-of-two textures with mipmaps are not supported in GLES 2.0 and therefore are emulated), and as this texture is internally power-of-two, repeat wrap mode should work correctly.
If mentioned workaround does not work for you, please provide your exact Unity version.
thanks for pointing me in the right direction.
Using new Texture2D(width, height, TextureFormat.RGB24,true, false) however throws a UnityException: LoadRawTextureData: not enough data provided (will result in overread).
But I was able to “hack fix” this by using new Texture2D(width, height, TextureFormat.RGB24,false*, false) as before and then do* ```csharp
Texture2D tex2 = new Texture2D(ReadTexture.width, ReadTexture.height, TextureFormat.RGB24, true);
tex2.SetPixels(ReadTexture.GetPixels());
tex2.Apply(true);*