Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

OnPostprocessModel can no longer edit meshes with Read/Write set to false (Worked in b7)

Discussion in '2020.1 Beta' started by Prodigga, Jun 21, 2020.

  1. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,123
    In 2020.1b7 and prior, I was able to edit incoming meshes with OnPostprocessModel by accessing the sharedMesh on the MeshFilter of the Prefab.

    I have some code that converts incoming Mesh colors into linear space, the function looks like this:

    Code (CSharp):
    1.  
    2.     private void OnPostprocessModel(GameObject g)
    3.     {
    4.         HashSet<Mesh> processed = new HashSet<Mesh>();
    5.         foreach (var mesh in g.GetComponentsInChildren<MeshFilter>().Select(a => a.sharedMesh))
    6.         {
    7.             if (processed.Contains(mesh)) return;
    8.            
    9.             processed.Add(mesh);
    10.             mesh.name += " [Linear]";
    11.             mesh.colors = mesh.colors.Select(a => a.linear).ToArray();
    12.         }
    13.     }
    This worked in 2020.1b7 and prior. I did not need to set meshes to read/write enabled. This is intended. I dont want these meshes to be read/write enabled at runtime. I only want to modify the mesh on import.

    I just updated to 2020.1b12 and now my meshes wont import. I get the following error:
    Code (CSharp):
    1. Not allowed to access colors on mesh 'MyMesh [Linear]' (isReadable is false; Read/Write must be enabled in import settings)
    2. UnityEngine.Mesh:get_colors()
    3. ModelLinearCorrection:OnPostprocessModel(GameObject)
    Note this did not happen in b7 and prior.

    This is the only way I have found to modify mesh on import. If this change is intended, there is no longer any way to edit meshes during Edit time without setting them to read/write enabled for runtime purposes. So I am hoping this is just a regression and will be fixed! And if not, I hope there is a 'proper' way to edit meshes at 'edit time' without enabling read/write for runtime.
     
    Deleted User likes this.
  2. SevenPointRed

    SevenPointRed

    Joined:
    Feb 3, 2016
    Posts:
    218
    This happens when reading from textures as well, our texture batcher reads from them at edit time and now it breaks.
    We have to mark them as read/write, then run the game in the editor, then run the scripts, then stop play mode, then turn off read/write.
    Very broken.
     
    Deleted User likes this.
  3. r_eckert

    r_eckert

    Joined:
    Dec 11, 2017
    Posts:
    3
    I have the same problem in 2019.4.0 and 2019.4.1 when trying to modify Meshes in OnPostprocessModel
     
    Deleted User likes this.
  4. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    Found this in the Unity 2020.1.0b13 release notes:
    https://unity3d.com/unity/beta/2020.1.0b13
     
  5. Deleted User

    Deleted User

    Guest

    Thanks for pointing that out!

    What about 2019.4.x though? No mention there or in 2019.3.x's release notes, yet it broke some time after 2019.3.7 (see #3). The new method is definitely missing in 2019.4.1.
     
  6. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    I can't find anything "AcquireReadOnlyMeshData" related in the public issue track (link), so Unity staff would be needed to answer if a bugfix is planned for 2019.4.

    @LeonhardP
     
  7. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,136
    Peter77 and Prodigga like this.
  8. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,123
    Hey thanks for looking in to this! Just to clarify, this fix will allow us to both read and write into the Mesh instance being imported?

    I'm not just interested in reading data, I also need to write data into the imported mesh. I think we got a little off track here and seem to only be focusing on the read aspect. :)
     
  9. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,136
    The fix is restoring the original behaviour.
     
    Prodigga likes this.
  10. SevenPointRed

    SevenPointRed

    Joined:
    Feb 3, 2016
    Posts:
    218
    What about the same issue for texture read/write?
     
  11. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,136
    No, the fix is specific to meshes. If you're experiencing issues on that end, it would be great if you could submit a bug report with reproduction steps. It doesn't look like this is a known issue.
     
  12. SevenPointRed

    SevenPointRed

    Joined:
    Feb 3, 2016
    Posts:
    218
    Bug reporter just crashes, if it gets as far as loading then it always complains the description is not long enough and I end up having to fill it with lorem ipsum.

    Here is a full reproduction script, it's used for a crude atlas generator.
    Assign textures, watch it fail to read them in the editor, even with read/write on.
    You have to enter playmode for this to run and you have to have read/write on.
    It should work at edit time without read/write on.

    Code (CSharp):
    1. using System.IO;
    2. using UnityEngine;
    3.  
    4. [CreateAssetMenu(fileName = "AtlasCreator", menuName = "Game/AtlasCreator", order = 1)]
    5. public class AtlasCreator : ScriptableObject
    6. {
    7.     public Texture2D[] sprites;
    8.  
    9.     [Header("Settings")]
    10.     public int size = 2048;
    11.     public int tileSize = 32;
    12.     public int tilesX = 12;
    13.     public int tilesY = 16;
    14.     public string atlasName = "GroundAtlas";
    15.  
    16.     [ContextMenu("CreateAtlas")]
    17.     public void CreateAtlas()
    18.     {
    19.         var x = 0;
    20.         var y = 0;
    21.  
    22.         var targetTexture = new Texture2D(size, size, TextureFormat.RGBA32, false);
    23.  
    24.         for (int i = 0; i < sprites.Length; i ++)
    25.         {
    26.             for (int yy = 0; yy < tilesX; yy += 2)
    27.             {
    28.                 for (int xx = 0; xx < tilesY; xx += 2 )
    29.                 {
    30.                     var pixels = sprites[i].GetPixels(xx * tileSize, yy * tileSize, tileSize, tileSize);
    31.                     targetTexture.SetPixels(x * tileSize, targetTexture.height - (y+1) * tileSize, tileSize, tileSize, pixels);
    32.  
    33.                     x++;
    34.                     if (x >= size / tileSize)
    35.                     {
    36.                         x = 0;
    37.                         y++;
    38.                     }
    39.                 }
    40.             }
    41.         }
    42.  
    43.         File.WriteAllBytes($"{Application.dataPath}/Game/Ground/{atlasName}.png", targetTexture.EncodeToPNG());
    44.     }
    45. }
     
  13. The_Unity_Cat

    The_Unity_Cat

    Unity Technologies

    Joined:
    Jun 12, 2017
    Posts:
    8
    Hello SevenPointRed! I've managed to make a repro project in 2020.1.0b13, but was unable to reproduce the issue. While it does seem that Atlas operation doesn't work without having Read/Write enabled on a texture, the same behaviour is present in 2018.4 as well, so it's not a regression.
    After setting Read/Write enabled on the textures, I was able to create the atlas in both Editor and play mode so it might be that the issue got fixed in later 2020.1 versions, or there is more to the bug that just that. I have attached my project.
     

    Attached Files:

  14. SevenPointRed

    SevenPointRed

    Joined:
    Feb 3, 2016
    Posts:
    218
    Thanks for checking, that's odd as it always worked for us in previous 2018 versions, though we never touched the latest 2018.4, I don't remember the last version we used. I will check through git as well, im sure we had some versions of 2019 where it worked.