fix: visual bug with ScreenSpaceCamera canvas on editor

Close #78
vr
mob-sakai 2020-05-01 21:30:20 +09:00
parent 046ad2401a
commit 482b96709b
3 changed files with 50 additions and 56 deletions

45
Packages/SoftMaskForUGUI/Scripts/SoftMask.cs Normal file → Executable file
View File

@ -98,7 +98,7 @@ namespace Coffee.UIExtensions
} }
} }
} }
/// <summary> /// <summary>
/// The transparency of the whole masked graphic. /// The transparency of the whole masked graphic.
/// </summary> /// </summary>
@ -296,6 +296,11 @@ namespace Coffee.UIExtensions
s_MainTexId = Shader.PropertyToID("_MainTex"); s_MainTexId = Shader.PropertyToID("_MainTex");
s_SoftnessId = Shader.PropertyToID("_Softness"); s_SoftnessId = Shader.PropertyToID("_Softness");
s_Alpha = Shader.PropertyToID("_Alpha"); s_Alpha = Shader.PropertyToID("_Alpha");
#if UNITY_EDITOR
UnityEditor.EditorApplication.update += UpdateGameViewMatrixForShader;
s_GameVPId = Shader.PropertyToID("_GameVP");
s_GameTVPId = Shader.PropertyToID("_GameTVP");
#endif
} }
} }
s_ActiveSoftMasks.Add(this); s_ActiveSoftMasks.Add(this);
@ -379,7 +384,33 @@ namespace Coffee.UIExtensions
hasChanged = true; hasChanged = true;
} }
#if UNITY_EDITOR // #if UNITY_EDITOR
/// <summary>
/// Update the scene view matrix for shader.
/// </summary>
static void UpdateGameViewMatrixForShader()
{
foreach (var sm in s_ActiveSoftMasks)
{
var c = sm.graphic.canvas.rootCanvas;
var wcam = c.worldCamera ?? Camera.main;
if (c.renderMode != RenderMode.ScreenSpaceOverlay && wcam)
{
var pv = GL.GetGPUProjectionMatrix (wcam.projectionMatrix, false) * wcam.worldToCameraMatrix;
Shader.SetGlobalMatrix(s_GameVPId, pv);
Shader.SetGlobalMatrix(s_GameTVPId, pv);
}
else
{
var scale = c.transform.localScale.x;
var size = (c.transform as RectTransform).sizeDelta;
var pos = c.transform.position;
Shader.SetGlobalMatrix(s_GameVPId, Matrix4x4.TRS(new Vector3(0, 0, 0.5f), Quaternion.identity, new Vector3(2 / size.x, 2 / size.y, 0.0005f * scale)));
Shader.SetGlobalMatrix(s_GameTVPId, Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(1 / pos.x, 1 / pos.y, -2/2000f)) * Matrix4x4.Translate(-pos));
}
}
}
/// <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).
/// </summary> /// </summary>
@ -390,7 +421,7 @@ namespace Coffee.UIExtensions
base.OnValidate(); base.OnValidate();
_hasStencilStateChanged = false; _hasStencilStateChanged = false;
} }
#endif // #endif
//################################ //################################
// Private Members. // Private Members.
@ -403,6 +434,8 @@ namespace Coffee.UIExtensions
static int s_ColorMaskId; static int s_ColorMaskId;
static int s_MainTexId; static int s_MainTexId;
static int s_SoftnessId; static int s_SoftnessId;
static int s_GameVPId;
static int s_GameTVPId;
static int s_Alpha; static int s_Alpha;
MaterialPropertyBlock _mpb; MaterialPropertyBlock _mpb;
CommandBuffer _cb; CommandBuffer _cb;
@ -491,6 +524,10 @@ namespace Coffee.UIExtensions
s_previousViewProjectionMatrices [id] = s_nowViewProjectionMatrices [id]; s_previousViewProjectionMatrices [id] = s_nowViewProjectionMatrices [id];
} }
s_nowViewProjectionMatrices.Clear (); s_nowViewProjectionMatrices.Clear ();
#if UNITY_EDITOR
UpdateGameViewMatrixForShader();
#endif
} }
/// <summary> /// <summary>
@ -695,4 +732,4 @@ namespace Coffee.UIExtensions
} }
} }
} }
} }

51
Packages/SoftMaskForUGUI/Scripts/SoftMaskable.cs Normal file → Executable file
View File

@ -91,7 +91,6 @@ namespace Coffee.UIExtensions
#if UNITY_EDITOR #if UNITY_EDITOR
result.EnableKeyword("SOFTMASK_EDITOR"); result.EnableKeyword("SOFTMASK_EDITOR");
UpdateSceneViewMatrixForShader();
#endif #endif
} }
else else
@ -112,7 +111,7 @@ namespace Coffee.UIExtensions
{ {
if (!isActiveAndEnabled || !_softMask) if (!isActiveAndEnabled || !_softMask)
return true; return true;
if (!RectTransformUtility.RectangleContainsScreenPoint(transform as RectTransform, sp, eventCamera)) if (!RectTransformUtility.RectangleContainsScreenPoint(transform as RectTransform, sp, eventCamera))
{ {
return false; return false;
@ -193,47 +192,11 @@ namespace Coffee.UIExtensions
static int s_SoftMaskTexId; static int s_SoftMaskTexId;
static int s_StencilCompId; static int s_StencilCompId;
static int s_MaskInteractionId; static int s_MaskInteractionId;
static int s_GameVPId;
static int s_GameTVPId;
static List<SoftMaskable> s_ActiveSoftMaskables; static List<SoftMaskable> s_ActiveSoftMaskables;
static int[] s_Interactions = new int[4]; static int[] s_Interactions = new int[4];
static Material s_DefaultMaterial; static Material s_DefaultMaterial;
#if UNITY_EDITOR #if UNITY_EDITOR
/// <summary>
/// Update the scene view matrix for shader.
/// </summary>
static void UpdateSceneViewMatrixForShader()
{
s_ActiveSoftMaskables.RemoveAll(x=>!x);
foreach (var sm in s_ActiveSoftMaskables)
{
if (!sm || !sm._maskMaterial || !sm.graphic || !sm.graphic.canvas)
{
continue;
}
Material mat = sm._maskMaterial;
var c = sm.graphic.canvas.rootCanvas;
var wcam = c.worldCamera ?? Camera.main;
if (c.renderMode != RenderMode.ScreenSpaceOverlay && wcam)
{
var pv = GL.GetGPUProjectionMatrix (wcam.projectionMatrix, false) * wcam.worldToCameraMatrix;
mat.SetMatrix(s_GameVPId, pv);
mat.SetMatrix(s_GameTVPId, pv);
}
else
{
var scale = c.transform.localScale.x;
var size = (c.transform as RectTransform).sizeDelta;
var pos = c.transform.position;
mat.SetMatrix(s_GameVPId, Matrix4x4.TRS(new Vector3(0, 0, 0.5f), Quaternion.identity, new Vector3(2 / size.x, 2 / size.y, 0.0005f * scale)));
mat.SetMatrix(s_GameTVPId, Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(1 / pos.x, 1 / pos.y, -2/2000f)) * Matrix4x4.Translate(-pos));
}
}
}
/// <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).
/// </summary> /// </summary>
@ -244,7 +207,7 @@ namespace Coffee.UIExtensions
graphic.SetMaterialDirty(); graphic.SetMaterialDirty();
} }
} }
#endif #endif
/// <summary> /// <summary>
/// This function is called when the object becomes enabled and active. /// This function is called when the object becomes enabled and active.
@ -256,12 +219,6 @@ namespace Coffee.UIExtensions
{ {
s_ActiveSoftMaskables = new List<SoftMaskable>(); s_ActiveSoftMaskables = new List<SoftMaskable>();
#if UNITY_EDITOR
UnityEditor.EditorApplication.update += UpdateSceneViewMatrixForShader;
s_GameVPId = Shader.PropertyToID("_GameVP");
s_GameTVPId = Shader.PropertyToID("_GameTVP");
#endif
s_SoftMaskTexId = Shader.PropertyToID("_SoftMaskTex"); s_SoftMaskTexId = Shader.PropertyToID("_SoftMaskTex");
s_StencilCompId = Shader.PropertyToID("_StencilComp"); s_StencilCompId = Shader.PropertyToID("_StencilComp");
s_MaskInteractionId = Shader.PropertyToID("_MaskInteraction"); s_MaskInteractionId = Shader.PropertyToID("_MaskInteraction");
@ -341,4 +298,4 @@ namespace Coffee.UIExtensions
#pragma warning restore 0612 #pragma warning restore 0612
} }
} }
} }

View File

@ -13,9 +13,9 @@ fixed Approximately(float4x4 a, float4x4 b)
return step( return step(
max(d._m00,max(d._m01,max(d._m02,max(d._m03, max(d._m00,max(d._m01,max(d._m02,max(d._m03,
max(d._m10,max(d._m11,max(d._m12,max(d._m13, max(d._m10,max(d._m11,max(d._m12,max(d._m13,
max(d._m20,max(d._m21,max(d._m22,//max(d._m23, max(d._m20,max(d._m21,max(d._m22,max(d._m23,
max(d._m30,max(d._m31,max(d._m32,d._m33)))))))))))))), max(d._m30,max(d._m31,max(d._m32,d._m33))))))))))))))),
0.01); 0.0000001);
} }
float GetMaskAlpha(float alpha, int stencilId, float interaction) float GetMaskAlpha(float alpha, int stencilId, float interaction)
@ -60,8 +60,8 @@ float SoftMaskInternal(float4 clipPos)
#define SOFTMASK_EDITOR_ONLY(x) x #define SOFTMASK_EDITOR_ONLY(x) x
#define SoftMask(clipPos, worldPosition) SoftMaskInternal(clipPos, worldPosition) #define SoftMask(clipPos, worldPosition) SoftMaskInternal(clipPos, worldPosition)
#else #else
#define SOFTMASK_EDITOR_ONLY(x) #define SOFTMASK_EDITOR_ONLY(x)
#define SoftMask(clipPos, worldPosition) SoftMaskInternal(clipPos) #define SoftMask(clipPos, worldPosition) SoftMaskInternal(clipPos)
#endif #endif
#endif // UI_SOFTMASK_INCLUDED #endif // UI_SOFTMASK_INCLUDED