Search Unity

WebGL, AssetBundles and Shaders

Discussion in 'Web' started by MaT227, Feb 14, 2018.

  1. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Hi there,

    We are using asset bundles in our WebGL project with Unity 2017.1.0p4 and facing some issues with shaders.
    Here's our setup :
    • AssetBundle 1 : Contains a module with several textures, materials, prefabs, etc. We have several of such assetbundles
    • AssetBundle 2 : An asset bundle which contains only shaders
    When loading those bundles the shaders appear pink, any idea why could this comes from ? Is the loading order important ?

    Thanks a lot .
     
  2. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    598
    Add your shader in your "Always Included Shader" in Edit => project settings => Graphics and rebuild your game.
     
  3. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Alright but when you do this, the shader is included in the build, right ? Or is it only a reference, telling that this shader exists.
     
  4. kognito1

    kognito1

    Joined:
    Apr 7, 2015
    Posts:
    331
    Yeah that will include it in the build. Do you get any shader compiler errors in the browser console? Also note that shader variants can be tricky with asset bundles in various circumstances. Have you built this project for a different platform (and if so, do the shaders work there)?
     
  5. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Thanks for the answer @kognito1, no I don't get any compilation issue. I guess that assetbundle loading order is important. Because even the material shader name is incorrect.
    Everything is also well built for the correct plateform.
     
  6. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    It doesn't comes from a loading order as we are using the manifest to properly download them.
     
  7. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,408
    usually i'd had to do something like this for asset bundles (when getting those missing shaders)

    Code (CSharp):
    1. var renderer = bundleInstance.GetComponent<Renderer>();
    2.         if (renderer != null) {
    3.             var material = Shader.Find(renderer.material.shader.name);
    4.             renderer.material.shader = null;
    5.             renderer.material.shader = material;
    6.         }
    7.  
     
  8. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Thanks for the answer @mgear. Some time ago I also used this solution but for me it's more of a workaround :(
    I would like to avoid doing such thing.
     
  9. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Here are the last discoveries :
    Materials are in one asset bundle.
    Shader is not included in the build and in another assetbundle. That causes the pink issue.
     
  10. kognito1

    kognito1

    Joined:
    Apr 7, 2015
    Posts:
    331
    It sounds like a shader variant issue. The only time we keep shaders in their own asset bundles is when it's a "non-variant shader". Otherwise we keep the shader untagged and let the "correct variant" get included with the material's bundle.
     
  11. fauzanmagnus

    fauzanmagnus

    Joined:
    Apr 11, 2022
    Posts:
    3
    where's u put this script?
     
  12. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,408
    it was after loading and instantiating asset bundle