I'm working on the 3D visualization of planets in the solar system. As I am going to apply texture, I have manually computed the texture coordinates (texcoords
), however, I get zig-zag artefacts appeearing in the image.
I believe that my computation might be wrong. I have attached the texcoords
computation below:
# Compute Texture Coordinates
def get_texcoords(vertices):
texcoords = []
for v in vertices:
#thresholding
for i in range(3):
if np.abs(v[i]) > 1e-6:
v[i] = v[i]
elif np.abs(v[i]) < 1e-6:
v[i] = 0.0
# Compute position in uv-space
radius = np.sqrt(v[0]**2 + v[1]**2 + v[2]**2)
latitude = np.arcsin(v[2]/radius)
longitude = np.arctan2(v[1],v[0])
# Convert to texture coordinates
u = round(0.5 + longitude/(2*np.pi),5)
v = round(0.5 + latitude/np.pi,5)
texcoords.append([u,v])
return np.array(texcoords)
Is there any way to remove the artefacts? Or is there a smarter way to obtain texture coordinates in vispy
?
maybe using "for v in vertices:" in conjunction with using texture cordinate variable names to be "u" and "v" ?? I'm not sure it would produce that artifact, but it is definitely not recommended to modify your iterator inside its loop without being extremely cautious. Not recommended in any manner, actually. That's my initial take. I'm using this same algorithm so once I get mine working I'll know if it's the cause of your issue.
I just found this link which explains the error lurking in the uv mapping algorithm: https://mft-dev.dk/uv-mapping-sphere/
In a nutshell, some of the mesh triangles cross the texture boundary, and some of those end up getting 'flipped' (i.e. their face normal points inward rather then outward). The article in the link shows how do find these rogue faces and fixes them.
I have been scratching my head over this one for a day or so.