Add GPUParticleSystem.fromParticleSystem helper#18330
Add GPUParticleSystem.fromParticleSystem helper#18330VicenteCartas wants to merge 4 commits intoBabylonJS:masterfrom
GPUParticleSystem.fromParticleSystem helper#18330Conversation
|
Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s). |
|
Snapshot stored with reference name: Test environment: To test a playground add it to the URL, for example: https://snapshots-cvgtc2eugrd3cgfd.z01.azurefd.net/refs/pull/18330/merge/index.html#WGZLGJ#4600 Links to test your changes to core in the published versions of the Babylon tools (does not contain changes you made to the tools themselves): https://playground.babylonjs.com/?snapshot=refs/pull/18330/merge To test the snapshot in the playground with a playground ID add it after the snapshot query string: https://playground.babylonjs.com/?snapshot=refs/pull/18330/merge#BCU1XR#0 If you made changes to the sandbox or playground in this PR, additional comments will be generated soon containing links to the dev versions of those tools. |
|
WebGL2 visualization test reporter: |
|
Visualization tests for WebGPU |
There was a problem hiding this comment.
Pull request overview
Adds a new conversion helper for particles, enabling creation of a GPUParticleSystem from an existing CPU ParticleSystem by copying/cloning supported state, warning on unsupported features, and converting CPU flow maps into GPU textures. Also includes lifecycle fixes (disposing flow-map textures) and a pre-warm transform-matrix update to ensure matrix-dependent steps behave correctly.
Changes:
- Added
GPUParticleSystem.fromParticleSystem(...)conversion helper with flow-map conversion and warning logs for unsupported CPU features. - Ensured
GPUParticleSystem.dispose()disposes the internal flow-map texture whendisposeTextureis true. - Added unit + visual tests for conversion behavior (including flow-map cases) and updated ThinParticleSystem pre-warm to update the scene transform matrix.
Reviewed changes
Copilot reviewed 4 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/dev/core/src/Particles/gpuParticleSystem.ts | Adds fromParticleSystem and disposes _flowMap in dispose() to prevent GPU texture leaks. |
| packages/dev/core/src/Particles/thinParticleSystem.ts | Updates scene transform matrix before pre-warm cycles to ensure matrix-dependent updates are correct. |
| packages/dev/core/test/unit/Particles/gpuParticleFromCPU.test.ts | Adds unit tests covering conversion behavior, warnings, gradients, attractors, textures, and flow-map conversion. |
| packages/tools/tests/test/visualization/config.json | Registers two new visual tests for CPU→GPU conversion and flow-map conversion. |
| packages/tools/tests/test/visualization/ReferenceImages/*.png | Adds reference images for the new visual tests. |
|
Visualization tests for WebGPU |
|
WebGL2 visualization test reporter: |
|
Visualization tests for WebGPU |
|
WebGL2 visualization test reporter: |
Migrate CPU particle systems to GPU (
GPUParticleSystem.fromParticleSystem)Summary
Adds
GPUParticleSystem.fromParticleSystem(source, scene, options?), a static helper that produces aGPUParticleSystemconfigured to match an existing CPUParticleSystemas closely as the GPU pipeline allows. Any GPU-unsupported feature on the source is logged as a warning and skipped; everything that the GPU pipeline can represent is copied 1:1.As part of this work, the GPU render pipeline now also supports the
color2component of color gradients (previously onlycolor1was honored), bringing GPU color gradients to parity with the CPU path.What's included
New API
GPUParticleSystem.fromParticleSystem(source, scene, options?)— migrates a CPUParticleSystemto a newGPUParticleSystem. Copies all transferable state (emitter, min/max ranges, gradients, attractors, animations, flow map, pre-warm, etc.) and returns the new system.GPU color gradient
color2supportGPUParticleSystem.addColorGradient(gradient, color1, color2?)now accepts a second color. When any stop has acolor2, the color gradient texture is built as a 2-row RGBA8 texture (row 0 = color1 ramp, row 1 = color2 ramp, withcolor2 ?? color1fallback).gpuRenderParticles.vertex.fx) gains aCOLORGRADIENTS_COLOR2define. Under that define it samples both rows andmixes them using the particle's persistentseed.x, producing a stable per-particle random color — matching the CPU system's behavior.seedattribute is wired into the render vertex buffers only whenCOLORGRADIENTS_COLOR2is active (zero cost when unused).removeColorGradientrecomputes the flag, so removing the lastcolor2stop drops back to the single-row path cleanly.Reviewer-feedback fixes (after initial code review)
color2was being silently dropped byfromParticleSystem. Now fully supported in the GPU path (see above);fromParticleSystemclonescolor2through to the destination._flowMapwas never disposed byGPUParticleSystem.dispose()— added disposal.animationsarray andbeginAnimationOn*flags were not copied byfromParticleSystem— now copied.fromParticleSystemclaimed to warn on "custom update functions", but the code doesn't (and cannot feasibly) detect a custom override — corrected the doc to list only what is actually detected.preWarmCycleswas not applying flow-map advection (bug inthinParticleSystem) — addedscene.updateTransformMatrix()inside the pre-warm loop so flow-map sampling uses a valid transform during warm-up.Tests
packages/dev/core/test/unit/Particles/gpuParticleFromCPU.test.ts): 15 tests covering emitter state, ranges, attractors, gradient copying (including a new test assertingcolor2is preserved with and without a second color per stop), flow map, animation flags, and warning paths for unsupported features (sub-emitters,startDirectionFunction,startPositionFunction,customShader, ramp/remap gradients).packages/tools/tests/test/visualization/config.json): 3 new entries, all passing on WebGL2 and WebGPU:GPU Particles - From Particle System - BasicGPU Particles - From Particle System - Gradients + AttractorsGPU Particles - Change - Color Gradient With Color2Quality
npm run format:check✓npm run lint:check✓npm run test:unit— 168/168 Particle tests passingBackward compatibility
addColorGradient(gradient, color1)signature unchanged —color2is an optional third parameter. Existing callers are unaffected.COLORGRADIENTS_COLOR2only activates when the user explicitly adds acolor2; existing content produces byte-identical output.