Skip to content

Commit

Permalink
fix: texture_ST properties aren't properly saved since ShaderGraph ma…
Browse files Browse the repository at this point in the history
…rks the textures as [NoScaleOffset} (IN-16486)
  • Loading branch information
hybridherbst committed May 7, 2023
1 parent d555f82 commit 9a1ba49
Showing 1 changed file with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,43 @@
using UnityEditor.ShaderGraph.Drawing;
#endif
using UnityEngine;
using UnityEngine.Rendering;
using Object = UnityEngine.Object;

namespace UnityGLTF
{
public static class ShaderGraphHelpers
{
public static void DrawShaderGraphGUI(MaterialEditor materialEditor, IEnumerable<MaterialProperty> properties)
private static FieldInfo m_Flags;
public static void DrawShaderGraphGUI(MaterialEditor materialEditor, List<MaterialProperty> properties)
{
#if HAVE_CATEGORIES
ShaderGraphPropertyDrawers.DrawShaderGraphGUI(materialEditor, properties);
#else
if (m_Flags == null)
m_Flags = typeof(MaterialProperty).GetField("m_Flags", (BindingFlags)(-1));

// This is a workaround for serialization bug IN-16486
// Changes made to texture with a specific a _ST property marked as [NoScaleOffset] in the Inspector are shown but not saved
// We're explicitly removing the _ST property from the list of properties to be drawn,
// and we mark all textures that actually have _ST properties so that they're rendering inline properties again.
if (m_Flags != null)
{
var names = properties.Select(x => x.name).ToList();
var removals = new List<MaterialProperty>();
foreach (var p in properties)
{
if (p.flags.HasFlag(MaterialProperty.PropFlags.NoScaleOffset) && names.Contains(p.name + "_ST"))
m_Flags.SetValue(p, (ShaderPropertyFlags) m_Flags.GetValue(p) & ~ShaderPropertyFlags.NoScaleOffset);

if (p.name.EndsWith("_ST", StringComparison.Ordinal) && p.type == MaterialProperty.PropType.Vector)
removals.Add(p);
}

foreach (var r in removals)
properties.Remove(r);
}

materialEditor.PropertiesDefaultGUI(properties.ToArray());
#endif
}
Expand Down

0 comments on commit 9a1ba49

Please sign in to comment.