#texture generation/optimization

1 messages · Page 1 of 1 (latest)

arctic jolt
#

i do know read/write and texture size will affect runtime memory usage

our project loads texture images on our own dedicated server and transform them into texture at runtime by code, we also have some sussy code that will manipulate pixel one by one to blur some of them

the workflow : download image from server -> save them in local files (like %appdata% on windows) -> reload them from the local files

->save

        if (savePath.Contains("blur"))
        {
#if UNITY_ANDROID || UNITY_IOS
            Texture2D convertedResource = new Texture2D(4, 4, TextureFormat.ETC2_RGBA8Crunched, false);
#else
            Texture2D convertedResource = new Texture2D(1, 1, TextureFormat.RGB24,false);
#endif

            convertedResource.LoadImage(webRequest.downloadHandler.data);
            convertedResource = BlurTexture(convertedResource); //warning: pixel data manipulation will cause heavy RAM usage
            File.WriteAllBytes(savePath, convertedResource.EncodeToPNG());
        }
        else
        {
            File.WriteAllBytes(savePath, webRequest.downloadHandler.data);
        }```

->load 
```cs
    public Texture2D LoadTextureFromLocal(string fileName, bool isMipMap = false)
    {
        string filePath = $"{Application.persistentDataPath}{agent_image_default_path}{fileName}.png";

        if (!File.Exists(filePath)) { return null; }

#if UNITY_ANDROID || UNITY_IOS
        Texture2D texture = new Texture2D(4, 4, TextureFormat.ETC2_RGBA8Crunched, isMipMap);
#else
        Texture2D texture = new Texture2D(1, 1, TextureFormat.RGB24, isMipMap);
#endif

        texture.LoadImage(File.ReadAllBytes(filePath), true);
        texture.name = fileName;

        return texture;
    }```

however, any texture that is generated from this cannot be viewed, inspected during runtime on editor, and from the memory profiler i can see they are wasting tons of resources, like 14mb per texture, no way
#

so , apart from that "blurtexture" function, will they costs less memory if i can set read/write to false, and manually force their size to 128/256 units?

#
        Texture2D blurred = new Texture2D(image.width, image.height, image.format, false);
        int _W = image.width;
        int _H = image.height;
        int xx, yy, x, y;
        
        Color[] imageColors = image.GetPixels();

        if (horizontal)
        {
            for (yy = 0; yy < _H; yy++)
            {
                for (xx = 0; xx < _W; xx++)
                {
                    ResetPixel();
                    for (x = xx; (x < xx + blurSize && x < _W); x++)
                    {
                        AddPixel(GetPixelInColorArray(imageColors ,x, yy, _W));
                    }
                    for (x = xx; (x > xx - blurSize && x > 0); x--)
                    {
                        AddPixel(GetPixelInColorArray(imageColors ,x, yy, _W));
                    }
                    CalcPixel();
                    for (x = xx; x < xx + blurSize && x < _W; x++)
                    {
                        blurred.SetPixel(x, yy, new Color(avgR, avgG, avgB, 1.0f));
                    }
                }
            }
        }```