Last active
November 10, 2020 22:34
-
-
Save tylermorganwall/1fd7adb32199e3d57d45768b6946f8fb to your computer and use it in GitHub Desktop.
ASCII rayshader + rayrender
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(rayshader) | |
library(crayon) | |
ivory <- make_style("ivory") | |
bgMaroon <- make_style(rgb(0.2,0.2,0.2,0), bg = TRUE) | |
fancy <- combine_styles(ivory, bgMaroon) | |
cat(fancy("This will have some fancy colors"), "\n") | |
montereybay %>% | |
sphere_shade() %>% | |
plot_3d(montereybay,water=TRUE,zscale=30,windowsize = c(80,80),theta=-45) | |
for(i in 1:30) { | |
render_camera(theta=-45 + i*12) | |
snapfile = tempfile() | |
render_snapshot(snapfile) | |
mapfile = rayshader:::fliplr(png::readPNG(paste0(snapfile,".png"))) | |
stringval = c() | |
for(i in seq(2,80,by=2)) { | |
for(j in 1:80) { | |
tempbg = make_style(rgb(mapfile[i,j,1]/1.04,mapfile[i,j,2]/1.04,mapfile[i,j,3]/1.04),bg=TRUE,colors = 256) | |
stringval = c(stringval,tempbg(" ")) | |
} | |
stringval = c(stringval,"\n") | |
} | |
cat(paste0(stringval,collapse = "")) | |
} | |
library(rayrender) | |
generate_cornell2 = function(light = TRUE, lightintensity = 5,lightcolor = "white",lightwidth = 332, lightdepth=343,rot=0) { | |
scene = yz_rect(x=555,y=555/2,z=555/2,555,555, | |
material = lambertian(color = "#1f7326"),flipped = TRUE) %>% | |
add_object(yz_rect(x=0,y=555/2,z=555/2,555,555, | |
material = lambertian(color = "#a60d0d"))) %>% | |
add_object(xz_rect(x=555/2,y=555,z=555/2,555,555, | |
material = lambertian(color="#bababa"),flipped = TRUE)) %>% | |
add_object(xz_rect(x=555/2,y=0,z=555/2,555,555, | |
material = lambertian(color="#bababa"))) %>% | |
add_object(xy_rect(x=555/2,y=555/2,z=555,555,555, | |
material = lambertian(color = "#bababa"),flipped = TRUE)) | |
if(light) { | |
scene = scene %>% | |
add_object(group_objects(xz_rect(x=555/2,y=554,z=555/4,lightdepth,lightwidth, | |
material = lambertian(color=lightcolor, | |
lightintensity=lightintensity,implicit_sample = TRUE), | |
flipped=TRUE),pivot_point = c(555/2,554,555/2), group_angle = c(0,rot,0)) ) | |
} | |
attr(scene,"cornell") = TRUE | |
scene | |
} | |
for(i in 1:30) { | |
scene = generate_cornell2(lightwidth=200,lightdepth=200,rot = 12*i,lightintensity=10) %>% | |
add_object(sphere(x=555/2,y=555/2,z=555/2,radius=555/4)) | |
snapfile = tempfile() | |
render_scene(scene, parallel=TRUE, width=80,height=80,lookfrom = c(278,278,-800), | |
lookat = c(278,278,0), | |
fov= 40, | |
samples = 500,clamp_value = 10,filename=snapfile,progress = FALSE) | |
mapfile = (png::readPNG(paste0(snapfile,".png"))) | |
stringval = c() | |
for(i in seq(2,80,by=2)) { | |
for(j in 1:80) { | |
tempbg = make_style(rgb(mapfile[i,j,1]/1.04,mapfile[i,j,2]/1.04,mapfile[i,j,3]/1.04),bg=TRUE,colors = 256) | |
stringval = c(stringval,tempbg(" ")) | |
} | |
stringval = c(stringval,"\n") | |
} | |
cat(paste0(stringval,collapse = "")) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment