Making an MDLMesh from vertexData

Hello I spoke to you a few years ago when I first bought the book (which I love).
I was wondering if you could help me with my problem. Im pretty sure it is me making a stupid mistake and I was wondering if you could help me spot it.

Screenshot 2021-08-06 at 09.31.29
Screenshot 2021-08-06 at 09.31.40
I have tried to create an MDLmesh from my own vertexData. However for some reason the code is skipping over the indices marked 999 in my vertex data. The program is acting like they don’t exist, so without the 999 there the code does not work.

Screenshot 2021-08-06 at 09.28.17
I used the GPU geometry view to figure out that if I place random numbers in those places then it displays the correct object. However this is obviously not a solution :joy::joy:.
So I was wondering if you could see what the issue is?

Hi Harry - yes I remember you. Something to do with glTF wasn’t it? Thank goodness we’ve moved on to USD.

Could it be your definition of UVs? Which are only supposed to have a u and a v. Your UVs seem to be 999, 0, 0. The vertex descriptor has a float2 correctly.

The thing that is strange about my program is that it seems to be ignoring the vertexData[3] vertexData[7], vertexData[13], vertexData[17], vertexData[23], vertexData[27]. The 999 are not the cause of the issue but my response to it. I have placed them there so my program skips the 999s and not the start of the vertexNormal. Without the 999s my program does not correctly receive the vertexData. For example if I had not placed the first 999 there

Screenshot 2021-08-06 at 12.24.45

this is what appears in the gpu geometry view.
Screenshot 2021-08-06 at 12.24.13

As you can see without the first 999 the program will completely ignore the x coordinate of the vertexNormal like it doesn’t even exist. Causing every other float to be pushed forward one place, resulting in an incorrect output

With the 999 as placeholders the code is actually working because it is producing the shape that I want, which is a square. However I do not understand why i need the 999s for it to work. I am very confused on why my program just seems to ignore specific indices of the vertex data. I want my code to work without the 999s but I cannot figure out why my program is just skipping certain values.

Are you able to give me the project to play around with? I find that’s easier to analyse what’s going on.

Yes definitely , I have sent the project to your email :star2: (I have also messaged it to you just incase it didn’t go to your email). The file I was talking about earlier is in the Scenes folder and is called individualHairVertexData.swift.

I’ve had a look at it. And messaged you back with some code.

I think it’s to do with a vertex descriptor mismatch. It generally is, when the data in the Metal debugger doesn’t line up with what you think is in your MTLBuffers.

Instead of putting in random characters in your vertex data, you should define it exactly as you want it, and then make the vertex descriptor match. And I suggest having a struct on the Swift side such as

struct VertexData {
  let position: float3
  let normal: float3
  let uv: float2
}

And then creating a vertexData array of [VertexData] rather than lots of Floats.

Here are some comments:

  1. Stride of float3 is 16. Stride of 3 x Float is 12.
  2. position, if you give it a w, the w should always be 1, otherwise your matrix multiplication will be out, and divide by w in the rasteriser won’t work properly.
  3. Class names should be capitalised (unless you’re making sure that you can tell what’s yours as opposed to the book, and even then it should be a prefix, not a lower case).
  4. Model.vertexDescriptor is suspect and may be causing problems (not sure in this exact case though). As a class property, it can only hold one vertexDescriptor, and you are defining multiple vertex descriptors.

Well done for managing to use different vertex descriptors in the same pipeline state though :clap: .