Skip to content

Commit

Permalink
fix: refactor internal helper types to fix compilation when ShaderGra…
Browse files Browse the repository at this point in the history
…ph isn't present
  • Loading branch information
hybridherbst committed Mar 27, 2023
1 parent bd1bec7 commit 8d33e27
Show file tree
Hide file tree
Showing 20 changed files with 123 additions and 107 deletions.
26 changes: 17 additions & 9 deletions UnityGLTF/Assets/UnityGLTF/Editor/Scripts/GLTFImporterInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,20 @@ private void MaterialInspectorGUI()
EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(GLTFImporter._importMaterials)));
EditorGUILayout.Separator();

const string key = nameof(GLTFImporterInspector) + "_RemapMaterials";
// extract and remap materials
if (mats != null && mats.serializedObject != null)
{
EditorGUI.indentLevel++;
var externalObjectMap = t.GetExternalObjectMap();

void ExtractMaterial(Material subAsset)
void ExtractMaterial(Material subAsset, bool importImmediately)
{
if (!subAsset) return;
var filename = SanitizePath(subAsset.name);
var destinationPath = Path.GetDirectoryName(t.assetPath) + "/" + filename + ".mat";
var dirName = Path.GetDirectoryName(t.assetPath) + "/Materials";
if (!Directory.Exists(dirName))
Directory.CreateDirectory(dirName);
var destinationPath = dirName + "/" + filename + ".mat";
var assetPath = AssetDatabase.GetAssetPath(subAsset);

var clone = Instantiate(subAsset);
Expand All @@ -144,12 +146,15 @@ void ExtractMaterial(Material subAsset)
var assetImporter = AssetImporter.GetAtPath(assetPath);
assetImporter.AddRemap(new AssetImporter.SourceAssetIdentifier(subAsset), clone);

AssetDatabase.WriteImportSettingsIfDirty(assetPath);
AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
if (importImmediately)
{
AssetDatabase.WriteImportSettingsIfDirty(assetPath);
AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
}
}

const string key2 = nameof(GLTFImporterInspector) + "_RemapMaterials_List";
var newVal2 = EditorGUILayout.BeginFoldoutHeaderGroup(SessionState.GetBool(key2, false), "Imported Materials (" + mats.arraySize + ")");
var newVal2 = EditorGUILayout.BeginFoldoutHeaderGroup(SessionState.GetBool(key2, false), "Remap Materials (" + mats.arraySize + ")");
SessionState.SetBool(key2, newVal2);
if (newVal2)
{
Expand All @@ -165,7 +170,7 @@ void ExtractMaterial(Material subAsset)
var newObj = EditorGUILayout.ObjectField(mat.name, remap, typeof(Material), false);
if (EditorGUI.EndChangeCheck())
{
if (newObj)
if (newObj && newObj != mat)
t.AddRemap(id, newObj);
else
t.RemoveRemap(id);
Expand All @@ -175,7 +180,7 @@ void ExtractMaterial(Material subAsset)
{
if (GUILayout.Button("Extract", GUILayout.Width(60)))
{
ExtractMaterial(mat);
ExtractMaterial(mat, true);
GUIUtility.ExitGUI();
}
}
Expand Down Expand Up @@ -210,8 +215,11 @@ void ExtractMaterial(Material subAsset)
for (var i = 0; i < mats.arraySize; i++)
{
AssetDatabase.StartAssetEditing();
ExtractMaterial(mats.GetArrayElementAtIndex(i).objectReferenceValue as Material);
ExtractMaterial(mats.GetArrayElementAtIndex(i).objectReferenceValue as Material, false);
AssetDatabase.StopAssetEditing();
var assetPath = AssetDatabase.GetAssetPath(target);
AssetDatabase.WriteImportSettingsIfDirty(assetPath);
AssetDatabase.Refresh();
}
}

Expand Down
8 changes: 8 additions & 0 deletions UnityGLTF/Assets/UnityGLTF/Editor/Scripts/Internal.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,90 @@ public static void ConvertMaterialToGLTF(Material material, Shader oldShader, Sh
material.shader = newShader;
#endif
}

public static void ValidateMaterialKeywords(Material material)
{
// TODO ensure we're setting correct keywords for
// - existence of a normal map
// - existence of emission color values or texture
// -

// var needsVolumeTransmission = false;
// needsVolumeTransmission |= material.HasProperty(thicknessFactor) && material.GetFloat(thicknessFactor) > 0;
// needsVolumeTransmission |= material.HasProperty(transmissionFactor) && material.GetFloat(transmissionFactor) > 0;
// material.SetKeyword("_VOLUME_TRANSMISSION", needsVolumeTransmission);
//
// var needsIridescence = material.HasProperty(iridescenceFactor) && material.GetFloat(iridescenceFactor) > 0;
// material.SetKeyword("_IRIDESCENCE", needsIridescence);
//
// var needsSpecular = material.HasProperty(specularFactor) && material.GetFloat(specularFactor) > 0;
// material.SetKeyword("_SPECULAR", needsSpecular);

if (material.IsKeywordEnabled("_VOLUME_TRANSMISSION_ON"))
{
// // approximation when transmission is on but roughness == 0
// // (no opaque pass required)
// TODO does weird things with transparency but still rendering into the Opaque Texture for some reason
// if (material.HasProperty("roughnessFactor") && material.GetFloat("roughnessFactor") == 0)
// {
// // enforce transparent
// if (material.HasProperty("_QueueControl")) material.SetFloat("_QueueControl", 0);
// if (material.HasProperty("_BUILTIN_QueueControl")) material.SetFloat("_BUILTIN_QueueControl", 0);
// if (material.HasProperty("_BUILTIN_Surface")) material.SetFloat("_BUILTIN_Surface", 1);
// if (material.HasProperty("_Surface")) material.SetFloat("_Surface", 1);
// material.EnableKeyword("_SURFACE_TYPE_TRANSPARENT");
// material.EnableKeyword("_BUILTIN_SURFACE_TYPE_TRANSPARENT");
// material.renderQueue = -1;
// }
// else
{
// enforce Opaque
if (material.HasProperty("_BUILTIN_Surface")) material.SetFloat("_BUILTIN_Surface", 0);
if (material.HasProperty("_Surface")) material.SetFloat("_Surface", 0);
material.DisableKeyword("_SURFACE_TYPE_TRANSPARENT");
material.DisableKeyword("_BUILTIN_SURFACE_TYPE_TRANSPARENT");

// enforce queue control and render queue 3000
if (material.HasProperty("_QueueControl")) material.SetFloat("_QueueControl", 1);
if (material.HasProperty("_BUILTIN_QueueControl")) material.SetFloat("_BUILTIN_QueueControl", 1);

// not a great choice: using 2999 as magic value for "we automatically set the queue for you"
// so the change can be reverted if someone toggles transmission on and then off again.
material.renderQueue = 2999;
}
}
else
{
if (material.renderQueue == 2999)
{
if (material.HasProperty("_QueueControl")) material.SetFloat("_QueueControl", 0);
if (material.HasProperty("_BUILTIN_QueueControl")) material.SetFloat("_BUILTIN_QueueControl", 0);
material.renderQueue = -1;
}
}

if (material.HasProperty("emissiveFactor"))
material.globalIlluminationFlags = MaterialEditor.FixupEmissiveFlag(material.GetColor("emissiveFactor"), material.globalIlluminationFlags);

if (!material.IsKeywordEnabled("_TEXTURE_TRANSFORM_ON"))
{
if (material.GetTextureScale("baseColorTexture") != Vector2.one || material.GetTextureOffset("baseColorTexture") != Vector2.zero)
{
material.SetKeyword("_TEXTURE_TRANSFORM", true);
}
}
}

public static void SetKeyword(this Material material, string keyword, bool state)
{
if (state)
material.EnableKeyword(keyword + "_ON");
else
material.DisableKeyword(keyword + "_ON");

if (material.HasProperty(keyword))
material.SetFloat(keyword, state ? 1 : 0);
}
}

#if UNITY_EDITOR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

namespace UnityGLTF
{
public interface IUnityGltfShaderUpgradeMeta
{
Shader SourceShader { get; }
bool IsUnlit { get; }
bool IsTransparent { get; }
bool IsDoublesided { get; }
}

public class UnityGltfShaderUpgradeMeta : ScriptableObject
{
public Shader sourceShader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class PBRGraphGUI :
public override void ValidateMaterial(Material material)
{
base.ValidateMaterial(material);
ShaderGraphHelpers.ValidateMaterialKeywords(material);
GLTFMaterialHelper.ValidateMaterialKeywords(material);
}
#endif

Expand Down Expand Up @@ -401,7 +401,7 @@ protected void _DrawSurfaceInputs(Material mat)
foreach (var t in materialEditor.targets)
{
if (t is Material material)
ShaderGraphHelpers.ValidateMaterialKeywords(material);
GLTFMaterialHelper.ValidateMaterialKeywords(material);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,90 +50,6 @@ public static Renderer GetRendererForMaterialEditor(MaterialEditor materialEdito
if (!c) c = target.GetComponent<SkinnedMeshRenderer>();
return c;
}

public static void ValidateMaterialKeywords(Material material)
{
// TODO ensure we're setting correct keywords for
// - existence of a normal map
// - existence of emission color values or texture
// -

// var needsVolumeTransmission = false;
// needsVolumeTransmission |= material.HasProperty(thicknessFactor) && material.GetFloat(thicknessFactor) > 0;
// needsVolumeTransmission |= material.HasProperty(transmissionFactor) && material.GetFloat(transmissionFactor) > 0;
// material.SetKeyword("_VOLUME_TRANSMISSION", needsVolumeTransmission);
//
// var needsIridescence = material.HasProperty(iridescenceFactor) && material.GetFloat(iridescenceFactor) > 0;
// material.SetKeyword("_IRIDESCENCE", needsIridescence);
//
// var needsSpecular = material.HasProperty(specularFactor) && material.GetFloat(specularFactor) > 0;
// material.SetKeyword("_SPECULAR", needsSpecular);

if (material.IsKeywordEnabled("_VOLUME_TRANSMISSION_ON"))
{
// // approximation when transmission is on but roughness == 0
// // (no opaque pass required)
// TODO does weird things with transparency but still rendering into the Opaque Texture for some reason
// if (material.HasProperty("roughnessFactor") && material.GetFloat("roughnessFactor") == 0)
// {
// // enforce transparent
// if (material.HasProperty("_QueueControl")) material.SetFloat("_QueueControl", 0);
// if (material.HasProperty("_BUILTIN_QueueControl")) material.SetFloat("_BUILTIN_QueueControl", 0);
// if (material.HasProperty("_BUILTIN_Surface")) material.SetFloat("_BUILTIN_Surface", 1);
// if (material.HasProperty("_Surface")) material.SetFloat("_Surface", 1);
// material.EnableKeyword("_SURFACE_TYPE_TRANSPARENT");
// material.EnableKeyword("_BUILTIN_SURFACE_TYPE_TRANSPARENT");
// material.renderQueue = -1;
// }
// else
{
// enforce Opaque
if (material.HasProperty("_BUILTIN_Surface")) material.SetFloat("_BUILTIN_Surface", 0);
if (material.HasProperty("_Surface")) material.SetFloat("_Surface", 0);
material.DisableKeyword("_SURFACE_TYPE_TRANSPARENT");
material.DisableKeyword("_BUILTIN_SURFACE_TYPE_TRANSPARENT");

// enforce queue control and render queue 3000
if (material.HasProperty("_QueueControl")) material.SetFloat("_QueueControl", 1);
if (material.HasProperty("_BUILTIN_QueueControl")) material.SetFloat("_BUILTIN_QueueControl", 1);

// not a great choice: using 2999 as magic value for "we automatically set the queue for you"
// so the change can be reverted if someone toggles transmission on and then off again.
material.renderQueue = 2999;
}
}
else
{
if (material.renderQueue == 2999)
{
if (material.HasProperty("_QueueControl")) material.SetFloat("_QueueControl", 0);
if (material.HasProperty("_BUILTIN_QueueControl")) material.SetFloat("_BUILTIN_QueueControl", 0);
material.renderQueue = -1;
}
}

if (material.HasProperty("emissiveFactor"))
material.globalIlluminationFlags = MaterialEditor.FixupEmissiveFlag(material.GetColor("emissiveFactor"), material.globalIlluminationFlags);

if (!material.IsKeywordEnabled("_TEXTURE_TRANSFORM_ON"))
{
if (material.GetTextureScale("baseColorTexture") != Vector2.one || material.GetTextureOffset("baseColorTexture") != Vector2.zero)
{
material.SetKeyword("_TEXTURE_TRANSFORM", true);
}
}
}

public static void SetKeyword(this Material material, string keyword, bool state)
{
if (state)
material.EnableKeyword(keyword + "_ON");
else
material.DisableKeyword(keyword + "_ON");

if (material.HasProperty(keyword))
material.SetFloat(keyword, state ? 1 : 0);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,6 @@ public override void OnImportAsset(AssetImportContext ctx)
#endif
}
}

public interface IUnityGltfShaderUpgradeMeta
{
Shader SourceShader { get; }
bool IsUnlit { get; }
bool IsTransparent { get; }
bool IsDoublesided { get; }
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private static bool ConvertStandardAndURPLit(Material material, Shader oldShader
material.globalIlluminationFlags = MaterialGlobalIlluminationFlags.BakedEmissive;

// ensure keywords are correctly set after conversion
ShaderGraphHelpers.ValidateMaterialKeywords(material);
GLTFMaterialHelper.ValidateMaterialKeywords(material);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected virtual async Task ConstructMaterial(GLTFMaterial def, int materialInd
mrMapper.BaseColorXScale = ext.Scale.ToUnityVector2Raw();
mrMapper.BaseColorXTexCoord = ext.TexCoord;

mapper.Material.SetKeyword("_TEXTURE_TRANSFORM", true);
MatHelper.SetKeyword(mapper.Material, "_TEXTURE_TRANSFORM", true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,9 @@ public AssetDatabaseStream(string imageUri)
#endif
}

internal static class MatExt
internal static class MatHelper
{
internal static void SetKeyword(this Material material, string keyword, bool state)
internal static void SetKeyword(Material material, string keyword, bool state)
{
if (state)
material.EnableKeyword(keyword + "_ON");
Expand Down

0 comments on commit 8d33e27

Please sign in to comment.