Skip to content

Commit

Permalink
fix: support animatable material property (again)
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Aug 28, 2020
1 parent fd4a3f2 commit cf6ca80
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
18 changes: 14 additions & 4 deletions Packages/UIParticle/Scripts/AnimatableProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,30 @@ public ShaderPropertyType type

public void UpdateMaterialProperties(Material material, MaterialPropertyBlock mpb)
{
if (!material.HasProperty(id)) return;

switch (type)
{
case ShaderPropertyType.Color:
material.SetColor(id, mpb.GetColor(id));
var color = mpb.GetColor(id);
if (color != default(Color))
material.SetColor(id, color);
break;
case ShaderPropertyType.Vector:
material.SetVector(id, mpb.GetVector(id));
var vector = mpb.GetVector(id);
if (vector != default(Vector4))
material.SetVector(id, vector);
break;
case ShaderPropertyType.Float:
case ShaderPropertyType.Range:
material.SetFloat(id, mpb.GetFloat(id));
var value = mpb.GetFloat(id);
if (value != default(float))
material.SetFloat(id, value);
break;
case ShaderPropertyType.Texture:
material.SetTexture(id, mpb.GetTexture(id));
var tex = mpb.GetTexture(id);
if (tex != default(Texture))
material.SetTexture(id, tex);
break;
}
}
Expand Down
24 changes: 23 additions & 1 deletion Packages/UIParticle/Scripts/UIParticle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class UIParticle : MaskableGraphic, ISerializationCallbackReceiver
private uint _activeMeshIndices;
private Vector3 _cachedPosition;
private static readonly List<Material> s_TempMaterials = new List<Material>(2);
private static MaterialPropertyBlock s_Mpb;


/// <summary>
Expand Down Expand Up @@ -173,7 +174,9 @@ protected override void UpdateMaterial()
if (0 < (activeMeshIndices & bit) && 0 < s_TempMaterials.Count)
{
var mat = GetModifiedMaterial(s_TempMaterials[0], ps.GetTextureForSprite());
canvasRenderer.SetMaterial(mat, j++);
canvasRenderer.SetMaterial(mat, j);
UpdateMaterialProperties(r, j);
j++;
}

// Trails
Expand Down Expand Up @@ -205,6 +208,25 @@ private Material GetModifiedMaterial(Material baseMaterial, Texture2D texture)
return baseMaterial;
}

internal void UpdateMaterialProperties(Renderer r, int index)
{
if (m_AnimatableProperties.Length == 0 || canvasRenderer.materialCount <= index) return;

r.GetPropertyBlock(s_Mpb ?? (s_Mpb = new MaterialPropertyBlock()));
if (s_Mpb.isEmpty) return;

// #41: Copy the value from MaterialPropertyBlock to CanvasRenderer
var mat = canvasRenderer.GetMaterial(index);
if (!mat) return;

foreach (var ap in m_AnimatableProperties)
{
ap.UpdateMaterialProperties(mat, s_Mpb);
}

s_Mpb.Clear();
}

/// <summary>
/// This function is called when the object becomes enabled and active.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions Packages/UIParticle/Scripts/UIParticleUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ private static void BakeMesh(UIParticle particle)

if (m.vertexCount == 0)
MeshHelper.DiscardTemporaryMesh();
else
{
var index = MeshHelper.activeMeshIndices.BitCount() - 1;
particle.UpdateMaterialProperties(r, index);
}
}

// Bake trails particles.
Expand Down

0 comments on commit cf6ca80

Please sign in to comment.