[SOLVED] XNA 3D Particles Sample port on MonoGame 3.4

Hello,

As you can see on the title of this post, i’m having some problems with porting the XNA 3D Particles Sample on MonoGame 3.4.

Before writing this topic, i’ve made some research about this problem and I can’t solve my problem.

Let’s resume what i’ve done so far:

  • Create a MonoGame 3.4 project
  • Add XNA 3D Particle Sample content into my project’s content
  • Add XNA 3D Particle Sample code

At this point, I started the debug and there is a problem with the shader.
Just a little error with those lines:

compile vs_2_0 ParticleVertexShader();
compile vs_2_0 ParticlePixelShader();

Just change them into:

VertexShader = compile vs_4_0 ParticleVertexShader();
PixelShader = compile ps_4_0 ParticlePixelShader();

And problem solved. Next problem is the VertexShaderInput of XNA doesn’t like MonoGame…

XNA structure:

// XNA
struct VertexShaderInput
{
    float2 Corner : POSITION0;
    float3 Position : POSITION1;
    float3 Velocity : NORMAL0;
    float4 Random : COLOR0;
    float Time : TEXCOORD0;
};

So i made some changes on this structure, and also on the VertexDeclaration to be compatible with MonoGame:

Shader VertexShaderInput structure:

// MonoGame
struct VertexShaderInput
{
    float3 Position : SV_POSITION;
    float2 Corner : NORMAL0;
    float3 Velocity : NORMAL1;
    float4 Random : COLOR0;
    float Time : TEXCOORD0;
};

VertexDeclaration:

// Describe the layout of this vertex structure.
    public static readonly VertexDeclaration VertexDeclaration = new VertexDeclaration
    (
        new VertexElement(0, 
            VertexElementFormat.Vector3,
                VertexElementUsage.Position, 0), // Position

        new VertexElement(sizeof(Single) * 3, // sizeof(Vector3)
            VertexElementFormat.Vector2,
                VertexElementUsage.Normal, 0), // Corner

        new VertexElement(sizeof(Single) * 3 + sizeof(Single) * 2, // sizeof(Vector3) + sizeof(Vector2)
            VertexElementFormat.Vector3,
                VertexElementUsage.Normal, 1), // Velocity

        new VertexElement((sizeof(Single) * 3) * 2 + sizeof(Single) * 2, // sizeof(Vector3) * 2 + sizeof(Vector2)
            VertexElementFormat.Color,
                VertexElementUsage.Color, 0), // Random

        new VertexElement((sizeof(Single) * 3) * 2 + sizeof(Single) * 2 + sizeof(Single) * 4, // sizeof(Vector3) * 2 + sizeof(Vector2) + sizeof(Vector4)
            VertexElementFormat.Single,
                VertexElementUsage.TextureCoordinate, 0) // Time
    );


    // Describe the size of this vertex structure.
    public const int SizeInBytes = (sizeof(Single) * 3) * 2 + sizeof(Single) * 2 + sizeof(Single) * 4 + sizeof(Single); // sizeof(Vector3) * 2 + sizeof(Vector2) + sizeof(Vector4) + sizeof(float)

With this structure, the program compiles and runs, but, nothing is displayed on my screen. No particles… I’m I missing something?

Thanks for your help!

EDIT:

There is the source code on GitHub: https://github.com/ShyroFR/MonoGame-3DParticlesSample

Dup of XNA 3D Particle Sample conversion to Monogame 3 help

Hi, thanks for your reply.

I already test the solutions of the topic you paste, and it’s not working. So i don’t no what to do anymore. I’m debuging the code to see if there is any value not set, but everything seems ok… :confused:

Ok, it’s on the list to publish the particle system I have from this as soon as I tailor a few things.

Hi,

I checked my code again, and I find something wrong on the particle initialisation. I forgot to change the Short2 Corner property into Vector2 Corner. (my bad…)

So I’ve done that and finally, there is something drawn on the screen. But not the result we all expect…

Btw, there’s just the SmokePlume who “works”, the two other didn’t work

Hi,
Did you try changing every “sizeof(…)” to constant integers like here: XNA 3D Particle Sample conversion to Monogame 3 help?
Remember that it is very important to make sure if the size of the VertexElement is correct.

Yes @Sizaar, i’m currently using this VertexDeclaration:

public static readonly VertexDeclaration VertexDeclaration = new VertexDeclaration
(
      new VertexElement(0, VertexElementFormat.Vector3,
                             VertexElementUsage.Position, 0),
      new VertexElement(12, VertexElementFormat.Vector2,
                             VertexElementUsage.Normal, 0),
      new VertexElement(20, VertexElementFormat.Vector3,
                             VertexElementUsage.Normal, 1),
      new VertexElement(32, VertexElementFormat.Color,
                             VertexElementUsage.Color, 0),
      new VertexElement(36, VertexElementFormat.Single,
                             VertexElementUsage.TextureCoordinate, 0)
);

public const int SizeInBytes = 40;

But I have the result of the previous screen.

Good, and what is the order of parameters in your Vertex struct?
I ask, because struct parameters should be ordered just like in shader (don’t ask me why :smile: ).

Oh men, I just do what you say, I aligned every field just like the shader and now it works! :smiley:

Just the texture are a little bit dark in comparison with XNA, that’s strange. I will find a solution.

Thank you for the help guys! I’ll commit my changes on my GitHub repo for those who wants to use the sample :smile:

Great stuff, thanks to another community member who pointed this out and I was able to fix the newer version in the XNAGameStudioArchive for the XML Loading sample.

Also Shawn Hargreaves XML articles are now stored there for all time as well (beginning another long journey to archive everything good about old XNA stuff)