Skip to content

Commit 1794c28

Browse files
author
David Kline
authored
Merge pull request #8804 from davidkline-ms/shaderImport
Fix immutable shader issue when switching to URP (#8783)
2 parents d6e5074 + a6e96ed commit 1794c28

10 files changed

+176
-1
lines changed

Assets/MRTK/StandardAssets/EditorUtilities.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "Microsoft.MixedReality.Toolkit.StandardAssets.Editor",
3+
"references": [],
4+
"optionalUnityReferences": [],
5+
"includePlatforms": [
6+
"Editor"
7+
],
8+
"excludePlatforms": [],
9+
"allowUnsafeCode": false,
10+
"overrideReferences": false,
11+
"precompiledReferences": [],
12+
"autoReferenced": true,
13+
"defineConstraints": []
14+
}

Assets/MRTK/StandardAssets/EditorUtilities/MRTK.StandardAssets.Editor.asmdef.meta

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using UnityEditor;
7+
using UnityEngine;
8+
9+
namespace Microsoft.MixedReality.Toolkit.Utilities.Editor
10+
{
11+
[InitializeOnLoad]
12+
static class OnLoadUtilities
13+
{
14+
private const string SessionStateKey = "StandardAssetsOnLoadUtilitiesSessionStateKey";
15+
16+
private const string ShaderSentinelGuid = "05852dd420bb9ec4cb7318bfa529d37c";
17+
private const string ShaderSentinelFile = "MRTK.Shaders.Sentinel";
18+
19+
private const string ShaderImportDestination = "MRTK/Shaders";
20+
21+
static OnLoadUtilities()
22+
{
23+
// This InitializeOnLoad handler only runs once at editor launch in order to adjust for Unity version
24+
// differences. These don't need to (and should not be) run on an ongoing basis. This uses the
25+
// volatile SessionState which is clear when Unity launches to ensure that this only runs the
26+
// expensive work (file system i/o) once.
27+
if (!SessionState.GetBool(SessionStateKey, false))
28+
{
29+
SessionState.SetBool(SessionStateKey, true);
30+
EnsureShaders();
31+
}
32+
}
33+
34+
/// <summary>
35+
/// Ensures that MRTK shader files are present in a writable location. To support the
36+
/// Universal Render Pipeline, shader modifications must be persisted.
37+
/// </summary>
38+
private static void EnsureShaders()
39+
{
40+
if (!AssetsContainsShaders())
41+
{
42+
ImportShaderFiles();
43+
}
44+
}
45+
46+
/// <summary>
47+
/// Checks to see if the Assets or Packages (if embedded) folder trees contains the MRTK shaders.
48+
/// </summary>
49+
/// <returns>True if the shader sentinel file is found, otherwise false.</returns>
50+
private static bool AssetsContainsShaders()
51+
{
52+
return !string.IsNullOrWhiteSpace(AssetDatabase.GUIDToAssetPath(ShaderSentinelGuid));
53+
}
54+
55+
/// <summary>
56+
/// Finds the shader folder within an installed or embedded package.
57+
/// </summary>
58+
/// <returns>
59+
/// DirectoryInfo object representing the shader folder in the package cache.
60+
/// If not found, returns null.
61+
/// </returns>
62+
private static DirectoryInfo FindShaderFolderInPackage()
63+
{
64+
List<string> searchPaths = new List<string>
65+
{
66+
Path.GetFullPath(Path.Combine("Library", "PackageCache")),
67+
Path.GetFullPath("Packages")
68+
};
69+
70+
foreach (string path in searchPaths)
71+
{
72+
DirectoryInfo di = new DirectoryInfo(path);
73+
if (!di.Exists) { continue; }
74+
75+
FileInfo[] files = di.GetFiles(ShaderSentinelFile, SearchOption.AllDirectories);
76+
if (files.Length > 0)
77+
{
78+
return new DirectoryInfo(files[0].DirectoryName);
79+
}
80+
}
81+
82+
return null;
83+
}
84+
85+
/// <summary>
86+
/// Copies the shader files from the package cache to the Assets folder tree.
87+
/// </summary>
88+
private static void ImportShaderFiles()
89+
{
90+
DirectoryInfo source = FindShaderFolderInPackage();
91+
if (source == null)
92+
{
93+
Debug.LogError("Unable to locate the shader source folder in the package");
94+
return;
95+
}
96+
97+
DirectoryInfo destination = new DirectoryInfo(Path.Combine(Application.dataPath, ShaderImportDestination));
98+
if (!destination.Exists)
99+
{
100+
destination.Create();
101+
}
102+
103+
FileInfo[] sourceFiles = source.GetFiles();
104+
foreach (FileInfo fi in sourceFiles)
105+
{
106+
fi.CopyTo(Path.Combine(destination.FullName, fi.Name));
107+
}
108+
}
109+
}
110+
}

Assets/MRTK/StandardAssets/EditorUtilities/OnLoadUtilities.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/MRTK/StandardAssets/Shaders/MRTK.Shaders.sentinel

Whitespace-only changes.

Assets/MRTK/StandardAssets/Shaders/MRTK.Shaders.sentinel.meta

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Documentation/ReleaseNotes.md

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ There are no breaking changes since version 2.5.0.
2828

2929
## Known issues
3030

31+
### Some Mixed Reality Toolkit Standard Shader features require the Foundation package
32+
33+
When imported via the Unity Package Manager, the MRTK Standard Shader utilities scripts (ex: HoverLight.cs) are not co-located with the shader in the Standard Assets package. To access this functionality, applications will require the Foundation package to be imported.
34+
3135
### CameraCache may create a new camera on shutdown
3236

3337
In some situations (e.g. when using the LeapMotion provider in the Unity Editor), it is possible for the CameraCache to re-create the MainCamera on shutdown. Please see [this issue](https://github.com/microsoft/MixedRealityToolkit-Unity/issues/8459) for more information.

scripts/ci/validatecode.ps1

+3-1
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,8 @@ $InitializeOnLoadExceptions = [System.Collections.Generic.HashSet[String]]@(
369369
"Assets/MRTK/Providers/Oculus/XRSDK/Editor/OculusXRSDKConfigurationChecker.cs",
370370
"Assets/MRTK/Providers/WindowsMixedReality/Shared/Editor/WindowsMixedRealityConfigurationChecker.cs",
371371
"Assets/MRTK/Providers/WindowsMixedReality/XRSDK/Editor/WindowsMixedRealityXRSDKConfigurationChecker.cs",
372-
"Assets/MRTK/Providers/XRSDK/Editor/XRSDKConfigurationChecker.cs"
372+
"Assets/MRTK/Providers/XRSDK/Editor/XRSDKConfigurationChecker.cs",
373+
"Assets/MRTK/StandardAssets/EditorUtilities/OnLoadUtilities.cs"
373374
)
374375

375376
<#
@@ -622,6 +623,7 @@ $AsmDefExceptions = [System.Collections.Generic.HashSet[String]]@(
622623
"Assets/MRTK/Services/SceneSystem/MRTK.SceneSystem.asmdef",
623624
"Assets/MRTK/Services/SpatialAwarenessSystem/MRTK.SpatialAwarenessSystem.asmdef",
624625
"Assets/MRTK/Services/TeleportSystem/MRTK.TeleportSystem.asmdef",
626+
"Assets/MRTK/StandardAssets/EditorUtilities/MRTK.StandardAssets.Editor.asmdef",
625627
"Assets/MRTK/Tests/EditModeTests/MRTK.EditModeTests.asmdef",
626628
"Assets/MRTK/Tests/PlayModeTests/MRTK.PlayModeTests.asmdef",
627629
"Assets/MRTK/Tests/TestUtilities/MRTK.Tests.Utilities.asmdef",

scripts/packaging/createupmpackages.ps1

+12
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ foreach ($entry in $packages.GetEnumerator()) {
130130
# helper script to prepare the folder.
131131
Start-Process -FilePath "$PSHOME/powershell.exe" -ArgumentList "$scriptPath/foundationpreupm.ps1 -PackageRoot $packagePath" -NoNewWindow -Wait
132132
}
133+
elseif ($packageName -eq "standardassets") {
134+
# The standard assets package contains shaders that need to be imported into the Assets folder so that they
135+
# can be modified if the render pipeline is changed. To avoid duplicate resources (in library and assets)
136+
# we rename the Shaders folder to Shaders~, which makes it hidden to the Unity Editor.
137+
Rename-Item -Path "$packagePath/Shaders" -NewName "$packagePath/Shaders~"
138+
Remove-Item -Path "$packagePath/Shaders.meta"
139+
}
133140
elseif ($packageName -eq "examples") {
134141
# The examples folder is a collection of sample projects. In order to perform the necessary
135142
# preparaton, without overly complicating this script, we will use a helper script to prepare
@@ -179,6 +186,11 @@ foreach ($entry in $packages.GetEnumerator()) {
179186
# The foundation package MOVES some content around. This restores the moved files.
180187
Start-Process -FilePath "git" -ArgumentList "checkout Services/SceneSystem/SceneSystemResources*" -NoNewWindow -Wait
181188
}
189+
elseif ($packageName -eq "standardassets") {
190+
# The standard assets package RENAMES and DELETES some content. This restores the original files.
191+
Rename-Item -Path "$packagePath/Shaders~" -NewName "$packagePath/Shaders"
192+
Start-Process -FilePath "git" -ArgumentList "checkout Shaders.meta" -NoNewWindow -Wait
193+
}
182194

183195
# Delete the files copied in previously
184196
Remove-Item -Path "$packagePath/LICENSE.md*"

0 commit comments

Comments
 (0)