Skip to content

Commit

Permalink
fix: Unintentional material destruction
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed May 11, 2020
1 parent 87f72b4 commit 0733243
Showing 1 changed file with 33 additions and 58 deletions.
91 changes: 33 additions & 58 deletions Packages/SoftMaskForUGUI/Scripts/SoftMaskable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,13 @@ public Graphic graphic
Material IMaterialModifier.GetModifiedMaterial(Material baseMaterial)
{
_softMask = null;
if (!isActiveAndEnabled)
{
return baseMaterial;
}

// Unregister the previous material
MaterialCache.Unregister(_effectMaterialHash);
_effectMaterialHash = k_InvalidHash;

// If this component is disabled, the material is returned as is.
if (!isActiveAndEnabled) return baseMaterial;

// Find the nearest parent softmask.
var parentTransform = transform.parent;
Expand All @@ -109,48 +112,39 @@ Material IMaterialModifier.GetModifiedMaterial(Material baseMaterial)
parentTransform = parentTransform.parent;
}

var oldHash = _effectMaterialHash;
var modifiedMaterial = baseMaterial;
if (_softMask)
// If the parents do not have a soft mask component, the material is returned as is.
if (!_softMask) return baseMaterial;

// Generate soft maskable material.
_effectMaterialHash = new Hash128(
(uint) baseMaterial.GetInstanceID(),
(uint) _softMask.GetInstanceID(),
(uint) m_MaskInteraction,
(uint) (m_UseStencil ? 1 : 0)
);

// Generate soft maskable material.
var modifiedMaterial = MaterialCache.Register(baseMaterial, _effectMaterialHash, mat =>
{
_effectMaterialHash = GetMaterialHash(baseMaterial);
modifiedMaterial = MaterialCache.Register(baseMaterial, _effectMaterialHash, mat =>
{
mat.shader = Shader.Find(string.Format("Hidden/{0} (SoftMaskable)", mat.shader.name));
mat.shader = Shader.Find(string.Format("Hidden/{0} (SoftMaskable)", mat.shader.name));
#if UNITY_EDITOR
mat.EnableKeyword("SOFTMASK_EDITOR");
mat.EnableKeyword("SOFTMASK_EDITOR");
#endif
mat.SetTexture(s_SoftMaskTexId, _softMask.softMaskBuffer);
mat.SetInt(s_StencilCompId,
m_UseStencil ? (int) CompareFunction.Equal : (int) CompareFunction.Always);
mat.SetVector(s_MaskInteractionId, new Vector4(
(m_MaskInteraction & 0x3),
((m_MaskInteraction >> 2) & 0x3),
((m_MaskInteraction >> 4) & 0x3),
((m_MaskInteraction >> 6) & 0x3)
));
});
ReleaseMaterial(ref _maskMaterial);
_maskMaterial = modifiedMaterial;
}
mat.SetTexture(s_SoftMaskTexId, _softMask.softMaskBuffer);
mat.SetInt(s_StencilCompId,
m_UseStencil ? (int) CompareFunction.Equal : (int) CompareFunction.Always);
mat.SetVector(s_MaskInteractionId, new Vector4(
(m_MaskInteraction & 0x3),
((m_MaskInteraction >> 2) & 0x3),
((m_MaskInteraction >> 4) & 0x3),
((m_MaskInteraction >> 6) & 0x3)
));
});
_maskMaterial = modifiedMaterial;

MaterialCache.Unregister(oldHash);
return modifiedMaterial;
}

private Hash128 GetMaterialHash(Material material)
{
if (!isActiveAndEnabled || !material || !material.shader)
return k_InvalidHash;

return new Hash128(
(uint) material.GetInstanceID(),
(uint) m_MaskInteraction,
(uint) (m_UseStencil ? 1 : 0),
0
);
}

/// <summary>
/// Given a point and a camera is the raycast valid.
/// </summary>
Expand Down Expand Up @@ -225,30 +219,11 @@ private void OnDisable()

graphic.SetMaterialDirtyEx();
_softMask = null;
ReleaseMaterial(ref _maskMaterial);

MaterialCache.Unregister(_effectMaterialHash);
_effectMaterialHash = k_InvalidHash;
}

/// <summary>
/// Release the material.
/// </summary>
static void ReleaseMaterial(ref Material mat)
{
if (!mat) return;

#if UNITY_EDITOR
if (!Application.isPlaying)
DestroyImmediate(mat);
else
#endif
Destroy(mat);

mat = null;
}


#if UNITY_EDITOR
/// <summary>
/// This function is called when the script is loaded or a value is changed in the inspector (Called in the editor only).
Expand Down

0 comments on commit 0733243

Please sign in to comment.