2022-06-08 11:54:11 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
using Coffee.UIParticleExtensions;
|
|
|
|
|
using UnityEngine.Profiling;
|
|
|
|
|
using UnityEngine.Rendering;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace Coffee.UIExtensions
|
|
|
|
|
{
|
|
|
|
|
[ExecuteAlways]
|
|
|
|
|
[RequireComponent(typeof(RectTransform))]
|
|
|
|
|
[RequireComponent(typeof(CanvasRenderer))]
|
|
|
|
|
[AddComponentMenu("")]
|
|
|
|
|
internal class UIParticleRenderer : MaskableGraphic
|
|
|
|
|
{
|
|
|
|
|
private static readonly CombineInstance[] s_CombineInstances = new CombineInstance[] { new CombineInstance() };
|
2022-06-11 21:32:14 +08:00
|
|
|
|
private static readonly List<Material> s_Materials = new List<Material>(2);
|
2022-06-08 11:54:11 +08:00
|
|
|
|
private static MaterialPropertyBlock s_Mpb;
|
2022-06-11 22:10:17 +08:00
|
|
|
|
private static readonly List<UIParticleRenderer> s_Renderers = new List<UIParticleRenderer>();
|
2022-08-10 09:21:39 +08:00
|
|
|
|
private static readonly Vector3[] s_Corners = new Vector3[4];
|
2022-06-08 11:54:11 +08:00
|
|
|
|
|
|
|
|
|
private ParticleSystemRenderer _renderer;
|
|
|
|
|
private ParticleSystem _particleSystem;
|
2022-08-10 09:21:39 +08:00
|
|
|
|
private int _prevParticleCount = 0;
|
2022-06-08 11:54:11 +08:00
|
|
|
|
private UIParticle _parent;
|
2022-06-11 21:32:14 +08:00
|
|
|
|
private int _index;
|
2022-06-08 11:54:11 +08:00
|
|
|
|
private bool _isTrail;
|
|
|
|
|
private Material _modifiedMaterial;
|
|
|
|
|
private Vector3 _prevScale;
|
|
|
|
|
private Vector3 _prevPsPos;
|
2022-06-09 00:47:42 +08:00
|
|
|
|
private Vector2Int _prevScreenSize;
|
2022-06-08 11:54:11 +08:00
|
|
|
|
private bool _delay = false;
|
|
|
|
|
private bool _prewarm = false;
|
2022-06-11 22:10:17 +08:00
|
|
|
|
private Material _currentMaterialForRendering;
|
2022-08-10 09:21:39 +08:00
|
|
|
|
private Bounds _lastBounds;
|
2022-06-08 11:54:11 +08:00
|
|
|
|
|
|
|
|
|
public override Texture mainTexture
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _isTrail ? null : _particleSystem.GetTextureForSprite();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool raycastTarget
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-10 09:21:39 +08:00
|
|
|
|
|
|
|
|
|
private Rect rootCanvasRect
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
s_Corners[0] = transform.TransformPoint(_lastBounds.min.x, _lastBounds.min.y, 0);
|
|
|
|
|
s_Corners[1] = transform.TransformPoint(_lastBounds.min.x, _lastBounds.max.y, 0);
|
|
|
|
|
s_Corners[2] = transform.TransformPoint(_lastBounds.max.x, _lastBounds.max.y, 0);
|
|
|
|
|
s_Corners[3] = transform.TransformPoint(_lastBounds.max.x, _lastBounds.min.y, 0);
|
|
|
|
|
if (canvas)
|
|
|
|
|
{
|
|
|
|
|
var worldToLocalMatrix = canvas.rootCanvas.transform.worldToLocalMatrix;
|
|
|
|
|
for (var i = 0; i < 4; ++i)
|
|
|
|
|
s_Corners[i] = worldToLocalMatrix.MultiplyPoint(s_Corners[i]);
|
|
|
|
|
}
|
|
|
|
|
var corner1 = (Vector2) s_Corners[0];
|
|
|
|
|
var corner2 = (Vector2) s_Corners[0];
|
|
|
|
|
for (var i = 1; i < 4; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (s_Corners[i].x < corner1.x)
|
|
|
|
|
corner1.x = s_Corners[i].x;
|
|
|
|
|
else if (s_Corners[i].x > corner2.x)
|
|
|
|
|
corner2.x = s_Corners[i].x;
|
|
|
|
|
if (s_Corners[i].y < corner1.y)
|
|
|
|
|
corner1.y = s_Corners[i].y;
|
|
|
|
|
else if (s_Corners[i].y > corner2.y)
|
|
|
|
|
corner2.y = s_Corners[i].y;
|
|
|
|
|
}
|
|
|
|
|
return new Rect(corner1, corner2 - corner1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-08 11:54:11 +08:00
|
|
|
|
|
2022-06-11 21:32:14 +08:00
|
|
|
|
public static UIParticleRenderer AddRenderer(UIParticle parent, int index)
|
2022-06-08 11:54:11 +08:00
|
|
|
|
{
|
|
|
|
|
// Create renderer object.
|
|
|
|
|
var go = new GameObject("UIParticleRenderer", typeof(UIParticleRenderer))
|
|
|
|
|
{
|
|
|
|
|
hideFlags = HideFlags.DontSave,
|
|
|
|
|
layer = parent.gameObject.layer,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Set parent.
|
|
|
|
|
var transform = go.transform;
|
|
|
|
|
transform.SetParent(parent.transform, false);
|
|
|
|
|
transform.localPosition = Vector3.zero;
|
|
|
|
|
transform.localRotation = Quaternion.identity;
|
|
|
|
|
transform.localScale = Vector3.one;
|
|
|
|
|
|
|
|
|
|
// Add renderer component.
|
|
|
|
|
var renderer = go.GetComponent<UIParticleRenderer>();
|
|
|
|
|
renderer._parent = parent;
|
2022-06-11 21:32:14 +08:00
|
|
|
|
renderer._index = index;
|
2022-06-08 11:54:11 +08:00
|
|
|
|
|
|
|
|
|
return renderer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Perform material modification in this function.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override Material GetModifiedMaterial(Material baseMaterial)
|
|
|
|
|
{
|
2022-06-11 22:10:17 +08:00
|
|
|
|
_currentMaterialForRendering = null;
|
|
|
|
|
|
2022-06-08 11:54:11 +08:00
|
|
|
|
if (!IsActive()) return baseMaterial;
|
|
|
|
|
|
|
|
|
|
var modifiedMaterial = base.GetModifiedMaterial(baseMaterial);
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
var texture = mainTexture;
|
|
|
|
|
if (texture == null && _parent.m_AnimatableProperties.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
ModifiedMaterial.Remove(_modifiedMaterial);
|
|
|
|
|
_modifiedMaterial = null;
|
|
|
|
|
return modifiedMaterial;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
var id = _parent.m_AnimatableProperties.Length == 0 ? 0 : GetInstanceID();
|
|
|
|
|
modifiedMaterial = ModifiedMaterial.Add(modifiedMaterial, texture, id);
|
|
|
|
|
ModifiedMaterial.Remove(_modifiedMaterial);
|
|
|
|
|
_modifiedMaterial = modifiedMaterial;
|
|
|
|
|
|
|
|
|
|
return modifiedMaterial;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-11 21:32:14 +08:00
|
|
|
|
public void Clear(int index = -1)
|
2022-06-08 11:54:11 +08:00
|
|
|
|
{
|
|
|
|
|
if (_renderer)
|
|
|
|
|
{
|
|
|
|
|
_renderer.enabled = true;
|
|
|
|
|
}
|
|
|
|
|
_parent = null;
|
|
|
|
|
_particleSystem = null;
|
|
|
|
|
_renderer = null;
|
2022-06-16 21:43:23 +08:00
|
|
|
|
_prevParticleCount = 0;
|
2022-06-17 12:35:19 +08:00
|
|
|
|
if (0 <= index)
|
2022-06-11 21:32:14 +08:00
|
|
|
|
{
|
|
|
|
|
_index = index;
|
|
|
|
|
}
|
2022-06-08 11:54:11 +08:00
|
|
|
|
//_emitter = null;
|
2022-06-17 12:35:19 +08:00
|
|
|
|
if (this && isActiveAndEnabled)
|
|
|
|
|
{
|
|
|
|
|
material = null;
|
|
|
|
|
workerMesh.Clear();
|
|
|
|
|
canvasRenderer.SetMesh(workerMesh);
|
2022-08-10 09:21:39 +08:00
|
|
|
|
_lastBounds = new Bounds();
|
2022-06-17 12:35:19 +08:00
|
|
|
|
enabled = false;
|
|
|
|
|
}
|
2022-06-08 11:54:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Set(UIParticle parent, ParticleSystem particleSystem, bool isTrail)
|
|
|
|
|
{
|
|
|
|
|
_parent = parent;
|
|
|
|
|
maskable = parent.maskable;
|
|
|
|
|
|
|
|
|
|
gameObject.layer = parent.gameObject.layer;
|
|
|
|
|
|
|
|
|
|
_particleSystem = particleSystem;
|
2022-06-21 18:47:08 +08:00
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
if (Application.isPlaying)
|
|
|
|
|
#endif
|
2022-06-08 11:54:11 +08:00
|
|
|
|
if (_particleSystem.isPlaying)
|
|
|
|
|
{
|
|
|
|
|
_particleSystem.Clear();
|
2022-06-16 21:43:23 +08:00
|
|
|
|
_particleSystem.Pause();
|
2022-06-08 11:54:11 +08:00
|
|
|
|
}
|
|
|
|
|
_prewarm = _particleSystem.main.prewarm;
|
|
|
|
|
|
|
|
|
|
_renderer = particleSystem.GetComponent<ParticleSystemRenderer>();
|
|
|
|
|
_renderer.enabled = false;
|
|
|
|
|
|
|
|
|
|
//_emitter = emitter;
|
|
|
|
|
_isTrail = isTrail;
|
|
|
|
|
|
|
|
|
|
_renderer.GetSharedMaterials(s_Materials);
|
|
|
|
|
material = s_Materials[isTrail ? 1 : 0];
|
|
|
|
|
s_Materials.Clear();
|
|
|
|
|
|
|
|
|
|
// Support sprite.
|
|
|
|
|
var tsa = particleSystem.textureSheetAnimation;
|
|
|
|
|
if (tsa.mode == ParticleSystemAnimationMode.Sprites && tsa.uvChannelMask == 0)
|
|
|
|
|
tsa.uvChannelMask = UVChannelFlags.UV0;
|
|
|
|
|
|
|
|
|
|
_prevScale = GetWorldScale();
|
|
|
|
|
_prevPsPos = _particleSystem.transform.position;
|
2022-06-09 00:47:42 +08:00
|
|
|
|
_prevScreenSize = new Vector2Int(Screen.width, Screen.height);
|
2022-06-08 11:54:11 +08:00
|
|
|
|
_delay = true;
|
2022-06-16 21:43:23 +08:00
|
|
|
|
_prevParticleCount = 0;
|
2022-06-08 11:54:11 +08:00
|
|
|
|
|
|
|
|
|
canvasRenderer.SetTexture(null);
|
|
|
|
|
|
|
|
|
|
enabled = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateMesh(Camera bakeCamera)
|
|
|
|
|
{
|
|
|
|
|
// No particle to render: Clear mesh.
|
|
|
|
|
if (
|
2022-06-17 12:35:19 +08:00
|
|
|
|
!isActiveAndEnabled || !_particleSystem || !_parent || !canvasRenderer || !canvas || !bakeCamera
|
2022-06-11 22:10:17 +08:00
|
|
|
|
|| _parent.meshSharing == UIParticle.MeshSharing.Reprica
|
2022-06-08 11:54:11 +08:00
|
|
|
|
|| !transform.lossyScale.GetScaled(_parent.scale3D).IsVisible() // Scale is not visible.
|
|
|
|
|
|| (!_particleSystem.IsAlive() && !_particleSystem.isPlaying) // No particle.
|
|
|
|
|
|| (_isTrail && !_particleSystem.trails.enabled) // Trail, but it is not enabled.
|
|
|
|
|
#if UNITY_2018_3_OR_NEWER
|
|
|
|
|
|| canvasRenderer.GetInheritedAlpha() < 0.01f // #102: Do not bake particle system to mesh when the alpha is zero.
|
|
|
|
|
#endif
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
Profiler.BeginSample("[UIParticleRenderer] Clear Mesh");
|
|
|
|
|
workerMesh.Clear();
|
|
|
|
|
canvasRenderer.SetMesh(workerMesh);
|
2022-08-10 09:21:39 +08:00
|
|
|
|
_lastBounds = new Bounds();
|
2022-06-08 11:54:11 +08:00
|
|
|
|
Profiler.EndSample();
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var main = _particleSystem.main;
|
|
|
|
|
var scale = GetWorldScale();
|
|
|
|
|
var psPos = _particleSystem.transform.position;
|
|
|
|
|
|
|
|
|
|
// Simulate particles.
|
2022-06-11 22:10:17 +08:00
|
|
|
|
Profiler.BeginSample("[UIParticle] Bake Mesh > Simulate Particles");
|
|
|
|
|
if (!_isTrail && _parent.canSimulate)
|
2022-06-08 11:54:11 +08:00
|
|
|
|
{
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
if (!Application.isPlaying)
|
|
|
|
|
{
|
|
|
|
|
SimulateForEditor(psPos - _prevPsPos, scale);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
{
|
2022-06-09 00:47:42 +08:00
|
|
|
|
ResolveResolutionChange(psPos, scale);
|
2022-06-08 11:54:11 +08:00
|
|
|
|
Simulate(scale, _parent.isPaused || _delay);
|
|
|
|
|
|
2022-06-09 00:47:42 +08:00
|
|
|
|
if (_delay && !_parent.isPaused)
|
|
|
|
|
{
|
|
|
|
|
Simulate(scale, _parent.isPaused);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-08 11:54:11 +08:00
|
|
|
|
// When the ParticleSystem simulation is complete, stop it.
|
|
|
|
|
if (!main.loop && main.duration <= _particleSystem.time && (_particleSystem.IsAlive() || _particleSystem.particleCount == 0))
|
|
|
|
|
{
|
|
|
|
|
_particleSystem.Stop(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_prevScale = scale;
|
|
|
|
|
_prevPsPos = psPos;
|
|
|
|
|
_delay = false;
|
|
|
|
|
}
|
2022-06-11 22:10:17 +08:00
|
|
|
|
Profiler.EndSample();
|
2022-06-08 11:54:11 +08:00
|
|
|
|
|
|
|
|
|
// Bake mesh.
|
|
|
|
|
Profiler.BeginSample("[UIParticleRenderer] Bake Mesh");
|
2022-06-11 22:10:17 +08:00
|
|
|
|
if (_isTrail && _parent.canSimulate)
|
2022-06-08 11:54:11 +08:00
|
|
|
|
{
|
2022-06-11 22:10:17 +08:00
|
|
|
|
_renderer.BakeTrailsMesh(s_CombineInstances[0].mesh, bakeCamera, true);
|
|
|
|
|
}
|
|
|
|
|
else if (_renderer.CanBakeMesh())
|
|
|
|
|
{
|
|
|
|
|
_renderer.BakeMesh(s_CombineInstances[0].mesh, bakeCamera, true);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-08-10 15:45:50 +08:00
|
|
|
|
s_CombineInstances[0].mesh.Clear(false);
|
2022-06-08 11:54:11 +08:00
|
|
|
|
}
|
|
|
|
|
Profiler.EndSample();
|
|
|
|
|
|
|
|
|
|
// Combine mesh to transform. ([ParticleSystem local ->] world -> renderer local)
|
|
|
|
|
Profiler.BeginSample("[UIParticleRenderer] Combine Mesh");
|
2022-06-11 22:10:17 +08:00
|
|
|
|
if (_parent.canSimulate)
|
2022-06-08 11:54:11 +08:00
|
|
|
|
{
|
2022-06-25 01:04:23 +08:00
|
|
|
|
if (_parent.absoluteMode)
|
|
|
|
|
{
|
|
|
|
|
s_CombineInstances[0].transform = canvasRenderer.transform.worldToLocalMatrix * GetWorldMatrix(psPos, scale);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var diff = _particleSystem.transform.position - _parent.transform.position;
|
|
|
|
|
s_CombineInstances[0].transform = canvasRenderer.transform.worldToLocalMatrix * Matrix4x4.Translate(diff.GetScaled(scale - Vector3.one)) * GetWorldMatrix(psPos, scale);
|
|
|
|
|
}
|
2022-06-08 11:54:11 +08:00
|
|
|
|
workerMesh.CombineMeshes(s_CombineInstances, true, true);
|
2022-06-11 22:08:27 +08:00
|
|
|
|
|
|
|
|
|
workerMesh.RecalculateBounds();
|
|
|
|
|
var bounds = workerMesh.bounds;
|
|
|
|
|
var center = bounds.center;
|
|
|
|
|
center.z = 0;
|
|
|
|
|
bounds.center = center;
|
|
|
|
|
var extents = bounds.extents;
|
|
|
|
|
extents.z = 0;
|
|
|
|
|
bounds.extents = extents;
|
|
|
|
|
workerMesh.bounds = bounds;
|
2022-08-10 09:21:39 +08:00
|
|
|
|
_lastBounds = bounds;
|
2022-06-08 11:54:11 +08:00
|
|
|
|
}
|
|
|
|
|
Profiler.EndSample();
|
|
|
|
|
|
2022-06-11 22:10:17 +08:00
|
|
|
|
|
|
|
|
|
// Get grouped renderers.
|
|
|
|
|
s_Renderers.Clear();
|
|
|
|
|
if (_parent.useMeshSharing)
|
|
|
|
|
{
|
|
|
|
|
UIParticleUpdater.GetGroupedRenderers(_parent.groupId, _index, s_Renderers);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-08 11:54:11 +08:00
|
|
|
|
// Set mesh to the CanvasRenderer.
|
|
|
|
|
Profiler.BeginSample("[UIParticleRenderer] Set Mesh");
|
2022-06-11 22:10:17 +08:00
|
|
|
|
for (int i = 0; i < s_Renderers.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (s_Renderers[i] == this) continue;
|
|
|
|
|
s_Renderers[i].canvasRenderer.SetMesh(workerMesh);
|
2022-08-10 09:21:39 +08:00
|
|
|
|
s_Renderers[i]._lastBounds = _lastBounds;
|
2022-06-11 22:10:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!_parent.canRender)
|
|
|
|
|
{
|
|
|
|
|
workerMesh.Clear();
|
|
|
|
|
}
|
2022-06-08 11:54:11 +08:00
|
|
|
|
canvasRenderer.SetMesh(workerMesh);
|
|
|
|
|
Profiler.EndSample();
|
|
|
|
|
|
|
|
|
|
// Update animatable material properties.
|
|
|
|
|
Profiler.BeginSample("[UIParticleRenderer] Update Animatable Material Properties");
|
|
|
|
|
UpdateMaterialProperties();
|
2022-06-11 22:10:17 +08:00
|
|
|
|
if (!_parent.useMeshSharing)
|
|
|
|
|
{
|
|
|
|
|
if (!_currentMaterialForRendering)
|
|
|
|
|
{
|
|
|
|
|
_currentMaterialForRendering = materialForRendering;
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < s_Renderers.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (s_Renderers[i] == this) continue;
|
|
|
|
|
|
|
|
|
|
s_Renderers[i].canvasRenderer.materialCount = 1;
|
|
|
|
|
s_Renderers[i].canvasRenderer.SetMaterial(_currentMaterialForRendering, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-08 11:54:11 +08:00
|
|
|
|
Profiler.EndSample();
|
2022-06-11 22:10:17 +08:00
|
|
|
|
|
|
|
|
|
s_Renderers.Clear();
|
2022-06-08 11:54:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-16 21:43:23 +08:00
|
|
|
|
internal void UpdateParticleCount()
|
|
|
|
|
{
|
|
|
|
|
if (!_particleSystem) return;
|
|
|
|
|
_prevParticleCount = _particleSystem.particleCount;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-08 11:54:11 +08:00
|
|
|
|
protected override void OnEnable()
|
|
|
|
|
{
|
|
|
|
|
base.OnEnable();
|
|
|
|
|
|
|
|
|
|
if (!s_CombineInstances[0].mesh)
|
|
|
|
|
{
|
|
|
|
|
s_CombineInstances[0].mesh = new Mesh()
|
|
|
|
|
{
|
|
|
|
|
name = "[UIParticleRenderer] Combine Instance Mesh",
|
|
|
|
|
hideFlags = HideFlags.HideAndDontSave,
|
|
|
|
|
};
|
|
|
|
|
}
|
2022-06-11 22:10:17 +08:00
|
|
|
|
_currentMaterialForRendering = null;
|
2022-06-08 11:54:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnDisable()
|
|
|
|
|
{
|
|
|
|
|
base.OnDisable();
|
|
|
|
|
|
|
|
|
|
ModifiedMaterial.Remove(_modifiedMaterial);
|
|
|
|
|
_modifiedMaterial = null;
|
2022-06-11 22:10:17 +08:00
|
|
|
|
_currentMaterialForRendering = null;
|
2022-06-08 11:54:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Call to update the geometry of the Graphic onto the CanvasRenderer.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected override void UpdateGeometry()
|
|
|
|
|
{
|
|
|
|
|
}
|
2022-08-10 09:21:39 +08:00
|
|
|
|
|
|
|
|
|
public override void Cull(Rect clipRect, bool validRect)
|
|
|
|
|
{
|
|
|
|
|
var cull = _lastBounds.extents == Vector3.zero || !validRect || !clipRect.Overlaps(rootCanvasRect, true);
|
|
|
|
|
if (canvasRenderer.cull == cull) return;
|
|
|
|
|
|
|
|
|
|
canvasRenderer.cull = cull;
|
|
|
|
|
UISystemProfilerApi.AddMarker("MaskableGraphic.cullingChanged", this);
|
|
|
|
|
onCullStateChanged.Invoke(cull);
|
|
|
|
|
OnCullingChanged();
|
|
|
|
|
}
|
2022-06-08 11:54:11 +08:00
|
|
|
|
|
|
|
|
|
private Vector3 GetWorldScale()
|
|
|
|
|
{
|
|
|
|
|
Profiler.BeginSample("[UIParticleRenderer] GetWorldScale");
|
|
|
|
|
var scale = _parent.scale3D;
|
2022-06-09 00:47:48 +08:00
|
|
|
|
//if (_parent.uiScaling)
|
|
|
|
|
{
|
|
|
|
|
scale.Scale(_parent.transform.localScale.Inverse());
|
|
|
|
|
}
|
2022-06-08 11:54:11 +08:00
|
|
|
|
//else if (_parent.scalingMode == UIParticle.ScalingMode.UI && _particleSystem.main.scalingMode != ParticleSystemScalingMode.Hierarchy)
|
|
|
|
|
//{
|
|
|
|
|
// var gscale = _parent.transform.lossyScale.GetScaled(canvas.transform.lossyScale.Inverse());
|
|
|
|
|
// scale.Scale(gscale * canvas.scaleFactor);
|
|
|
|
|
//}
|
|
|
|
|
Profiler.EndSample();
|
|
|
|
|
return scale;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Matrix4x4 GetWorldMatrix(Vector3 psPos, Vector3 scale)
|
|
|
|
|
{
|
|
|
|
|
var space = _particleSystem.GetActualSimulationSpace();
|
|
|
|
|
if (_isTrail && _particleSystem.trails.worldSpace)
|
|
|
|
|
{
|
|
|
|
|
space = ParticleSystemSimulationSpace.World;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
if (!Application.isPlaying)
|
|
|
|
|
{
|
|
|
|
|
switch (space)
|
|
|
|
|
{
|
|
|
|
|
case ParticleSystemSimulationSpace.World:
|
|
|
|
|
return Matrix4x4.Translate(psPos)
|
|
|
|
|
* Matrix4x4.Scale(scale)
|
|
|
|
|
* Matrix4x4.Translate(-psPos);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
switch (space)
|
|
|
|
|
{
|
|
|
|
|
case ParticleSystemSimulationSpace.Local:
|
|
|
|
|
return Matrix4x4.Translate(psPos)
|
|
|
|
|
* Matrix4x4.Scale(scale);
|
|
|
|
|
case ParticleSystemSimulationSpace.World:
|
|
|
|
|
return Matrix4x4.Scale(scale);
|
|
|
|
|
case ParticleSystemSimulationSpace.Custom:
|
|
|
|
|
return Matrix4x4.Translate(_particleSystem.main.customSimulationSpace.position.GetScaled(scale))
|
|
|
|
|
//* Matrix4x4.Translate(wpos)
|
|
|
|
|
* Matrix4x4.Scale(scale)
|
|
|
|
|
//* Matrix4x4.Translate(-wpos)
|
|
|
|
|
;
|
|
|
|
|
default:
|
|
|
|
|
throw new System.NotSupportedException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-09 00:47:42 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// For world simulation, interpolate particle positions when the screen size is changed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="psPos"></param>
|
|
|
|
|
/// <param name="scale"></param>
|
|
|
|
|
private void ResolveResolutionChange(Vector3 psPos, Vector3 scale)
|
|
|
|
|
{
|
|
|
|
|
var screenSize = new Vector2Int(Screen.width, Screen.height);
|
2022-06-09 00:47:48 +08:00
|
|
|
|
//if ((_prevScreenSize != screenSize || _prevScale != scale) && _particleSystem.main.simulationSpace == ParticleSystemSimulationSpace.World && _parent.uiScaling)
|
|
|
|
|
if ((_prevScreenSize != screenSize || _prevScale != scale) && _particleSystem.main.simulationSpace == ParticleSystemSimulationSpace.World)
|
2022-06-09 00:47:42 +08:00
|
|
|
|
{
|
|
|
|
|
// Update particle array size and get particles.
|
|
|
|
|
var size = _particleSystem.particleCount;
|
2022-06-15 08:09:56 +08:00
|
|
|
|
var particles = ParticleSystemExtensions.GetParticleArray(size);
|
|
|
|
|
_particleSystem.GetParticles(particles, size);
|
2022-06-09 00:47:42 +08:00
|
|
|
|
|
|
|
|
|
// Resolusion resolver:
|
|
|
|
|
// (psPos / scale) / (prevPsPos / prevScale) -> psPos * scale.inv * prevPsPos.inv * prevScale
|
|
|
|
|
var modifier = psPos.GetScaled(scale.Inverse(), _prevPsPos.Inverse(), _prevScale);
|
|
|
|
|
for (var i = 0; i < size; i++)
|
|
|
|
|
{
|
2022-06-15 08:09:56 +08:00
|
|
|
|
var particle = particles[i];
|
2022-06-09 00:47:42 +08:00
|
|
|
|
particle.position = particle.position.GetScaled(modifier);
|
2022-06-15 08:09:56 +08:00
|
|
|
|
particles[i] = particle;
|
2022-06-09 00:47:42 +08:00
|
|
|
|
}
|
2022-06-15 08:09:56 +08:00
|
|
|
|
_particleSystem.SetParticles(particles, size);
|
2022-06-09 00:47:42 +08:00
|
|
|
|
|
|
|
|
|
// Delay: Do not progress in the frame where the resolution has been changed.
|
|
|
|
|
_delay = true;
|
|
|
|
|
_prevScale = scale;
|
|
|
|
|
_prevPsPos = psPos;
|
|
|
|
|
}
|
|
|
|
|
_prevScreenSize = screenSize;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-08 11:54:11 +08:00
|
|
|
|
private void Simulate(Vector3 scale, bool paused)
|
|
|
|
|
{
|
|
|
|
|
var main = _particleSystem.main;
|
|
|
|
|
var deltaTime = paused
|
|
|
|
|
? 0
|
|
|
|
|
: main.useUnscaledTime
|
|
|
|
|
? Time.unscaledDeltaTime
|
|
|
|
|
: Time.deltaTime;
|
|
|
|
|
|
|
|
|
|
// Prewarm:
|
|
|
|
|
if (0 < deltaTime && _prewarm)
|
|
|
|
|
{
|
|
|
|
|
deltaTime += main.duration;
|
|
|
|
|
_prewarm = false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-16 21:43:23 +08:00
|
|
|
|
// Emitted particles found.
|
|
|
|
|
if (_prevParticleCount != _particleSystem.particleCount)
|
|
|
|
|
{
|
|
|
|
|
var size = _particleSystem.particleCount;
|
|
|
|
|
var particles = ParticleSystemExtensions.GetParticleArray(size);
|
|
|
|
|
_particleSystem.GetParticles(particles, size);
|
|
|
|
|
for (var i = _prevParticleCount; i < size; i++)
|
|
|
|
|
{
|
|
|
|
|
var p = particles[i];
|
|
|
|
|
p.position = p.position.GetScaled(scale.Inverse());
|
|
|
|
|
particles[i] = p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_particleSystem.SetParticles(particles, size);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-08 11:54:11 +08:00
|
|
|
|
// get world position.
|
|
|
|
|
var psTransform = _particleSystem.transform;
|
|
|
|
|
var originWorldPosition = psTransform.position;
|
|
|
|
|
var originWorldRotation = psTransform.rotation;
|
|
|
|
|
|
|
|
|
|
var emission = _particleSystem.emission;
|
|
|
|
|
var rateOverDistance = emission.enabled && 0 < emission.rateOverDistance.constant && 0 < emission.rateOverDistanceMultiplier;
|
|
|
|
|
if (rateOverDistance)
|
|
|
|
|
{
|
|
|
|
|
// (For rate-over-distance emission,) Move to previous scaled position, simulate (delta = 0).
|
|
|
|
|
Vector3 prevScaledPos = _prevPsPos.GetScaled(_prevScale.Inverse());
|
|
|
|
|
psTransform.SetPositionAndRotation(prevScaledPos, originWorldRotation);
|
|
|
|
|
_particleSystem.Simulate(0, false, false, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Move to scaled position, simulate, revert to origin position.
|
|
|
|
|
var scaledPos = originWorldPosition.GetScaled(scale.Inverse());
|
|
|
|
|
psTransform.SetPositionAndRotation(scaledPos, originWorldRotation);
|
|
|
|
|
_particleSystem.Simulate(deltaTime, false, false, false);
|
|
|
|
|
psTransform.SetPositionAndRotation(originWorldPosition, originWorldRotation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
private void SimulateForEditor(Vector3 diffPos, Vector3 scale)
|
|
|
|
|
{
|
|
|
|
|
// Extra world simulation.
|
|
|
|
|
if (_particleSystem.main.simulationSpace == ParticleSystemSimulationSpace.World && 0 < Vector3.SqrMagnitude(diffPos))
|
|
|
|
|
{
|
|
|
|
|
Profiler.BeginSample("[UIParticle] Bake Mesh > Extra world simulation");
|
|
|
|
|
diffPos.x *= 1f - 1f / Mathf.Max(0.001f, scale.x);
|
|
|
|
|
diffPos.y *= 1f - 1f / Mathf.Max(0.001f, scale.y);
|
|
|
|
|
diffPos.z *= 1f - 1f / Mathf.Max(0.001f, scale.z);
|
|
|
|
|
|
2022-06-15 08:09:56 +08:00
|
|
|
|
var size = _particleSystem.particleCount;
|
|
|
|
|
var particles = ParticleSystemExtensions.GetParticleArray(size);
|
|
|
|
|
_particleSystem.GetParticles(particles, size);
|
|
|
|
|
for (var i = 0; i < size; i++)
|
2022-06-08 11:54:11 +08:00
|
|
|
|
{
|
2022-06-15 08:09:56 +08:00
|
|
|
|
var p = particles[i];
|
2022-06-08 11:54:11 +08:00
|
|
|
|
p.position += diffPos;
|
2022-06-15 08:09:56 +08:00
|
|
|
|
particles[i] = p;
|
2022-06-08 11:54:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-15 08:09:56 +08:00
|
|
|
|
_particleSystem.SetParticles(particles, size);
|
2022-06-08 11:54:11 +08:00
|
|
|
|
Profiler.EndSample();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
private void UpdateMaterialProperties()
|
|
|
|
|
{
|
|
|
|
|
if (_parent.m_AnimatableProperties.Length == 0) return;
|
|
|
|
|
|
|
|
|
|
if (s_Mpb == null)
|
|
|
|
|
s_Mpb = new MaterialPropertyBlock();
|
|
|
|
|
|
|
|
|
|
_renderer.GetPropertyBlock(s_Mpb);
|
|
|
|
|
if (s_Mpb.isEmpty) return;
|
|
|
|
|
|
|
|
|
|
// #41: Copy the value from MaterialPropertyBlock to CanvasRenderer
|
|
|
|
|
if (!_modifiedMaterial) return;
|
|
|
|
|
|
|
|
|
|
foreach (var ap in _parent.m_AnimatableProperties)
|
|
|
|
|
{
|
|
|
|
|
ap.UpdateMaterialProperties(_modifiedMaterial, s_Mpb);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s_Mpb.Clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|