Skip to content

Instantly share code, notes, and snippets.

@tylermorganwall
Created February 7, 2022 22:53
Show Gist options
  • Save tylermorganwall/688ec524a58a32694cd691c981067b7c to your computer and use it in GitHub Desktop.
Save tylermorganwall/688ec524a58a32694cd691c981067b7c to your computer and use it in GitHub Desktop.
JWST rendered with ambient occlusion in R with rayrender
#JWST OBJ file located at www.tylermw.com/webb.zip
#Parse OBJ MTL file to read and write red color for highlighted component
mtl_file = readLines("jwebb.mtl")
kd_entries = (grepl("Kd",mtl_file,fixed=T))
kd_ind = which(grepl("Kd",mtl_file,fixed=T))
#Set all to white
mtl_file[kd_entries] = "Kd 1.000000 1.000000 1.000000"
#Indices of certain components
groups = list()
groups[[1]] = c(39, 40) #Sunshield
groups[[2]] = c(22) #mirror
groups[[3]] = c(3,44) #Sensor package
groups[[4]] = c(17) #Momentum
groups[[5]] = c(29) #Solar Panel
#Camera location positions
lookfromlist = list()
lookfromlist[[1]] =c(0,3,-7)
lookfromlist[[2]] =c(7*sinpi(235/180),2, 7*cospi(235/180))
lookfromlist[[3]] =c(7*sinpi(235+170/180),2, 7*cospi(235+170/180))
lookfromlist[[4]] =c(7*sinpi(235+230/180),0, 7*cospi(235+230/180))
lookfromlist[[5]] =c(7*sinpi(235+250/180),-2,7*cospi(235+250/180))
#Generate camera motion
motion = generate_camera_motion(do.call(rbind,lookfromlist), lookats = matrix(c(0,0.5,0),nrow=5,ncol=3,byrow=T),
type="cubic",frames=60, closed=T)
#Start and end frames for switching featured component
start_frame = c(1,37,109,181,253, 325)
end_frame = c(36,109,181,253,325, 360)
#Loop through component groups
for(i in 1:6) {
if(i < 6) {
mtl_file[kd_ind[groups[[i]] ] ] = "Kd 1.000000 0.000000 0.000000"
writeLines(mtl_file, con = "~/Desktop/jwebb_c.mtl")
mtl_file[kd_entries] = "Kd 1.000000 1.000000 1.000000"
obj_model("~/Desktop/jwebb.obj", texture=T, scale_obj=2/1000, angle=c(0,45,0)) |>
render_animation(motion, samples = 256, ambient_occlusion = TRUE, backgroundhigh = "grey10",
keep_colors = TRUE,width=1000, height=1000, filename = "webbcam",
start_frame = start_frame[i], end_frame = end_frame[i])
} else {
mtl_file[kd_ind[groups[[1]] ] ] = "Kd 1.000000 0.000000 0.000000"
writeLines(mtl_file, con = "~/Desktop/jwebb_c.mtl")
mtl_file[kd_entries] = "Kd 1.000000 1.000000 1.000000"
obj_model("~/Desktop/jwebb.obj", texture=T, scale_obj=2/1000, angle=c(0,45,0)) |>
render_animation(motion, samples = 256, ambient_occlusion = TRUE, backgroundhigh = "grey10",
keep_colors = TRUE, width=1000, height=1000, filename = "webbcam",
start_frame = start_frame[i], end_frame = end_frame[i])
}
}
#Post-processing title function
add_jwst_title = function(title, i) {
rayimage::add_title(sprintf("webbcam%d.png",i), title_text = title,
title_size = 60, title_color = "red",title_bar_color = "black",
title_position = "northeast", title_offset = c(15,15)) |>
rayimage::add_title(title_text = "James Webb Space Telescope\nA Mini-Tour",
title_size = 30, title_color = "white",title_bar_color = NULL,
title_position = "northwest", title_offset = c(15,10), filename = sprintf("webbcamtitle%d.png",i))
}
#Add title with rayimage
for(i in 1:360) {
if(i <= 36 || i >= 325) {
add_jwst_title("Sunshield", i)
}
if(i > 36 && i < 109) {
add_jwst_title("Primary Mirror", i)
}
if(i >= 109 && i < 181) {
add_jwst_title("Instrument Module", i)
}
if(i >= 181 && i < 253) {
add_jwst_title("Momentum Flap",i)
}
if(i >= 253 && i < 325) {
add_jwst_title("Solar Array", i)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment