0

I am porting a large XNA project into Unity project. I did not write XNA project and don't have any background with it. The porting was going smoothly until I came across a part that's used to draw a geometry. It looks something like below:

using (VertexDeclaration vertexdecl = 
new VertexDeclaration(GraphicsDevice,                                    
VertexPositionNormalTexture.
VertexElements))
{
    GraphicsDevice.VertexDeclaration = vertexdecl;
    GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, verticesArray, 0, 2);
}

The verticesArray variable it is trying to draw is represented in the VertexPositionNormalTexture structure and initialized as below:

VertexPositionNormalTexture[] verticesArray;

private void InitGeometry()
{
    verticesArray = new VertexPositionNormalTexture[6];
    verticesArray[0] = new VertexPositionNormalTexture( new Vector3(-20,0,0),
                                                  -Vector3.UnitZ,
                                                   new Vector2(0,0));
    verticesArray[1] = new VertexPositionNormalTexture( new Vector3(20,10,0),
                                                   -Vector3.UnitZ,
                                                   new Vector2(13,12));
    verticesArray[2] = new VertexPositionNormalTexture( new Vector3(-5, 20, 0),
                                                   -Vector3.UnitZ,
                                                   new Vector2(0, 1));

    verticesArray[3] = verticesArray[0];
    verticesArray[4] = new VertexPositionNormalTexture( new Vector3(5, 0, 0),
                                                   -Vector3.UnitZ,
                                                   new Vector2(3, 0));
    verticesArray[5] = verticesArray[1];
}

I was able to re-create VertexPositionNormalTexture from the source but now struggling to draw it like GraphicsDevice.DrawUserPrimitives as used above.

The closest Unity function I found is Graphics.DrawMesh but it can only draw Mesh passed into it. Also, even if I manage to convert VertexPositionNormalTexture to Mesh, Unity's Graphics.DrawMesh function does not have the PrimitiveType.TriangleList enum or anything similar.

Maybe I missed it but is there a similar function in Unity to draw the VertexPositionNormalTexture struct?

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • 1
    Unity's mesh struct only really has TriangleList, so there's no need to specify it. Any strip optimization I presume is handled by the engine. If you want to use a lower level graphics API, you need to use GL. https://docs.unity3d.com/2018.3/Documentation/ScriptReference/GL.Begin.html Out of curiosity, is there any reason you are using Unity instead of FNA? – Leo Bartkus Sep 24 '18 at 22:03
  • @LeoBartkus 1.XNA has been discontinued. 2.I was given this XNA project to port into Unity. I will give this a try and let you know if it works or not – Programmer Sep 24 '18 at 23:21
  • I don't quite see the resemblance between XNA and Unity, is there a reason that Unity is preferred over more XNA-like studios like Monogame? – Steven Sep 26 '18 at 06:22
  • 1
    @Steven I have no XNA background but during my work to port a project, I saw many resemblances. The shaders were really similar. Only few things I had to change in them to work in Unity. The Texture class and every other XNA API class I had to port has a similar API in Unity. The only part I'm stuck with is this part in the question. Note that I cannot use Monogame. Like I said in the question, I did not create the original XNA project. I was tasked with doing this port to Unity. No other game engine. – Programmer Sep 26 '18 at 06:35
  • 1
    Both Unity and XNA have support for Direct3D-style shaders so that's why there is a resemblance. Unity also supports Cg shaders. As Leo said, Unity has TriangleList and unlike XNA, the programmer is abstracted from nitty-gritty vertex types. Just define your mesh, progmatically if you wish; apply a material and off you go. –  Oct 12 '18 at 06:41
  • Thanks for your comment. I solved this by writing [`VertexPositionNormalTextureToUnityMesh`](https://stackoverflow.com/q/52721907/3785314) function that takes that format and converts it into Unity Mesh. – Programmer Oct 12 '18 at 07:07

0 Answers0