-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMeshParticleEmitter.cs
More file actions
145 lines (121 loc) · 7.28 KB
/
MeshParticleEmitter.cs
File metadata and controls
145 lines (121 loc) · 7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
/* Copyright (c) 2025 Petar Petrov (PeterSvP)
* https://pi-dev.com * https://store.steampowered.com/pub/pidev
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* ============= Description =============
* Emits particles from mesh and skinned mesh vertex positions using a given ParticleSystem.
* Supports dynamic velocity calculation based on mesh vertex distance and optional mesh exclusion.
* Useful for effects like mesh-based bursts, trails, or vertex-driven particle systems.
*
* ============= Usage =============
* MeshParticleEmitter.EmitFromMeshes(particleSystem, transform);
* MeshParticleEmitter.EmitFromMeshVertices(particleSystem, transform, velocityMultiplier, ignoreList);
*/
namespace PiDev.Utilities
{
public class MeshParticleEmitter : MonoBehaviour
{
public float strength;
public void Emit() => EmitFromMeshes(GetComponent<ParticleSystem>(), transform);
public static void EmitFromMeshes(ParticleSystem particleSystem, Transform rootTransform)
{
// Cache the shape module for optimization
var shapeModule = particleSystem.shape;
// Iterate over all MeshRenderers
foreach (var meshRenderer in rootTransform.GetComponentsInChildren<MeshRenderer>())
{
if (meshRenderer == null) continue;
var meshFilter = meshRenderer.GetComponent<MeshFilter>();
if (meshFilter == null || meshFilter.sharedMesh == null) continue;
int vertexCount = meshFilter.sharedMesh.vertexCount; // Get the vertex count
// Set the shape module to use the MeshRenderer
shapeModule.shapeType = ParticleSystemShapeType.MeshRenderer;
shapeModule.meshRenderer = meshRenderer;
// Emit particles based on the vertex count
particleSystem.Emit(vertexCount);
}
// Iterate over all SkinnedMeshRenderers
foreach (var skinnedMeshRenderer in rootTransform.GetComponentsInChildren<SkinnedMeshRenderer>())
{
if (skinnedMeshRenderer == null || skinnedMeshRenderer.sharedMesh == null) continue;
int vertexCount = skinnedMeshRenderer.sharedMesh.vertexCount; // Get the vertex count
// Set the shape module to use the SkinnedMeshRenderer
shapeModule.shapeType = ParticleSystemShapeType.SkinnedMeshRenderer;
shapeModule.skinnedMeshRenderer = skinnedMeshRenderer;
// Emit particles based on the vertex count
particleSystem.Emit(vertexCount);
}
}
public static void EmitFromMeshVertices(ParticleSystem particleSystem, Transform rootTransform, float velocityMultiplier = 1f, IEnumerable<string> ignoreList = default)
{
ParticleSystem.Particle[] particles = new ParticleSystem.Particle[particleSystem.main.maxParticles];
int particleCount = particleSystem.GetParticles(particles);
foreach (var meshFilter in rootTransform.GetComponentsInChildren<MeshFilter>())
{
if (ignoreList != null && ignoreList.Contains(meshFilter.name)) continue;
if (meshFilter.sharedMesh == null) continue;
Vector3[] vertices = meshFilter.sharedMesh.vertices;
//Vector3[] normals = meshFilter.sharedMesh.normals;
for (int i = 0; i < vertices.Length; i++)
{
if (particleCount >= particles.Length) break;
Vector3 localVertex = meshFilter.transform.TransformPoint(vertices[i]);
Vector3 directionFromRoot = (localVertex - rootTransform.position);
Vector3 worldNormal = directionFromRoot * velocityMultiplier; // Scale velocity based on distance from root
Vector3 worldPosition = meshFilter.transform.TransformPoint(vertices[i]);
particles[particleCount].position = worldPosition;
particles[particleCount].velocity = (worldPosition - rootTransform.position) * velocityMultiplier; // worldNormal;
particles[particleCount].startLifetime = particleSystem.main.startLifetime.constant;
particles[particleCount].startSize = particleSystem.main.startSize.constant;
particles[particleCount].startColor = particleSystem.main.startColor.color;
particles[particleCount].remainingLifetime = particles[particleCount].startLifetime;
particleCount++;
}
}
foreach (var skinnedMeshRenderer in rootTransform.GetComponentsInChildren<SkinnedMeshRenderer>())
{
if (ignoreList != null && ignoreList.Contains(skinnedMeshRenderer.name)) continue;
if (skinnedMeshRenderer.sharedMesh == null) continue;
Mesh bakedMesh = new Mesh();
skinnedMeshRenderer.BakeMesh(bakedMesh);
Vector3[] vertices = bakedMesh.vertices;
//Vector3[] normals = bakedMesh.normals;
for (int i = 0; i < vertices.Length; i++)
{
if (particleCount >= particles.Length) break;
Vector3 worldPosition = skinnedMeshRenderer.transform.TransformPoint(vertices[i]);
particles[particleCount].position = worldPosition;
particles[particleCount].velocity = (worldPosition - rootTransform.position) * velocityMultiplier; // worldNormal;
particles[particleCount].startLifetime = particleSystem.main.startLifetime.constant;
particles[particleCount].startSize = particleSystem.main.startSize.constant;
particles[particleCount].startColor = particleSystem.main.startColor.color;
particles[particleCount].remainingLifetime = particles[particleCount].startLifetime;
particleCount++;
}
}
particleSystem.SetParticles(particles, particleCount);
}
}
}