Data Driven Particle System: How to specify textures?

Hi all,

I’ve been having a play with the Extended.Particles System and have struggled to find any documentation to help me - thankfully there are some demos with source code. However, the demo doesn’t show how to load the particle data from .json files.

I’ve had a play and managed to get the .json loaded using serialization - there’s an example ‘test3.particles’ in the source that I’m trying to load. There’s also ParticleEffect.FromFile & ParticleEffect.FromStream which do similar things. This all seems great and from stepping through the code it looks like it’s working - however nothing is being drawn on the screen.

The json references a textureregion called particle. I’m not sure how to tell it to where to find it.

I’ve tried creating this manually, naming the texture and putting it inside a TextureAtlas which is then placed inside a TextureRegionService. I can then pass the TextureRegionService to the Deserializer and the ParticleEffect will know what textures to show.

What I’m struggling to do is create a TextureRegionService with TextureAtlases as this property is readonly. I’m assuming that there is a much simpler way to create these perhaps via the Content Pipeline?

Thanks for any pointers!

I’ve had a fresh look at this and have got it working. So, I figured I should do the decent thing and post my own answer to my question in case anyone comes across this in years to come. Who were you DenverCoder9???

It was a series of issues related to getting a TextureRegionService and then adding Textures to it. I think it’s been designed that you already are familiar with TextureAtlases and you’re going to be loading your content that way. I just wanted to associate some textures witht the ParticleEffect. So instead I need to load the textures myself and manually build the TextureAtlases. Probably obvious if you’re familiar with ME but not if you’re just starting out with it :slight_smile:

// create a 1x1 texture for the particle and set it to white
_particleTexture = new Texture2D(GraphicsDevice, 1, 1);
_particleTexture.SetData(new[] { Color.White });

// create an dictionary or atlas data (this essentially what is usually loaded from an XML file)
// this states the name of the texture and it's region on the.  Normally a textureRegion is part of a larger texture 
Dictionary<string, Rectangle> atlas_dict = new Dictionary<string, Rectangle>();
atlas_dict.Add("particle", new Rectangle(0, 0, 1, 1));

// create an atlas using this texture and data 
TextureAtlas atlas = new TextureAtlas("who_cares", _particleTexture, atlas_dict);

// Now you can create your texture region service and add all your atlases to it
MonoGame.Extended.Serialization.TextureRegionService textureRegionService = new MonoGame.Extended.Serialization.TextureRegionService();
textureRegionService.TextureAtlases.Add(atlas);

// open the json file - there are a few methods such as FromFile() that combine these steps
string json = File.ReadAllText("Content/Particles/test3.particles");
JsonTextReader reader = new JsonTextReader(new StringReader(json));

// now you can deserialise it
MonoGame.Extended.Particles.Serialization.ParticleJsonSerializer serializer = new MonoGame.Extended.Particles.Serialization.ParticleJsonSerializer(textureRegionService);
_particleEffect = serializer.Deserialize<ParticleEffect>(reader);

Anyway, perhaps this bit of code points someone in the right direction in the future.

And that creating a 1x1 texture bit can obviously be replaced by loading your own xnb files :slight_smile: