Search Unity

Texture2D.LoadRawTextureData with TextureWrapMode.Repeat not working

Discussion in 'Web' started by PatientZero, Nov 23, 2016.

  1. PatientZero

    PatientZero

    Joined:
    Apr 8, 2015
    Posts:
    31
    Hi,

    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:
    Code (CSharp):
    1. using (MemoryStream stream = new MemoryStream(data))
    2.             {
    3.                 byte[] temp = new byte[4];
    4.  
    5.                 stream.Read(temp, 0, 4);
    6.  
    7.                 int width = BitConverter.ToInt32(temp, 0);
    8.  
    9.                 stream.Read(temp, 0, 4);
    10.  
    11.                 int height = BitConverter.ToInt32(temp, 0);
    12.  
    13.                 temp = new byte[data.Length - 8];
    14.  
    15.                 stream.Read(temp, 0, temp.Length);
    16.  
    17.                 Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false, false);
    18.  
    19.                 tex.LoadRawTextureData(temp);
    20.  
    21.                 tex.Apply();
    22.  
    23.                 return tex;
    24.             }
     
  2. alexsuvorov

    alexsuvorov

    Unity Technologies

    Joined:
    Nov 15, 2015
    Posts:
    327
    Hello PatientZero.

    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.
     
  3. PatientZero

    PatientZero

    Joined:
    Apr 8, 2015
    Posts:
    31
    Hey Alex,

    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
    Code (CSharp):
    1.                 Texture2D tex2 = new Texture2D(ReadTexture.width, ReadTexture.height, TextureFormat.RGB24, true);
    2.                 tex2.SetPixels(ReadTexture.GetPixels());
    3.                 tex2.Apply(true);


    My Unity Version is 5.4.2f2