diff --git a/CHANGELOG.md b/CHANGELOG.md index d159ce3..cf2f015 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +# [3.0.0-preview.21](https://github.com/mob-sakai/ParticleEffectForUGUI/compare/v3.0.0-preview.20...v3.0.0-preview.21) (2020-08-28) + + +### Bug Fixes + +* if in the mask, rendering material will be destroyed ([0db40cf](https://github.com/mob-sakai/ParticleEffectForUGUI/commit/0db40cf160b2a5a27c83ef15d648b2771a47b51a)) +* support animatable material property (again) ([cf6ca80](https://github.com/mob-sakai/ParticleEffectForUGUI/commit/cf6ca80d1273bcf49e18d805260afa8e36e94617)) + # [3.0.0-preview.20](https://github.com/mob-sakai/ParticleEffectForUGUI/compare/v3.0.0-preview.19...v3.0.0-preview.20) (2020-08-28) diff --git a/Scripts/AnimatableProperty.cs b/Scripts/AnimatableProperty.cs index e5869e6..5656db5 100644 --- a/Scripts/AnimatableProperty.cs +++ b/Scripts/AnimatableProperty.cs @@ -26,20 +26,30 @@ namespace Coffee.UIExtensions 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/Scripts/UIParticle.cs b/Scripts/UIParticle.cs index 7fc2fe3..53b5f03 100755 --- a/Scripts/UIParticle.cs +++ b/Scripts/UIParticle.cs @@ -34,9 +34,11 @@ namespace Coffee.UIExtensions private DrivenRectTransformTracker _tracker; private Mesh _bakedMesh; private readonly List _modifiedMaterials = new List(); + private readonly List _maskMaterials = new List(); private uint _activeMeshIndices; private Vector3 _cachedPosition; private static readonly List s_TempMaterials = new List(2); + private static MaterialPropertyBlock s_Mpb; /// @@ -129,10 +131,18 @@ namespace Coffee.UIExtensions protected override void UpdateMaterial() { + // Clear mask materials. + for (var i = 0; i < _maskMaterials.Count; i++) + { + StencilMaterial.Remove(_maskMaterials[i]); + _maskMaterials[i] = null; + } + + _maskMaterials.Clear(); + // Clear modified materials. for (var i = 0; i < _modifiedMaterials.Count; i++) { - StencilMaterial.Remove(_modifiedMaterials[i]); DestroyImmediate(_modifiedMaterials[i]); _modifiedMaterials[i] = null; } @@ -173,7 +183,9 @@ namespace Coffee.UIExtensions 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 @@ -192,7 +204,7 @@ namespace Coffee.UIExtensions if (0 < m_StencilValue) { baseMaterial = StencilMaterial.Add(baseMaterial, (1 << m_StencilValue) - 1, StencilOp.Keep, CompareFunction.Equal, ColorWriteMask.All, (1 << m_StencilValue) - 1, 0); - _modifiedMaterials.Add(baseMaterial); + _maskMaterials.Add(baseMaterial); } if (texture == null && m_AnimatableProperties.Length == 0) return baseMaterial; @@ -205,6 +217,25 @@ namespace Coffee.UIExtensions 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/Scripts/UIParticleUpdater.cs b/Scripts/UIParticleUpdater.cs index f14803a..9a9b9d3 100755 --- a/Scripts/UIParticleUpdater.cs +++ b/Scripts/UIParticleUpdater.cs @@ -205,6 +205,11 @@ namespace Coffee.UIExtensions if (m.vertexCount == 0) MeshHelper.DiscardTemporaryMesh(); + else + { + var index = MeshHelper.activeMeshIndices.BitCount() - 1; + particle.UpdateMaterialProperties(r, index); + } } // Bake trails particles. diff --git a/package.json b/package.json index 8c58b82..3dc4b4f 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "com.coffee.ui-particle", "displayName": "UI Particle", "description": "This plugin provide a component to render particle effect for uGUI.\nThe particle rendering is maskable and sortable, without Camera, RenderTexture or Canvas.", - "version": "3.0.0-preview.20", + "version": "3.0.0-preview.21", "unity": "2018.2", "license": "MIT", "repository": {