Skip to content

Commit

Permalink
feat: skip baking when ColorMask=0 (for masking)
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Nov 11, 2023
1 parent 59215e2 commit 5bc7064
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
18 changes: 14 additions & 4 deletions Packages/src/Runtime/CompositeCanvasSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,24 @@ internal void Bake(CommandBuffer cb)
Texture graphicTex = null;

{
Profiler.BeginSample("(CCR)[CompositeCanvasSource] Bake > Get Modified Material");
Profiler.BeginSample("(CCR)[CompositeCanvasSource] Bake > Get Modified Material For Baking");
_isBaking = true;
graphicMat = graphic.materialForRendering;
_isBaking = false;
Profiler.EndSample();

// Skip baking when `ColorMask=0` (for masking)
if (graphicMat.HasProperty(ShaderPropertyIds.colorMask)
&& graphicMat.GetInt(ShaderPropertyIds.colorMask) == 0)
{
return;
}

var isDefaultShader = graphicMat.shader == Graphic.defaultGraphicMaterial.shader;
if (isDefaultShader)
{
// Use CCR Material instead of default material to blend with One-OneMinusSrcAlpha.
Profiler.BeginSample("(CCR)[CompositeCanvasSource] Bake > Use CCR Material");
const ColorMode colorMode = ColorMode.Multiply;
const BlendMode srcBlend = BlendMode.One;
const BlendMode dstBlend = BlendMode.OneMinusSrcAlpha;
Expand All @@ -285,13 +295,12 @@ internal void Bake(CommandBuffer cb)
CompositeCanvasRendererProjectSettings.cacheRendererMaterial);
graphicMat = _material;
_mpb.SetFloat(ShaderPropertyIds.alphaMultiplier, 1);
Profiler.EndSample();
}
else
{
MaterialRegistry.Release(ref _material);
}

Profiler.EndSample();
}

{
Expand All @@ -318,7 +327,8 @@ internal void Bake(CommandBuffer cb)
Profiler.EndSample();

Profiler.BeginSample("(CCR)[CompositeCanvasSource] Bake > DrawMesh");
if (CompositeCanvasProcess.instance.OnPreBake(_renderer, graphic, ref _mesh, _mpb, _renderer.alphaScale) && _mesh)
if (CompositeCanvasProcess.instance.OnPreBake(_renderer, graphic, ref _mesh, _mpb, _renderer.alphaScale) &&
_mesh)
{
Logging.Log(this, $"<color=orange> >>>> Mesh '{name}' will render.</color>");
cb.DrawMesh(_mesh, matrix, graphicMat, 0, 0, _mpb);
Expand Down
1 change: 1 addition & 0 deletions Packages/src/Runtime/Utilities/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ internal static class ShaderPropertyIds
{
public static readonly int mainTex = Shader.PropertyToID("_MainTex");
public static readonly int color = Shader.PropertyToID("_Color");
public static readonly int colorMask = Shader.PropertyToID("_ColorMask");
public static readonly int textureSampleAdd = Shader.PropertyToID("_TextureSampleAdd");
public static readonly int srcBlendMode = Shader.PropertyToID("_SrcBlendMode");
public static readonly int dstBlendMode = Shader.PropertyToID("_DstBlendMode");
Expand Down

0 comments on commit 5bc7064

Please sign in to comment.