Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Texture generated by code don't work in WebGL

Discussion in 'World Building' started by oWASDo, Jun 30, 2020.

  1. oWASDo

    oWASDo

    Joined:
    Jun 30, 2020
    Posts:
    3
    I got a problem with the generation of texture in WebGL.
    This is the result in the editor:
    Annotazione 2020-06-30 125944.png
    This is the result in build WebGL:
    Annotazione 2020-06-30 125601.png
    And this is the code that genreate the texture:
    Code (CSharp):
    1. private void AssignTextureByModel(string textureName, MeshRenderer meshRender, CompleteModel1 model, TextureSlot1 index)
    2.     {
    3.         bool transparent = false;
    4.         if (index.TextureType == TextureType1.None)
    5.             return;
    6.         if (index.TextureType == TextureType1.Diffuse)
    7.             transparent = true;
    8.  
    9.         string indexString = index.FilePath;
    10.         indexString = indexString.Remove(0, 1);
    11.         int textureIndex = int.Parse(indexString);
    12.         var imageByteArray = model.byteList[textureIndex].bytes.ToArray();
    13.         StbSharp.ImageReader imageReader = new StbSharp.ImageReader();
    14.         StbSharp.StbImage.stbi_set_flip_vertically_on_load(1);
    15.         MemoryStream stream = new MemoryStream(imageByteArray, 0, imageByteArray.Length, true);
    16.         StbSharp.Image image = new StbSharp.Image();
    17.         image = imageReader.Read(stream);
    18.  
    19.         stream.Close();
    20.         Texture2D texture;
    21.         texture = new Texture2D(image.Width, image.Height, TextureFormat.RGB24, false);
    22.         texture.filterMode = FilterMode.Point;
    23.         texture.wrapMode = TextureWrapMode.Clamp;
    24.         texture.anisoLevel = 0;
    25.         List<Color32> colors = new List<Color32>();
    26.  
    27.         for (int i = 0; i < image.Data.Length; i += image.Comp)
    28.         {
    29.             Color32 color;
    30.             switch (image.Comp)
    31.             {
    32.                 case 4:
    33.                     color = new Color32(image.Data[i + 0], image.Data[i + 1], image.Data[i + 2], image.Data[i + 3]);
    34.                     colors.Add(color);
    35.                     if (transparent && i == image.Data.Length - 1)
    36.                     {
    37.                         meshRender.material.SetFloat("_Mode", 3.0f);
    38.                         meshRender.material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
    39.                         meshRender.material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
    40.                         meshRender.material.EnableKeyword("_ALPHATEST_ON");
    41.                         meshRender.material.EnableKeyword("_ALPHABLEND_ON");
    42.                         meshRender.material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
    43.                         meshRender.material.renderQueue = 3000;
    44.                         meshRender.material.SetFloat("_Glossiness", 0.0f);
    45.                         transparent = false;
    46.                     }
    47.                     break;
    48.                 case 3:
    49.                     color = new Color32(image.Data[i + 0], image.Data[i + 1], image.Data[i + 2], 255);
    50.                     colors.Add(color);
    51.                     break;
    52.                 case 1:
    53.                     color = new Color32(image.Data[i + 0], image.Data[i + 0], image.Data[i + 0], image.Data[i + 0]);
    54.                     colors.Add(color);
    55.                     break;
    56.             }
    57.         }
    58.         texture.SetPixels32(colors.ToArray());
    59.         texture.Apply();
    60.         meshRender.material.SetTexture(textureName, texture);
    61.      
    62.  
    63.     }
    I don't really know what wrong with that.
     
  2. oWASDo

    oWASDo

    Joined:
    Jun 30, 2020
    Posts:
    3
    I solved it myself, My parser parses a float wrong because Unity WebGL use ", " for parse float and the editor use ". ".
    I solved it like that:
    Code (CSharp):
    1.  
    2. #if UNITY_WEBGL && !UNITY_EDITOR
    3.                        
    4. #else
    5.  
    6.                         floatToClean = floatToClean.Replace('.', ',');
    7. #endif