sbox-issues icon indicating copy to clipboard operation
sbox-issues copied to clipboard

GetVertices() and GetIndices() out of order

Open ZanderCode opened this issue 1 year ago • 6 comments

Describe the bug

I have a rectangular mesh with numbered verticies (blender) image when I call the GetVertices() and GetIndices() functions of the ModelRenderer.Model class, I get out of order vertices.

I loop through the indices and I try to trace the generation of triangles and somehow the vertex of the third triangle references the wrong index. Output from console for the GetIndces() function:

02:56:05 GameMenu 0 02:56:05 GameMenu 1 02:56:05 GameMenu 2

02:56:05 GameMenu 0 02:56:05 GameMenu 2 02:56:05 GameMenu 3

02:56:05 GameMenu 2 02:56:05 GameMenu 1 <- (no edge connects these points) 02:56:05 GameMenu 4 <- (no edge connects these points)

... Also another thing I noticed when instancing gameobjects along the vertices was that the vertices were not in order. So you could imagine I want to spawn some sort of way point navigation system procedurally along this mesh, but since the vertex and indicies are all messed up, I can't image how I would do this without hand placing waypoints.

Last notes and thoughts: Maybe the indices list only connects two indices together to form an edge and the list is processed by edges? I could work with this, but it doesn't explain why when I instance things on the vertices the instances end up jumping from the beginning of the mesh to the end and places in-between - instead of tracing the vertices as they are laid out in the image

To Reproduce

  1. Create a rectangular mesh in blender with subdivisions along the long axis
  2. export as fbx and create model in s&box ModelDoc editor
  3. add to the model render mesh list - a "RenderMeshMarkup" and tick the CPU vertex access option, then compile
  4. Create script that references gameobject with ModelRenderer component.
  5. Use the .Model.GetIndices() function of the referenced ModelRenderer.
  6. Examine Indices

Expected behavior

Indices are ordered the way the model is imported

Media/Files

No response

Additional context

No response

ZanderCode avatar Aug 18 '24 19:08 ZanderCode

It might help if you posted the fbx, an obj, or a .blend file with a model that exhibits this behaviour. If someone doesn't know exactly what you did in blender then they might not be able to reproduce your issue.

image This minor visual darkening near the zero implies the mesh may have strange topology, which wouldn't be a fault of the engine.

MD485 avatar Aug 18 '24 20:08 MD485

Yeah last time I've messed with GetVertices() and GetIndices() I've also noticed that sometimes output is inconsistent. But code repro would definitely be useful here

wheatleymf avatar Aug 19 '24 06:08 wheatleymf

Build a mesh in 3D software like how I have above and run this (I am unable to upload .fbx):

[Property] public ModelRenderer modelRenderer;

protected override void OnStart(){
    int[] indices = modelRenderer.Model.GetIndices();
    foreach (int indx in indices){
       Info.Log(indx);
   }
}

Examine how every two indices printed represent an edge connection. Before, I suspected triangle formations (so every three indices were a triangle), but it seems that every two indices is an edge.

Other thoughts : I wonder if s&box will chunk the mesh in an optimized way. Maybe it finds and orders edges to render based on height or something. I have no clue, but the image I show above has ordered indices and s&box does not maintain the order.

Context: I am building a game that requires waypoints for a track system. Given that the mesh is a track, I want to extract the points in order and create a data structure representing the connections. This will allow for proper path guidance and custom ai navigation.

ZanderCode avatar Aug 19 '24 16:08 ZanderCode

It might help if you posted the fbx, an obj, or a .blend file with a model that exhibits this behaviour. If someone doesn't know exactly what you did in blender then they might not be able to reproduce your issue.

image This minor visual darkening near the zero implies the mesh may have strange topology, which wouldn't be a fault of the engine.

I checked with the 3D artist and they said the visual is an artifact of the procedural UV generation and not the placement / ordering of the vertices themselves. I can test with other meshes too just to be sure

ZanderCode avatar Aug 19 '24 16:08 ZanderCode

You didn't give a repro project so I had to guess. See if it's fixed when the game next updates

aylaylay avatar Aug 19 '24 20:08 aylaylay

Oh jeez I think I got mixed up as I kept reading repro as steps, not the actual project. I'll try to find some time to get y'all a test project hopefully soon.

ZanderCode avatar Aug 19 '24 21:08 ZanderCode

Is this working now?

aylaylay avatar Aug 20 '24 17:08 aylaylay

I don't think so. I'm observing what looks like proper indices, but not vertices. Their positions are off by a lot.

ZanderCode avatar Aug 20 '24 23:08 ZanderCode

How are they off?

aylaylay avatar Aug 21 '24 01:08 aylaylay

It appears that the positions look transformed by multiple thousand units. I've got limited time to post a test project. Hopefully tomorrow I can give an example.

ZanderCode avatar Aug 21 '24 01:08 ZanderCode

You could just send the model

aylaylay avatar Aug 21 '24 01:08 aylaylay

https://drive.google.com/file/d/1yPW0onC_8oAwB7SJodsM4oMGtSQHhN1V/view?usp=sharing

Github wouldn't let me upload an fbx.

Also, ill be deleting this soon.

ZanderCode avatar Aug 21 '24 01:08 ZanderCode

sbox-dev_FCrHccA8I3

Works completely fine?

aylaylay avatar Aug 21 '24 01:08 aylaylay

Do you see how the index changes at the apex of the loop? This is the problem I have. I expect a continuously ascending index as I trace along the mesh. The index goes from 75 to 271. Say I wanted to instance things along these points in a specific order. How could I achieve this given the indices?

Let me try with another mesh. I'm starting to suspect my entire 3D workflow is the culprit. Having these indices change so close to the apex makes me think there is some magic to connect sub meshes somewhere in my blender node graph which might be the actual problem

ZanderCode avatar Aug 21 '24 01:08 ZanderCode

I don't think it's wise to rely on a compiled render mesh having the same triangulation from blender to fbx to modeldoc

aylaylay avatar Aug 21 '24 02:08 aylaylay