How to create new state texture for GPU particles?

I’m modifying the engine source for a project I’m doing, and I’m trying to work with GPU particles.

For GPU particles, there are two state textures used for particle simulation: PositionTexture and VelocityTexture. I’m trying to add another texture ColorTexture that will store and update color in the GPU simulation just like the position and velocity texture.

I thought I could just go through the code and mirror the operations on the PositionTexture or VelocityTexture, but that hasn’t been working. The part I’m missing is the specific place in the code where the FNewParticle attributes are first written to texture.

Have a look at this shader in the engine’s installed location: Engine\Shaders\Private\ParticleGPUSpriteVertexFactory.ush

/** Texture containing positions for all particles. */
Texture2D PositionTexture;
SamplerState PositionTextureSampler;
Texture2D VelocityTexture;
SamplerState VelocityTextureSampler;
Texture2D AttributesTexture;
SamplerState AttributesTextureSampler;
Texture2D CurveTexture;
SamplerState CurveTextureSampler;

That is where you’d add your own texture and sampler and add in the gpu logic in that shader

This is a vertex factory shader. So you’ll have a corresponding vertex factory class in C++. Have a look at FGPUSpriteVertexFactory under Engine\Source\Runtime\Engine\Private\Particles\ParticleGpuSimulation.cpp

This class takes in the references of the state textures (e.g. PositionTextureRHI, VelocityTextureRHI)

You set these textures from here: FGPUSpriteDynamicEmitterData::GetDynamicMeshElementsEmitter

I’ve been looking a lot at both Engine\Shaders\Private\ParticleGPUSpriteVertexFactory.ush and Engine\Source\Runtime\Engine\Private\Particles\ParticleGpuSimulation.cpp. If I could access my color texture in ParticleGPUpriteVertexFactory, it’s clear to me what to do with it in that shader. I’m currently setting my color texture in FGPUSpriteDynamicEmitterData::GetDynamicMeshElementsEmitter like so:

//Engine code to set Position and Velocity Texture
VertexFactory.PositionTextureRHI = StateTextures.PositionTextureRHI;
VertexFactory.VelocityTextureRHI = StateTextures.VelocityTextureRHI;

 //My code to set up my color texture
VertexFactory.ColorTextureRHI = StateTextures.ColorTextureRHI;

Also, I’ve been looking at Engine\Shaders\Private\ParticleSimulationShader.usf, which is where the Position and Velocity of GPU simulated particles are updated. Do I also need to add my texture there so I can do color simulations?

I’m also still not sure exactly how we get from creating new particles in FGPUSpriteEmitterInstance::BuildNewParticles to actually writing the properties of those particles to the state textures.