Hello, I used Lighting fundamentals to create cylinders and spheres to represent atoms and connections in proteins. Everything works fine except colours and lighting.
To connect 2 atoms, I calculated vector between atomA and atomB and used float3(0, 1, 0) vector to find the correct rotation matrix with this function:
The vertex position looks good with that change to the rotation matrix , so maybe the vertex function isn’t reading the normal and the color attributes correctly? Can you capture the GPU frame and inspect the vertex buffer to see whether the values in the normal and color are what you are expecting?
If they are not what you are expecting, then you will have to work out why. It could be the vertex descriptor if you are using [[stage_in]] in the vertex function, or it could that you define the vertex buffer differently between the Swift code and the GPU code.
Maybe it has something to do with how I generate meshes?
I use MDLMesh.newCylinder(withHeight: height, radii: radii, radialSegments: 10, verticalSegments: 10, geometryType: .triangles, inwardNormals: false, allocator: MTKMeshBufferAllocator(device: Renderer.device))
Rotation is what I get from rotateMatrix function from my first post.
I changed color generation from random to predefined and I’ve noticed that all colours are darker than expected. After playing with vertex function I found out that changing in.normal to -in.normal solves almost every problem.
It’s really hard to say what’s going on without seeing the full code.
You could try substituting the result for the color. Eg instead of returning a color, return the normal / worldPosition / worldNormal for that fragment. Then you can visualise what’s happening more easily.
The initial normals in object space look wildly off. All faces pointing up should be green. Your normals, especially on some of the cylinders have half the normals facing one way, and half the other. The rotation matrix merely emphasises this.
This is what normals should look like. Up is green, and red is for the normals pointing in the x direction. The black ones are pointing at -z.