nicklockwood / Euclid

A Swift library for creating and manipulating 3D geometry
MIT License
640 stars 53 forks source link

Normals Not Copied to SCNGeometry When Vertex Normals Match Face Normals #122

Open Theophania opened 6 months ago

Theophania commented 6 months ago

I am attempting to create an SCNGeometry from a Mesh, and I have encountered an issue where normals are not copied to the SCNGeometry if the vertex normals have the same values as the face normals. In the Euclid source code, I noticed that the normals are set to .zero initially and are only copied into the normals vector if hasVertexNormals is true. In scenarios where the vertex normals are the same as the face normals, hasVertexNormals remains false, and thus the normals aren't copied. Is there a workaround available for this? Thanks a lot for the support!

nicklockwood commented 6 months ago

This was deliberate because by default SceneKit generates flat surface normals so passing zero produces the same output. If you need the normals it wouldn't be hard to add but can I ask what you need them for?

Theophania commented 6 months ago

Thank you for the quick reply. I'm applying PBR materials to my SCNNodes, and I've noticed that in some areas the material looks incorrect, and there are also odd shadows when I export my scene as a USDZ file. I thought these issues might be related to the normals of my SCNGeometry. Upon inspecting the geometries, I realized that they actually had no normals, which could explain the rendering problems I'm seeing.

exampl

nicklockwood commented 6 months ago

That makes sense. It will be a day or two before I can investigate this - maybe you can try enabling the normals locally in your project to see if it solves the PBR issues? If so I'll enable it in the next release.

it should be simple to enable because the logic is already there to generate the normals - you just need to eliminate the special case check for when the normals match the polygon normal.

Theophania commented 6 months ago

thanks, I will try it out and let you know.

Theophania commented 6 months ago

I enabled the normals, and now my model looks completely off in my SceneKit view: scnview However, when I export it as a USDZ file, it appears correct, and the problems with the shadows seem to be resolved: model

At the moment, I'm not sure why there is such a discrepancy. There are some possible causes, but I will need to investigate further.