if (FALSE) { # interactive() || identical(Sys.getenv("IN_PKGDOWN"), "true")
# Generate a large checkered sphere as the ground
scene = generate_ground(depth = -0.5,
material = diffuse(color = "white", checkercolor = "darkgreen"))
render_scene(scene, parallel = TRUE, samples = 16, sample_method = "sobol")
# Add a sphere to the center
scene = scene |>
add_object(sphere(x = 0, y = 0, z = 0, radius = 0.5, material = diffuse(color = c(1, 0, 1))))
render_scene(scene, fov = 20, parallel = TRUE, samples = 16)
# Add a marbled cube
scene = scene |>
add_object(cube(x = 1.1, y = 0, z = 0, material = diffuse(noise = 3)))
render_scene(scene, fov = 20, parallel = TRUE, samples = 16)
# Add a metallic gold sphere, using stratified sampling for a higher quality render
# We also add a light, which turns off the default ambient lighting
scene = scene |>
add_object(sphere(x = -1.1, y = 0, z = 0, radius = 0.5,
material = metal(color = "gold", fuzz = 0.1))) |>
add_object(sphere(y=10,z=-13,radius=2,material=light(intensity=40)))
render_scene(scene, fov = 20, parallel = TRUE, samples = 16)
# Lower the number of samples to render more quickly (here, we also use only one core).
render_scene(scene, samples = 4, parallel = FALSE)
# Add a floating R plot using the iris dataset as a png onto a floating 2D rectangle
tempfileplot = tempfile()
png(filename = tempfileplot, height = 400, width = 800)
plot(iris$Petal.Length, iris$Sepal.Width, col = iris$Species, pch = 18, cex = 4)
dev.off()
image_array = aperm(png::readPNG(tempfileplot), c(2, 1, 3))
scene = scene |>
add_object(xy_rect(x = 0, y = 1.1, z = 0, xwidth = 2, angle = c(0, 0, 0),
flipped = TRUE,
material = diffuse(image_texture = image_array)))
render_scene(scene, fov = 20, parallel = TRUE, samples = 16)
# Move the camera
render_scene(scene, lookfrom = c(7, 1.5, 10), lookat = c(0, 0.5, 0), fov = 15, parallel = TRUE)
# Change the background gradient to a firey sky
render_scene(scene, lookfrom = c(7, 1.5, 10), lookat = c(0, 0.5, 0), fov = 15,
backgroundhigh = "orange", backgroundlow = "red", parallel = TRUE,
ambient = TRUE,
samples = 16)
# Increase the aperture to blur objects that are further from the focal plane.
render_scene(scene, lookfrom = c(7, 1.5, 10), lookat = c(0, 0.5, 0), fov = 15,
aperture = 1, parallel = TRUE, samples = 16)
# We can also capture a 360 environment image by setting `fov = 360` (can be used for VR).
# The left edge of the image is directly where the camera is pointing--we point the image
# backwards so the full cornell box is in the "center" of the environment map.
generate_cornell() |>
add_object(ellipsoid(x = 555 / 2, y = 100, z = 555 / 2, a = 50, b = 100, c = 50,
material = metal(color = "lightblue"))) |>
add_object(cube(x = 100, y = 130 / 2, z = 200, xwidth = 130, ywidth = 130, zwidth = 130,
material = diffuse(checkercolor = "purple",
checkerperiod = 30), angle = c(0, 10, 0))) |>
add_object(pig(x = 100, y = 190, z = 200, scale = 40, angle = c(0, 30, 0))) |>
add_object(sphere(x = 420, y = 555 / 8, z = 100, radius = 555 / 8,
material = dielectric(color = "orange"))) |>
add_object(xz_rect(x = 555 / 2, z = 555 / 2, y = 1, xwidth = 555, zwidth = 555,
material = glossy(checkercolor = "white",
checkerperiod = 10, color = "dodgerblue"))) |>
render_scene(lookfrom = c(278, 278, -10), lookat = c(278, 278, -300), clamp_value = 100,
fov = 360, samples = 16, width = 800, height = 800)
# Spin the camera around the scene, decreasing the number of samples to render faster. To make
# an animation, specify the a filename in `render_scene` for each frame and use the `av` package
# or ffmpeg to combine them all into a movie.
t = 1:9
xpos = 10 * sin(t * 18 * pi / 180 + pi / 2)
zpos = 10 * cos(t * 18 * pi / 180 + pi / 2)
image_output = list()
for (i in 1:9) {
image_output[[i]] = render_scene(scene, samples = 16, plot_scene = FALSE,
lookfrom = c(xpos[i], 1.5, zpos[i]), lookat = c(0, 0.5, 0), parallel = TRUE)
}
rayimage::plot_image_grid(image_output, dim = c(3,3) )
}
Run the code above in your browser using DataLab