Search Unity

@Unity, Shaders getting lost in 2018.4 LTS

Discussion in 'Asset Bundles' started by DanVioletSagmiller, Nov 19, 2019.

  1. DanVioletSagmiller

    DanVioletSagmiller

    Joined:
    Aug 26, 2010
    Posts:
    204
    First, please note https://issuetracker.unity3d.com/is...ence-to-textures-if-loaded-from-asset-bundles this shows this same issue happening, and then said it was fixed in 5.1, then nearly every version after, for years now, people have been replying to that saying it is still happening in later versions. I.e. setup an asset bundle, add the prefabs/materials, and when the object loads, it is missing the shader, either going pink all together, or only using the albedo color.

    I just setup a system in the most basic way, importing materials, gameobjects, etc, and it happened to me. The fix was literally to put in a function like this after loading.

    Code (CSharp):
    1.         private void ResetMaterials(GameObject go)
    2.         {
    3.             foreach(var renderer in go.GetComponentsInChildren<Renderer>())
    4.             {
    5.                 foreach(var m in renderer.materials)
    6.                 {
    7.                     m.shader = Shader.Find(m.shader.name);
    8.                 }
    9.             }
    10.         }
    Which literally just replaces the shader reference, and in this case, everything is using the standard shader.

    It is a little frustrating to come across this every year or so, and to come across this but that says its fixed, with pages and pages of responses saying its not, and no way for us to say "NO, that bug is not fixed, or has come back."

    - Don't get me wrong, I appreciate the site/services, but please don't give us a channel to get updates on fixes, and then ignore it once you think its fixed. :(

    I have my code resolved for now with this code, but the problem is clearly still around.
     
  2. DanVioletSagmillerAtBrightMachines

    DanVioletSagmillerAtBrightMachines

    Joined:
    Jun 25, 2019
    Posts:
    3
    I will point out that while there may be steps to take to resolve this, this seems like something there should be an intelligent check and warning for. I.e. if you have a reference to the default shaders, I'm not even sure what steps I could do to use this the "right way" if I had my own shaders, I would just reference them in the asset as well. Perhaps I'm not fully understanding the problem, but I've faced this in version 5 through now.
     
  3. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    973
    The reason that this is market as fixed in 5.4 is that the bug, as described in the original bug report, was fixed by that release. Loading objects depending on materials depending on textures and shaders does generally work, otherwise hardly any usage of asset bundles would be working without the code you pasted, and absolutely nothing using Addressables would work, so you need to further investigate *why* they are being lost.

    This sounds like a dependency issue to me. If an object in an AssetBundle references an object and that object is not explicitly in another AssetBundle, it will be bundled into its AssetBundle. If you then end up with that object being duplicated by being bundled into multiple AssetBundles or with the main build, loading it will fail, as you cannot have multiple objects with the same Id.

    If this is what is occurring, there are multiple *potential* solutions to this. One is to add your shader variants to the Always Included Shaders. Another is to use the ScriptableBuildPipeline to build your asset bundles, using the AssetBundleBuiltInShaderExtraction preset.

    A third would be to look at how that works and automatically extract a shader variant collection from the asset bundles to add to your always included shaders.

    In other words, I would strongly recommend you actually investigate further, as you could otherwise end up with a fragile workaround, rather than a proper solution.
     
  4. pdinklag

    pdinklag

    Joined:
    Jan 24, 2017
    Posts:
    154
    Same issue here as @DanVioletSagmiller in 2019.2.8, and it's driving me completely nuts.
    My issues occur only in a build - in the editor, all is fine.

    I used the Asset Bundle Browser to build three asset bundles:
    1. shaders - Containing duplicates of the standard shaders.
    2. core - Containing a bunch of prefabs, materials, etc., using only shaders contained in shaders.
    3. levels - Containing currently one scene that uses prefabs, materials etc. from core.
    The assets of core and levels are not included in my build and only available in bundles in StreamingAssets that I load on startup.

    Now I load a scene from levels and it looks perfectly fine, suggesting that dependencies and everything is resolved correctly. I also load my characters (prefabs containing SkinnedMeshRenderers) from core using LoadAsset and everything is fine with them as well.

    Then my script loads prefabs and materials from core, also using LoadAsset, and adds them to the scene and procedurally generated meshes, and for all of that, I get the exact behaviour that Dan describes - they go pink.
    If I fix the materials using Shader.Find, then the materials at least use their correct Albedo color - however, the texture references are then gone.

    It's bonkers - everything is obviously loaded correctly as the fine looking level and characters prove - shaders and textures are working perfectly there. They are definitely not lost. But for some reason, when you explicitly load a certain material or a something with a mesh renderer that uses any material using LoadAsset, then they are pink and texture references are gone.

    How?
    I see absolutely no way to figure out what is wrong here, especially since what I see is completely contradictory.

    Please, give some info on how to investigate here. Dependencies are fine and everything works for some things, and not for others, even though they are loaded the exact same way from the exact same bundles. Calling LoadAllAssets on the bundle to make sure everything is loaded does not help either.

    Unity logs no errors or any information that points in any useful direction.

    EDIT:
    I realize the best thing to do here is create a minimal example to demonstrate. I'll try to set one up one of these days.
     
    Last edited: Nov 26, 2019
  5. pdinklag

    pdinklag

    Joined:
    Jan 24, 2017
    Posts:
    154
    I solved my issue somewhat accidentally by solving another one, and it's a bit strange they're related.

    Whenever I load my asset bundles, I look for a bootstrap object with a fixed path by loading it immediately using LoadAsset. In my example above, this exists in core. Now, I had some direct object references in that object - and guess what, those were exactly the materials and prefabs that had pink shaders. That caused them to be loaded immediately with the bootstrap object, which was a bug in my code I was fixing.

    What I don't quite get is how this causes the broken shaders.
    I only have a guess: between loading the asset bundle and loading the assets that had the missing shaders, there was a scene change. So maybe the directly referred assets got unloaded during the scene load (because they were unused) and after that, when loading them again, the shader resolution would fail. If this was the case, it'd be a Unity bug.

    With this info I should be able to set up an example when I got the time for it.
    Maybe this already helps @DanVioletSagmiller?
     
  6. giantkilleroverunity3d

    giantkilleroverunity3d

    Joined:
    Feb 28, 2014
    Posts:
    383
    I am in 2018.3. I exported a prefab from project 'A' and imported to project 'B'. Some mesh and material links are broken.
    Now here is the important data point. The Amplify Shader meshes and materials cross over. The meshes and materials of the model in project 'A' are from a Wings3d .obj import and are working fine. Some of the .obj files are missing from the pkg import. My hope and thinking are that the folders in file explorer can be navigated and corrected outside of Unity. When I copy files and return to Unity the recompile take place but the materials do not appear in the material selection panel in project 'B'. I have two sessions of Unity open, one for each project so I am able to cursor back and forth in the Windows task bar to see that the gameobject panels match.
    The game object icon in the heirarchy for this GO is a blue box with no white property sheet in the icons lower right hand corner. The other prefabs all have this.
    Another day lost screwing with Unity. Seriously, I dont know why I bother. Today I have experienced exactly what I have read in the posts of a day of researching and Unity seems to have dropped the ball again. This is the 7th project that has come to a dead effin halt because of my blatant misbelief the Unity is a sound product. In my 45 years of PC experience this product should die. No telling how many untold hours and lives wasted.
    @pdinklag @DanVioletSagmiller
     
    Last edited: Dec 20, 2019
  7. DanVioletSagmiller

    DanVioletSagmiller

    Joined:
    Aug 26, 2010
    Posts:
    204
    I never fully solved it. I thought I had, because the shaders seemed to be the same, but after a while, we noticed that shadows got lost on the corrected ones, vs ones pulled in directly from the build. We did not fix that before I left the project. :( so I won't be able to test any thing for a resolution. Though anyone else, feel free to post potential issues/resolutions for other people's sake.
     
  8. giantkilleroverunity3d

    giantkilleroverunity3d

    Joined:
    Feb 28, 2014
    Posts:
    383
    Well you know how this works. I walked away and went to bed.
    As I started falling asleep I received two other options and another question.
    #1: move things around in the folders. I had seen some one else do this and it worked.
    #2: make all shaders in ASE in the project 'A' then they all migrate with an export. Not the best answer but the effort is guaranteed.
    But then the other work comes in to play. What makes inherent shaders / materials work compared to .obj imported ones or the assign standard ones? And do the prefab icons really tell the story?
    @DanVioletSagmiller Thanks for getting back with us here. You might have dodged the bullet but others have or are not going to be able to because the problem still exists. It looks like the community is going to have to do some else's work and find the snafu and or clean up. This dev would be the real hero in this case.The closest I come is rebuilding the shaders and materials in ASE.
    The other thing I have seen is some one figures it out and holds the answer unto themselves because finding the solution is too exhaustive.
     
  9. giantkilleroverunity3d

    giantkilleroverunity3d

    Joined:
    Feb 28, 2014
    Posts:
    383
    @DanVioletSagmiller
    I am including you to keep you appraised of the steps I took and what I have found.
    I believe I have found the bug in a different way.
    I have a model .obj that I created in Wings3d 2.01. I created an UV map for these subObjects also.
    I Imported to Unity and the model looks fine. The colors placed on faces and meshes came through to the new gameobjects.

    *******************************************************************************
    But....
    There are correct meshes and where the imported colors show up there are no shaders!
    I found my problem here but it is in Unity. The child game objects / sub parts visually display in the editor and in game play of the colors imported from Wings. But there is no shader component there in this new game object from the import because there is no material selected in the mesh renderer box.
    No wonder I can not edit the shader in the open prefab selection.
    After pressing the cog on the shader, the edit buttons on the new panel do nothing probably because there is no code associated with this panel. Again, even though the child gameobjects display correctly in the scenes and play windows.
    *******************************************************************************

    In Unity, on some of those child gameobjects I placed Amplified shaders. This all looks good in this project, this scene.
    I select the Gameobject and export package.
    In new project, new scene I import custom package.
    In this new gameobject the components with the ASE shaders come through but the materials left as what came in from Wings3d did not come through. All meshes come through too which I do not have a problem with any where in the process. Unity made up the new materials even though in the first initial import the materials showed up.
    So it looks like I will have to assign ASE shaders to all game object components.
    Or what I have found is the standard shaders created from the original import are some how broken. They show correctly in the first project but not in the package passing to the second project.
    At least I have somewhat solved my problem here. I will hand assign the materials or ASEs to all children game objects and continue on.

    Unity standard shaders are not exporting.

    Thank you to whom have attended this.
     
    Last edited: Dec 21, 2019
  10. giantkilleroverunity3d

    giantkilleroverunity3d

    Joined:
    Feb 28, 2014
    Posts:
    383
    Where did you place this code?
    Because my next task is to try model image wrapping and mapping. It will import into Unity. I can make a prefab and it will work in that project and scene. But if I want it in another project/scene I have to import from Wings3d which loses any thing I want to do to it in Unity like the previous import. I lose the standard shaders when doing a Unity Gameobject export / import.
     
  11. giantkilleroverunity3d

    giantkilleroverunity3d

    Joined:
    Feb 28, 2014
    Posts:
    383
  12. pdinklag

    pdinklag

    Joined:
    Jan 24, 2017
    Posts:
    154
    What does that video even have to do with our problem and what comment is supposed to give us a fix?
    This thread is about materials loaded from asset bundles, your video is about FBX importing. I see no connection.

    This issue still isn't resolved.
     
    grrpnr likes this.
  13. Shaba1

    Shaba1

    Joined:
    Jan 21, 2010
    Posts:
    63
    it is not resolved for me. Everyone says to start by selecting the model then pressing the material button in the inspector. Well that does not do anything for the model that I dragged into the asset folder. Click that button does nothing. There is not checkbox no drop down menus. Just a big solid opaque box with a slider on the left but with no content.