Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Graphics.ConvertTexture issue

Discussion in 'General Graphics' started by jawad_ahmad, Jun 24, 2020.

  1. jawad_ahmad

    jawad_ahmad

    Joined:
    Apr 28, 2017
    Posts:
    35
    Hey all,

    I've been playing around with Graphics.ConvertTexture recently as part of an Editor build pipeline to create a Texture Array. The idea was to convert a list of textures to conform to the Texture Array width/height/format restriction in an automated way so that artists do not have to ensure the array source textures are set up correctly in Unity. However, the method is not working as I'd have hoped.

    The main issue I'm having is that the data written into the destination texture is not consistent. Sometimes it's all white, sometimes it's the correct data. The code below is a basic example of what I'm trying to do:
    Code (CSharp):
    1. Object[] srcTextures = Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);
    2.  
    3. Texture2DArray textureArray = new Texture2DArray(256, 256, srcTextures.Length, TextureFormat.ETC2_RGBA8, true);
    4.  
    5. for (int i = 0; i < srcTextures.Length; ++i)
    6. {
    7.     Texture2D srcTexture = srcTextures[i] as Texture2D;
    8.     Texture2D dstTexture = new Texture2D(256, 256, TextureFormat.ARGB32, true);
    9.  
    10.     if (Graphics.ConvertTexture(srcTexture, dstTexture))
    11.     {
    12.         EditorUtility.CompressTexture(dstTexture, TextureFormat.ETC2_RGBA8, TextureCompressionQuality.Normal);
    13.         Graphics.CopyTexture(dstTexture, 0, textureArray, i);
    14.     }
    15. }
    (Note: this isn't the exact code I was trying but it was more or less it. Also, I typed this directly here, so excuse typos/compiler errors)

    The idea behind this code is to try and create Texture Array with N textures selected in the Project Tree. The resulting texture array only occasionally has any data in it. For example, if I select one texture, that seems to be fine (although not always the first time I create the new texture). However, if I select 2 textures, it will most certainly fail.

    The only thought I have here is that the issue is a timing one, in which maybe you have to wait for the results of ConvertTexture since it runs on the GPU? Running on the GPU is an assumption here. I looked into GPU fences to make the CPU wait for the GPU to complete, but it seems that it's not implemented/supported on a Mac(?).

    The Unity docs on the method do not mention anything in the remarks that seem relevant to the issue. Googling around on this gave very little. So I thought I'd have a try here and see if anyone knows something on the matter.

    Any help is appreciated!
     
    ronan34 and Fenikkel like this.