dmurdoch / rgl2gltf

Read and write .gltf and .glb files in R.
https://dmurdoch.github.io/rgl2gltf
7 stars 0 forks source link

Non-zero 'view3d()' angles "physically" rotating exported glTF meshes #42

Closed trevorld closed 2 years ago

trevorld commented 2 years ago
library("rgl")
library("rgl2gltf")
# same assets from `tile.zip` from https://github.com/dmurdoch/rgl2gltf/issues/38
material <- list(texture = "tile.png", color="white")
mesh <- readOBJ("tile.obj")

open3d(useNULL = FALSE)

view3d(theta = 0, phi = 0)
shade3d(mesh, material = material)
x <- as.gltf(scene3d())
writeGLB(x, "scene_t0_p0.glb")

# Export no longer parallel to plane
view3d(theta = 0, phi = 45)
x <- as.gltf(scene3d())
writeGLB(x, "scene_t0_p45.glb")

view3d(theta = 0, phi = 90)
x <- as.gltf(scene3d())
writeGLB(x, "scene_t0_p90.glb")

Screenshot from Babylon viewer of scene_t0_p45.glb with grid enabled showing that the mesh exported after calling view3d(theta = 0, phi = 45) is "physically" rotated 45 degrees from the plane

screenshot_22-5-23_11-05

dmurdoch commented 2 years ago

That's an issue in rgl: when you rotate the object with the mouse or view3d(), you are actually rotating the object, not your view of it. Normally the viewer stays in a fixed location, and the lights are in a fixed direction.

You can change the observer location using observer3d. Changing the lights would require they be deleted and recreated.

Calling view3d(0,0) before writing to a file would be a good idea if this matters to you. I don't think it should happen automatically in rgl2gltf, because I think most users would want the orientation of the object to be the same as what they saw in R. I don't know if this happens with external viewers, but I try to make it happen if you export and import the scene back into R.

trevorld commented 2 years ago

Thanks for the explanation!

I don't know if this happens with external viewers, but I try to make it happen if you export and import the scene back into R.

I have observed that this does indeed happen with many external glTF viewers

Calling view3d(0,0) before writing to a file would be a good idea if this matters to you. I don't think it should happen automatically in rgl2gltf, because I think most users would want the orientation of the object to be the same as what they saw in R.

Okay, this matters for my intended use cases (creating visual assets for import into Godot and Unity that may be mixed with other external assets) but manually adding in a view3d(0,0) is a pretty easy thing to do.