Skip to content

Commit

Permalink
fix: visual bug with ScreenSpaceCamera canvas on editor
Browse files Browse the repository at this point in the history
Close #78
  • Loading branch information
mob-sakai committed May 1, 2020
1 parent 046ad24 commit 482b967
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 56 deletions.
45 changes: 41 additions & 4 deletions Packages/SoftMaskForUGUI/Scripts/SoftMask.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public float softness
}
}
}

/// <summary>
/// The transparency of the whole masked graphic.
/// </summary>
Expand Down Expand Up @@ -296,6 +296,11 @@ protected override void OnEnable()
s_MainTexId = Shader.PropertyToID("_MainTex");
s_SoftnessId = Shader.PropertyToID("_Softness");
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);
Expand Down Expand Up @@ -379,7 +384,33 @@ protected override void OnRectTransformDimensionsChange()
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>
/// This function is called when the script is loaded or a value is changed in the inspector (Called in the editor only).
/// </summary>
Expand All @@ -390,7 +421,7 @@ protected override void OnValidate()
base.OnValidate();
_hasStencilStateChanged = false;
}
#endif
// #endif

//################################
// Private Members.
Expand All @@ -403,6 +434,8 @@ protected override void OnValidate()
static int s_ColorMaskId;
static int s_MainTexId;
static int s_SoftnessId;
static int s_GameVPId;
static int s_GameTVPId;
static int s_Alpha;
MaterialPropertyBlock _mpb;
CommandBuffer _cb;
Expand Down Expand Up @@ -491,6 +524,10 @@ static void UpdateMaskTextures()
s_previousViewProjectionMatrices [id] = s_nowViewProjectionMatrices [id];
}
s_nowViewProjectionMatrices.Clear ();

#if UNITY_EDITOR
UpdateGameViewMatrixForShader();
#endif
}

/// <summary>
Expand Down Expand Up @@ -695,4 +732,4 @@ float GetPixelValue(int x, int y, int[] interactions)
}
}
}
}
}
51 changes: 4 additions & 47 deletions Packages/SoftMaskForUGUI/Scripts/SoftMaskable.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ public Material GetModifiedMaterial(Material baseMaterial)

#if UNITY_EDITOR
result.EnableKeyword("SOFTMASK_EDITOR");
UpdateSceneViewMatrixForShader();
#endif
}
else
Expand All @@ -112,7 +111,7 @@ public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
{
if (!isActiveAndEnabled || !_softMask)
return true;

if (!RectTransformUtility.RectangleContainsScreenPoint(transform as RectTransform, sp, eventCamera))
{
return false;
Expand Down Expand Up @@ -193,47 +192,11 @@ public void SetMaskInteraction(SpriteMaskInteraction layer0, SpriteMaskInteracti
static int s_SoftMaskTexId;
static int s_StencilCompId;
static int s_MaskInteractionId;
static int s_GameVPId;
static int s_GameTVPId;
static List<SoftMaskable> s_ActiveSoftMaskables;
static int[] s_Interactions = new int[4];
static Material s_DefaultMaterial;

#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));
}
}
}

#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).
/// </summary>
Expand All @@ -244,7 +207,7 @@ void OnValidate()
graphic.SetMaterialDirty();
}
}
#endif
#endif

/// <summary>
/// This function is called when the object becomes enabled and active.
Expand All @@ -256,12 +219,6 @@ void OnEnable()
{
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_StencilCompId = Shader.PropertyToID("_StencilComp");
s_MaskInteractionId = Shader.PropertyToID("_MaskInteraction");
Expand Down Expand Up @@ -341,4 +298,4 @@ void ISerializationCallbackReceiver.OnAfterDeserialize()
#pragma warning restore 0612
}
}
}
}
10 changes: 5 additions & 5 deletions Packages/SoftMaskForUGUI/Shaders/SoftMask.cginc
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ fixed Approximately(float4x4 a, float4x4 b)
return step(
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._m20,max(d._m21,max(d._m22,//max(d._m23,
max(d._m30,max(d._m31,max(d._m32,d._m33)))))))))))))),
0.01);
max(d._m20,max(d._m21,max(d._m22,max(d._m23,
max(d._m30,max(d._m31,max(d._m32,d._m33))))))))))))))),
0.0000001);
}

float GetMaskAlpha(float alpha, int stencilId, float interaction)
Expand Down Expand Up @@ -60,8 +60,8 @@ float SoftMaskInternal(float4 clipPos)
#define SOFTMASK_EDITOR_ONLY(x) x
#define SoftMask(clipPos, worldPosition) SoftMaskInternal(clipPos, worldPosition)
#else
#define SOFTMASK_EDITOR_ONLY(x)
#define SOFTMASK_EDITOR_ONLY(x)
#define SoftMask(clipPos, worldPosition) SoftMaskInternal(clipPos)
#endif

#endif // UI_SOFTMASK_INCLUDED
#endif // UI_SOFTMASK_INCLUDED

0 comments on commit 482b967

Please sign in to comment.