sr4dev / Unity-SpriteAssist

Unity Sprite mesh extension
https://github.com/sr4dev/Unity-SpriteAssist
227 stars 29 forks source link

Mesh normals aren't correct #38

Open MateuszRe opened 2 years ago

MateuszRe commented 2 years ago

Hey again, i noticed that the generated normals arent correct (left is how it looks, right is how it should look for a flat surface): normals

Its caused by the triangles sharing vertices (which is perfectly fine when you dont need to use normals, as it reduces the vert count to minimum), i fixed it on my end by splitting the vertices on the generated mesh using this little method: https://pastebin.com/QqpVQT75 You could make it an optional thing, as it does increase vert count quite drastically (triple the amount of triangles). They can however be welded back together afterwards (but respecting the normals this time), which will still be quite a bit more than the original but that's the choice here if someone wants correct normals. I can provide you with a welding method too, although its not as clean as its part of my much older code base.

sr4dev commented 2 years ago

Thank you for your report!! I will refer to your code and fix the problem. (as a new option like 'Normal: Optimized Normal' or 'Normal: Currect Normal')

MateuszRe commented 2 years ago

Hey, great! So in case you want it, here's the code to weld vertices back together: https://pastebin.com/fSNYiUKf (like i mentioned, its a bit messy, but it works). You need to split them first, then do mesh.RecalculateNormals() and then weld it back together.

Also, Im not sure how far you're interested in developing this tool, but as a brief FYI there are some interesting options there, especially in the area of manipulating the output mesh to make it more defined. Ive added several stuff to better suit the needs of my game, and aside from rotating/scaling the mesh one thats particularily intersting is modifying the vertex 'height' with the use of unity animation curves like so: meshoutput

It doesnt always work perfectly though, as its very reliant on vertex distribution around the edges, if its not dense enough then there might be some glitchy looking results. It would work best, if there was a way to add some spare vertices within the polygon after generating the sprite outline, but before triangulation, sort of a middle ground between what the 'OpaqueMesh' and 'GridMesh' do, im not sure if that's easily doable or not, i had a brief look at the generation code for both of those but it wasnt easy enough to read for me, maybe you'll have a better idea! :)

sr4dev commented 2 years ago

Looks great!

modifying the vertex 'height' with the use of unity animation curves

How about using a height map texture? image This is a height map texture used for the terrain. I think I can apply it on sprite too.

MateuszRe commented 2 years ago

Yeah, a height map would work too! Although the same issue would still apply, it would only be sampled where the vertexes are, so only around the outline of the sprite, to get any more detail we'd need more vertexes added within the polygon.

sr4dev commented 2 years ago

Work in progress image

image

MateuszRe commented 2 years ago

That looks awesome, man, well done! Thank you for the update, Its definitely moving in the right direction!

sr4dev commented 2 years ago

New version updated!

MateuszRe commented 2 years ago

This is awesome, thanks! I've played around with it for a few hours and have some information for you:

The triangulation of the OpaqueWithoutTightGrid part uses less vertices than the Grid mesh, so the faces end up being raised differently between them. This can be mitigated by using smaller grid mesh, which has a good chance of making the gaps less noticeable, but not eliminating them completely: Example3

Im only sharing this as an FYI in case you're interested, not asking you to fix it further, as its definitely good enough for my needs, the gaps wont be noticeable in my use cases (weapons being too small on the screen to notice). Once again, thank you for the amazing tool, its super useful!

sr4dev commented 2 years ago

OK. I'll check this!