79 auto& registry = collect_ctx.
registry;
84 if (!particle_system_ || particle_shader_id_ == 0)
return;
88 for (
auto entity : emitterView) {
89 if (!collect_ctx.
accepts(entity))
continue;
91 if (!emitter.enabled)
continue;
96 const auto* state = particle_system_->getState(entity);
103 u32 gpuSegmentCount = 0;
104 u32 particleCount = 0;
106#ifdef ES_ENABLE_GPU_PARTICLES
109 if (!gpu || collect_ctx.
isOverlay())
continue;
112 if (!particle_system_->peekGpuStep(entity, step))
continue;
113 if (!gpu->
submit(entity, emitter, transform, *particle_system_, step, collect_ctx.
camera.
eye, gpuInstances, gpuSegments, gpuSegmentCount))
continue;
114 particleCount = step.drawCount;
119 particleCount = state->pool.aliveCount();
121 if (particleCount == 0)
continue;
123 u32 textureId =
ctx.white_texture_id;
124 if (emitter.texture.isValid()) {
125 Texture* tex =
ctx.resources.getTexture(emitter.texture);
126 if (tex) textureId = tex->
getId();
129 i32 cols = std::max(emitter.spriteColumns, 1);
130 i32 rows = std::max(emitter.spriteRows, 1);
131 f32 uvScaleX = 1.0f /
static_cast<f32>(cols);
132 f32 uvScaleY = 1.0f /
static_cast<f32>(rows);
133 bool sheet = (cols > 1 || rows > 1);
135 bool isLocalSpace = emitter.simulationSpace ==
137 glm::vec3 emitterWorldPos =
rebased(transform.worldPosition);
138 f32 emitterAngle = 0.0f;
139 glm::vec2 emitterScale(transform.worldScale);
140 f32 cosA = 1.0f, sinA = 0.0f;
142 const auto& rot = transform.worldRotation;
143 emitterAngle = 2.0f * std::atan2(rot.z, rot.w);
144 if (std::abs(emitterAngle) > 0.001f) {
145 cosA = std::cos(emitterAngle);
146 sinA = std::sin(emitterAngle);
152 u32 instByteOffset = 0;
153 ParticleInstanceData* inst =
nullptr;
156 particleCount *
sizeof(ParticleInstanceData));
157 inst =
reinterpret_cast<ParticleInstanceData*
>(
162 const f32 lengthScale = emitter.lengthScale;
163 const f32 velocityScale = emitter.velocityScale;
164 const f32 intensity = std::max(emitter.intensity, 0.0f);
173 glm::vec3 rel = p.
position * glm::vec3(emitterScale, 1.0f);
174 if (std::abs(emitterAngle) > 0.001f) {
175 worldPos = emitterWorldPos +
176 glm::vec3(rel.x * cosA - rel.y * sinA, rel.x * sinA + rel.y * cosA, rel.z);
178 worldPos = emitterWorldPos + rel;
180 size = glm::vec2(p.
size) * emitterScale;
183 size = glm::vec2(p.
size);
186 f32 u0 = 0.0f, v0 = 0.0f;
190 u0 =
static_cast<f32>(col) * uvScaleX;
191 v0 =
static_cast<f32>(row) * uvScaleY;
198 const glm::vec2 flat = glm::vec2(v) * emitterScale;
199 v = glm::vec3(flat.x * cosA - flat.y * sinA, flat.x * sinA + flat.y * cosA, v.z);
207 ParticleInstanceData& d = inst[i++];
208 d.px = worldPos.x; d.py = worldPos.y; d.pz = worldPos.z;
209 d.sx = size.x; d.sy = size.y;
210 d.rotation = rotation;
212 d.uvOffsetX = u0; d.uvOffsetY = v0;
213 d.uvScaleX = sheet ? uvScaleX : 1.0f;
214 d.uvScaleY = sheet ? uvScaleY : 1.0f;
217 d.intensity = intensity;
224 u32 shaderId = particle_shader_id_;
229 bool materialDepth =
false, depthTest =
false, depthWrite =
false;
230 if (emitter.material != 0 &&
ctx.materials) {
232 const u32 program =
ctx.materials->particleProgram(emitter.material,
ctx.resources);
235 materialId = emitter.material;
237 materialDepth =
true;
238 depthTest = m->depthTest;
239 depthWrite = m->depthWrite;
244 const i32 layer = collect_ctx.
treeLayer(entity, emitter.layer);
246 .stage =
ctx.current_stage,
248 .shaderId = shaderId,
250 .textureId = textureId,
251 .depth = collect_ctx.
sortDepth(emitterWorldPos),
255 .y = emitterWorldPos.y,
257 .type = RenderType::Particle,
258 .materialId = materialId,
260 .instanceCount = particleCount,
261 .instanceBuffer = gpuInstances,
269 pushBatchDraw(draw_list, clips, instByteOffset, 0, 0, 6, key);
284 if (!state->gpu && emitter.trailEnabled && emitter.trailWidth > 0.0f &&
285 state->trail_count.size() == state->pool.particles().size()) {
287 .stage =
ctx.current_stage,
289 .shaderId =
ctx.batch_shader_id,
291 .textureId =
ctx.white_texture_id,
292 .depth = collect_ctx.
sortDepth(emitterWorldPos),
293 .y = emitterWorldPos.y,
298 const auto& particles = state->pool.particles();
299 auto toWorld = [&](glm::vec3 pos) -> glm::vec3 {
300 if (!isLocalSpace)
return pos;
301 glm::vec3 rel = pos * glm::vec3(emitterScale, 1.0f);
302 if (std::abs(emitterAngle) > 0.001f)
303 return emitterWorldPos +
304 glm::vec3(rel.x * cosA - rel.y * sinA, rel.x * sinA + rel.y * cosA, rel.z);
305 return emitterWorldPos + rel;
308 for (std::size_t idx = 0; idx < particles.size(); ++idx) {
310 if (!p.
alive || state->trail_count[idx] == 0)
continue;
314 trail_center_.clear();
315 int m = std::min<int>(state->trail_count[idx], keep);
316 for (
int k = 0; k < m; ++k) trail_center_.push_back(toWorld(ring[k]));
317 glm::vec3 head = toWorld(p.
position);
318 glm::vec3 dHead = head - trail_center_.back();
319 if (glm::dot(dHead, dHead) > 1e-4f) trail_center_.push_back(head);
320 const int n =
static_cast<int>(trail_center_.size());
327 trail_verts_.clear();
328 glm::vec3 lastTangent(1.0f, 0.0f, 0.0f);
329 glm::vec3 lastSide(0.0f, 1.0f, 0.0f);
330 for (
int j = 0; j < n; ++j) {
331 const glm::vec3& a = trail_center_[j == 0 ? 0 : j - 1];
332 const glm::vec3& b = trail_center_[j + 1 == n ? j : j + 1];
333 glm::vec3 seg = b - a;
334 f32 len = glm::length(seg);
335 glm::vec3 tangent = len > 1e-6f ? seg / len : lastTangent;
336 lastTangent = tangent;
338 f32 t =
static_cast<f32>(j) /
static_cast<f32>(n - 1);
339 f32 halfW = 0.5f * emitter.trailWidth * t;
340 glm::vec4 col = p.
color;
344 const glm::vec3& c = trail_center_[j];
345 const glm::vec3 side =
ribbonSide(tangent, c, collect_ctx.
camera, lastSide);
347 trail_verts_.push_back({c + side * halfW, packed, glm::vec2(1.0f - t, 0.0f)});
348 trail_verts_.push_back({c - side * halfW, packed, glm::vec2(1.0f - t, 1.0f)});
351 trail_indices_.clear();
352 for (
int j = 0; j + 1 < n; ++j) {
353 u32 l0 =
static_cast<u32>(j) * 2, r0 = l0 + 1, l1 = l0 + 2, r1 = l0 + 3;
354 trail_indices_.push_back(l0); trail_indices_.push_back(r0); trail_indices_.push_back(r1);
355 trail_indices_.push_back(l0); trail_indices_.push_back(r1); trail_indices_.push_back(l1);
359 trail_verts_.data(),
static_cast<u32>(trail_verts_.size()),
360 trail_indices_.data(),
static_cast<u32>(trail_indices_.size()),
Particles simulated by a compute kernel, drawn from the buffer the kernel wrote (WebGPU).
bool submit(Entity entity, const ecs::ParticleEmitter &emitter, const ecs::Transform &transform, particle::ParticleSystem &system, const particle::GpuStep &step, const glm::vec4 &eye, BufferHandle &instances, BufferHandle &segments, u32 &segmentCount)
Queues the step an emitter needs and says where its particles are. The step is committed to the emitt...