Skip to content

Commit

Permalink
fix: unlit graph used wrong texture transform in some cases
Browse files Browse the repository at this point in the history
fix: alpha mode didn't properly disable on import when it was on by default in the shader
fix: compile error on 2019.4
  • Loading branch information
hybridherbst committed Sep 27, 2022
1 parent 5532a05 commit 7247bbe
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 95 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#if !NO_INTERNALS_ACCESS && UNITY_2020_1_OR_NEWER

#if UNITY_2020_2_OR_NEWER
using UnityEditor.AssetImporters;
#else
Expand All @@ -20,3 +22,5 @@ public override void OnInspectorGUI()
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,21 @@ private bool IsCommonConstant(Material material)
material.HasProperty("_LightFactor");
}

private static bool CheckForPropertyInShader(Shader shader, string name, ShaderPropertyType type)
{
var c = shader.GetPropertyCount();
var foundProperty = false;
for (var i = 0; i < c; i++)
{
if (shader.GetPropertyName(i) == name && shader.GetPropertyType(i) == type)
{
foundProperty = true;
break;
}
}
return foundProperty;
}

private void ExportTextureTransform(TextureInfo def, Material mat, string texName)
{
// early out if texture transform is explicitly disabled
Expand All @@ -345,7 +360,7 @@ private void ExportTextureTransform(TextureInfo def, Material mat, string texNam
#if UNITY_2021_1_OR_NEWER
if (mat.HasFloat(rotProp))
#else
if (mat.HasProperty(rotProp))
if (mat.HasProperty(rotProp) && CheckForPropertyInShader(mat.shader, rotProp, ShaderPropertyType.Float))
#endif
rotation = mat.GetFloat(rotProp);

Expand All @@ -360,22 +375,8 @@ private void ExportTextureTransform(TextureInfo def, Material mat, string texNam
// the material ALWAYS says true for mat.HasProperty(someTex_ST) when someTex is defined and doesn't have [NoTextureScale] attribute
if (textureHasTilingOffset)
{
var shader = mat.shader;
var c = shader.GetPropertyCount();
var actuallyFoundTilingOffset = false;
for (var i = 0; i < c; i++)
{
if (shader.GetPropertyName(i) == checkForName)
{
actuallyFoundTilingOffset = true;
break;
}
}

if (!actuallyFoundTilingOffset)
{
if (!CheckForPropertyInShader(mat.shader, checkForName, ShaderPropertyType.Vector))
textureHasTilingOffset = false;
}
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ protected virtual async Task ConstructMaterial(GLTFMaterial def, int materialInd
var ext = GetTextureTransform(pbr.BaseColorTexture);
if (ext != null)
{
unlitMapper.BaseColorXOffset = ext.Offset.ToUnityVector2Raw();
var offset = ext.Offset.ToUnityVector2Raw();
offset.y = 1 - ext.Scale.Y - offset.y;
unlitMapper.BaseColorXOffset = offset;
unlitMapper.BaseColorXRotation = ext.Rotation;
unlitMapper.BaseColorXScale = ext.Scale.ToUnityVector2Raw();
unlitMapper.BaseColorXTexCoord = ext.TexCoord;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,10 @@ protected virtual async Task ConstructPrimitiveAttributes(MeshPrimitive primitiv
}
if (meshAttributes.ContainsKey(SemanticProperties.Color[0]))
{
meshAttributes[SemanticProperties.Color[0]].AccessorContent.AsColors.ToUnityColorLinear(unityData.Colors, vertOffset);
if (QualitySettings.activeColorSpace == ColorSpace.Gamma)
meshAttributes[SemanticProperties.Color[0]].AccessorContent.AsColors.ToUnityColorRaw(unityData.Colors, vertOffset);
else
meshAttributes[SemanticProperties.Color[0]].AccessorContent.AsColors.ToUnityColorLinear(unityData.Colors, vertOffset);
}

var targets = primData.Targets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public virtual AlphaMode AlphaMode
_material.SetInt("_BUILTIN_DstBlend", (int)BlendMode.Zero);
_material.SetInt("_ZWrite", 1);
_material.SetInt("_BUILTIN_ZWrite", 1);
_material.SetInt("_AlphaClip", 1);
_material.SetInt("_BUILTIN_AlphaClip", 1);
_material.EnableKeyword("_ALPHATEST_ON");
_material.EnableKeyword("_BUILTIN_ALPHATEST_ON");
_material.DisableKeyword("_ALPHABLEND_ON");
Expand All @@ -56,9 +58,9 @@ public virtual AlphaMode AlphaMode
_material.DisableKeyword("_BUILTIN_ALPHAPREMULTIPLY_ON");
_material.renderQueue = (int)RenderQueue.AlphaTest;
if (_material.HasProperty("_Cutoff"))
{
_material.SetFloat("_Cutoff", (float)AlphaCutoff);
}
if (_material.HasProperty("alphaCutoff"))
_material.SetFloat("alphaCutoff", (float)AlphaCutoff);

SetAlphaModeMask(_material, true);
}
Expand All @@ -72,6 +74,8 @@ public virtual AlphaMode AlphaMode
_material.SetInt("_BUILTIN_DstBlend", (int)BlendMode.OneMinusSrcAlpha);
_material.SetInt("_ZWrite", 0);
_material.SetInt("_BUILTIN_ZWrite", 0);
_material.SetInt("_AlphaClip", 0);
_material.SetInt("_BUILTIN_AlphaClip", 0);
_material.DisableKeyword("_ALPHATEST_ON");
_material.DisableKeyword("_BUILTIN_ALPHATEST_ON");
_material.EnableKeyword("_ALPHABLEND_ON");
Expand All @@ -94,6 +98,8 @@ public virtual AlphaMode AlphaMode
_material.SetInt("_BUILTIN_DstBlend", (int)BlendMode.Zero);
_material.SetInt("_ZWrite", 1);
_material.SetInt("_BUILTIN_ZWrite", 1);
_material.SetInt("_AlphaClip", 0);
_material.SetInt("_BUILTIN_AlphaClip", 0);
_material.DisableKeyword("_ALPHATEST_ON");
_material.DisableKeyword("_ALPHABLEND_ON");
_material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
Expand Down Expand Up @@ -187,7 +193,15 @@ protected void SetShaderModeBlend(Material material)
public double AlphaCutoff
{
get => _material.GetFloat("alphaCutoff");
set => _material.SetFloat("alphaCutoff", (float) value);
set
{
_material.SetFloat("alphaCutoff", (float) value);
#if !UNITY_2021_2_OR_NEWER
// PBRGraph/UnlitGraph always have alphaCutoff on 2020.x, so we need to set it to 0 for non-masked modes
if (_alphaMode != AlphaMode.MASK)
_material.SetFloat("alphaCutoff", 0f);
#endif
}
}

public virtual bool DoubleSided
Expand Down

0 comments on commit 7247bbe

Please sign in to comment.