From cf6ca80d1273bcf49e18d805260afa8e36e94617 Mon Sep 17 00:00:00 2001 From: mob-sakai Date: Sat, 29 Aug 2020 03:48:10 +0900 Subject: [PATCH] fix: support animatable material property (again) --- .../UIParticle/Scripts/AnimatableProperty.cs | 18 ++++++++++---- Packages/UIParticle/Scripts/UIParticle.cs | 24 ++++++++++++++++++- .../UIParticle/Scripts/UIParticleUpdater.cs | 5 ++++ 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/Packages/UIParticle/Scripts/AnimatableProperty.cs b/Packages/UIParticle/Scripts/AnimatableProperty.cs index e5869e6..5656db5 100644 --- a/Packages/UIParticle/Scripts/AnimatableProperty.cs +++ b/Packages/UIParticle/Scripts/AnimatableProperty.cs @@ -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; } } diff --git a/Packages/UIParticle/Scripts/UIParticle.cs b/Packages/UIParticle/Scripts/UIParticle.cs index 7fc2fe3..82d9cb6 100755 --- a/Packages/UIParticle/Scripts/UIParticle.cs +++ b/Packages/UIParticle/Scripts/UIParticle.cs @@ -37,6 +37,7 @@ public class UIParticle : MaskableGraphic, ISerializationCallbackReceiver private uint _activeMeshIndices; private Vector3 _cachedPosition; private static readonly List s_TempMaterials = new List(2); + private static MaterialPropertyBlock s_Mpb; /// @@ -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 @@ -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(); + } + /// /// This function is called when the object becomes enabled and active. /// diff --git a/Packages/UIParticle/Scripts/UIParticleUpdater.cs b/Packages/UIParticle/Scripts/UIParticleUpdater.cs index f14803a..9a9b9d3 100755 --- a/Packages/UIParticle/Scripts/UIParticleUpdater.cs +++ b/Packages/UIParticle/Scripts/UIParticleUpdater.cs @@ -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.