Skip to content

Commit

Permalink
Fix venues shaders on OSX (again)
Browse files Browse the repository at this point in the history
Previously we were exporting shaders for OSX separately.
Unfortunately that does not seem to guarantee that all shader variants were exported.
To mitigate that we now export materials as well. However to avoid
doubling of resulting yarground size we replace all textures with dummy on export.

Loading side does not change
  • Loading branch information
Anton Romanov authored and theli-ua committed Feb 6, 2025
1 parent 4439beb commit 07345a3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Assets/Script/Gameplay/BackgroundManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private async UniTaskVoid Start()
// Yarground comes with shaders for dx11/dx12/glcore/vulkan
// Metal shaders used on OSX come in this separate bundle
// Update our renderers to use them
var renderers = bg.GetComponentsInChildren<Renderer>();
var renderers = bg.GetComponentsInChildren<Renderer>(true);

foreach (var renderer in renderers)
{
Expand Down
56 changes: 45 additions & 11 deletions Assets/Script/Venue/BundleBackgroundManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class BundleBackgroundManager : MonoBehaviour
// DO NOT CHANGE THIS! It will break existing venues
public const string BACKGROUND_PREFAB_PATH = "Assets/_Background.prefab";
public const string BACKGROUND_SHADER_BUNDLE_NAME = "_metal_shaders.bytes";
public const string BACKGOUND_OSX_MATERIAL_PREFIX = "_metal_";

// DO NOT CHANGE the name of this! I *know* it doesn't follow naming conventions, but it will also break existing
// venues if we do change it.
Expand Down Expand Up @@ -58,11 +59,9 @@ public void ExportBackground()
_backgroundReference = gameObject;
string path = EditorUtility.SaveFilePanel("Save Background", string.Empty, "bg", "yarground");

var selectedBuildTargetGroup = EditorUserBuildSettings.selectedBuildTargetGroup;
var activeBuildTarget = EditorUserBuildSettings.activeBuildTarget;

GameObject clonedBackground = null;

AssetDatabase.DisallowAutoRefresh();

try
{
Expand All @@ -74,17 +73,45 @@ public void ExportBackground()
// First we'll collect all shaders and build a separate bundle out of them
// for Mac as no other build target will include Metal shaders
// And we want our background to work everywhere
var shaderAssets = EditorUtility.CollectDependencies(new[] { gameObject })
.OfType<Shader>() // Only shader dependencices
.Select(shader => AssetDatabase.GetAssetPath(shader)) // Get asset path
.Where(assetPath => !assetPath.StartsWith("Packages/com.unity")) // Not builtins

// We use materials as "anchors" to make sure all required
// shader variants are included
// var materialAssets = EditorUtility.CollectDependencies(new[] { gameObject })
var materialAssets = EditorUtility.CollectDependencies(new[] { gameObject })
.OfType<Material>() // Only material dependencices
.Select((mat, i) =>
{
// Create a clone
var matClone = new Material(mat);
// Avoid name collision
matClone.name = BACKGOUND_OSX_MATERIAL_PREFIX + mat.name;
// Drop all textures to not double resulting yarground in size
if (matClone.mainTexture != null)
{
matClone.mainTexture = Texture2D.whiteTexture;
}
foreach (var id in matClone.GetTexturePropertyNameIDs())
{
if (matClone.GetTexture(id) != null)
{
matClone.SetTexture(id, Texture2D.whiteTexture);
}
}
var assetPath = Path.Combine("Assets", matClone.name + ".mat");
AssetDatabase.CreateAsset(matClone, assetPath);

return assetPath;
})
.ToArray();

if (shaderAssets.Length > 0)
var shaderAssets = EditorUtility.CollectDependencies(new[] { gameObject })
.OfType<Shader>().Select(AssetDatabase.GetAssetPath);

if (materialAssets.Length > 0)
{
var metalAssetBundleBuild = default(AssetBundleBuild);
metalAssetBundleBuild.assetBundleName = BACKGROUND_SHADER_BUNDLE_NAME;
metalAssetBundleBuild.assetNames = shaderAssets;
metalAssetBundleBuild.assetNames = materialAssets.Concat(shaderAssets).ToArray();

BuildPipeline.BuildAssetBundles(Application.temporaryCachePath,
new[]
Expand All @@ -94,8 +121,14 @@ public void ExportBackground()
BuildTarget.StandaloneOSX);

var filePath = Path.Combine(Application.temporaryCachePath, BACKGROUND_SHADER_BUNDLE_NAME);
File.Move(filePath, Path.Combine(Application.dataPath, BACKGROUND_SHADER_BUNDLE_NAME));
AssetDatabase.Refresh();
var assetPath = Path.Combine(Application.dataPath, BACKGROUND_SHADER_BUNDLE_NAME);
File.Move(filePath, assetPath);
AssetDatabase.ImportAsset(assetPath);
}
// Now delete our material clones
foreach (var assetPath in materialAssets)
{
AssetDatabase.DeleteAsset(assetPath);
}

clonedBackground = Instantiate(_backgroundReference.gameObject);
Expand Down Expand Up @@ -144,6 +177,7 @@ public void ExportBackground()
}
finally
{
AssetDatabase.AllowAutoRefresh();
if (clonedBackground != null)
{
DestroyImmediate(clonedBackground);
Expand Down

0 comments on commit 07345a3

Please sign in to comment.