Skip to content

Commit

Permalink
Assign Shader Variants to the same asset bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhail-dcl committed Nov 7, 2023
1 parent 135b2a6 commit ca58063
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,7 @@ private void CreateMaterialAsset(Material originalMaterial, string materialRoot,
var shader = newMaterial.shader;

if (settings.stripShaders)
env.assetDatabase.MarkAssetBundle(env.assetDatabase, shader,
shader.name + "_IGNORE" + PlatformUtils.GetPlatform());
env.assetDatabase.AssignAssetBundle(shader);

var textureProperties = GetTextureProperties(shader);

Expand Down Expand Up @@ -725,7 +724,7 @@ public virtual bool BuildAssetBundles(BuildTarget target, out IAssetBundleManife

// 1. Convert flagged folders to asset bundles only to automatically get dependencies for the metadata
manifest = env.buildPipeline.BuildAssetBundles(settings.finalAssetBundlePath,
BuildAssetBundleOptions.UncompressedAssetBundle | BuildAssetBundleOptions.ForceRebuildAssetBundle,
BuildAssetBundleOptions.UncompressedAssetBundle | BuildAssetBundleOptions.ForceRebuildAssetBundle | BuildAssetBundleOptions.AssetBundleStripUnityVersion,
target);

if (manifest == null)
Expand All @@ -750,7 +749,7 @@ public virtual bool BuildAssetBundles(BuildTarget target, out IAssetBundleManife

// 3. Convert flagged folders to asset bundles again but this time they have the metadata file inside
manifest = env.buildPipeline.BuildAssetBundles(settings.finalAssetBundlePath,
BuildAssetBundleOptions.UncompressedAssetBundle | BuildAssetBundleOptions.ForceRebuildAssetBundle,
BuildAssetBundleOptions.UncompressedAssetBundle | BuildAssetBundleOptions.ForceRebuildAssetBundle | BuildAssetBundleOptions.AssetBundleStripUnityVersion,
target);

var afterSecondBuild = EditorApplication.timeSinceStartup;
Expand Down
19 changes: 19 additions & 0 deletions asset-bundle-converter/Assets/AssetBundleConverter/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,25 @@ internal static void MarkFolderForAssetBundleBuild(string fullPath, string abNam
importer.SetAssetBundleNameAndVariant(abName, "");
}

internal static void AssignShaderBundle(IAssetDatabase db, Shader shader)
{
var abName = shader.name + "_IGNORE" + PlatformUtils.GetPlatform();

string assetPath = PathUtils.GetRelativePathTo(Application.dataPath, db.GetAssetPath(shader));

var importer = AssetImporter.GetAtPath(assetPath);

if (importer)
importer.SetAssetBundleNameAndVariant(abName, "");

// find a variants collection
var variantsPath = assetPath.Replace(".shader", "Variants.shadervariants");

importer = AssetImporter.GetAtPath(variantsPath);
if (importer)
importer.SetAssetBundleNameAndVariant(abName, "");
}

internal static bool MarkAssetForAssetBundleBuild(IAssetDatabase assetDb, Object asset, string abName)
{
string assetPath = PathUtils.GetRelativePathTo(Application.dataPath, assetDb.GetAssetPath(asset));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DCL.GLTFast.Wrappers;
using DCL.Shaders;
using GLTFast;
using UnityEditor;
using UnityEngine;
Expand Down Expand Up @@ -27,10 +28,10 @@ public override Material GenerateMaterial(int materialIndex, GLTFast.Schema.Mate
if (useSceneShader)
{
// Enable Forward+ and soft shadows
mat.EnableKeyword("_FORWARD_PLUS");
mat.EnableKeyword("_ADDITIONAL_LIGHT_SHADOWS");
mat.EnableKeyword("_MAIN_LIGHT_SHADOWS_CASCADE");
mat.EnableKeyword("_SHADOWS_SOFT");
mat.EnableKeyword(ShaderUtils.FW_PLUS);
mat.EnableKeyword(ShaderUtils.FW_PLUS_LIGHT_SHADOWS);
mat.EnableKeyword(ShaderUtils.FW_PLUS_SHADOWS_CASCADE);
mat.EnableKeyword(ShaderUtils.FW_PLUS_SHADOWS_SOFT);
}

return mat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,14 @@ public void CreateAsset(Object obj, string path)
UnityEditor.AssetDatabase.CreateAsset(obj, path);
}

public void MarkAssetBundle(IAssetDatabase assetDb, Object asset, string abName)
public void AssignAssetBundle(Shader shader)
{
Utils.MarkAssetForAssetBundleBuild(assetDb, asset, abName);
Utils.AssignShaderBundle(this, shader);
}

public void MarkAssetBundle(Object asset, string abName)
{
Utils.MarkAssetForAssetBundleBuild(this, asset, abName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public interface IAssetDatabase
void SaveImporter(AssetImporter gltfImporter);

void CreateAsset(Object obj, string path);

void MarkAssetBundle(IAssetDatabase assetDb, Object asset, string abName);
void AssignAssetBundle(Shader shader);
void MarkAssetBundle(Object asset, string abName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ MonoBehaviour:
m_ColorGradingMode: 0
m_ColorGradingLutSize: 32
m_UseFastSRGBLinearConversion: 0
m_SupportDataDrivenLensFlare: 1
m_ShadowType: 1
m_LocalShadowsSupported: 0
m_LocalShadowsAtlasResolution: 256
Expand Down Expand Up @@ -103,6 +104,10 @@ MonoBehaviour:
m_PrefilterDBufferMRT1: 1
m_PrefilterDBufferMRT2: 1
m_PrefilterDBufferMRT3: 1
m_PrefilterSoftShadowsQualityLow: 1
m_PrefilterSoftShadowsQualityMedium: 1
m_PrefilterSoftShadowsQualityHigh: 1
m_PrefilterSoftShadows: 0
m_PrefilterScreenCoord: 1
m_PrefilterNativeRenderPass: 1
m_ShaderVariantLogLevel: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ MonoBehaviour:
blitHDROverlay: {fileID: 4800000, guid: a89bee29cffa951418fc1e2da94d1959, type: 3}
cameraMotionVector: {fileID: 4800000, guid: c56b7e0d4c7cb484e959caeeedae9bbf, type: 3}
objectMotionVector: {fileID: 4800000, guid: 7b3ede40266cd49a395def176e1bc486, type: 3}
dataDrivenLensFlare: {fileID: 4800000, guid: 6cda457ac28612740adb23da5d39ea92, type: 3}
m_AssetVersion: 2
m_OpaqueLayerMask:
serializedVersion: 2
Expand Down

0 comments on commit ca58063

Please sign in to comment.