Skip to content

Commit

Permalink
feat: add 'bakingTrigger' option (automatic, manually, always, on ena…
Browse files Browse the repository at this point in the history
…ble)
  • Loading branch information
mob-sakai committed Nov 11, 2023
1 parent 9747fc1 commit d2f3d34
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 32 deletions.
4 changes: 3 additions & 1 deletion Packages/src/Editor/CompositeCanvasRendererEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class CompositeCanvasRendererEditor : GraphicEditor
Type.GetType("UnityEditor.UI.SpriteDrawUtility, UnityEditor.UI")
?.GetMethod("DrawSprite", BindingFlags.NonPublic | BindingFlags.Static);

private SerializedProperty _bakingTrigger;
private SerializedProperty _blendType;
private SerializedProperty _colorMode;
private SerializedProperty _culling;
Expand All @@ -29,7 +30,6 @@ public class CompositeCanvasRendererEditor : GraphicEditor
private SerializedProperty _dstBlendMode;
private SerializedProperty _extents;
private SerializedProperty _foreground;

private Editor _materialEditor;
private SerializedProperty _orthographic;
private SerializedProperty _showSourceGraphics;
Expand All @@ -45,6 +45,7 @@ protected override void OnEnable()
_foreground = serializedObject.FindProperty("m_Foreground");
_extents = serializedObject.FindProperty("m_Extents");
_culling = serializedObject.FindProperty("m_Culling");
_bakingTrigger = serializedObject.FindProperty("m_BakingTrigger");
_orthographic = serializedObject.FindProperty("m_Orthographic");
_colorMode = serializedObject.FindProperty("m_ColorMode");
_blendType = serializedObject.FindProperty("m_BlendType");
Expand Down Expand Up @@ -76,6 +77,7 @@ public override void OnInspectorGUI()
EditorGUILayout.PropertyField(_extents);
EditorGUILayout.PropertyField(_orthographic);
EditorGUILayout.PropertyField(_culling);
EditorGUILayout.PropertyField(_bakingTrigger);
ShowSourceGraphicsControlGUI();

EditorGUILayout.PropertyField(_foreground);
Expand Down
95 changes: 64 additions & 31 deletions Packages/src/Runtime/CompositeCanvasRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public class CompositeCanvasRenderer : MaskableGraphic
[SerializeField]
private bool m_Culling;

[SerializeField]
private BakingTrigger m_BakingTrigger;

[Header("Rendering")]
[SerializeField]
private bool m_ShowSourceGraphics = true;
Expand Down Expand Up @@ -142,7 +145,7 @@ public DownSamplingRate downSamplingRate
{
if (m_DownSamplingRate == value) return;
m_DownSamplingRate = value;
SetDirty();
SetDirty(true);
}
}

Expand All @@ -167,7 +170,7 @@ public Vector2 extents
m_Extents = value;

SetVerticesDirty();
SetDirty();
SetDirty(true);
}
}

Expand Down Expand Up @@ -226,7 +229,7 @@ public bool culling
{
if (m_Culling == value) return;
m_Culling = value;
SetDirty();
SetDirty(true);
}
}

Expand All @@ -239,7 +242,9 @@ public bool perspective
if (!m_Orthographic
&& canvas && canvas.renderMode == RenderMode.ScreenSpaceCamera
&& canvas.worldCamera && !canvas.worldCamera.orthographic)
{
return true;
}

if (FrameCache.TryGet(this, nameof(perspective), out bool isPerspective))
{
Expand Down Expand Up @@ -285,7 +290,17 @@ public bool orthographic
if (m_Orthographic == value) return;
m_Orthographic = value;
SetVerticesDirty();
SetDirty();
SetDirty(true);
}
}

public BakingTrigger bakingTrigger
{
get => m_BakingTrigger;
set
{
if (Equals(m_BakingTrigger, value)) return;
m_BakingTrigger = value;
}
}

Expand Down Expand Up @@ -315,7 +330,11 @@ protected override void OnEnable()

SetSourcesVerticesDirty();

isDirty = true;
// Set dirty on enable.
if (m_BakingTrigger != BakingTrigger.Manually)
{
SetDirty(true);
}
}

protected override void OnDisable()
Expand Down Expand Up @@ -504,33 +523,36 @@ public static Material CreateMaterial(ColorMode colorMode, BlendMode srcBlendMod
return mat;
}

public override void SetVerticesDirty()
{
base.SetVerticesDirty();
SetDirty();
}

public override void SetMaterialDirty()
{
base.SetMaterialDirty();
SetDirty();
}

public void SetDirty()
public void SetDirty(bool force = false)
{
if (isDirty || !isActiveAndEnabled) return;
Logging.LogIf(!isDirty, this, $"! SetDirty {GetInstanceID()}");
isDirty = true;
if (perspective)
if (!force && m_BakingTrigger != BakingTrigger.Automatic)
{
SetVerticesDirty();
Logging.LogIf(!isDirty, this,
$"<color=orange>! SetDirty {GetInstanceID()} is canceled due to non automatic mode).</color>");
return;
}

Logging.LogIf(!isDirty, this, $"! SetDirty {GetInstanceID()}");
isDirty = true;
}

private void CheckTransformChanged()
{
if (isDirty) return;

switch (m_BakingTrigger)
{
case BakingTrigger.Always:
SetDirty(true);
return;
case BakingTrigger.Manually:
case BakingTrigger.OnEnable:
; // Do nothing.
return;
}

// If the transform of any source graphic has changed, set dirty.
Profiler.BeginSample("(CCR)[CompositeCanvasRenderer] CheckTransformChanged > Sources");
var isPerspective = perspective;
var baseTransform = isPerspective ? null : transform;
Expand All @@ -541,18 +563,20 @@ private void CheckTransformChanged()

if (source.HasTransformChanged(baseTransform))
{
SetDirty();
SetDirty(true);
}
}

Profiler.EndSample();

// Set dirty when transform changed.
if (isPerspective)
{
Profiler.BeginSample("(CCR)[CompositeCanvasRenderer] CheckTransformChanged > Perspective");
if (transform.HasChanged(null, ref _prevTransformMatrix))
{
SetDirty();
SetVerticesDirty();
}
}
else
Expand All @@ -561,6 +585,7 @@ private void CheckTransformChanged()
var m = Matrix4x4.Rotate(transform.localRotation) * Matrix4x4.Scale(transform.localScale);
if (_prevTransformMatrix != m)
{
SetDirty();
SetVerticesDirty();
}

Expand All @@ -575,7 +600,7 @@ internal void Register(CompositeCanvasSource canvasSource)
if (!canvasSource || sources.Contains(canvasSource)) return;

sources.Add(canvasSource);
isDirty = true;
SetDirty();
Logging.Log(this, $"Register #{sources.Count}: {canvasSource} {canvasSource.GetInstanceID()}");
}

Expand All @@ -584,7 +609,7 @@ internal void Unregister(CompositeCanvasSource canvasSource)
if (!sources.Contains(canvasSource)) return;

sources.Remove(canvasSource);
isDirty = true;
SetDirty();
Logging.Log(this, $"Unregister #{sources.Count}: {canvasSource} {canvasSource.GetInstanceID()}");
}

Expand All @@ -611,8 +636,18 @@ private void Bake()

if (!isDirty) return;
isDirty = false;
if (!canvas)
{
Logging.Log(this, "<color=orange> Baking is canceled due to not in canvas.</color>");
return;
}

if (!canvas || !IsInScreen()) return;
if (!IsInScreen())
{
Logging.Log(this,
"<color=orange> Baking is canceled due to all source graphics are not in screen.</color>");
return;
}

if (_cb == null)
{
Expand Down Expand Up @@ -781,12 +816,10 @@ public void SetSourcesVerticesDirty()
#if UNITY_EDITOR
protected override void OnValidate()
{
base.OnValidate();

blendType = blendType;
isDirty = true;

SetAllDirty();
SetDirty();
SetVerticesDirty();
SetMaterialDirty();
}

private void OnDrawGizmos()
Expand Down
8 changes: 8 additions & 0 deletions Packages/src/Runtime/Utilities/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ public enum DownSamplingRate
x8 = 8
}

public enum BakingTrigger
{
Automatic,
Manually,
Always,
OnEnable
}

public enum BlendType
{
Custom,
Expand Down

0 comments on commit d2f3d34

Please sign in to comment.