fix: Unintentional material destruction
parent
7c353ee3cd
commit
bf17b19ef2
|
@ -90,10 +90,13 @@ namespace Coffee.UISoftMask
|
||||||
Material IMaterialModifier.GetModifiedMaterial(Material baseMaterial)
|
Material IMaterialModifier.GetModifiedMaterial(Material baseMaterial)
|
||||||
{
|
{
|
||||||
_softMask = null;
|
_softMask = null;
|
||||||
if (!isActiveAndEnabled)
|
|
||||||
{
|
// Unregister the previous material
|
||||||
return baseMaterial;
|
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.
|
// Find the nearest parent softmask.
|
||||||
var parentTransform = transform.parent;
|
var parentTransform = transform.parent;
|
||||||
|
@ -109,46 +112,37 @@ namespace Coffee.UISoftMask
|
||||||
parentTransform = parentTransform.parent;
|
parentTransform = parentTransform.parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
var oldHash = _effectMaterialHash;
|
// If the parents do not have a soft mask component, the material is returned as is.
|
||||||
var modifiedMaterial = baseMaterial;
|
if (!_softMask) return baseMaterial;
|
||||||
if (_softMask)
|
|
||||||
{
|
|
||||||
_effectMaterialHash = GetMaterialHash(baseMaterial);
|
|
||||||
modifiedMaterial = MaterialCache.Register(baseMaterial, _effectMaterialHash, mat =>
|
|
||||||
{
|
|
||||||
mat.shader = Shader.Find(string.Format("Hidden/{0} (SoftMaskable)", mat.shader.name));
|
|
||||||
#if UNITY_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;
|
|
||||||
}
|
|
||||||
|
|
||||||
MaterialCache.Unregister(oldHash);
|
// Generate soft maskable material.
|
||||||
return modifiedMaterial;
|
_effectMaterialHash = new Hash128(
|
||||||
}
|
(uint) baseMaterial.GetInstanceID(),
|
||||||
|
(uint) _softMask.GetInstanceID(),
|
||||||
private Hash128 GetMaterialHash(Material material)
|
|
||||||
{
|
|
||||||
if (!isActiveAndEnabled || !material || !material.shader)
|
|
||||||
return k_InvalidHash;
|
|
||||||
|
|
||||||
return new Hash128(
|
|
||||||
(uint) material.GetInstanceID(),
|
|
||||||
(uint) m_MaskInteraction,
|
(uint) m_MaskInteraction,
|
||||||
(uint) (m_UseStencil ? 1 : 0),
|
(uint) (m_UseStencil ? 1 : 0)
|
||||||
0
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Generate soft maskable material.
|
||||||
|
var modifiedMaterial = MaterialCache.Register(baseMaterial, _effectMaterialHash, mat =>
|
||||||
|
{
|
||||||
|
mat.shader = Shader.Find(string.Format("Hidden/{0} (SoftMaskable)", mat.shader.name));
|
||||||
|
#if UNITY_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)
|
||||||
|
));
|
||||||
|
});
|
||||||
|
_maskMaterial = modifiedMaterial;
|
||||||
|
|
||||||
|
return modifiedMaterial;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -225,30 +219,11 @@ namespace Coffee.UISoftMask
|
||||||
|
|
||||||
graphic.SetMaterialDirtyEx();
|
graphic.SetMaterialDirtyEx();
|
||||||
_softMask = null;
|
_softMask = null;
|
||||||
ReleaseMaterial(ref _maskMaterial);
|
|
||||||
|
|
||||||
MaterialCache.Unregister(_effectMaterialHash);
|
MaterialCache.Unregister(_effectMaterialHash);
|
||||||
_effectMaterialHash = k_InvalidHash;
|
_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
|
#if UNITY_EDITOR
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This function is called when the script is loaded or a value is changed in the inspector (Called in the editor only).
|
/// This function is called when the script is loaded or a value is changed in the inspector (Called in the editor only).
|
||||||
|
|
Loading…
Reference in New Issue