2020-08-20 03:42:16 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2020-09-28 20:35:40 +08:00
|
|
|
using Coffee.UIParticleExtensions;
|
2020-08-20 03:42:16 +08:00
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.Profiling;
|
|
|
|
|
|
|
|
namespace Coffee.UIExtensions
|
|
|
|
{
|
2020-10-01 15:15:57 +08:00
|
|
|
internal static class UIParticleUpdater
|
2020-08-20 03:42:16 +08:00
|
|
|
{
|
|
|
|
static readonly List<UIParticle> s_ActiveParticles = new List<UIParticle>();
|
|
|
|
static MaterialPropertyBlock s_Mpb;
|
2020-08-28 13:38:13 +08:00
|
|
|
static ParticleSystem.Particle[] s_Particles = new ParticleSystem.Particle[2048];
|
2021-06-03 02:03:16 +08:00
|
|
|
private static int frameCount = 0;
|
2020-08-28 13:38:13 +08:00
|
|
|
|
2020-08-20 03:42:16 +08:00
|
|
|
|
|
|
|
public static void Register(UIParticle particle)
|
|
|
|
{
|
|
|
|
if (!particle) return;
|
|
|
|
s_ActiveParticles.Add(particle);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Unregister(UIParticle particle)
|
|
|
|
{
|
|
|
|
if (!particle) return;
|
|
|
|
s_ActiveParticles.Remove(particle);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
[UnityEditor.InitializeOnLoadMethod]
|
|
|
|
#endif
|
|
|
|
[RuntimeInitializeOnLoadMethod]
|
|
|
|
private static void InitializeOnLoad()
|
|
|
|
{
|
2020-09-02 01:39:05 +08:00
|
|
|
MeshHelper.Init();
|
|
|
|
MeshPool.Init();
|
|
|
|
CombineInstanceArrayPool.Init();
|
|
|
|
|
2020-08-20 03:42:16 +08:00
|
|
|
Canvas.willRenderCanvases -= Refresh;
|
|
|
|
Canvas.willRenderCanvases += Refresh;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void Refresh()
|
|
|
|
{
|
2021-06-03 02:03:16 +08:00
|
|
|
// Do not allow it to be called in the same frame.
|
|
|
|
if (frameCount == Time.frameCount) return;
|
|
|
|
frameCount = Time.frameCount;
|
|
|
|
|
2020-09-02 13:15:30 +08:00
|
|
|
Profiler.BeginSample("[UIParticle] Refresh");
|
2020-08-20 03:42:16 +08:00
|
|
|
for (var i = 0; i < s_ActiveParticles.Count; i++)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Refresh(s_ActiveParticles[i]);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Debug.LogException(e);
|
|
|
|
}
|
|
|
|
}
|
2020-10-28 02:14:26 +08:00
|
|
|
|
2020-09-02 13:15:30 +08:00
|
|
|
Profiler.EndSample();
|
2020-08-20 03:42:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void Refresh(UIParticle particle)
|
|
|
|
{
|
2020-11-10 00:51:24 +08:00
|
|
|
if (!particle || !particle.bakedMesh || !particle.canvas || !particle.canvasRenderer) return;
|
2020-08-20 03:42:16 +08:00
|
|
|
|
2020-09-02 01:39:05 +08:00
|
|
|
Profiler.BeginSample("[UIParticle] Modify scale");
|
2020-08-20 03:42:16 +08:00
|
|
|
ModifyScale(particle);
|
|
|
|
Profiler.EndSample();
|
|
|
|
|
2020-09-02 01:39:05 +08:00
|
|
|
Profiler.BeginSample("[UIParticle] Bake mesh");
|
2020-08-28 13:38:13 +08:00
|
|
|
BakeMesh(particle);
|
2020-08-20 03:42:16 +08:00
|
|
|
Profiler.EndSample();
|
|
|
|
|
|
|
|
if (QualitySettings.activeColorSpace == ColorSpace.Linear)
|
|
|
|
{
|
2020-09-02 01:39:05 +08:00
|
|
|
Profiler.BeginSample("[UIParticle] Modify color space to linear");
|
2020-08-20 03:42:16 +08:00
|
|
|
particle.bakedMesh.ModifyColorSpaceToLinear();
|
|
|
|
Profiler.EndSample();
|
|
|
|
}
|
|
|
|
|
2020-09-02 01:39:05 +08:00
|
|
|
Profiler.BeginSample("[UIParticle] Set mesh to CanvasRenderer");
|
2020-08-28 13:38:13 +08:00
|
|
|
particle.canvasRenderer.SetMesh(particle.bakedMesh);
|
2020-08-20 03:42:16 +08:00
|
|
|
Profiler.EndSample();
|
|
|
|
|
2020-09-02 01:39:05 +08:00
|
|
|
Profiler.BeginSample("[UIParticle] Update Animatable Material Properties");
|
2020-09-14 17:29:16 +08:00
|
|
|
particle.UpdateMaterialProperties();
|
2020-08-20 03:42:16 +08:00
|
|
|
Profiler.EndSample();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void ModifyScale(UIParticle particle)
|
|
|
|
{
|
2020-08-28 13:38:13 +08:00
|
|
|
if (!particle.ignoreCanvasScaler || !particle.canvas) return;
|
2020-08-20 03:42:16 +08:00
|
|
|
|
|
|
|
// Ignore Canvas scaling.
|
2020-08-28 13:38:13 +08:00
|
|
|
var s = particle.canvas.rootCanvas.transform.localScale;
|
|
|
|
var modifiedScale = new Vector3(
|
|
|
|
Mathf.Approximately(s.x, 0) ? 1 : 1 / s.x,
|
|
|
|
Mathf.Approximately(s.y, 0) ? 1 : 1 / s.y,
|
|
|
|
Mathf.Approximately(s.z, 0) ? 1 : 1 / s.z);
|
2020-08-20 03:42:16 +08:00
|
|
|
|
|
|
|
// Scale is already modified.
|
2020-08-28 13:38:13 +08:00
|
|
|
var transform = particle.transform;
|
|
|
|
if (Mathf.Approximately((transform.localScale - modifiedScale).sqrMagnitude, 0)) return;
|
2020-08-20 03:42:16 +08:00
|
|
|
|
2020-08-28 13:38:13 +08:00
|
|
|
transform.localScale = modifiedScale;
|
2020-08-20 03:42:16 +08:00
|
|
|
}
|
|
|
|
|
2020-08-28 13:38:13 +08:00
|
|
|
private static Matrix4x4 GetScaledMatrix(ParticleSystem particle)
|
2020-08-20 03:42:16 +08:00
|
|
|
{
|
|
|
|
var transform = particle.transform;
|
2020-08-28 13:38:13 +08:00
|
|
|
var main = particle.main;
|
2020-08-20 03:42:16 +08:00
|
|
|
var space = main.simulationSpace;
|
|
|
|
if (space == ParticleSystemSimulationSpace.Custom && !main.customSimulationSpace)
|
|
|
|
space = ParticleSystemSimulationSpace.Local;
|
|
|
|
|
|
|
|
switch (space)
|
|
|
|
{
|
|
|
|
case ParticleSystemSimulationSpace.Local:
|
2020-08-28 13:38:13 +08:00
|
|
|
return Matrix4x4.Rotate(transform.rotation).inverse
|
|
|
|
* Matrix4x4.Scale(transform.lossyScale).inverse;
|
2020-08-20 03:42:16 +08:00
|
|
|
case ParticleSystemSimulationSpace.World:
|
|
|
|
return transform.worldToLocalMatrix;
|
|
|
|
case ParticleSystemSimulationSpace.Custom:
|
|
|
|
// #78: Support custom simulation space.
|
|
|
|
return transform.worldToLocalMatrix
|
|
|
|
* Matrix4x4.Translate(main.customSimulationSpace.position);
|
|
|
|
default:
|
|
|
|
return Matrix4x4.identity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 13:38:13 +08:00
|
|
|
private static void BakeMesh(UIParticle particle)
|
2020-08-20 03:42:16 +08:00
|
|
|
{
|
|
|
|
// Clear mesh before bake.
|
2020-10-27 23:39:22 +08:00
|
|
|
Profiler.BeginSample("[UIParticle] Bake Mesh > Clear mesh before bake");
|
2020-08-20 03:42:16 +08:00
|
|
|
MeshHelper.Clear();
|
2020-08-28 13:38:13 +08:00
|
|
|
particle.bakedMesh.Clear(false);
|
2020-10-27 23:39:22 +08:00
|
|
|
Profiler.EndSample();
|
2020-08-20 03:42:16 +08:00
|
|
|
|
|
|
|
// Get camera for baking mesh.
|
2020-08-28 13:38:13 +08:00
|
|
|
var camera = BakingCamera.GetCamera(particle.canvas);
|
|
|
|
var root = particle.transform;
|
|
|
|
var rootMatrix = Matrix4x4.Rotate(root.rotation).inverse
|
|
|
|
* Matrix4x4.Scale(root.lossyScale).inverse;
|
2020-09-15 20:17:44 +08:00
|
|
|
var scale = particle.ignoreCanvasScaler
|
2020-10-28 02:14:26 +08:00
|
|
|
? Vector3.Scale(particle.canvas.rootCanvas.transform.localScale, particle.scale3D)
|
2020-10-04 22:26:53 +08:00
|
|
|
: particle.scale3D;
|
|
|
|
var scaleMatrix = Matrix4x4.Scale(scale);
|
2020-08-28 13:38:13 +08:00
|
|
|
|
|
|
|
// Cache position
|
|
|
|
var position = particle.transform.position;
|
2020-10-04 22:26:53 +08:00
|
|
|
var diff = position - particle.cachedPosition;
|
|
|
|
diff.x *= 1f - 1f / Mathf.Max(0.001f, scale.x);
|
|
|
|
diff.y *= 1f - 1f / Mathf.Max(0.001f, scale.y);
|
|
|
|
diff.z *= 1f - 1f / Mathf.Max(0.001f, scale.z);
|
|
|
|
|
2020-08-28 13:38:13 +08:00
|
|
|
particle.cachedPosition = position;
|
|
|
|
|
2021-02-24 15:22:37 +08:00
|
|
|
if (particle.activeMeshIndices.CountFast() == 0)
|
|
|
|
diff = Vector3.zero;
|
|
|
|
|
2020-08-28 13:38:13 +08:00
|
|
|
for (var i = 0; i < particle.particles.Count; i++)
|
2020-08-20 03:42:16 +08:00
|
|
|
{
|
2020-10-27 23:39:22 +08:00
|
|
|
Profiler.BeginSample("[UIParticle] Bake Mesh > Push index");
|
|
|
|
MeshHelper.activeMeshIndices.Add(false);
|
|
|
|
MeshHelper.activeMeshIndices.Add(false);
|
|
|
|
Profiler.EndSample();
|
|
|
|
|
2020-08-28 13:38:13 +08:00
|
|
|
// No particle to render.
|
|
|
|
var currentPs = particle.particles[i];
|
|
|
|
if (!currentPs || !currentPs.IsAlive() || currentPs.particleCount == 0) continue;
|
2020-11-20 09:44:17 +08:00
|
|
|
var r = currentPs.GetComponent<ParticleSystemRenderer>();
|
|
|
|
if (!r.sharedMaterial && !r.trailMaterial) continue;
|
2020-08-20 03:42:16 +08:00
|
|
|
|
2020-08-28 13:38:13 +08:00
|
|
|
// Calc matrix.
|
2020-10-27 23:39:22 +08:00
|
|
|
Profiler.BeginSample("[UIParticle] Bake Mesh > Calc matrix");
|
2020-08-28 13:38:13 +08:00
|
|
|
var matrix = rootMatrix;
|
|
|
|
if (currentPs.transform != root)
|
|
|
|
{
|
|
|
|
if (currentPs.main.simulationSpace == ParticleSystemSimulationSpace.Local)
|
|
|
|
{
|
|
|
|
var relativePos = root.InverseTransformPoint(currentPs.transform.position);
|
|
|
|
matrix = Matrix4x4.Translate(relativePos) * matrix;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
matrix = matrix * Matrix4x4.Translate(-root.position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
matrix = GetScaledMatrix(currentPs);
|
|
|
|
}
|
|
|
|
|
2020-09-02 01:39:05 +08:00
|
|
|
matrix = scaleMatrix * matrix;
|
2020-10-27 23:39:22 +08:00
|
|
|
Profiler.EndSample();
|
2020-08-28 13:38:13 +08:00
|
|
|
|
|
|
|
// Extra world simulation.
|
|
|
|
if (currentPs.main.simulationSpace == ParticleSystemSimulationSpace.World && 0 < diff.sqrMagnitude)
|
|
|
|
{
|
2020-09-02 01:39:05 +08:00
|
|
|
Profiler.BeginSample("[UIParticle] Bake Mesh > Extra world simulation");
|
2020-08-28 13:38:13 +08:00
|
|
|
var count = currentPs.particleCount;
|
|
|
|
if (s_Particles.Length < count)
|
|
|
|
{
|
|
|
|
var size = Mathf.NextPowerOfTwo(count);
|
|
|
|
s_Particles = new ParticleSystem.Particle[size];
|
|
|
|
}
|
|
|
|
|
|
|
|
currentPs.GetParticles(s_Particles);
|
|
|
|
for (var j = 0; j < count; j++)
|
|
|
|
{
|
|
|
|
var p = s_Particles[j];
|
|
|
|
p.position += diff;
|
|
|
|
s_Particles[j] = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentPs.SetParticles(s_Particles, count);
|
2020-09-02 01:39:05 +08:00
|
|
|
Profiler.EndSample();
|
2020-08-28 13:38:13 +08:00
|
|
|
}
|
|
|
|
|
2020-10-29 00:36:58 +08:00
|
|
|
#if UNITY_2018_3_OR_NEWER
|
2020-09-28 20:35:40 +08:00
|
|
|
// #102: Do not bake particle system to mesh when the alpha is zero.
|
2020-10-28 02:14:26 +08:00
|
|
|
if (Mathf.Approximately(particle.canvasRenderer.GetInheritedAlpha(), 0))
|
2020-09-28 20:35:40 +08:00
|
|
|
continue;
|
2020-10-29 00:36:58 +08:00
|
|
|
#endif
|
2020-09-28 20:35:40 +08:00
|
|
|
|
2020-08-28 13:38:13 +08:00
|
|
|
// Bake main particles.
|
|
|
|
if (CanBakeMesh(r))
|
|
|
|
{
|
2020-09-02 01:39:05 +08:00
|
|
|
Profiler.BeginSample("[UIParticle] Bake Mesh > Bake Main Particles");
|
|
|
|
var hash = currentPs.GetMaterialHash(false);
|
|
|
|
if (hash != 0)
|
2020-08-29 02:55:46 +08:00
|
|
|
{
|
2020-09-02 01:39:05 +08:00
|
|
|
var m = MeshHelper.GetTemporaryMesh();
|
|
|
|
r.BakeMesh(m, camera, true);
|
|
|
|
MeshHelper.Push(i * 2, hash, m, matrix);
|
2020-08-29 02:55:46 +08:00
|
|
|
}
|
2020-09-02 01:39:05 +08:00
|
|
|
|
|
|
|
Profiler.EndSample();
|
2020-08-28 13:38:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bake trails particles.
|
|
|
|
if (currentPs.trails.enabled)
|
|
|
|
{
|
2020-09-02 01:39:05 +08:00
|
|
|
Profiler.BeginSample("[UIParticle] Bake Mesh > Bake Trails Particles");
|
|
|
|
var hash = currentPs.GetMaterialHash(true);
|
|
|
|
if (hash != 0)
|
2020-08-28 13:38:13 +08:00
|
|
|
{
|
2021-02-03 10:16:16 +08:00
|
|
|
matrix = currentPs.main.simulationSpace == ParticleSystemSimulationSpace.Local && currentPs.trails.worldSpace
|
2021-02-01 22:21:22 +08:00
|
|
|
? matrix * Matrix4x4.Translate(-currentPs.transform.position)
|
|
|
|
: matrix;
|
|
|
|
|
2020-09-02 01:39:05 +08:00
|
|
|
var m = MeshHelper.GetTemporaryMesh();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
r.BakeTrailsMesh(m, camera, true);
|
|
|
|
MeshHelper.Push(i * 2 + 1, hash, m, matrix);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
MeshHelper.DiscardTemporaryMesh(m);
|
|
|
|
}
|
2020-08-28 13:38:13 +08:00
|
|
|
}
|
2020-09-02 01:39:05 +08:00
|
|
|
|
|
|
|
Profiler.EndSample();
|
2020-08-28 13:38:13 +08:00
|
|
|
}
|
2020-08-20 03:42:16 +08:00
|
|
|
}
|
|
|
|
|
2020-08-28 13:38:13 +08:00
|
|
|
// Set active indices.
|
2020-10-27 23:39:22 +08:00
|
|
|
Profiler.BeginSample("[UIParticle] Bake Mesh > Set active indices");
|
2020-08-28 13:38:13 +08:00
|
|
|
particle.activeMeshIndices = MeshHelper.activeMeshIndices;
|
2020-10-27 23:39:22 +08:00
|
|
|
Profiler.EndSample();
|
2020-08-28 13:38:13 +08:00
|
|
|
|
|
|
|
// Combine
|
2020-09-02 01:39:05 +08:00
|
|
|
Profiler.BeginSample("[UIParticle] Bake Mesh > CombineMesh");
|
2020-08-28 13:38:13 +08:00
|
|
|
MeshHelper.CombineMesh(particle.bakedMesh);
|
2020-09-03 00:02:59 +08:00
|
|
|
MeshHelper.Clear();
|
2020-09-02 01:39:05 +08:00
|
|
|
Profiler.EndSample();
|
2020-08-20 03:42:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static bool CanBakeMesh(ParticleSystemRenderer renderer)
|
|
|
|
{
|
|
|
|
// #69: Editor crashes when mesh is set to null when `ParticleSystem.RenderMode = Mesh`
|
2020-08-28 13:38:13 +08:00
|
|
|
if (renderer.renderMode == ParticleSystemRenderMode.Mesh && renderer.mesh == null) return false;
|
2020-08-20 03:42:16 +08:00
|
|
|
|
|
|
|
// #61: When `ParticleSystem.RenderMode = None`, an error occurs
|
|
|
|
if (renderer.renderMode == ParticleSystemRenderMode.None) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|