coolbutuseless / isocubes

Other
66 stars 2 forks source link

Feature: Export to .obj #1

Closed marcosci closed 1 week ago

marcosci commented 2 years ago

Hi there!

Would be really nifty to have the grobs as .obj file to use it in other 3D software :)

No idea if and how this is possible, only know that @tylermorganwall is doing something similar in rayshader:

https://www.rayshader.com/reference/save_obj.html

Cheers

ries9112 commented 2 years ago

+1 to this! I'd like to export it as .vox for my use case, and if I could export to .obj I could convert it to what I need 🙂

trevorld commented 2 years ago

Pretty straightforward to write R code which writes Wavefront OBJ code that generates colored cubes (piecepackr::save_piece_obj() is an example of R code that writes OBJ code for individual textured board game pieces). Simplified hard-coded example for two colored cubes:

obj_text <- "
mtllib cubes.mtl

## geometric vertices (okay to repeat)
# cube 1
# x y z
v 0 1 1
v 0 0 1
v 1 0 1
v 1 1 1
v 0 1 0
v 0 0 0
v 1 0 0
v 1 1 0

# cube 2
# x y z
v 2 1 1
v 2 0 1
v 3 0 1
v 3 1 1
v 2 1 0
v 2 0 0
v 3 0 0
v 3 1 0

## polygonal face elements
# if order vertices counter-clockwise order then no need to mess with normals
g cube_1
usemtl material_1
f 1 2 3 4
f 8 4 3 7
f 1 4 8 5
f 6 5 8 7
f 1 5 6 2
f 3 2 6 7

g cube_2
usemtl material_2
f  9 10 11 12
f 16 12 11 15
f  9 12 16 13
f 14 13 16 15
f  9 13 14 10
f 11 10 14 15
"

mtl_text <- "
# Kd R G B is diffuse color
# RGB colors from 0 to 1---can use `grDevices::col2rgb()`
newmtl material_1
Kd 0.000 0.000 0.000 # black

newmtl material_2
Kd 1.000 1.000 1.000 # white
"

writeLines(obj_text, "cubes.obj")
writeLines(mtl_text, "cubes.mtl")

library("readobj")
library("rgl")
mesh <- readobj::read.obj("cubes.obj", convert.rgl=TRUE)
rgl::shade3d(mesh)
rgl::rgl.snapshot("cubes.png")

cubes

stla commented 1 year ago

But how to get the vertices of the cubes?

trevorld commented 1 year ago

But how to get the vertices of the cubes?

NB. If I remember correctly coord_heightmap() auto-culls any cubes that aren't visible in the target isometric projection so you may want to instead use oblicubes::xyz_heightmap() if your goal is .obj export but I think the sdf_*() functions in this package don't auto-cull the cubes so output from these should still be good for .obj export.

coolbutuseless commented 1 week ago

I've decided that isocubes is only concerned with rendering voxel coordinates to R graphics objects.

converting coordinates to a .obj file is outside the scope of this package, (but seems like it would be a fun project)