| Title: | 3D Software Rasterizer |
|---|---|
| Description: | Rasterize images using a 3D software renderer. 3D scenes are created either by importing external files, building scenes out of the included objects, or by constructing meshes manually. Supports point and directional lights, anti-aliased lines, shadow mapping, transparent objects, translucent objects, multiple materials types, reflection, refraction, environment maps, multicore rendering, bloom, tone-mapping, and screen-space ambient occlusion. |
| Authors: | Tyler Morgan-Wall [aut, cph, cre] (ORCID: <https://orcid.org/0000-0002-3131-3814>), Syoyo Fujita [ctb, cph], Vilya Harvey [ctb, cph], G-Truc Creation [ctb, cph] |
| Maintainer: | Tyler Morgan-Wall <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.16.1 |
| Built: | 2026-09-27 19:05:05 UTC |
| Source: | https://github.com/tylermorganwall/rayvertex |
Add light
add_light(lights, light)add_light(lights, light)
lights |
Current light scene. |
light |
New light to add. |
A matrix representing the light information.
#Add a light to scene (manually specify the light automatically added to the Cornell Box lights = point_light(position=c(555/2,450,555/2), falloff_quad = 0.0, constant = 0.0002, falloff = 0.005) generate_cornell_mesh(light=FALSE) |> rasterize_scene(light_info = lights) #Add directional lights and a point light lights_d = add_light(lights, directional_light(direction=c(1,1.5,-1), intensity=0.2)) |> add_light(directional_light(direction=c(-1,1.5,-1),color="red", intensity=0.2)) |> add_light(point_light(position=c(555/2,50,555/2), color="blue", intensity=0.3, falloff_quad = 0.0, constant = 0.0002, falloff = 0.005)) generate_cornell_mesh(light=FALSE) |> rasterize_scene(light_info = lights_d)#Add a light to scene (manually specify the light automatically added to the Cornell Box lights = point_light(position=c(555/2,450,555/2), falloff_quad = 0.0, constant = 0.0002, falloff = 0.005) generate_cornell_mesh(light=FALSE) |> rasterize_scene(light_info = lights) #Add directional lights and a point light lights_d = add_light(lights, directional_light(direction=c(1,1.5,-1), intensity=0.2)) |> add_light(directional_light(direction=c(-1,1.5,-1),color="red", intensity=0.2)) |> add_light(point_light(position=c(555/2,50,555/2), color="blue", intensity=0.3, falloff_quad = 0.0, constant = 0.0002, falloff = 0.005)) generate_cornell_mesh(light=FALSE) |> rasterize_scene(light_info = lights_d)
Add Line
add_lines(lines, line)add_lines(lines, line)
lines |
Existing lines or empty (0-row) matrix. |
line |
Line to add, generated with |
New line matrix.
#Generate a cube out of lines cube_outline = generate_line(start = c(-1, -1, -1), end = c(-1, -1, 1)) |> add_lines(generate_line(start = c(-1, -1, -1), end = c(-1, 1, -1))) |> add_lines(generate_line(start = c(-1, -1, -1), end = c(1, -1, -1))) |> add_lines(generate_line(start = c(-1, -1, 1), end = c(-1, 1, 1))) |> add_lines(generate_line(start = c(-1, -1, 1), end = c(1, -1, 1))) |> add_lines(generate_line(start = c(-1, 1, 1), end = c(-1, 1, -1))) |> add_lines(generate_line(start = c(-1, 1, 1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(1, 1, -1), end = c(1, -1, -1))) |> add_lines(generate_line(start = c(1, 1, -1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(1, -1, -1), end = c(1, -1, 1))) |> add_lines(generate_line(start = c(1, -1, 1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(-1, 1, -1), end = c(1, 1, -1))) rasterize_lines(cube_outline,fov=90,lookfrom=c(0,0,3))#Generate a cube out of lines cube_outline = generate_line(start = c(-1, -1, -1), end = c(-1, -1, 1)) |> add_lines(generate_line(start = c(-1, -1, -1), end = c(-1, 1, -1))) |> add_lines(generate_line(start = c(-1, -1, -1), end = c(1, -1, -1))) |> add_lines(generate_line(start = c(-1, -1, 1), end = c(-1, 1, 1))) |> add_lines(generate_line(start = c(-1, -1, 1), end = c(1, -1, 1))) |> add_lines(generate_line(start = c(-1, 1, 1), end = c(-1, 1, -1))) |> add_lines(generate_line(start = c(-1, 1, 1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(1, 1, -1), end = c(1, -1, -1))) |> add_lines(generate_line(start = c(1, 1, -1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(1, -1, -1), end = c(1, -1, 1))) |> add_lines(generate_line(start = c(1, -1, 1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(-1, 1, -1), end = c(1, 1, -1))) rasterize_lines(cube_outline,fov=90,lookfrom=c(0,0,3))
Applies a planar UV mapping to a mesh based on a given direction and set of U/V vectors.
If full_mesh_bbox is true, the UV mapping is scaled based on the bounding box
of the entire mesh. If false, each shape's bounding box is used.
One of direction/u/v must be NULL and will be calculated from the others.
add_plane_uv_mesh( mesh, direction = c(0, 1, 0), u = NULL, v = NULL, override_existing = FALSE, full_mesh_bbox = TRUE )add_plane_uv_mesh( mesh, direction = c(0, 1, 0), u = NULL, v = NULL, override_existing = FALSE, full_mesh_bbox = TRUE )
mesh |
The mesh to which the UV mapping will be applied. |
direction |
Default |
u |
Default |
v |
Default |
override_existing |
Default |
full_mesh_bbox |
Default |
Modified mesh with added UV mapping.
#Let's construct a mesh from the volcano dataset #Build the vertex matrix vertex_list = list() counter = 1 for(i in 1:nrow(volcano)) { for(j in 1:ncol(volcano)) { vertex_list[[counter]] = matrix(c(j,volcano[i,j]/3,i), ncol=3) counter = counter + 1 } } vertices = do.call(rbind,vertex_list) #Build the index matrix index_list = list() counter = 0 for(i in 1:(nrow(volcano)-1)) { for(j in 1:(ncol(volcano)-1)) { index_list[[counter+1]] = matrix(c(counter,counter+ncol(volcano),counter+1, counter+ncol(volcano),counter+ncol(volcano)+1,counter + 1), nrow=2, ncol=3, byrow=TRUE) counter = counter + 1 } counter = counter + 1 } indices = do.call("rbind",index_list) #Create a checkerboard image create_checkerboard_texture = function(filename, n = 16) { old_par = par(no.readonly = TRUE) on.exit(par(old_par)) plot.new() par(mar = c(0, 0, 0, 0)) checkerboard = matrix(c(1, 0), nrow = n+1, ncol = n) png(filename, width = 800, height = 800) image(1:(n+1), 1:n, checkerboard, col = c("dodgerblue", "red"), axes = FALSE, xlab = "", ylab = "") dev.off() } checkerboard_file = tempfile(fileext = ".png") create_checkerboard_texture(checkerboard_file) rayimage::plot_image(checkerboard_file) #Construct the mesh volc_mesh = construct_mesh(vertices = vertices, indices = indices, material = material_list(type="phong", diffuse="darkred", ambient = "darkred", ambient_intensity=0.2)) #Set the direction so that the checkerboard will be mapped to the surface like a carpet uv = add_plane_uv_mesh(volc_mesh, direction=c(0,200,0), u = c(1,0,0)) uv = set_material(uv, texture_location = checkerboard_file, ambient = "white", ambient_intensity=0.1) #Rasterize the scene rasterize_scene(center_mesh(uv), lookfrom=c(200,200,200),fov=0,width=1200,height=1200, light_info = directional_light(c(0,1,1)) |> add_light(directional_light(c(1,1,-1))),ortho_dimensions=c(120,120)) #Set the direction so that the checkerboard will be mapped directly at the camera uv = add_plane_uv_mesh(volc_mesh, direction=c(200,200,200), v = c(-1,1,-1)) uv = set_material(uv, texture_location = checkerboard_file, ambient = "white", ambient_intensity=0.1) #Rasterize the scene rasterize_scene(center_mesh(uv), lookfrom=c(200,200,200),fov=0,width=1200,height=1200, light_info = directional_light(c(0,1,1)) |> add_light(directional_light(c(1,1,-1))), ortho_dimensions=c(120,120))#Let's construct a mesh from the volcano dataset #Build the vertex matrix vertex_list = list() counter = 1 for(i in 1:nrow(volcano)) { for(j in 1:ncol(volcano)) { vertex_list[[counter]] = matrix(c(j,volcano[i,j]/3,i), ncol=3) counter = counter + 1 } } vertices = do.call(rbind,vertex_list) #Build the index matrix index_list = list() counter = 0 for(i in 1:(nrow(volcano)-1)) { for(j in 1:(ncol(volcano)-1)) { index_list[[counter+1]] = matrix(c(counter,counter+ncol(volcano),counter+1, counter+ncol(volcano),counter+ncol(volcano)+1,counter + 1), nrow=2, ncol=3, byrow=TRUE) counter = counter + 1 } counter = counter + 1 } indices = do.call("rbind",index_list) #Create a checkerboard image create_checkerboard_texture = function(filename, n = 16) { old_par = par(no.readonly = TRUE) on.exit(par(old_par)) plot.new() par(mar = c(0, 0, 0, 0)) checkerboard = matrix(c(1, 0), nrow = n+1, ncol = n) png(filename, width = 800, height = 800) image(1:(n+1), 1:n, checkerboard, col = c("dodgerblue", "red"), axes = FALSE, xlab = "", ylab = "") dev.off() } checkerboard_file = tempfile(fileext = ".png") create_checkerboard_texture(checkerboard_file) rayimage::plot_image(checkerboard_file) #Construct the mesh volc_mesh = construct_mesh(vertices = vertices, indices = indices, material = material_list(type="phong", diffuse="darkred", ambient = "darkred", ambient_intensity=0.2)) #Set the direction so that the checkerboard will be mapped to the surface like a carpet uv = add_plane_uv_mesh(volc_mesh, direction=c(0,200,0), u = c(1,0,0)) uv = set_material(uv, texture_location = checkerboard_file, ambient = "white", ambient_intensity=0.1) #Rasterize the scene rasterize_scene(center_mesh(uv), lookfrom=c(200,200,200),fov=0,width=1200,height=1200, light_info = directional_light(c(0,1,1)) |> add_light(directional_light(c(1,1,-1))),ortho_dimensions=c(120,120)) #Set the direction so that the checkerboard will be mapped directly at the camera uv = add_plane_uv_mesh(volc_mesh, direction=c(200,200,200), v = c(-1,1,-1)) uv = set_material(uv, texture_location = checkerboard_file, ambient = "white", ambient_intensity=0.1) #Rasterize the scene rasterize_scene(center_mesh(uv), lookfrom=c(200,200,200),fov=0,width=1200,height=1200, light_info = directional_light(c(0,1,1)) |> add_light(directional_light(c(1,1,-1))), ortho_dimensions=c(120,120))
Add shape to the scene.
add_shape(scene, shape = NULL)add_shape(scene, shape = NULL)
scene |
The scene to add the shape. |
shape |
The mesh to add to the scene. |
Scene with shape added.
#Generate several spheres in the cornell box scene = generate_cornell_mesh() set.seed(1) for(i in 1:30) { col = hsv(runif(1)) scene = add_shape(scene, sphere_mesh(position=runif(3)*400+155/2, material=material_list(diffuse=col, type="phong", ambient=col,ambient_intensity=0.2), radius=30)) } rasterize_scene(scene, light_info=directional_light(direction=c(0.1,0.6,-1)))#Generate several spheres in the cornell box scene = generate_cornell_mesh() set.seed(1) for(i in 1:30) { col = hsv(runif(1)) scene = add_shape(scene, sphere_mesh(position=runif(3)*400+155/2, material=material_list(diffuse=col, type="phong", ambient=col,ambient_intensity=0.2), radius=30)) } rasterize_scene(scene, light_info=directional_light(direction=c(0.1,0.6,-1)))
Applies a planar UV mapping to a mesh based on a spherical direction from the origin.
add_sphere_uv_mesh(mesh, origin = c(0, 0, 0), override_existing = FALSE)add_sphere_uv_mesh(mesh, origin = c(0, 0, 0), override_existing = FALSE)
mesh |
The mesh to which the UV mapping will be applied. |
origin |
Default |
override_existing |
Default |
Modified mesh with added UV mapping.
#Let's construct a mesh from the volcano dataset#Let's construct a mesh from the volcano dataset
Arrow 3D Model
arrow_mesh( start = c(0, 0, 0), end = c(0, 1, 0), radius_top = 0.5, radius_tail = 0.25, tail_proportion = 0.5, direction = NA, from_center = TRUE, material = material_list() )arrow_mesh( start = c(0, 0, 0), end = c(0, 1, 0), radius_top = 0.5, radius_tail = 0.25, tail_proportion = 0.5, direction = NA, from_center = TRUE, material = material_list() )
start |
Default |
end |
Default |
radius_top |
Default |
radius_tail |
Default |
tail_proportion |
Default |
direction |
Default |
from_center |
Default |
material |
Default |
List describing the mesh.
#Generate an arrow generate_cornell_mesh() |> add_shape(arrow_mesh(start = c(555/2, 20, 555/2), end = c(555/2, 300, 555/2), radius_tail=50, radius_top = 100, material = material_list(diffuse="dodgerblue"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1))) #Generate a blue arrow with a wide tail generate_cornell_mesh() |> add_shape(arrow_mesh(start = c(555/2, 20, 555/2), end = c(555/2, 300, 555/2), radius_tail=100, radius_top = 150, material = material_list(diffuse="dodgerblue"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1))) #Generate a long, thin arrow and change the proportions generate_cornell_mesh() |> add_shape(arrow_mesh(start = c(555/2, 20, 555/2), end = c(555/2, 400, 555/2), radius_top=30, radius_tail = 10, tail_proportion = 0.8, material = material_list(diffuse="dodgerblue"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1))) #Change the start and end points generate_cornell_mesh() |> add_shape(arrow_mesh(start = c(500, 20, 555/2), end = c(50, 500, 555/2), radius_top=30, radius_tail = 10, tail_proportion = 0.8, material = material_list(diffuse="dodgerblue"))) |> add_shape(arrow_mesh(start = c(500, 500, 500), end = c(50, 50, 50), radius_top=30, radius_tail = 10, tail_proportion = 0.8, material = material_list(diffuse="red"))) |> add_shape(arrow_mesh(start = c(555/2, 50, 500), end = c(555/2, 50, 50), radius_top=30, radius_tail = 10, tail_proportion = 0.8, material = material_list(diffuse="green"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1)))#Generate an arrow generate_cornell_mesh() |> add_shape(arrow_mesh(start = c(555/2, 20, 555/2), end = c(555/2, 300, 555/2), radius_tail=50, radius_top = 100, material = material_list(diffuse="dodgerblue"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1))) #Generate a blue arrow with a wide tail generate_cornell_mesh() |> add_shape(arrow_mesh(start = c(555/2, 20, 555/2), end = c(555/2, 300, 555/2), radius_tail=100, radius_top = 150, material = material_list(diffuse="dodgerblue"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1))) #Generate a long, thin arrow and change the proportions generate_cornell_mesh() |> add_shape(arrow_mesh(start = c(555/2, 20, 555/2), end = c(555/2, 400, 555/2), radius_top=30, radius_tail = 10, tail_proportion = 0.8, material = material_list(diffuse="dodgerblue"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1))) #Change the start and end points generate_cornell_mesh() |> add_shape(arrow_mesh(start = c(500, 20, 555/2), end = c(50, 500, 555/2), radius_top=30, radius_tail = 10, tail_proportion = 0.8, material = material_list(diffuse="dodgerblue"))) |> add_shape(arrow_mesh(start = c(500, 500, 500), end = c(50, 50, 50), radius_top=30, radius_tail = 10, tail_proportion = 0.8, material = material_list(diffuse="red"))) |> add_shape(arrow_mesh(start = c(555/2, 50, 500), end = c(555/2, 50, 50), radius_top=30, radius_tail = 10, tail_proportion = 0.8, material = material_list(diffuse="green"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1)))
Centers the mesh at the origin.
center_mesh(mesh)center_mesh(mesh)
mesh |
The mesh object. |
Centered mesh
#Center the Cornell box and the R OBJ at the origin center_mesh(generate_cornell_mesh()) |> add_shape(center_mesh(obj_mesh(r_obj(),scale=100,angle=c(0,180,0)))) |> rasterize_scene(lookfrom=c(0,0,-1100),fov=40,lookat=c(0,0,0), light_info = directional_light(c(0.4,0.4,-1)) |> add_light(point_light(c(0,450,0), falloff_quad = 0.0, constant = 0.0002, falloff = 0.005)))#Center the Cornell box and the R OBJ at the origin center_mesh(generate_cornell_mesh()) |> add_shape(center_mesh(obj_mesh(r_obj(),scale=100,angle=c(0,180,0)))) |> rasterize_scene(lookfrom=c(0,0,-1100),fov=40,lookat=c(0,0,0), light_info = directional_light(c(0.4,0.4,-1)) |> add_light(point_light(c(0,450,0), falloff_quad = 0.0, constant = 0.0002, falloff = 0.005)))
Change individual material properties, leaving others alone.
change_material( mesh, id = NULL, sub_id = 1, diffuse = NULL, ambient = NULL, specular = NULL, transmittance = NULL, emission = NULL, shininess = NULL, ior = NULL, dissolve = NULL, illum = NULL, texture_location = NULL, normal_texture_location = NULL, bump_texture_location = NULL, specular_texture_location = NULL, ambient_texture_location = NULL, emissive_texture_location = NULL, diffuse_intensity = NULL, bump_intensity = NULL, specular_intensity = NULL, emission_intensity = NULL, ambient_intensity = NULL, culling = NULL, type = NULL, translucent = NULL, toon_levels = NULL, toon_outline_width = NULL, toon_outline_color = NULL, reflection_intensity = NULL, reflection_sharpness = NULL, two_sided = NULL, sigma = NULL )change_material( mesh, id = NULL, sub_id = 1, diffuse = NULL, ambient = NULL, specular = NULL, transmittance = NULL, emission = NULL, shininess = NULL, ior = NULL, dissolve = NULL, illum = NULL, texture_location = NULL, normal_texture_location = NULL, bump_texture_location = NULL, specular_texture_location = NULL, ambient_texture_location = NULL, emissive_texture_location = NULL, diffuse_intensity = NULL, bump_intensity = NULL, specular_intensity = NULL, emission_intensity = NULL, ambient_intensity = NULL, culling = NULL, type = NULL, translucent = NULL, toon_levels = NULL, toon_outline_width = NULL, toon_outline_color = NULL, reflection_intensity = NULL, reflection_sharpness = NULL, two_sided = NULL, sigma = NULL )
mesh |
Mesh to change. |
id |
Default |
sub_id |
Default |
diffuse |
Default |
ambient |
Default |
specular |
Default |
transmittance |
Default |
emission |
Default |
shininess |
Default |
ior |
Default |
dissolve |
Default |
illum |
Default |
texture_location |
Default |
normal_texture_location |
Default |
bump_texture_location |
Default |
specular_texture_location |
Default |
ambient_texture_location |
Default |
emissive_texture_location |
Default |
diffuse_intensity |
Default |
bump_intensity |
Default |
specular_intensity |
Default |
emission_intensity |
Default |
ambient_intensity |
Default |
culling |
Default |
type |
Default |
translucent |
Default |
toon_levels |
Default |
toon_outline_width |
Default |
toon_outline_color |
Default |
reflection_intensity |
Default |
reflection_sharpness |
Default |
two_sided |
Default |
sigma |
Default |
Shape with new material settings
p_sphere = sphere_mesh(position=c(555/2,555/2,555/2), radius=40,material=material_list(diffuse="purple")) generate_cornell_mesh() |> add_shape(translate_mesh(p_sphere,c(0,-100,0))) |> add_shape(change_material(translate_mesh(p_sphere,c(200,-100,0)),diffuse="red")) |> add_shape(change_material(translate_mesh(p_sphere,c(100,-100,0)),dissolve=0.5)) |> add_shape(change_material(translate_mesh(p_sphere,c(-100,-100,0)),type="phong")) |> add_shape(change_material(translate_mesh(p_sphere,c(-200,-100,0)),type="phong",shininess=30)) |> rasterize_scene(light_info=directional_light(direction=c(0.1,0.6,-1))) #Change several shapes at once p_sphere |> add_shape(change_material(translate_mesh(p_sphere,c(200,0,0)),diffuse="red")) |> add_shape(change_material(translate_mesh(p_sphere,c(100,0,0)),dissolve=0.5)) |> add_shape(change_material(translate_mesh(p_sphere,c(-100,0,0)),type="phong")) |> add_shape(change_material(translate_mesh(p_sphere,c(-200,0,0)),type="phong",shininess=30)) |> change_material(diffuse = "red") |> add_shape(generate_cornell_mesh()) |> rasterize_scene(light_info=directional_light(direction=c(0.1,0.6,-1)))p_sphere = sphere_mesh(position=c(555/2,555/2,555/2), radius=40,material=material_list(diffuse="purple")) generate_cornell_mesh() |> add_shape(translate_mesh(p_sphere,c(0,-100,0))) |> add_shape(change_material(translate_mesh(p_sphere,c(200,-100,0)),diffuse="red")) |> add_shape(change_material(translate_mesh(p_sphere,c(100,-100,0)),dissolve=0.5)) |> add_shape(change_material(translate_mesh(p_sphere,c(-100,-100,0)),type="phong")) |> add_shape(change_material(translate_mesh(p_sphere,c(-200,-100,0)),type="phong",shininess=30)) |> rasterize_scene(light_info=directional_light(direction=c(0.1,0.6,-1))) #Change several shapes at once p_sphere |> add_shape(change_material(translate_mesh(p_sphere,c(200,0,0)),diffuse="red")) |> add_shape(change_material(translate_mesh(p_sphere,c(100,0,0)),dissolve=0.5)) |> add_shape(change_material(translate_mesh(p_sphere,c(-100,0,0)),type="phong")) |> add_shape(change_material(translate_mesh(p_sphere,c(-200,0,0)),type="phong",shininess=30)) |> change_material(diffuse = "red") |> add_shape(generate_cornell_mesh()) |> rasterize_scene(light_info=directional_light(direction=c(0.1,0.6,-1)))
Color Lines
color_lines(lines, color = "white")color_lines(lines, color = "white")
lines |
The line scene. |
color |
Default |
Colored line matrix.
#Generate a cube out of lines cube_outline = generate_line(start = c(-1, -1, -1), end = c(-1, -1, 1)) |> add_lines(generate_line(start = c(-1, -1, -1), end = c(-1, 1, -1))) |> add_lines(generate_line(start = c(-1, -1, -1), end = c(1, -1, -1))) |> add_lines(generate_line(start = c(-1, -1, 1), end = c(-1, 1, 1))) |> add_lines(generate_line(start = c(-1, -1, 1), end = c(1, -1, 1))) |> add_lines(generate_line(start = c(-1, 1, 1), end = c(-1, 1, -1))) |> add_lines(generate_line(start = c(-1, 1, 1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(1, 1, -1), end = c(1, -1, -1))) |> add_lines(generate_line(start = c(1, 1, -1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(1, -1, -1), end = c(1, -1, 1))) |> add_lines(generate_line(start = c(1, -1, 1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(-1, 1, -1), end = c(1, 1, -1))) cube_outline |> color_lines(color="red") |> rasterize_lines()#Generate a cube out of lines cube_outline = generate_line(start = c(-1, -1, -1), end = c(-1, -1, 1)) |> add_lines(generate_line(start = c(-1, -1, -1), end = c(-1, 1, -1))) |> add_lines(generate_line(start = c(-1, -1, -1), end = c(1, -1, -1))) |> add_lines(generate_line(start = c(-1, -1, 1), end = c(-1, 1, 1))) |> add_lines(generate_line(start = c(-1, -1, 1), end = c(1, -1, 1))) |> add_lines(generate_line(start = c(-1, 1, 1), end = c(-1, 1, -1))) |> add_lines(generate_line(start = c(-1, 1, 1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(1, 1, -1), end = c(1, -1, -1))) |> add_lines(generate_line(start = c(1, 1, -1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(1, -1, -1), end = c(1, -1, 1))) |> add_lines(generate_line(start = c(1, -1, 1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(-1, 1, -1), end = c(1, 1, -1))) cube_outline |> color_lines(color="red") |> rasterize_lines()
Cone 3D Model
cone_mesh( start = c(0, 0, 0), end = c(0, 1, 0), radius = 0.5, direction = NA, from_center = FALSE, material = material_list() )cone_mesh( start = c(0, 0, 0), end = c(0, 1, 0), radius = 0.5, direction = NA, from_center = FALSE, material = material_list() )
start |
Default |
end |
Default |
radius |
Default |
direction |
Default |
from_center |
Default |
material |
Default |
List describing the mesh.
#Generate a cone generate_cornell_mesh() |> add_shape(cone_mesh(start = c(555/2, 20, 555/2), end = c(555/2, 300, 555/2), radius = 100)) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1))) #Generate a blue cone with a wide base generate_cornell_mesh() |> add_shape(cone_mesh(start = c(555/2, 20, 555/2), end = c(555/2, 300, 555/2), radius=200, material = material_list(diffuse="dodgerblue"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1))) #Generate a long, thin cone generate_cornell_mesh() |> add_shape(cone_mesh(start = c(555/2, 20, 555/2), end = c(555/2, 400, 555/2), radius=50, material = material_list(diffuse="dodgerblue"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1)))#Generate a cone generate_cornell_mesh() |> add_shape(cone_mesh(start = c(555/2, 20, 555/2), end = c(555/2, 300, 555/2), radius = 100)) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1))) #Generate a blue cone with a wide base generate_cornell_mesh() |> add_shape(cone_mesh(start = c(555/2, 20, 555/2), end = c(555/2, 300, 555/2), radius=200, material = material_list(diffuse="dodgerblue"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1))) #Generate a long, thin cone generate_cornell_mesh() |> add_shape(cone_mesh(start = c(555/2, 20, 555/2), end = c(555/2, 400, 555/2), radius=50, material = material_list(diffuse="dodgerblue"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1)))
Manually construct a mesh
construct_mesh( vertices, indices, normals = NULL, norm_indices = NULL, texcoords = NULL, tex_indices = NULL, material = material_list() )construct_mesh( vertices, indices, normals = NULL, norm_indices = NULL, texcoords = NULL, tex_indices = NULL, material = material_list() )
vertices |
Nx3 matrix of vertex coordinates.. |
indices |
Nx3 integer matrix, where each row defines a triangle using the
vertices defined in |
normals |
Default |
norm_indices |
Nx3 integer matrix, where each row defines the normal for a vertex using the
normals defined in |
texcoords |
Default |
tex_indices |
Nx3 integer matrix, where each row defines the texture coordinates for a triangle
using the tex coords defined in |
material |
Default |
List containing mesh info.
#Let's construct a mesh from the volcano dataset #Build the vertex matrix vertex_list = list() counter = 1 for(i in 1:nrow(volcano)) { for(j in 1:ncol(volcano)) { vertex_list[[counter]] = matrix(c(j,volcano[i,j],i), ncol=3) counter = counter + 1 } } vertices = do.call(rbind,vertex_list) #Build the index matrix index_list = list() counter = 0 for(i in 1:(nrow(volcano)-1)) { for(j in 1:(ncol(volcano)-1)) { index_list[[counter+1]] = matrix(c(counter,counter+ncol(volcano),counter+1, counter+ncol(volcano),counter+ncol(volcano)+1,counter + 1), nrow=2, ncol=3, byrow=TRUE) counter = counter + 1 } counter = counter + 1 } indices = do.call(rbind,index_list) #Construct the mesh volc_mesh = construct_mesh(vertices = vertices, indices = indices, material = material_list(type="phong", diffuse="darkred", ambient = "darkred", ambient_intensity=0.2)) #Rasterize the scene rasterize_scene(volc_mesh, lookfrom=c(-50,230,100),fov=60,width=1200,height=1200, light_info = directional_light(c(0,1,1)) |> add_light(directional_light(c(1,1,-1))))#Let's construct a mesh from the volcano dataset #Build the vertex matrix vertex_list = list() counter = 1 for(i in 1:nrow(volcano)) { for(j in 1:ncol(volcano)) { vertex_list[[counter]] = matrix(c(j,volcano[i,j],i), ncol=3) counter = counter + 1 } } vertices = do.call(rbind,vertex_list) #Build the index matrix index_list = list() counter = 0 for(i in 1:(nrow(volcano)-1)) { for(j in 1:(ncol(volcano)-1)) { index_list[[counter+1]] = matrix(c(counter,counter+ncol(volcano),counter+1, counter+ncol(volcano),counter+ncol(volcano)+1,counter + 1), nrow=2, ncol=3, byrow=TRUE) counter = counter + 1 } counter = counter + 1 } indices = do.call(rbind,index_list) #Construct the mesh volc_mesh = construct_mesh(vertices = vertices, indices = indices, material = material_list(type="phong", diffuse="darkred", ambient = "darkred", ambient_intensity=0.2)) #Rasterize the scene rasterize_scene(volc_mesh, lookfrom=c(-50,230,100),fov=60,width=1200,height=1200, light_info = directional_light(c(0,1,1)) |> add_light(directional_light(c(1,1,-1))))
3D obj model of the letter R
cube_mesh( position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), material = material_list() )cube_mesh( position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), material = material_list() )
position |
Default |
scale |
Default |
angle |
Default |
pivot_point |
Default |
order_rotation |
Default |
material |
Default |
List describing the mesh.
#Generate a cube generate_cornell_mesh() |> add_shape(cube_mesh(position = c(555/2, 100, 555/2), scale = 100)) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1))) #Generate a blue rotated cube generate_cornell_mesh() |> add_shape(cube_mesh(position = c(555/2, 100, 555/2), scale = 100, angle=c(0,45,0), material = material_list(diffuse="dodgerblue"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1))) #Generate a scaled, blue rotated cube generate_cornell_mesh() |> add_shape(cube_mesh(position = c(555/2, 100, 555/2), angle=c(0,45,0), scale = c(2,0.5,0.8)*100, material = material_list(diffuse="dodgerblue"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1)))#Generate a cube generate_cornell_mesh() |> add_shape(cube_mesh(position = c(555/2, 100, 555/2), scale = 100)) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1))) #Generate a blue rotated cube generate_cornell_mesh() |> add_shape(cube_mesh(position = c(555/2, 100, 555/2), scale = 100, angle=c(0,45,0), material = material_list(diffuse="dodgerblue"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1))) #Generate a scaled, blue rotated cube generate_cornell_mesh() |> add_shape(cube_mesh(position = c(555/2, 100, 555/2), angle=c(0,45,0), scale = c(2,0.5,0.8)*100, material = material_list(diffuse="dodgerblue"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1)))
Cylinder 3D Model
cylinder_mesh( position = c(0, 0, 0), radius = 0.5, length = 1, angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), material = material_list() )cylinder_mesh( position = c(0, 0, 0), radius = 0.5, length = 1, angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), material = material_list() )
position |
Default |
radius |
Default |
length |
Default |
angle |
Default |
pivot_point |
Default |
order_rotation |
Default |
material |
Default |
List describing the mesh.
#Generate a cylinder generate_cornell_mesh() |> add_shape(cylinder_mesh(position=c(555/2,150,555/2), radius = 50, length=300, material = material_list(diffuse="purple"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1))) #Generate a wide, thin disk generate_cornell_mesh() |> add_shape(cylinder_mesh(position=c(555/2,20,555/2), radius = 200, length=5, material = material_list(diffuse="purple"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1))) #Generate a narrow cylinder generate_cornell_mesh() |> add_shape(cylinder_mesh(position=c(555/2,555/2,555/2),angle=c(45,-45,0), radius = 10, length=500, material = material_list(diffuse="purple"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1)))#Generate a cylinder generate_cornell_mesh() |> add_shape(cylinder_mesh(position=c(555/2,150,555/2), radius = 50, length=300, material = material_list(diffuse="purple"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1))) #Generate a wide, thin disk generate_cornell_mesh() |> add_shape(cylinder_mesh(position=c(555/2,20,555/2), radius = 200, length=5, material = material_list(diffuse="purple"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1))) #Generate a narrow cylinder generate_cornell_mesh() |> add_shape(cylinder_mesh(position=c(555/2,555/2,555/2),angle=c(45,-45,0), radius = 10, length=500, material = material_list(diffuse="purple"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1)))
Generate Directional Lights
directional_light(direction = c(0, 1, 0), color = "white", intensity = 1)directional_light(direction = c(0, 1, 0), color = "white", intensity = 1)
direction |
Default |
color |
Default |
intensity |
Default |
A matrix representing the light information.
#Add a light to scene (manually specify the light automatically added to the Cornell Box lights = point_light(position=c(555/2,450,555/2), falloff_quad = 0.0, constant = 0.0002, falloff = 0.005) generate_cornell_mesh(light=FALSE) |> rasterize_scene(light_info = lights) #Add a directional light lights_d = add_light(lights, directional_light(direction=c(1,1.5,-1))) generate_cornell_mesh(light=FALSE) |> rasterize_scene(light_info = lights_d) #Change the intensity and color lights_d = add_light(lights, directional_light(direction=c(1,1.5,-1),color="orange", intensity=0.5)) generate_cornell_mesh(light=FALSE) |> rasterize_scene(light_info = lights_d)#Add a light to scene (manually specify the light automatically added to the Cornell Box lights = point_light(position=c(555/2,450,555/2), falloff_quad = 0.0, constant = 0.0002, falloff = 0.005) generate_cornell_mesh(light=FALSE) |> rasterize_scene(light_info = lights) #Add a directional light lights_d = add_light(lights, directional_light(direction=c(1,1.5,-1))) generate_cornell_mesh(light=FALSE) |> rasterize_scene(light_info = lights_d) #Change the intensity and color lights_d = add_light(lights, directional_light(direction=c(1,1.5,-1),color="orange", intensity=0.5)) generate_cornell_mesh(light=FALSE) |> rasterize_scene(light_info = lights_d)
Displace a Mesh
displace_mesh( mesh, displacement_texture, displacement_scale = 1, displacement_vector = FALSE, id = NA, verbose = TRUE )displace_mesh( mesh, displacement_texture, displacement_scale = 1, displacement_vector = FALSE, id = NA, verbose = TRUE )
mesh |
The mesh. |
displacement_texture |
Image or matrix/array that will be used to displace the mesh |
displacement_scale |
Default |
displacement_vector |
Default |
id |
Default |
verbose |
Default |
raymesh object
#Let's construct a mesh from the volcano dataset#Let's construct a mesh from the volcano dataset
Construct Displacement Sphere
displacement_sphere( displacement_texture, displacement_scale = 1, use_cube = FALSE, cube_subdivision_levels = NA, displace = TRUE, verbose = TRUE, position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), material = material_list() )displacement_sphere( displacement_texture, displacement_scale = 1, use_cube = FALSE, cube_subdivision_levels = NA, displace = TRUE, verbose = TRUE, position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), material = material_list() )
displacement_texture |
Image or matrix/array that will be used to displace the sphere. |
displacement_scale |
Default |
use_cube |
Default |
cube_subdivision_levels |
Default |
displace |
Default |
verbose |
Default |
position |
Default |
scale |
Default |
angle |
Default |
pivot_point |
Default |
order_rotation |
Default |
material |
Default |
raymesh object
texture_dim = 800 u = seq(0, 2 * pi, length.out = texture_dim) v = seq(0, 2 * pi, length.out = texture_dim) knit_texture = outer(u, v, function(x, y) { sin(10 * x + 0.75 * sin(10 * y))^2 + 0.35 * cos(12 * y)^2 }) knit_texture = (knit_texture - min(knit_texture)) / diff(range(knit_texture)) knit_texture = array( rep(knit_texture, 3), dim = c(texture_dim, texture_dim, 3) ) rayimage::plot_image(knit_texture) light_info = directional_light(c(1,1,1), color="dodgerblue",intensity=0.8) |> add_light(directional_light(c(-1,-1,0.1), color="red",intensity=0.8)) |> add_light(directional_light(c(0.5,1,0.5),intensity=0.8)) displacement_sphere(knit_texture, displacement_scale = 0.08, verbose = TRUE) |> rasterize_scene(light_info = light_info, fov=15) #The default sphere has issues near the poles displacement_sphere(knit_texture, displacement_scale = 0.08) |> rasterize_scene(light_info = light_info, fov=10, lookfrom=c(0,10,10)) # A cube will render more nicely near the poles displacement_sphere(knit_texture, use_cube = TRUE, displacement_scale = 0.08) |> rasterize_scene(light_info = light_info, fov=10, lookfrom=c(0,10,10))texture_dim = 800 u = seq(0, 2 * pi, length.out = texture_dim) v = seq(0, 2 * pi, length.out = texture_dim) knit_texture = outer(u, v, function(x, y) { sin(10 * x + 0.75 * sin(10 * y))^2 + 0.35 * cos(12 * y)^2 }) knit_texture = (knit_texture - min(knit_texture)) / diff(range(knit_texture)) knit_texture = array( rep(knit_texture, 3), dim = c(texture_dim, texture_dim, 3) ) rayimage::plot_image(knit_texture) light_info = directional_light(c(1,1,1), color="dodgerblue",intensity=0.8) |> add_light(directional_light(c(-1,-1,0.1), color="red",intensity=0.8)) |> add_light(directional_light(c(0.5,1,0.5),intensity=0.8)) displacement_sphere(knit_texture, displacement_scale = 0.08, verbose = TRUE) |> rasterize_scene(light_info = light_info, fov=15) #The default sphere has issues near the poles displacement_sphere(knit_texture, displacement_scale = 0.08) |> rasterize_scene(light_info = light_info, fov=10, lookfrom=c(0,10,10)) # A cube will render more nicely near the poles displacement_sphere(knit_texture, use_cube = TRUE, displacement_scale = 0.08) |> rasterize_scene(light_info = light_info, fov=10, lookfrom=c(0,10,10))
Click on a base R plot to draw a radius/height polygon for lathe_mesh().
After the second click, the preview joins the newest point to the first
point, updating the closing segment after each additional click.
draw_lathe_profile(xlim = c(0, 1), ylim = c(0, 1), snap = 0.02)draw_lathe_profile(xlim = c(0, 1), ylim = c(0, 1), snap = 0.02)
xlim |
Default |
ylim |
Default |
snap |
Default |
Requires an interactive graphics device that supports graphics::locator().
Left-click to add vertices in boundary order. Click Undo above the plot
to remove the most recent vertex and update the preview. Undo can be used
repeatedly, including to clear the drawing; it has no effect when empty.
Finish using the device's
locator termination action: for example, Escape on Quartz, another mouse
button on X11, or Finish in RStudio. The plot uses equal X and Y scales.
Other clicks outside the supplied limits and consecutive duplicate clicks are
ignored. A dashed line marks the automatically supplied closing segment.
At least three noncollinear vertices are needed to finish a polygon. Self-intersections and retraced edges are rejected. Redundant collinear vertices are removed, and the boundary is oriented counterclockwise to produce outward-facing lathe surfaces.
For a polygon away from the axis, the first vertex is repeated at the end
so lathe_mesh() includes the closing segment. When the polygon meets the
axis, its boundary must touch it at a single vertex or along a single edge.
Axis vertices become the profile endpoints; an edge along the axis is
omitted because it does not sweep out a surface. This keeps radius-zero
vertices out of the profile interior, as required by lathe_mesh().
A numeric matrix with columns radius and height, ready to pass
as the profile argument of lathe_mesh(). Returns NULL if drawing is
finished with no points remaining.
profile = draw_lathe_profile(xlim = c(0, 2), ylim = c(0, 4)) if (!is.null(profile)) { mesh = lathe_mesh(profile) rasterize_scene(mesh, lookfrom = c(6, 5, 8), lookat = c(0, 2, 0)) }profile = draw_lathe_profile(xlim = c(0, 2), ylim = c(0, 4)) if (!is.null(profile)) { mesh = lathe_mesh(profile) rasterize_scene(mesh, lookfrom = c(6, 5, 8), lookat = c(0, 2, 0)) }
Construct an indexed sweep with independent geometry, normal, and texture indices. Geometry construction is independent of any renderer.
extruded_path_mesh( points, polygon = NA, polygon_end = NA, breaks = NA, closed = FALSE, closed_smooth = TRUE, polygon_add_points = 0, twists = 0, texture_repeats = 1, straight = FALSE, precomputed_control_points = FALSE, width = 1, width_end = NA, width_ease = "spline", smooth_normals = FALSE, u_min = 0, u_max = 1, linear_step = FALSE, end_caps = c(TRUE, TRUE), initial_normal = NULL, smooth_angle = 180, arc_tolerance = 1e-05, position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), material = material_list(), material_caps = NULL, separate_caps = FALSE )extruded_path_mesh( points, polygon = NA, polygon_end = NA, breaks = NA, closed = FALSE, closed_smooth = TRUE, polygon_add_points = 0, twists = 0, texture_repeats = 1, straight = FALSE, precomputed_control_points = FALSE, width = 1, width_end = NA, width_ease = "spline", smooth_normals = FALSE, u_min = 0, u_max = 1, linear_step = FALSE, end_caps = c(TRUE, TRUE), initial_normal = NULL, smooth_angle = 180, arc_tolerance = 1e-05, position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), material = material_list(), material_caps = NULL, separate_caps = FALSE )
points |
Matrix or data frame with three columns, or a list of numeric three-vectors, describing the path. With precomputed controls, a list of four-by-three cubic Bezier control-point matrices. |
polygon |
Default |
polygon_end |
Default |
breaks |
Default |
closed |
Default |
closed_smooth |
Default |
polygon_add_points |
Default |
twists |
Default |
texture_repeats |
Default |
straight |
Default |
precomputed_control_points |
Default |
width |
Default |
width_end |
Default |
width_ease |
Default |
smooth_normals |
Default |
u_min |
Default |
u_max |
Default |
linear_step |
Default |
end_caps |
Default |
initial_normal |
Default |
smooth_angle |
Default |
arc_tolerance |
Default |
position |
Default |
scale |
Default |
angle |
Default |
pivot_point |
Default |
order_rotation |
Default |
material |
Default |
material_caps |
Default |
separate_caps |
Default |
Closed sweeps correct accumulated frame rotation. Full loops and intervals crossing the seam require compatible endpoint profiles, widths, and twists. For example, a square profile permits quarter-turn seam rotations. Invalid seams raise an error. Profile winding is normalized while retaining the first vertex. Texture seams and shading creases use separate attribute indices.
A ray_mesh, or NULL for an empty interval or entirely zero-width
sweep. With separate_caps = TRUE, a list with surface and caps entries,
each containing a ray_mesh or NULL.
extruded_polygon_mesh(), lathe_mesh()
path = rbind(c(0, 0, 0), c(0, 1, 1), c(1, 2, 1)) tube = extruded_path_mesh(path, width = 0.2, smooth_normals = TRUE) parts = extruded_path_mesh(path, width = 0.2, separate_caps = TRUE)path = rbind(c(0, 0, 0), c(0, 1, 1), c(1, 2, 1)) tube = extruded_path_mesh(path, width = 0.2, smooth_normals = TRUE) parts = extruded_path_mesh(path, width = 0.2, separate_caps = TRUE)
Build polygon caps and walls with shared geometric vertices, including multiple holes, spatial features, and multipart polygons.
extruded_polygon_mesh( polygon = NULL, plane = "xz", top = 1, bottom = 0, holes = NULL, center = FALSE, flip_horizontal = FALSE, flip_vertical = FALSE, data_column_top = NULL, data_column_bottom = NULL, scale_data = 1, position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), material = material_list() )extruded_polygon_mesh( polygon = NULL, plane = "xz", top = 1, bottom = 0, holes = NULL, center = FALSE, flip_horizontal = FALSE, flip_vertical = FALSE, data_column_top = NULL, data_column_bottom = NULL, scale_data = 1, position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), material = material_list() )
polygon |
Default |
plane |
Default |
top |
Default |
bottom |
Default |
holes |
Default |
center |
Default |
flip_horizontal |
Default |
flip_vertical |
Default |
data_column_top |
Default |
data_column_bottom |
Default |
scale_data |
Default |
position |
Default |
scale |
Default |
angle |
Default |
pivot_point |
Default |
order_rotation |
Default |
material |
Default |
A ray_mesh containing all polygon components. Equal-height features
contribute flat surfaces, which cannot enclose participating media.
extruded_path_mesh(), lathe_mesh()
outline = rbind(c(-1, -1), c(1, -1), c(1, 1), c(-1, 1)) block = extruded_polygon_mesh(outline, top = 2) hole = outline * 0.3 frame = extruded_polygon_mesh(rbind(outline, hole), holes = 5, top = 0.5)outline = rbind(c(-1, -1), c(1, -1), c(1, 1), c(-1, 1)) block = extruded_polygon_mesh(outline, top = 2) hole = outline * 0.3 frame = extruded_polygon_mesh(rbind(outline, hole), holes = 5, top = 0.5)
Flip Orientation
flip_orientation_mesh(mesh)flip_orientation_mesh(mesh)
mesh |
The mesh to swap orientations. |
Mesh with flipped vertex orientation
# Flip a mesh sphere_mesh(position=c(-1,0,0)) |> add_shape(flip_orientation_mesh(sphere_mesh(position=c(1,0,0)))) |> rasterize_scene(debug="normals",fov=30)# Flip a mesh sphere_mesh(position=c(-1,0,0)) |> add_shape(flip_orientation_mesh(sphere_mesh(position=c(1,0,0)))) |> rasterize_scene(debug="normals",fov=30)
Cornell Box 3D Model
generate_cornell_mesh( leftcolor = "#1f7326", rightcolor = "#a60d0d", roomcolor = "#bababa", ceiling = TRUE, light = TRUE )generate_cornell_mesh( leftcolor = "#1f7326", rightcolor = "#a60d0d", roomcolor = "#bababa", ceiling = TRUE, light = TRUE )
leftcolor |
Default |
rightcolor |
Default |
roomcolor |
Default |
ceiling |
Default |
light |
Default |
List describing the mesh.
#Generate and render the default Cornell box and add an object. generate_cornell_mesh() |> rasterize_scene() #Add an object to the scene generate_cornell_mesh() |> add_shape(obj_mesh(r_obj(),position=c(555/2,555/2,555/2),scale=300,angle=c(0,180,0))) |> rasterize_scene() #Turn off the ceiling so the default directional light reaches inside the box generate_cornell_mesh(ceiling=FALSE) |> add_shape(obj_mesh(r_obj(),position=c(555/2,555/2,555/2),scale=300,angle=c(0,180,0))) |> rasterize_scene() #Adjust the light to the front generate_cornell_mesh(ceiling=FALSE) |> add_shape(obj_mesh(r_obj(),position=c(555/2,555/2,555/2),scale=300,angle=c(0,180,0))) |> rasterize_scene(light_info = directional_light(direction=c(0,1,-1))) #Change the color palette generate_cornell_mesh(ceiling=FALSE,leftcolor="purple", rightcolor="yellow") |> add_shape(obj_mesh(r_obj(),position=c(555/2,555/2,555/2),scale=300,angle=c(0,180,0))) |> rasterize_scene(light_info = directional_light(direction=c(0,1,-1)))#Generate and render the default Cornell box and add an object. generate_cornell_mesh() |> rasterize_scene() #Add an object to the scene generate_cornell_mesh() |> add_shape(obj_mesh(r_obj(),position=c(555/2,555/2,555/2),scale=300,angle=c(0,180,0))) |> rasterize_scene() #Turn off the ceiling so the default directional light reaches inside the box generate_cornell_mesh(ceiling=FALSE) |> add_shape(obj_mesh(r_obj(),position=c(555/2,555/2,555/2),scale=300,angle=c(0,180,0))) |> rasterize_scene() #Adjust the light to the front generate_cornell_mesh(ceiling=FALSE) |> add_shape(obj_mesh(r_obj(),position=c(555/2,555/2,555/2),scale=300,angle=c(0,180,0))) |> rasterize_scene(light_info = directional_light(direction=c(0,1,-1))) #Change the color palette generate_cornell_mesh(ceiling=FALSE,leftcolor="purple", rightcolor="yellow") |> add_shape(obj_mesh(r_obj(),position=c(555/2,555/2,555/2),scale=300,angle=c(0,180,0))) |> rasterize_scene(light_info = directional_light(direction=c(0,1,-1)))
Generate Lines
generate_line(start = c(0, 0, 0), end = c(0, 1, 0), color = "white")generate_line(start = c(0, 0, 0), end = c(0, 1, 0), color = "white")
start |
Default |
end |
Default |
color |
Default |
Line matrix
# Make a spiral of lines t = seq(0,8*pi,length.out=361) line_mat = matrix(nrow=0,ncol=9) for(i in 1:360) { line_mat = add_lines(line_mat, generate_line(start = c(0.5*sin(t[i]), t[i]/(8*pi), 0.5*cos(t[i])), end = c(0.5*sin(t[i+1]), t[i+1]/(8*pi), 0.5*cos(t[i+1])))) } rasterize_lines(line_mat) #Change the line color line_mat = matrix(nrow=0,ncol=9) cols = hsv(seq(0,1,length.out=360)) for(i in 1:360) { line_mat = add_lines(line_mat, generate_line(start = c(sin(t[i]), 2*t[i]/(8*pi), cos(t[i])), end = c(sin(t[i+1]), 2*t[i+1]/(8*pi), cos(t[i+1])), color = cols[i])) } rasterize_lines(line_mat,lookfrom=c(0,10,10),fov=15) #Use in a scene with a mesh obj_mesh(r_obj(simple_r = TRUE),material=material_list(diffuse="dodgerblue")) |> rasterize_scene(line_info = line_mat, light_info = directional_light(c(0,1,1)), lookfrom=c(0,5,10),lookat=c(0,0.8,0),fov=15)# Make a spiral of lines t = seq(0,8*pi,length.out=361) line_mat = matrix(nrow=0,ncol=9) for(i in 1:360) { line_mat = add_lines(line_mat, generate_line(start = c(0.5*sin(t[i]), t[i]/(8*pi), 0.5*cos(t[i])), end = c(0.5*sin(t[i+1]), t[i+1]/(8*pi), 0.5*cos(t[i+1])))) } rasterize_lines(line_mat) #Change the line color line_mat = matrix(nrow=0,ncol=9) cols = hsv(seq(0,1,length.out=360)) for(i in 1:360) { line_mat = add_lines(line_mat, generate_line(start = c(sin(t[i]), 2*t[i]/(8*pi), cos(t[i])), end = c(sin(t[i+1]), 2*t[i+1]/(8*pi), cos(t[i+1])), color = cols[i])) } rasterize_lines(line_mat,lookfrom=c(0,10,10),fov=15) #Use in a scene with a mesh obj_mesh(r_obj(simple_r = TRUE),material=material_list(diffuse="dodgerblue")) |> rasterize_scene(line_info = line_mat, light_info = directional_light(c(0,1,1)), lookfrom=c(0,5,10),lookat=c(0,0.8,0),fov=15)
Calculates the bounding box of a mesh
get_mesh_bbox(mesh)get_mesh_bbox(mesh)
mesh |
The mesh object. |
2x3 numeric matrix
#Calculates the center of the mesh get_mesh_bbox(generate_cornell_mesh())#Calculates the center of the mesh get_mesh_bbox(generate_cornell_mesh())
Calculates the coordinates of the center of a mesh
get_mesh_center(mesh)get_mesh_center(mesh)
mesh |
The mesh object. |
Length-3 numeric vector
#Calculates the center of the mesh get_mesh_center(generate_cornell_mesh())#Calculates the center of the mesh get_mesh_center(generate_cornell_mesh())
Revolve a radius/height profile around the Y axis to create a triangle mesh.
lathe_mesh( profile, segments = 128, smooth = TRUE, position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), material = material_list() )lathe_mesh( profile, segments = 128, smooth = TRUE, position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), material = material_list() )
profile |
Numeric matrix or data frame with two columns: nonnegative radius and Y coordinate. Rows trace the surface in order. Use a bottom-to-top profile for outward-facing sides; for a hollow vessel, continue over the rim and down the inside. Reversing the profile reverses the surface orientation. |
segments |
Default |
smooth |
Default |
position |
Default |
scale |
Default |
angle |
Default |
pivot_point |
Default |
order_rotation |
Default |
material |
Default |
Positive-radius endpoints leave open rings. To close an end, include a zero-radius endpoint in the profile. Each axis endpoint uses a single vertex, and the angular seam shares vertices, so a simple profile with both endpoints on the axis produces a watertight mesh without degenerate cap triangles. Axis points are only allowed at the endpoints. Consecutive duplicate points and immediate reversals are rejected; other self-intersections are not checked. The first and last profile rows are not automatically joined.
Smooth normals average adjacent unit profile tangents. Axis endpoint normals point along the axis. Texture coordinates use angle for U and normalized distance along the profile for V. Separate texture indices allow U to wrap from 1 to 0 without duplicating geometry at the seam.
A ray_mesh object, usable with rasterize_scene(),
add_shape(), and write_scene_to_obj().
draw_lathe_profile() to draw a profile interactively.
# A closed bottle, specified as (radius, height) pairs. profile = rbind(c(0, 0), c(1, 0), c(1, 2), c(0.4, 2.5), c(0.4, 3), c(0, 3)) plot(profile, type="l") bottle = lathe_mesh(profile, material = material_list(diffuse = "steelblue")) rasterize_scene(bottle, lookfrom = c(5, 5, 7), lookat = c(0, 1.5, 0), fov = 30, light_info = directional_light(c(1, 2, 3))) # A hollow tumbler with a solid bottom and an open mouth. t_profile = rbind(c(0, 0), c(1, 0), c(1.1, 3), c(1, 3), c(0.9, 0.15), c(0, 0.15)) plot(t_profile, type="l") tumbler = lathe_mesh(t_profile) rasterize_scene(tumbler, lookfrom = c(5, 5, 7), lookat = c(0, 1.5, 0), fov = 30, light_info = directional_light(c(1, 2, 3)), shadow_map = FALSE)# A closed bottle, specified as (radius, height) pairs. profile = rbind(c(0, 0), c(1, 0), c(1, 2), c(0.4, 2.5), c(0.4, 3), c(0, 3)) plot(profile, type="l") bottle = lathe_mesh(profile, material = material_list(diffuse = "steelblue")) rasterize_scene(bottle, lookfrom = c(5, 5, 7), lookat = c(0, 1.5, 0), fov = 30, light_info = directional_light(c(1, 2, 3))) # A hollow tumbler with a solid bottom and an open mouth. t_profile = rbind(c(0, 0), c(1, 0), c(1.1, 3), c(1, 3), c(0.9, 0.15), c(0, 0.15)) plot(t_profile, type="l") tumbler = lathe_mesh(t_profile) rasterize_scene(tumbler, lookfrom = c(5, 5, 7), lookat = c(0, 1.5, 0), fov = 30, light_info = directional_light(c(1, 2, 3)), shadow_map = FALSE)
Builds a 4x4 transform whose rows are the world-space basis
(right, up, forward). Use with transform_mesh() (rotation) and
translate_mesh() (position).
lookat_transform(pos = c(0, 0, 0), look = c(0, 0, 1), up = c(0, 1, 0))lookat_transform(pos = c(0, 0, 0), look = c(0, 0, 1), up = c(0, 1, 0))
pos |
Default |
look |
Default |
up |
Default |
4x4 numeric matrix. The upper-left 3x3 is the rotation used by
transform_mesh(). The last column contains pos for convenience, but
translation should be applied via translate_mesh().
m = sphere_mesh(radius = 0.5) pos = c(-1,0,0) # place Moon on the left look = c(0,0,0) # look toward the origin (e.g., Earth/camera) up = c(0,1,0) M = lookat_transform(pos = pos, look = look, up = up) m |> transform_mesh(M) |> translate_mesh(pos) |> rasterize_scene(lookfrom = c(0,0,0), lookat = pos, light_info = directional_light(direction = c(1,0,0)))m = sphere_mesh(radius = 0.5) pos = c(-1,0,0) # place Moon on the left look = c(0,0,0) # look toward the origin (e.g., Earth/camera) up = c(0,1,0) M = lookat_transform(pos = pos, look = look, up = up) m |> transform_mesh(M) |> translate_mesh(pos) |> rasterize_scene(lookfrom = c(0,0,0), lookat = pos, light_info = directional_light(direction = c(1,0,0)))
Generate a material properties list.
material_list( diffuse = c(0.8, 0.8, 0.8), ambient = c(0, 0, 0), specular = c(1, 1, 1), transmittance = c(0, 0, 0), emission = c(0, 0, 0), shininess = 50, ior = 1, dissolve = 1, illum = 1, texture_location = "", normal_texture_location = "", bump_texture_location = "", specular_texture_location = "", ambient_texture_location = "", emissive_texture_location = "", diffuse_intensity = 1, bump_intensity = 1, specular_intensity = 1, emission_intensity = 1, ambient_intensity = 1, culling = "back", type = "diffuse", translucent = TRUE, toon_levels = 5, toon_outline_width = 3, toon_outline_color = "black", reflection_intensity = 0, reflection_sharpness = 1, two_sided = FALSE, sigma = 0 )material_list( diffuse = c(0.8, 0.8, 0.8), ambient = c(0, 0, 0), specular = c(1, 1, 1), transmittance = c(0, 0, 0), emission = c(0, 0, 0), shininess = 50, ior = 1, dissolve = 1, illum = 1, texture_location = "", normal_texture_location = "", bump_texture_location = "", specular_texture_location = "", ambient_texture_location = "", emissive_texture_location = "", diffuse_intensity = 1, bump_intensity = 1, specular_intensity = 1, emission_intensity = 1, ambient_intensity = 1, culling = "back", type = "diffuse", translucent = TRUE, toon_levels = 5, toon_outline_width = 3, toon_outline_color = "black", reflection_intensity = 0, reflection_sharpness = 1, two_sided = FALSE, sigma = 0 )
diffuse |
Default |
ambient |
Default |
specular |
Default |
transmittance |
Default |
emission |
Default |
shininess |
Default |
ior |
Default |
dissolve |
Default |
illum |
Default |
texture_location |
Default |
normal_texture_location |
Default |
bump_texture_location |
Default |
specular_texture_location |
Default |
ambient_texture_location |
Default |
emissive_texture_location |
Default |
diffuse_intensity |
Default |
bump_intensity |
Default |
specular_intensity |
Default |
emission_intensity |
Default |
ambient_intensity |
Default |
culling |
Default |
type |
Default |
translucent |
Default |
toon_levels |
Default |
toon_outline_width |
Default |
toon_outline_color |
Default |
reflection_intensity |
Default |
reflection_sharpness |
Default |
two_sided |
Default |
sigma |
Default |
List of material properties.
mat_prop = material_list(diffuse="purple", type="phong", shininess = 20, ambient="purple", ambient_intensity=0.3, specular = "red", specular_intensity=2) p_sphere = sphere_mesh(position=c(555/2,555/2,555/2), radius=40,material=mat_prop) rasterize_scene(p_sphere, light_info=directional_light(direction=c(0.1,0.6,-1)))mat_prop = material_list(diffuse="purple", type="phong", shininess = 20, ambient="purple", ambient_intensity=0.3, specular = "red", specular_intensity=2) p_sphere = sphere_mesh(position=c(555/2,555/2,555/2), radius=40,material=mat_prop) rasterize_scene(p_sphere, light_info=directional_light(direction=c(0.1,0.6,-1)))
Mesh3d 3D Model
mesh3d_mesh( mesh, center = FALSE, position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), materialspath = NULL, material = material_list() )mesh3d_mesh( mesh, center = FALSE, position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), materialspath = NULL, material = material_list() )
mesh |
Mesh3d object. |
center |
Default |
position |
Default |
scale |
Default |
angle |
Default |
pivot_point |
Default |
order_rotation |
Default |
materialspath |
Default |
material |
Default |
List describing the mesh.
# Read in a mesh3d object and rasterize it library(Rvcg) data(humface) mesh3d_mesh(humface,position = c(0,-0.3,0),scale = 1/70, material=material_list(diffuse="dodgerblue4", type="phong", shininess=20, ambient = "dodgerblue4", ambient_intensity=0.3)) |> rasterize_scene(lookat = c(0,0.5,1), light_info = directional_light(c(1,0.5,1))) # Subdivide the mesh for a smoother appearance mesh3d_mesh(humface,position = c(0,-0.3,0),scale = 1/70, material=material_list(diffuse="dodgerblue4", type="phong", shininess=20, ambient = "dodgerblue4", ambient_intensity=0.3)) |> subdivide_mesh() |> rasterize_scene(lookat = c(0,0.5,1), light_info = directional_light(c(1,0.5,1)))# Read in a mesh3d object and rasterize it library(Rvcg) data(humface) mesh3d_mesh(humface,position = c(0,-0.3,0),scale = 1/70, material=material_list(diffuse="dodgerblue4", type="phong", shininess=20, ambient = "dodgerblue4", ambient_intensity=0.3)) |> rasterize_scene(lookat = c(0,0.5,1), light_info = directional_light(c(1,0.5,1))) # Subdivide the mesh for a smoother appearance mesh3d_mesh(humface,position = c(0,-0.3,0),scale = 1/70, material=material_list(diffuse="dodgerblue4", type="phong", shininess=20, ambient = "dodgerblue4", ambient_intensity=0.3)) |> subdivide_mesh() |> rasterize_scene(lookat = c(0,0.5,1), light_info = directional_light(c(1,0.5,1)))
OBJ Mesh 3D Model
obj_mesh( filename, position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), materialspath = NULL, center = FALSE, material = NULL )obj_mesh( filename, position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), materialspath = NULL, center = FALSE, material = NULL )
filename |
OBJ filename. |
position |
Default |
scale |
Default |
angle |
Default |
pivot_point |
Default |
order_rotation |
Default |
materialspath |
Default |
center |
Default |
material |
Default |
List describing the mesh.
#Read in the provided 3D R mesh generate_cornell_mesh(ceiling=FALSE) |> add_shape(obj_mesh(r_obj(),position=c(555/2,555/2,555/2),scale=400,angle=c(0,180,0))) |> rasterize_scene(light_info = directional_light(direction=c(0.2,0.5,-1)))#Read in the provided 3D R mesh generate_cornell_mesh(ceiling=FALSE) |> add_shape(obj_mesh(r_obj(),position=c(555/2,555/2,555/2),scale=400,angle=c(0,180,0))) |> rasterize_scene(light_info = directional_light(direction=c(0.2,0.5,-1)))
PLY Mesh 3D Model
ply_mesh( filename, center = FALSE, position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), material = material_list() )ply_mesh( filename, center = FALSE, position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), material = material_list() )
filename |
PLY filename. |
center |
Default |
position |
Default |
scale |
Default |
angle |
Default |
pivot_point |
Default |
order_rotation |
Default |
material |
Default |
List describing the mesh.
#See the documentation for [obj_mesh()]--no example PLY models are included with this package, #but the process of loading a model is the same (but no materials are included in PLY files).#See the documentation for [obj_mesh()]--no example PLY models are included with this package, #but the process of loading a model is the same (but no materials are included in PLY files).
The falloff of the point light intensity is given by the following equation (referenc:
Intensity = intensity / (constant + falloff * distance + falloff_quad * (distance * distance));
point_light( position = c(0, 0, 0), color = "white", intensity = 1, constant = 1, falloff = 1, falloff_quad = 1 )point_light( position = c(0, 0, 0), color = "white", intensity = 1, constant = 1, falloff = 1, falloff_quad = 1 )
position |
A two-dimensional matrix, where each entry in the matrix is the elevation at that point. All points are assumed to be evenly spaced. |
color |
Default |
intensity |
Default |
constant |
Default |
falloff |
Default |
falloff_quad |
Default |
A matrix representing the light information.
#Add point lights and vary the intensity lights_int = point_light(position=c(100,100,400), color="white", intensity=0.125, falloff_quad = 0.0, constant = 0.0002, falloff = 0.005) |> add_light(point_light(position=c(100,455,400), color="white", intensity=0.25, falloff_quad = 0.0, constant = 0.0002, falloff = 0.005)) |> add_light(point_light(position=c(455,100,400), color="white", intensity=0.5, falloff_quad = 0.0, constant = 0.0002, falloff = 0.005)) |> add_light(point_light(position=c(455,455,400), color="white", intensity=1, falloff_quad = 0.0, constant = 0.0002, falloff = 0.005)) generate_cornell_mesh(light=FALSE) |> rasterize_scene(light_info = lights_int) #Add point lights and vary the color lights_c = point_light(position=c(100,100,500), color="red", falloff_quad = 0.0, constant = 0.0002, falloff = 0.005) |> add_light(point_light(position=c(100,455,500), color="blue", falloff_quad = 0.0, constant = 0.0002, falloff = 0.005)) |> add_light(point_light(position=c(455,100,500), color="purple", falloff_quad = 0.0, constant = 0.0002, falloff = 0.005)) |> add_light(point_light(position=c(455,455,500), color="yellow", falloff_quad = 0.0, constant = 0.0002, falloff = 0.005)) generate_cornell_mesh(light=FALSE) |> rasterize_scene(light_info = lights_c) #Add point lights and vary the falloff term lights_fo = point_light(position=c(100,100,500), color="white", falloff_quad = 0.0, constant = 0.0002, falloff = 0.005) |> add_light(point_light(position=c(100,455,500), color="white", falloff_quad = 0.0, constant = 0.0002, falloff = 0.01)) |> add_light(point_light(position=c(455,100,500), color="white", falloff_quad = 0.0, constant = 0.0002, falloff = 0.02)) |> add_light(point_light(position=c(455,455,500), color="white", falloff_quad = 0.0, constant = 0.0002, falloff = 0.04)) generate_cornell_mesh(light=FALSE) |> rasterize_scene(light_info = lights_fo) #Add point lights and vary the quadradic falloff term lights_quad = point_light(position=c(100,100,500), color="white", falloff_quad = 0.0001, constant = 0.0002, falloff = 0.005) |> add_light(point_light(position=c(100,455,500), color="white", falloff_quad = 0.0002, constant = 0.0002, falloff = 0.005)) |> add_light(point_light(position=c(455,100,500), color="white", falloff_quad = 0.0004, constant = 0.0002, falloff = 0.005)) |> add_light(point_light(position=c(455,455,500), color="white", falloff_quad = 0.0008, constant = 0.0002, falloff = 0.005)) generate_cornell_mesh(light=FALSE) |> rasterize_scene(light_info = lights_quad)#Add point lights and vary the intensity lights_int = point_light(position=c(100,100,400), color="white", intensity=0.125, falloff_quad = 0.0, constant = 0.0002, falloff = 0.005) |> add_light(point_light(position=c(100,455,400), color="white", intensity=0.25, falloff_quad = 0.0, constant = 0.0002, falloff = 0.005)) |> add_light(point_light(position=c(455,100,400), color="white", intensity=0.5, falloff_quad = 0.0, constant = 0.0002, falloff = 0.005)) |> add_light(point_light(position=c(455,455,400), color="white", intensity=1, falloff_quad = 0.0, constant = 0.0002, falloff = 0.005)) generate_cornell_mesh(light=FALSE) |> rasterize_scene(light_info = lights_int) #Add point lights and vary the color lights_c = point_light(position=c(100,100,500), color="red", falloff_quad = 0.0, constant = 0.0002, falloff = 0.005) |> add_light(point_light(position=c(100,455,500), color="blue", falloff_quad = 0.0, constant = 0.0002, falloff = 0.005)) |> add_light(point_light(position=c(455,100,500), color="purple", falloff_quad = 0.0, constant = 0.0002, falloff = 0.005)) |> add_light(point_light(position=c(455,455,500), color="yellow", falloff_quad = 0.0, constant = 0.0002, falloff = 0.005)) generate_cornell_mesh(light=FALSE) |> rasterize_scene(light_info = lights_c) #Add point lights and vary the falloff term lights_fo = point_light(position=c(100,100,500), color="white", falloff_quad = 0.0, constant = 0.0002, falloff = 0.005) |> add_light(point_light(position=c(100,455,500), color="white", falloff_quad = 0.0, constant = 0.0002, falloff = 0.01)) |> add_light(point_light(position=c(455,100,500), color="white", falloff_quad = 0.0, constant = 0.0002, falloff = 0.02)) |> add_light(point_light(position=c(455,455,500), color="white", falloff_quad = 0.0, constant = 0.0002, falloff = 0.04)) generate_cornell_mesh(light=FALSE) |> rasterize_scene(light_info = lights_fo) #Add point lights and vary the quadradic falloff term lights_quad = point_light(position=c(100,100,500), color="white", falloff_quad = 0.0001, constant = 0.0002, falloff = 0.005) |> add_light(point_light(position=c(100,455,500), color="white", falloff_quad = 0.0002, constant = 0.0002, falloff = 0.005)) |> add_light(point_light(position=c(455,100,500), color="white", falloff_quad = 0.0004, constant = 0.0002, falloff = 0.005)) |> add_light(point_light(position=c(455,455,500), color="white", falloff_quad = 0.0008, constant = 0.0002, falloff = 0.005)) generate_cornell_mesh(light=FALSE) |> rasterize_scene(light_info = lights_quad)
3D obj model of R logo (created from the R SVG logo with the raybevel package),
to be used with obj_model()
r_obj(simple_r = FALSE)r_obj(simple_r = FALSE)
simple_r |
Default |
File location of the 3d_r_logo.obj file (saved with a .txt extension)
#Load and render the included example R object file. obj_mesh(r_obj()) |> rasterize_scene(lookfrom = c(0, 1, 10), fov=7,light_info = directional_light(c(1,1,1)))#Load and render the included example R object file. obj_mesh(r_obj()) |> rasterize_scene(lookfrom = c(0, 1, 10), fov=7,light_info = directional_light(c(1,1,1)))
Render a 3D scene made out of lines using a software rasterizer.
rasterize_lines( line_info = NULL, filename = NA, width = 800, height = 800, alpha_line = 1, parallel = TRUE, fov = 20, lookfrom = c(0, 0, 10), lookat = NULL, camera_up = c(0, 1, 0), color = "red", background = "black", debug = "none", near_plane = 0.1, far_plane = 100, block_size = 4, ortho_dimensions = c(1, 1), bloom = FALSE, antialias_lines = TRUE )rasterize_lines( line_info = NULL, filename = NA, width = 800, height = 800, alpha_line = 1, parallel = TRUE, fov = 20, lookfrom = c(0, 0, 10), lookat = NULL, camera_up = c(0, 1, 0), color = "red", background = "black", debug = "none", near_plane = 0.1, far_plane = 100, block_size = 4, ortho_dimensions = c(1, 1), bloom = FALSE, antialias_lines = TRUE )
line_info |
The mesh object. |
filename |
Default |
width |
Default |
height |
Default |
alpha_line |
Default |
parallel |
Default |
fov |
Default |
lookfrom |
Default |
lookat |
Default |
camera_up |
Default |
color |
Default |
background |
Default |
debug |
Default |
near_plane |
Default |
far_plane |
Default |
block_size |
Default |
ortho_dimensions |
Default |
bloom |
Default |
antialias_lines |
Default |
Rasterized image.
#Generate a cube out of lines cube_outline = generate_line(start = c(-1, -1, -1), end = c(-1, -1, 1)) |> add_lines(generate_line(start = c(-1, -1, -1), end = c(-1, 1, -1))) |> add_lines(generate_line(start = c(-1, -1, -1), end = c(1, -1, -1))) |> add_lines(generate_line(start = c(-1, -1, 1), end = c(-1, 1, 1))) |> add_lines(generate_line(start = c(-1, -1, 1), end = c(1, -1, 1))) |> add_lines(generate_line(start = c(-1, 1, 1), end = c(-1, 1, -1))) |> add_lines(generate_line(start = c(-1, 1, 1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(1, 1, -1), end = c(1, -1, -1))) |> add_lines(generate_line(start = c(1, 1, -1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(1, -1, -1), end = c(1, -1, 1))) |> add_lines(generate_line(start = c(1, -1, 1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(-1, 1, -1), end = c(1, 1, -1))) rasterize_lines(cube_outline,fov=90,lookfrom=c(0,0,3)) #Scale the cube uniformly scaled_cube = color_lines(scale_lines(cube_outline,scale=0.5),color="red") rasterize_lines(add_lines(cube_outline,scaled_cube),fov=90,lookfrom=c(0,0,3)) #Scale the cube non-uniformly scaled_cube = color_lines(scale_lines(cube_outline,scale=c(0.8,2,0.4)),color="red") rasterize_lines(add_lines(cube_outline,scaled_cube),fov=60,lookfrom=c(3,3,3))#Generate a cube out of lines cube_outline = generate_line(start = c(-1, -1, -1), end = c(-1, -1, 1)) |> add_lines(generate_line(start = c(-1, -1, -1), end = c(-1, 1, -1))) |> add_lines(generate_line(start = c(-1, -1, -1), end = c(1, -1, -1))) |> add_lines(generate_line(start = c(-1, -1, 1), end = c(-1, 1, 1))) |> add_lines(generate_line(start = c(-1, -1, 1), end = c(1, -1, 1))) |> add_lines(generate_line(start = c(-1, 1, 1), end = c(-1, 1, -1))) |> add_lines(generate_line(start = c(-1, 1, 1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(1, 1, -1), end = c(1, -1, -1))) |> add_lines(generate_line(start = c(1, 1, -1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(1, -1, -1), end = c(1, -1, 1))) |> add_lines(generate_line(start = c(1, -1, 1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(-1, 1, -1), end = c(1, 1, -1))) rasterize_lines(cube_outline,fov=90,lookfrom=c(0,0,3)) #Scale the cube uniformly scaled_cube = color_lines(scale_lines(cube_outline,scale=0.5),color="red") rasterize_lines(add_lines(cube_outline,scaled_cube),fov=90,lookfrom=c(0,0,3)) #Scale the cube non-uniformly scaled_cube = color_lines(scale_lines(cube_outline,scale=c(0.8,2,0.4)),color="red") rasterize_lines(add_lines(cube_outline,scaled_cube),fov=60,lookfrom=c(3,3,3))
Render a 3D scene with meshes, lights, and lines using a software rasterizer.
rasterize_scene( scene, filename = NA, width = 800, height = 800, line_info = NULL, alpha_line = 1, parallel = TRUE, plot = is.na(filename), fov = 20, lookfrom = c(0, 0, 10), lookat = NULL, camera_up = c(0, 1, 0), fsaa = 2, light_info = directional_light(), color = "red", type = "diffuse", background = "black", tangent_space_normals = TRUE, shadow_map = TRUE, shadow_map_bias = 0.003, shadow_map_intensity = 0, shadow_map_dims = NULL, ssao = FALSE, ssao_intensity = 10, ssao_radius = 0.1, tonemap = "raw", debug = "none", near_plane = 0.1, far_plane = 100, shader = "default", block_size = 4, shape = NULL, line_offset = 1e-05, ortho_dimensions = c(1, 1), bloom = FALSE, antialias_lines = TRUE, environment_map = "", background_sharpness = 1, verbose = FALSE, vertex_transform = NULL, validate_scene = TRUE, transparent_background = FALSE )rasterize_scene( scene, filename = NA, width = 800, height = 800, line_info = NULL, alpha_line = 1, parallel = TRUE, plot = is.na(filename), fov = 20, lookfrom = c(0, 0, 10), lookat = NULL, camera_up = c(0, 1, 0), fsaa = 2, light_info = directional_light(), color = "red", type = "diffuse", background = "black", tangent_space_normals = TRUE, shadow_map = TRUE, shadow_map_bias = 0.003, shadow_map_intensity = 0, shadow_map_dims = NULL, ssao = FALSE, ssao_intensity = 10, ssao_radius = 0.1, tonemap = "raw", debug = "none", near_plane = 0.1, far_plane = 100, shader = "default", block_size = 4, shape = NULL, line_offset = 1e-05, ortho_dimensions = c(1, 1), bloom = FALSE, antialias_lines = TRUE, environment_map = "", background_sharpness = 1, verbose = FALSE, vertex_transform = NULL, validate_scene = TRUE, transparent_background = FALSE )
scene |
The scene object. |
filename |
Default |
width |
Default |
height |
Default |
line_info |
Default |
alpha_line |
Default |
parallel |
Default |
plot |
Default |
fov |
Default |
lookfrom |
Default |
lookat |
Default |
camera_up |
Default |
fsaa |
Default |
light_info |
Default |
color |
Default |
type |
Default |
background |
Default |
tangent_space_normals |
Default |
shadow_map |
Default |
shadow_map_bias |
Default |
shadow_map_intensity |
Default |
shadow_map_dims |
Default |
ssao |
Default |
ssao_intensity |
Default |
ssao_radius |
Default |
tonemap |
Default |
debug |
Default |
near_plane |
Default |
far_plane |
Default |
shader |
Default |
block_size |
Default |
shape |
Default |
line_offset |
Default |
ortho_dimensions |
Default |
bloom |
Default |
antialias_lines |
Default |
environment_map |
Default |
background_sharpness |
Default |
verbose |
Default |
vertex_transform |
Default |
validate_scene |
Default |
transparent_background |
Default |
Rasterized image.
#Let's load the cube OBJ file included with the package rasterize_scene(cube_mesh(),lookfrom=c(2,4,10), light_info = directional_light(direction=c(0.5,1,0.7))) #Flatten the cube, translate downwards, and set to grey base_model = cube_mesh() |> scale_mesh(scale=c(5,0.2,5)) |> translate_mesh(c(0,-0.1,0)) |> set_material(diffuse="grey80") rasterize_scene(base_model, lookfrom=c(2,4,10), light_info = directional_light(direction=c(0.5,1,0.7))) #load the R OBJ file, scale it down, color it blue, and add it to the grey base r_model = obj_mesh(r_obj(simple_r = TRUE)) |> scale_mesh(scale=0.5) |> set_material(diffuse="dodgerblue") |> add_shape(base_model) rasterize_scene(r_model, lookfrom=c(2,4,10), light_info = directional_light(direction=c(0.5,1,0.7))) #Zoom in and reduce the shadow mapping intensity rasterize_scene(r_model, lookfrom=c(2,4,10), fov=10,shadow_map = TRUE, shadow_map_intensity=0.3, light_info = directional_light(direction=c(0.5,1,0.7))) #Include the resolution (4x) of the shadow map for less pixellation around the edges #Also decrease the shadow_map_bias slightly to remove the "peter panning" floating shadow effect rasterize_scene(r_model, lookfrom=c(2,4,10), fov=10, shadow_map_dims=4, light_info = directional_light(direction=c(0.5,1,0.7))) #Add some more directional lights and change their color lights = directional_light(c(0.7,1.1,-0.9),color = "orange",intensity = 1) |> add_light(directional_light(c(0.7,1,1),color = "dodgerblue",intensity = 1)) |> add_light(directional_light(c(2,4,10),color = "white",intensity = 0.5)) rasterize_scene(r_model, lookfrom=c(2,4,10), fov=10, light_info = lights) #Add some point lights lights_p = lights |> add_light(point_light(position=c(-1,1,0),color="red", intensity=2)) |> add_light(point_light(position=c(1,1,0),color="purple", intensity=2)) rasterize_scene(r_model, lookfrom=c(2,4,10), fov=10, light_info = lights_p) #change the camera position rasterize_scene(r_model, lookfrom=c(-2,2,-10), fov=10, light_info = lights_p) #Add a spiral of lines around the model by generating a matrix of line segments t = seq(0,8*pi,length.out=361) line_mat = matrix(nrow=0,ncol=9) for(i in 1:360) { line_mat = add_lines(line_mat, generate_line(start = c(0.5*sin(t[i]), t[i]/(8*pi), 0.5*cos(t[i])), end = c(0.5*sin(t[i+1]), t[i+1]/(8*pi), 0.5*cos(t[i+1])))) } rasterize_scene(r_model, lookfrom=c(2,4,10), fov=10, line_info = line_mat, light_info = lights)#Let's load the cube OBJ file included with the package rasterize_scene(cube_mesh(),lookfrom=c(2,4,10), light_info = directional_light(direction=c(0.5,1,0.7))) #Flatten the cube, translate downwards, and set to grey base_model = cube_mesh() |> scale_mesh(scale=c(5,0.2,5)) |> translate_mesh(c(0,-0.1,0)) |> set_material(diffuse="grey80") rasterize_scene(base_model, lookfrom=c(2,4,10), light_info = directional_light(direction=c(0.5,1,0.7))) #load the R OBJ file, scale it down, color it blue, and add it to the grey base r_model = obj_mesh(r_obj(simple_r = TRUE)) |> scale_mesh(scale=0.5) |> set_material(diffuse="dodgerblue") |> add_shape(base_model) rasterize_scene(r_model, lookfrom=c(2,4,10), light_info = directional_light(direction=c(0.5,1,0.7))) #Zoom in and reduce the shadow mapping intensity rasterize_scene(r_model, lookfrom=c(2,4,10), fov=10,shadow_map = TRUE, shadow_map_intensity=0.3, light_info = directional_light(direction=c(0.5,1,0.7))) #Include the resolution (4x) of the shadow map for less pixellation around the edges #Also decrease the shadow_map_bias slightly to remove the "peter panning" floating shadow effect rasterize_scene(r_model, lookfrom=c(2,4,10), fov=10, shadow_map_dims=4, light_info = directional_light(direction=c(0.5,1,0.7))) #Add some more directional lights and change their color lights = directional_light(c(0.7,1.1,-0.9),color = "orange",intensity = 1) |> add_light(directional_light(c(0.7,1,1),color = "dodgerblue",intensity = 1)) |> add_light(directional_light(c(2,4,10),color = "white",intensity = 0.5)) rasterize_scene(r_model, lookfrom=c(2,4,10), fov=10, light_info = lights) #Add some point lights lights_p = lights |> add_light(point_light(position=c(-1,1,0),color="red", intensity=2)) |> add_light(point_light(position=c(1,1,0),color="purple", intensity=2)) rasterize_scene(r_model, lookfrom=c(2,4,10), fov=10, light_info = lights_p) #change the camera position rasterize_scene(r_model, lookfrom=c(-2,2,-10), fov=10, light_info = lights_p) #Add a spiral of lines around the model by generating a matrix of line segments t = seq(0,8*pi,length.out=361) line_mat = matrix(nrow=0,ncol=9) for(i in 1:360) { line_mat = add_lines(line_mat, generate_line(start = c(0.5*sin(t[i]), t[i]/(8*pi), 0.5*cos(t[i])), end = c(0.5*sin(t[i+1]), t[i+1]/(8*pi), 0.5*cos(t[i+1])))) } rasterize_scene(r_model, lookfrom=c(2,4,10), fov=10, line_info = line_mat, light_info = lights)
Loads an OBJ file and return a ray_mesh list structure. No processing is done on
the object other than loading it (unlike obj_model()).
read_obj(filename, materialspath = NULL)read_obj(filename, materialspath = NULL)
filename |
Filename of the OBJ file. |
materialspath |
Directory where the MTL file is located. Defaults to the directory of |
ray_mesh list object
#Load an arrow OBJ
sphere = read_obj(system.file("extdata", "arrow.txt", package="rayvertex"))
Rotate Lines
rotate_lines( lines, angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3) )rotate_lines( lines, angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3) )
lines |
The existing line scene. |
angle |
Default |
pivot_point |
Default |
order_rotation |
Default |
Rotated lines.
#Generate a cube out of lines cube_outline = generate_line(start = c(-1, -1, -1), end = c(-1, -1, 1)) |> add_lines(generate_line(start = c(-1, -1, -1), end = c(-1, 1, -1))) |> add_lines(generate_line(start = c(-1, -1, -1), end = c(1, -1, -1))) |> add_lines(generate_line(start = c(-1, -1, 1), end = c(-1, 1, 1))) |> add_lines(generate_line(start = c(-1, -1, 1), end = c(1, -1, 1))) |> add_lines(generate_line(start = c(-1, 1, 1), end = c(-1, 1, -1))) |> add_lines(generate_line(start = c(-1, 1, 1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(1, 1, -1), end = c(1, -1, -1))) |> add_lines(generate_line(start = c(1, 1, -1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(1, -1, -1), end = c(1, -1, 1))) |> add_lines(generate_line(start = c(1, -1, 1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(-1, 1, -1), end = c(1, 1, -1))) rasterize_lines(cube_outline,lookfrom=c(0,6,10)) #Rotate the cube 30 degrees around the y-axis rotated_cube = color_lines(rotate_lines(cube_outline,angle=c(0,30,0)),color="red") rasterize_lines(add_lines(cube_outline,rotated_cube),lookfrom=c(0,6,10)) #Rotate the cube 30 degrees around each axis, in this order: x,y,z rotated_cube = color_lines(rotate_lines(cube_outline,angle=c(30,30,30)),color="red") rasterize_lines(add_lines(cube_outline,rotated_cube),lookfrom=c(0,6,10)) #Rotate the cube 30 degrees around each axis, in this order: z,y,x rotated_cube = color_lines(rotate_lines(cube_outline,angle=c(30,30,30), order_rotation = c(3,2,1)),color="red") rasterize_lines(add_lines(cube_outline,rotated_cube),lookfrom=c(0,6,10))#Generate a cube out of lines cube_outline = generate_line(start = c(-1, -1, -1), end = c(-1, -1, 1)) |> add_lines(generate_line(start = c(-1, -1, -1), end = c(-1, 1, -1))) |> add_lines(generate_line(start = c(-1, -1, -1), end = c(1, -1, -1))) |> add_lines(generate_line(start = c(-1, -1, 1), end = c(-1, 1, 1))) |> add_lines(generate_line(start = c(-1, -1, 1), end = c(1, -1, 1))) |> add_lines(generate_line(start = c(-1, 1, 1), end = c(-1, 1, -1))) |> add_lines(generate_line(start = c(-1, 1, 1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(1, 1, -1), end = c(1, -1, -1))) |> add_lines(generate_line(start = c(1, 1, -1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(1, -1, -1), end = c(1, -1, 1))) |> add_lines(generate_line(start = c(1, -1, 1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(-1, 1, -1), end = c(1, 1, -1))) rasterize_lines(cube_outline,lookfrom=c(0,6,10)) #Rotate the cube 30 degrees around the y-axis rotated_cube = color_lines(rotate_lines(cube_outline,angle=c(0,30,0)),color="red") rasterize_lines(add_lines(cube_outline,rotated_cube),lookfrom=c(0,6,10)) #Rotate the cube 30 degrees around each axis, in this order: x,y,z rotated_cube = color_lines(rotate_lines(cube_outline,angle=c(30,30,30)),color="red") rasterize_lines(add_lines(cube_outline,rotated_cube),lookfrom=c(0,6,10)) #Rotate the cube 30 degrees around each axis, in this order: z,y,x rotated_cube = color_lines(rotate_lines(cube_outline,angle=c(30,30,30), order_rotation = c(3,2,1)),color="red") rasterize_lines(add_lines(cube_outline,rotated_cube),lookfrom=c(0,6,10))
Rotate Mesh
rotate_mesh( mesh, angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3) )rotate_mesh( mesh, angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3) )
mesh |
The mesh. |
angle |
Default |
pivot_point |
Default |
order_rotation |
Default |
Rotated Mesh
#Rotate a mesh in the Cornell box robj = obj_mesh(r_obj(), scale=150,angle=c(0,180,0)) generate_cornell_mesh() |> add_shape(rotate_mesh(translate_mesh(robj,c(400,100,155)),c(0,30,0), pivot_point=c(400,100,155))) |> add_shape(rotate_mesh(translate_mesh(robj,c(555/2,200,555/2)),c(-30,60,30), pivot_point=c(555/2,200,555/2))) |> add_shape(rotate_mesh(translate_mesh(robj,c(155,300,400)),c(-30,60,30), pivot_point=c(155,300,400), order_rotation=c(3,2,1))) |> rasterize_scene(light_info=directional_light(direction=c(0.1,0.6,-1)))#Rotate a mesh in the Cornell box robj = obj_mesh(r_obj(), scale=150,angle=c(0,180,0)) generate_cornell_mesh() |> add_shape(rotate_mesh(translate_mesh(robj,c(400,100,155)),c(0,30,0), pivot_point=c(400,100,155))) |> add_shape(rotate_mesh(translate_mesh(robj,c(555/2,200,555/2)),c(-30,60,30), pivot_point=c(555/2,200,555/2))) |> add_shape(rotate_mesh(translate_mesh(robj,c(155,300,400)),c(-30,60,30), pivot_point=c(155,300,400), order_rotation=c(3,2,1))) |> rasterize_scene(light_info=directional_light(direction=c(0.1,0.6,-1)))
Scale Lines
scale_lines(lines, scale = 1)scale_lines(lines, scale = 1)
lines |
The line scene. |
scale |
Default |
Scaled line matrix.
#Generate a cube out of lines cube_outline = generate_line(start = c(-1, -1, -1), end = c(-1, -1, 1)) |> add_lines(generate_line(start = c(-1, -1, -1), end = c(-1, 1, -1))) |> add_lines(generate_line(start = c(-1, -1, -1), end = c(1, -1, -1))) |> add_lines(generate_line(start = c(-1, -1, 1), end = c(-1, 1, 1))) |> add_lines(generate_line(start = c(-1, -1, 1), end = c(1, -1, 1))) |> add_lines(generate_line(start = c(-1, 1, 1), end = c(-1, 1, -1))) |> add_lines(generate_line(start = c(-1, 1, 1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(1, 1, -1), end = c(1, -1, -1))) |> add_lines(generate_line(start = c(1, 1, -1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(1, -1, -1), end = c(1, -1, 1))) |> add_lines(generate_line(start = c(1, -1, 1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(-1, 1, -1), end = c(1, 1, -1))) rasterize_lines(cube_outline,fov=90,lookfrom=c(0,0,3)) #Scale the cube uniformly scaled_cube = color_lines(scale_lines(cube_outline,scale=0.5),color="red") rasterize_lines(add_lines(cube_outline,scaled_cube),fov=90,lookfrom=c(0,0,3)) #Scale the cube non-uniformly scaled_cube = color_lines(scale_lines(cube_outline,scale=c(0.8,2,0.4)),color="red") rasterize_lines(add_lines(cube_outline,scaled_cube),fov=60,lookfrom=c(3,3,3))#Generate a cube out of lines cube_outline = generate_line(start = c(-1, -1, -1), end = c(-1, -1, 1)) |> add_lines(generate_line(start = c(-1, -1, -1), end = c(-1, 1, -1))) |> add_lines(generate_line(start = c(-1, -1, -1), end = c(1, -1, -1))) |> add_lines(generate_line(start = c(-1, -1, 1), end = c(-1, 1, 1))) |> add_lines(generate_line(start = c(-1, -1, 1), end = c(1, -1, 1))) |> add_lines(generate_line(start = c(-1, 1, 1), end = c(-1, 1, -1))) |> add_lines(generate_line(start = c(-1, 1, 1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(1, 1, -1), end = c(1, -1, -1))) |> add_lines(generate_line(start = c(1, 1, -1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(1, -1, -1), end = c(1, -1, 1))) |> add_lines(generate_line(start = c(1, -1, 1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(-1, 1, -1), end = c(1, 1, -1))) rasterize_lines(cube_outline,fov=90,lookfrom=c(0,0,3)) #Scale the cube uniformly scaled_cube = color_lines(scale_lines(cube_outline,scale=0.5),color="red") rasterize_lines(add_lines(cube_outline,scaled_cube),fov=90,lookfrom=c(0,0,3)) #Scale the cube non-uniformly scaled_cube = color_lines(scale_lines(cube_outline,scale=c(0.8,2,0.4)),color="red") rasterize_lines(add_lines(cube_outline,scaled_cube),fov=60,lookfrom=c(3,3,3))
Scale Mesh
scale_mesh(mesh, scale = 1, center = c(0, 0, 0))scale_mesh(mesh, scale = 1, center = c(0, 0, 0))
mesh |
The mesh. |
scale |
Default |
center |
Default |
Scaled mesh
#Scale a mesh in the Cornell box robj = obj_mesh(r_obj(), scale=150,angle=c(0,180,0)) generate_cornell_mesh() |> add_shape(scale_mesh(translate_mesh(robj,c(400,100,155)),0.5, center=c(400,100,155))) |> add_shape(scale_mesh(translate_mesh(robj,c(555/2,200,555/2)),1.5, center=c(555/2,200,555/2))) |> add_shape(scale_mesh(translate_mesh(robj,c(55,300,400)),c(0.5,2,0.5), center=c(155,300,400))) |> rasterize_scene(light_info=directional_light(direction=c(0.1,0.6,-1)))#Scale a mesh in the Cornell box robj = obj_mesh(r_obj(), scale=150,angle=c(0,180,0)) generate_cornell_mesh() |> add_shape(scale_mesh(translate_mesh(robj,c(400,100,155)),0.5, center=c(400,100,155))) |> add_shape(scale_mesh(translate_mesh(robj,c(555/2,200,555/2)),1.5, center=c(555/2,200,555/2))) |> add_shape(scale_mesh(translate_mesh(robj,c(55,300,400)),c(0.5,2,0.5), center=c(155,300,400))) |> rasterize_scene(light_info=directional_light(direction=c(0.1,0.6,-1)))
Scale Mesh to Unit Bounding Box
scale_unit_mesh(mesh, center_mesh = FALSE)scale_unit_mesh(mesh, center_mesh = FALSE)
mesh |
The mesh. |
center_mesh |
Default |
Scaled mesh
#Scale the Cornell box (and contents) down to the unit box. robj = obj_mesh(r_obj(), scale=150,angle=c(0,180,0)) generate_cornell_mesh() |> add_shape(scale_mesh(translate_mesh(robj,c(400,100,155)),0.5, center=c(400,100,155))) |> add_shape(scale_mesh(translate_mesh(robj,c(555/2,200,555/2)),1.5, center=c(555/2,200,555/2))) |> add_shape(scale_mesh(translate_mesh(robj,c(55,300,400)),c(0.5,2,0.5), center=c(155,300,400))) |> scale_unit_mesh(center_mesh = TRUE) |> rasterize_scene(light_info=directional_light(direction=c(0.1,0.6,-1)), lookfrom = c(0,0,-2), lookat=c(0,0,0))#Scale the Cornell box (and contents) down to the unit box. robj = obj_mesh(r_obj(), scale=150,angle=c(0,180,0)) generate_cornell_mesh() |> add_shape(scale_mesh(translate_mesh(robj,c(400,100,155)),0.5, center=c(400,100,155))) |> add_shape(scale_mesh(translate_mesh(robj,c(555/2,200,555/2)),1.5, center=c(555/2,200,555/2))) |> add_shape(scale_mesh(translate_mesh(robj,c(55,300,400)),c(0.5,2,0.5), center=c(155,300,400))) |> scale_unit_mesh(center_mesh = TRUE) |> rasterize_scene(light_info=directional_light(direction=c(0.1,0.6,-1)), lookfrom = c(0,0,-2), lookat=c(0,0,0))
Fast generation of rayvertex scenes from a list of objects (much faster than calling
add_shape() on each object individually to build the scene). This returns a ray_scene object
that cdoes
scene_from_list(scene_list)scene_from_list(scene_list)
scene_list |
List containing rayvertex mesh objects. |
ray_scene containing mesh info.
#Build a scene out of cubes including 87 * 61 = 5307 objects scene = list() volcol = rainbow(103) counter = 1 for(i in 1:nrow(volcano)) { for(j in 1:ncol(volcano)) { scene[[counter]] = cube_mesh(position = c(i,(volcano[i,j]-94),j), material = material_list(diffuse = volcol[volcano[i,j]-92], ambient = volcol[volcano[i,j]-92], ambient_intensity = 0.2)) counter = counter + 1 } } #Quickly generate the new_scene = scene_from_list(scene) new_scene |> rotate_mesh(c(0,10,0), pivot_point = c(44,0,31)) |> add_shape(xz_rect_mesh(position=c(44,0,31),scale=500, material = material_list(diffuse="lightblue", ambient = "lightblue", ambient_intensity = 0.2))) |> rasterize_scene(lookfrom=c(500,500,500), lookat = c(44.00, 40.50, 31.00), width=800,height=800, fov=0, ortho_dimensions = c(140,140), light_info = directional_light(c(-0.6,1,0.6)))#Build a scene out of cubes including 87 * 61 = 5307 objects scene = list() volcol = rainbow(103) counter = 1 for(i in 1:nrow(volcano)) { for(j in 1:ncol(volcano)) { scene[[counter]] = cube_mesh(position = c(i,(volcano[i,j]-94),j), material = material_list(diffuse = volcol[volcano[i,j]-92], ambient = volcol[volcano[i,j]-92], ambient_intensity = 0.2)) counter = counter + 1 } } #Quickly generate the new_scene = scene_from_list(scene) new_scene |> rotate_mesh(c(0,10,0), pivot_point = c(44,0,31)) |> add_shape(xz_rect_mesh(position=c(44,0,31),scale=500, material = material_list(diffuse="lightblue", ambient = "lightblue", ambient_intensity = 0.2))) |> rasterize_scene(lookfrom=c(500,500,500), lookat = c(44.00, 40.50, 31.00), width=800,height=800, fov=0, ortho_dimensions = c(140,140), light_info = directional_light(c(-0.6,1,0.6)))
Segment 3D Model
segment_mesh( start = c(0, -1, 0), end = c(0, 1, 0), radius = 0.5, direction = NA, from_center = TRUE, square = FALSE, material = material_list() )segment_mesh( start = c(0, -1, 0), end = c(0, 1, 0), radius = 0.5, direction = NA, from_center = TRUE, square = FALSE, material = material_list() )
start |
Default |
end |
Default |
radius |
Default |
direction |
Default |
from_center |
Default |
square |
Default |
material |
Default |
List describing the mesh.
#Generate a segment in the cornell box. generate_cornell_mesh() |> add_shape(segment_mesh(start = c(100, 100, 100), end = c(455, 455, 455), radius = 50)) |> rasterize_scene(light_info = directional_light(c(0,0.5,-1))) # Draw a line graph representing a normal distribution, but with metal: xvals = seq(-3, 3, length.out = 30) yvals = dnorm(xvals) scene_list = list() for(i in 1:(length(xvals) - 1)) { scene_list = add_shape(scene_list, segment_mesh(start = c(555/2 + xvals[i] * 80, yvals[i] * 800, 555/2), end = c(555/2 + xvals[i + 1] * 80, yvals[i + 1] * 800, 555/2), radius = 10, material = material_list(diffuse="purple", type="phong"))) } generate_cornell_mesh() |> add_shape(scene_list) |> rasterize_scene(light_info = directional_light(c(0,0.5,-1))) #Draw the outline of a cube: cube_outline = segment_mesh(start = c(100, 100, 100), end = c(100, 100, 455), radius = 10) |> add_shape(segment_mesh(start = c(100, 100, 100), end = c(100, 455, 100), radius = 10)) |> add_shape(segment_mesh(start = c(100, 100, 100), end = c(455, 100, 100), radius = 10)) |> add_shape(segment_mesh(start = c(100, 100, 455), end = c(100, 455, 455), radius = 10)) |> add_shape(segment_mesh(start = c(100, 100, 455), end = c(455, 100, 455), radius = 10)) |> add_shape(segment_mesh(start = c(100, 455, 455), end = c(100, 455, 100), radius = 10)) |> add_shape(segment_mesh(start = c(100, 455, 455), end = c(455, 455, 455), radius = 10)) |> add_shape(segment_mesh(start = c(455, 455, 100), end = c(455, 100, 100), radius = 10)) |> add_shape(segment_mesh(start = c(455, 455, 100), end = c(455, 455, 455), radius = 10)) |> add_shape(segment_mesh(start = c(455, 100, 100), end = c(455, 100, 455), radius = 10)) |> add_shape(segment_mesh(start = c(455, 100, 455), end = c(455, 455, 455), radius = 10)) |> add_shape(segment_mesh(start = c(100, 455, 100), end = c(455, 455, 100), radius = 10)) generate_cornell_mesh() |> add_shape(set_material(cube_outline,diffuse="dodgerblue",type="phong")) |> rasterize_scene(light_info = directional_light(c(0,0.5,-1))) #Shrink and rotate the cube generate_cornell_mesh() |> add_shape( scale_mesh(rotate_mesh(set_material(cube_outline,diffuse="dodgerblue",type="phong"), angle=c(45,45,45), pivot_point=c(555/2,555/2,555/2)),0.5, center=c(555/2,555/2,555/2))) |> rasterize_scene(light_info = directional_light(c(0,0.5,-1)))#Generate a segment in the cornell box. generate_cornell_mesh() |> add_shape(segment_mesh(start = c(100, 100, 100), end = c(455, 455, 455), radius = 50)) |> rasterize_scene(light_info = directional_light(c(0,0.5,-1))) # Draw a line graph representing a normal distribution, but with metal: xvals = seq(-3, 3, length.out = 30) yvals = dnorm(xvals) scene_list = list() for(i in 1:(length(xvals) - 1)) { scene_list = add_shape(scene_list, segment_mesh(start = c(555/2 + xvals[i] * 80, yvals[i] * 800, 555/2), end = c(555/2 + xvals[i + 1] * 80, yvals[i + 1] * 800, 555/2), radius = 10, material = material_list(diffuse="purple", type="phong"))) } generate_cornell_mesh() |> add_shape(scene_list) |> rasterize_scene(light_info = directional_light(c(0,0.5,-1))) #Draw the outline of a cube: cube_outline = segment_mesh(start = c(100, 100, 100), end = c(100, 100, 455), radius = 10) |> add_shape(segment_mesh(start = c(100, 100, 100), end = c(100, 455, 100), radius = 10)) |> add_shape(segment_mesh(start = c(100, 100, 100), end = c(455, 100, 100), radius = 10)) |> add_shape(segment_mesh(start = c(100, 100, 455), end = c(100, 455, 455), radius = 10)) |> add_shape(segment_mesh(start = c(100, 100, 455), end = c(455, 100, 455), radius = 10)) |> add_shape(segment_mesh(start = c(100, 455, 455), end = c(100, 455, 100), radius = 10)) |> add_shape(segment_mesh(start = c(100, 455, 455), end = c(455, 455, 455), radius = 10)) |> add_shape(segment_mesh(start = c(455, 455, 100), end = c(455, 100, 100), radius = 10)) |> add_shape(segment_mesh(start = c(455, 455, 100), end = c(455, 455, 455), radius = 10)) |> add_shape(segment_mesh(start = c(455, 100, 100), end = c(455, 100, 455), radius = 10)) |> add_shape(segment_mesh(start = c(455, 100, 455), end = c(455, 455, 455), radius = 10)) |> add_shape(segment_mesh(start = c(100, 455, 100), end = c(455, 455, 100), radius = 10)) generate_cornell_mesh() |> add_shape(set_material(cube_outline,diffuse="dodgerblue",type="phong")) |> rasterize_scene(light_info = directional_light(c(0,0.5,-1))) #Shrink and rotate the cube generate_cornell_mesh() |> add_shape( scale_mesh(rotate_mesh(set_material(cube_outline,diffuse="dodgerblue",type="phong"), angle=c(45,45,45), pivot_point=c(555/2,555/2,555/2)),0.5, center=c(555/2,555/2,555/2))) |> rasterize_scene(light_info = directional_light(c(0,0.5,-1)))
Set the material(s) of the mesh.
set_material( mesh, material = NULL, id = NULL, diffuse = c(0.5, 0.5, 0.5), ambient = c(0, 0, 0), specular = c(1, 1, 1), transmittance = c(0, 0, 0), emission = c(0, 0, 0), shininess = 50, ior = 1, dissolve = 1, illum = 1, texture_location = "", normal_texture_location = "", bump_texture_location = "", specular_texture_location = "", ambient_texture_location = "", emissive_texture_location = "", diffuse_intensity = 1, bump_intensity = 1, specular_intensity = 1, emission_intensity = 1, ambient_intensity = 1, culling = "back", type = "diffuse", translucent = TRUE, toon_levels = 5, toon_outline_width = 3, toon_outline_color = "black", reflection_intensity = 0, reflection_sharpness = 0, two_sided = FALSE, sigma = 0 )set_material( mesh, material = NULL, id = NULL, diffuse = c(0.5, 0.5, 0.5), ambient = c(0, 0, 0), specular = c(1, 1, 1), transmittance = c(0, 0, 0), emission = c(0, 0, 0), shininess = 50, ior = 1, dissolve = 1, illum = 1, texture_location = "", normal_texture_location = "", bump_texture_location = "", specular_texture_location = "", ambient_texture_location = "", emissive_texture_location = "", diffuse_intensity = 1, bump_intensity = 1, specular_intensity = 1, emission_intensity = 1, ambient_intensity = 1, culling = "back", type = "diffuse", translucent = TRUE, toon_levels = 5, toon_outline_width = 3, toon_outline_color = "black", reflection_intensity = 0, reflection_sharpness = 0, two_sided = FALSE, sigma = 0 )
mesh |
The target mesh. |
material |
Default |
id |
Default |
diffuse |
Default |
ambient |
Default |
specular |
Default |
transmittance |
Default |
emission |
Default |
shininess |
Default |
ior |
Default |
dissolve |
Default |
illum |
Default |
texture_location |
Default |
normal_texture_location |
Default |
bump_texture_location |
Default |
specular_texture_location |
Default |
ambient_texture_location |
Default |
emissive_texture_location |
Default |
diffuse_intensity |
Default |
bump_intensity |
Default |
specular_intensity |
Default |
emission_intensity |
Default |
ambient_intensity |
Default |
culling |
Default |
type |
Default |
translucent |
Default |
toon_levels |
Default |
toon_outline_width |
Default |
toon_outline_color |
Default |
reflection_intensity |
Default |
reflection_sharpness |
Default |
two_sided |
Default |
sigma |
Default |
Shape with new material
#Set the material of an object generate_cornell_mesh() |> add_shape(set_material(sphere_mesh(position=c(400,555/2,555/2),radius=40), diffuse="purple", type="phong")) |> add_shape(set_material(sphere_mesh(position=c(555/2,220,555/2),radius=40), dissolve=0.2,culling="none",diffuse="red")) |> add_shape(set_material(sphere_mesh(position=c(155,300,555/2),radius=60), material = material_list(diffuse="gold", type="phong", ambient="gold", ambient_intensity=0.4))) |> rasterize_scene(light_info=directional_light(direction=c(0.1,0.6,-1)))#Set the material of an object generate_cornell_mesh() |> add_shape(set_material(sphere_mesh(position=c(400,555/2,555/2),radius=40), diffuse="purple", type="phong")) |> add_shape(set_material(sphere_mesh(position=c(555/2,220,555/2),radius=40), dissolve=0.2,culling="none",diffuse="red")) |> add_shape(set_material(sphere_mesh(position=c(155,300,555/2),radius=60), material = material_list(diffuse="gold", type="phong", ambient="gold", ambient_intensity=0.4))) |> rasterize_scene(light_info=directional_light(direction=c(0.1,0.6,-1)))
Calculate Smooth Mesh Normals
smooth_normals_mesh(mesh, id = NA)smooth_normals_mesh(mesh, id = NA)
mesh |
The mesh. |
id |
Default |
Mesh with new vertex normals
#Let's construct a mesh from the volcano dataset #Build the vertex matrix vertex_list = list() counter = 1 for(i in 1:nrow(volcano)) { for(j in 1:ncol(volcano)) { vertex_list[[counter]] = matrix(c(j,volcano[i,j],i), ncol=3) counter = counter + 1 } } vertices = do.call(rbind,vertex_list) #Build the index matrix index_list = list() counter = 0 for(i in 1:(nrow(volcano)-1)) { for(j in 1:(ncol(volcano)-1)) { index_list[[counter+1]] = matrix(c(counter,counter+ncol(volcano),counter+1, counter+ncol(volcano),counter+ncol(volcano)+1,counter + 1), nrow=2, ncol=3, byrow=TRUE) counter = counter + 1 } counter = counter + 1 } indices = do.call(rbind,index_list) #Construct the mesh volc_mesh = construct_mesh(vertices = vertices, indices = indices, material = material_list(type="diffuse", diffuse="darkred", ambient = "darkred", ambient_intensity=0.2)) #Rasterize the no-normal scene scale_mesh(volc_mesh, scale = c(1,1/3,1)) |> center_mesh() |> rasterize_scene(lookfrom=c(-50,50,100),lookat=c(7,-15,0), fov=40,width=800,height=800, light_info = directional_light(c(0,1,1)) |> add_light(directional_light(c(1,1,-1)))) #Smooth the mesh volc_mesh_smooth = smooth_normals_mesh(volc_mesh) #Rasterize the scene scale_mesh(volc_mesh_smooth, scale = c(1,1/3,1)) |> center_mesh() |> rasterize_scene(lookfrom=c(-50,50,100),lookat=c(7,-15,0), fov=40,width=800,height=800, light_info = directional_light(c(0,1,1)) |> add_light(directional_light(c(1,1,-1))))#Let's construct a mesh from the volcano dataset #Build the vertex matrix vertex_list = list() counter = 1 for(i in 1:nrow(volcano)) { for(j in 1:ncol(volcano)) { vertex_list[[counter]] = matrix(c(j,volcano[i,j],i), ncol=3) counter = counter + 1 } } vertices = do.call(rbind,vertex_list) #Build the index matrix index_list = list() counter = 0 for(i in 1:(nrow(volcano)-1)) { for(j in 1:(ncol(volcano)-1)) { index_list[[counter+1]] = matrix(c(counter,counter+ncol(volcano),counter+1, counter+ncol(volcano),counter+ncol(volcano)+1,counter + 1), nrow=2, ncol=3, byrow=TRUE) counter = counter + 1 } counter = counter + 1 } indices = do.call(rbind,index_list) #Construct the mesh volc_mesh = construct_mesh(vertices = vertices, indices = indices, material = material_list(type="diffuse", diffuse="darkred", ambient = "darkred", ambient_intensity=0.2)) #Rasterize the no-normal scene scale_mesh(volc_mesh, scale = c(1,1/3,1)) |> center_mesh() |> rasterize_scene(lookfrom=c(-50,50,100),lookat=c(7,-15,0), fov=40,width=800,height=800, light_info = directional_light(c(0,1,1)) |> add_light(directional_light(c(1,1,-1)))) #Smooth the mesh volc_mesh_smooth = smooth_normals_mesh(volc_mesh) #Rasterize the scene scale_mesh(volc_mesh_smooth, scale = c(1,1/3,1)) |> center_mesh() |> rasterize_scene(lookfrom=c(-50,50,100),lookat=c(7,-15,0), fov=40,width=800,height=800, light_info = directional_light(c(0,1,1)) |> add_light(directional_light(c(1,1,-1))))
Sphere 3D Model
sphere_mesh( position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), radius = 1, low_poly = FALSE, normals = TRUE, material = material_list() )sphere_mesh( position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), radius = 1, low_poly = FALSE, normals = TRUE, material = material_list() )
position |
Default |
scale |
Default |
angle |
Default |
pivot_point |
Default |
order_rotation |
Default |
radius |
Default |
low_poly |
Default |
normals |
Default |
material |
Default |
List describing the mesh.
#Generate a sphere in the Cornell box. generate_cornell_mesh() |> add_shape(sphere_mesh(position = c(555/2, 555/2, 555/2), radius = 100)) |> rasterize_scene(light_info = directional_light(c(0,0.5,-1))) #Generate a shiny sphere in the Cornell box generate_cornell_mesh() |> add_shape(sphere_mesh(position = c(555/2, 100, 555/2), radius = 100, material = material_list(diffuse = "gold",type="phong"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1))) #Generate an ellipsoid in the Cornell box generate_cornell_mesh() |> add_shape(sphere_mesh(position = c(555/2, 210, 555/2), radius = 100, angle=c(0,30,0), scale = c(0.5,2,0.5), material = material_list(diffuse = "dodgerblue",type="phong"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1)))#Generate a sphere in the Cornell box. generate_cornell_mesh() |> add_shape(sphere_mesh(position = c(555/2, 555/2, 555/2), radius = 100)) |> rasterize_scene(light_info = directional_light(c(0,0.5,-1))) #Generate a shiny sphere in the Cornell box generate_cornell_mesh() |> add_shape(sphere_mesh(position = c(555/2, 100, 555/2), radius = 100, material = material_list(diffuse = "gold",type="phong"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1))) #Generate an ellipsoid in the Cornell box generate_cornell_mesh() |> add_shape(sphere_mesh(position = c(555/2, 210, 555/2), radius = 100, angle=c(0,30,0), scale = c(0.5,2,0.5), material = material_list(diffuse = "dodgerblue",type="phong"))) |> rasterize_scene(light_info = directional_light(c(0.5,0.5,-1)))
Applies Loop subdivision to the scene (or selected meshes).
subdivide_mesh( scene, id = NA, subdivision_levels = 2, simple = FALSE, normals = TRUE, verbose = FALSE )subdivide_mesh( scene, id = NA, subdivision_levels = 2, simple = FALSE, normals = TRUE, verbose = FALSE )
scene |
The scene to subdivide. |
id |
Default |
subdivision_levels |
Default |
simple |
Default |
normals |
Default |
verbose |
Default |
Scene with shape added.
#Subdivide the included R mesh obj_mesh(r_obj(),position=c(-0.5,0,0)) |> add_shape(subdivide_mesh(obj_mesh(r_obj(),position=c(0.5,0,0)), subdivision_levels = 2)) |> rasterize_scene(light_info = directional_light(direction=c(0.2,0.5,1)),fov=13)#Subdivide the included R mesh obj_mesh(r_obj(),position=c(-0.5,0,0)) |> add_shape(subdivide_mesh(obj_mesh(r_obj(),position=c(0.5,0,0)), subdivision_levels = 2)) |> rasterize_scene(light_info = directional_light(direction=c(0.2,0.5,1)),fov=13)
Swap Y/Z Axis
swap_yz(mesh)swap_yz(mesh)
mesh |
A raymesh object. |
Mesh with Y and Z axis exchanged
# Flip a mesh that's originally aligned along the y-axis cyl_mat = material_list(ambient="red", ambient_intensity=0.3, diffuse="red", diffuse_intensity=0.7) change_material(cylinder_mesh(length = 3, position=c(0,2,0), material = cyl_mat), diffuse="green", ambient="green") |> add_shape(swap_yz(cylinder_mesh(position=c(0,2,0), length=3, material = cyl_mat))) |> rasterize_scene(lookfrom=c(10,10,10), lookat=c(0,0,0), fov=40, light_info = directional_light(c(1,1,-1)), line_info = generate_line(end=c(10,0,0)) |> add_lines(generate_line(end=c(0,10,0),color="green")) |> add_lines(generate_line(end=c(0,0,10),color="red")))# Flip a mesh that's originally aligned along the y-axis cyl_mat = material_list(ambient="red", ambient_intensity=0.3, diffuse="red", diffuse_intensity=0.7) change_material(cylinder_mesh(length = 3, position=c(0,2,0), material = cyl_mat), diffuse="green", ambient="green") |> add_shape(swap_yz(cylinder_mesh(position=c(0,2,0), length=3, material = cyl_mat))) |> rasterize_scene(lookfrom=c(10,10,10), lookat=c(0,0,0), fov=40, light_info = directional_light(c(1,1,-1)), line_info = generate_line(end=c(10,0,0)) |> add_lines(generate_line(end=c(0,10,0),color="green")) |> add_lines(generate_line(end=c(0,0,10),color="red")))
Text Object
text3d_mesh( label, position = c(0, 0, 0), text_height = 1, orientation = "xy", font_color = "black", font_size = 100, font = "sans", font_lineheight = 12, background_color = "white", background_alpha = 0, angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), scale = c(1, 1, 1), color = NULL )text3d_mesh( label, position = c(0, 0, 0), text_height = 1, orientation = "xy", font_color = "black", font_size = 100, font = "sans", font_lineheight = 12, background_color = "white", background_alpha = 0, angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), scale = c(1, 1, 1), color = NULL )
label |
Text string. |
position |
Default |
text_height |
Default |
orientation |
Default |
font_color |
Default |
font_size |
Default |
font |
Default |
font_lineheight |
Default |
background_color |
Default |
background_alpha |
Default |
angle |
Default |
pivot_point |
Default |
order_rotation |
Default |
scale |
Default |
color |
Default |
List describing the mesh.
#Generate a label in the Cornell box. generate_cornell_mesh() |> add_shape(text3d_mesh(label="Cornell Box", position=c(555/2,555/2,555/2),angle=c(0,180,0), text_height=120)) |> rasterize_scene(light_info = directional_light(c(0.1,0.4,-1))) #Change the orientation generate_cornell_mesh() |> add_shape(text3d_mesh(label="YZ Plane", position=c(540,555/2,555/2),text_height=180, orientation = "yz",angle=c(0,180,0))) |> add_shape(text3d_mesh(label="XY Plane", position=c(555/2,555/2,540),text_height=180, orientation = "xy", angle=c(0,180,0))) |> add_shape(text3d_mesh(label="XZ Plane", position=c(555/2,15,555/2),text_height=180, orientation = "xz", angle=c(0,180,0))) |> rasterize_scene(light_info = directional_light(c(0.1,0.4,-1))) #Add an label in front of a sphere and change the font generate_cornell_mesh() |> add_shape(text3d_mesh(label="Cornell Box", position=c(555/2,555/2,555/2),text_height=180, font = "Serif", font_color="orange", angle=c(0,180,0))) |> add_shape(text3d_mesh(label="Sphere", position=c(555/2,130,100),text_height=100, font = "sans", font_color="lightblue",angle=c(0,180,40))) |> add_shape(sphere_mesh(radius=100,position=c(555/2,100,555/2), material=material_list(diffuse="purple",type="phong"))) |> rasterize_scene(light_info = directional_light(c(0.1,0.4,-1))) #A room full of b's set.seed(1) bee_scene = list() for(i in 1:100) { bee_scene = add_shape(bee_scene, text3d_mesh("B", position=c(20+runif(3)*525), font_color="yellow", text_height = 100, angle=c(0,180,0))) } generate_cornell_mesh() |> add_shape(bee_scene) |> rasterize_scene(light=directional_light(c(0,1,-1))) #A room full of bees bee_scene = list() set.seed(1) for(i in 1:100) { bee_scene = add_shape(bee_scene, text3d_mesh("\U1F41D", position=c(20+runif(3)*525), font_color="yellow", text_height = 100, angle=c(0,180,0))) } generate_cornell_mesh() |> add_shape(bee_scene) |> rasterize_scene(light=directional_light(c(0,1,-1)))#Generate a label in the Cornell box. generate_cornell_mesh() |> add_shape(text3d_mesh(label="Cornell Box", position=c(555/2,555/2,555/2),angle=c(0,180,0), text_height=120)) |> rasterize_scene(light_info = directional_light(c(0.1,0.4,-1))) #Change the orientation generate_cornell_mesh() |> add_shape(text3d_mesh(label="YZ Plane", position=c(540,555/2,555/2),text_height=180, orientation = "yz",angle=c(0,180,0))) |> add_shape(text3d_mesh(label="XY Plane", position=c(555/2,555/2,540),text_height=180, orientation = "xy", angle=c(0,180,0))) |> add_shape(text3d_mesh(label="XZ Plane", position=c(555/2,15,555/2),text_height=180, orientation = "xz", angle=c(0,180,0))) |> rasterize_scene(light_info = directional_light(c(0.1,0.4,-1))) #Add an label in front of a sphere and change the font generate_cornell_mesh() |> add_shape(text3d_mesh(label="Cornell Box", position=c(555/2,555/2,555/2),text_height=180, font = "Serif", font_color="orange", angle=c(0,180,0))) |> add_shape(text3d_mesh(label="Sphere", position=c(555/2,130,100),text_height=100, font = "sans", font_color="lightblue",angle=c(0,180,40))) |> add_shape(sphere_mesh(radius=100,position=c(555/2,100,555/2), material=material_list(diffuse="purple",type="phong"))) |> rasterize_scene(light_info = directional_light(c(0.1,0.4,-1))) #A room full of b's set.seed(1) bee_scene = list() for(i in 1:100) { bee_scene = add_shape(bee_scene, text3d_mesh("B", position=c(20+runif(3)*525), font_color="yellow", text_height = 100, angle=c(0,180,0))) } generate_cornell_mesh() |> add_shape(bee_scene) |> rasterize_scene(light=directional_light(c(0,1,-1))) #A room full of bees bee_scene = list() set.seed(1) for(i in 1:100) { bee_scene = add_shape(bee_scene, text3d_mesh("\U1F41D", position=c(20+runif(3)*525), font_color="yellow", text_height = 100, angle=c(0,180,0))) } generate_cornell_mesh() |> add_shape(bee_scene) |> rasterize_scene(light=directional_light(c(0,1,-1)))
Torus 3D Model
torus_mesh( position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), radius = 0.5, ring_radius = 0.2, sides = 36, rings = 36, material = material_list() )torus_mesh( position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), radius = 0.5, ring_radius = 0.2, sides = 36, rings = 36, material = material_list() )
position |
Default |
scale |
Default |
angle |
Default |
pivot_point |
Default |
order_rotation |
Default |
radius |
Default |
ring_radius |
Default |
sides |
Default |
rings |
Default |
material |
Default |
List describing the mesh.
#Plot a group of tori in the cornell box generate_cornell_mesh(ceiling = FALSE) |> add_shape(torus_mesh(position=c(555/2,555/3,555/2), angle=c(20,0,45), radius=120, ring_radius = 40, material = material_list(diffuse="dodgerblue4",type="phong", ambient="dodgerblue4",ambient_intensity=0.2))) |> add_shape(torus_mesh(position=c(400,400,555/2), angle=c(20,200,45),radius=80, ring_radius = 30, material=material_list(diffuse="orange",type="phong", ambient="orange",ambient_intensity=0.2))) |> add_shape(torus_mesh(position=c(150,450,555/2), angle=c(60,180,0),radius=40, ring_radius = 20, material=material_list(diffuse="red",type="phong"))) |> rasterize_scene(light_info = directional_light(c(0,1,-2)))#Plot a group of tori in the cornell box generate_cornell_mesh(ceiling = FALSE) |> add_shape(torus_mesh(position=c(555/2,555/3,555/2), angle=c(20,0,45), radius=120, ring_radius = 40, material = material_list(diffuse="dodgerblue4",type="phong", ambient="dodgerblue4",ambient_intensity=0.2))) |> add_shape(torus_mesh(position=c(400,400,555/2), angle=c(20,200,45),radius=80, ring_radius = 30, material=material_list(diffuse="orange",type="phong", ambient="orange",ambient_intensity=0.2))) |> add_shape(torus_mesh(position=c(150,450,555/2), angle=c(60,180,0),radius=40, ring_radius = 20, material=material_list(diffuse="red",type="phong"))) |> rasterize_scene(light_info = directional_light(c(0,1,-2)))
Transform Mesh (3x3 or 4x4, row-vector)
transform_mesh(mesh, transform_matrix)transform_mesh(mesh, transform_matrix)
mesh |
The mesh. |
transform_matrix |
3x3 (linear only) or 4x4 row-vector homogeneous. |
Transformed mesh. For 4x4, applies rotation/scale and translation.
Translate Lines
translate_lines(lines, position = 1)translate_lines(lines, position = 1)
lines |
The line scene. |
position |
Default |
Translated line matrix.
#Generate a cube out of lines cube_outline = generate_line(start = c(-1, -1, -1), end = c(-1, -1, 1)) |> add_lines(generate_line(start = c(-1, -1, -1), end = c(-1, 1, -1))) |> add_lines(generate_line(start = c(-1, -1, -1), end = c(1, -1, -1))) |> add_lines(generate_line(start = c(-1, -1, 1), end = c(-1, 1, 1))) |> add_lines(generate_line(start = c(-1, -1, 1), end = c(1, -1, 1))) |> add_lines(generate_line(start = c(-1, 1, 1), end = c(-1, 1, -1))) |> add_lines(generate_line(start = c(-1, 1, 1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(1, 1, -1), end = c(1, -1, -1))) |> add_lines(generate_line(start = c(1, 1, -1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(1, -1, -1), end = c(1, -1, 1))) |> add_lines(generate_line(start = c(1, -1, 1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(-1, 1, -1), end = c(1, 1, -1))) rasterize_lines(cube_outline,fov=40,lookfrom=c(1,2,10),lookat=c(0,0,0)) #Scale the cube uniformly translated_cube = color_lines(translate_lines(cube_outline,c(1,1,1)),"red") translated_cube2 = color_lines(translate_lines(cube_outline,c(-1,-1,-1)),"green") cube_outline |> add_lines(translated_cube) |> add_lines(translated_cube2) |> rasterize_lines(fov=40,lookfrom=c(1,2,10),lookat=c(0,0,0))#Generate a cube out of lines cube_outline = generate_line(start = c(-1, -1, -1), end = c(-1, -1, 1)) |> add_lines(generate_line(start = c(-1, -1, -1), end = c(-1, 1, -1))) |> add_lines(generate_line(start = c(-1, -1, -1), end = c(1, -1, -1))) |> add_lines(generate_line(start = c(-1, -1, 1), end = c(-1, 1, 1))) |> add_lines(generate_line(start = c(-1, -1, 1), end = c(1, -1, 1))) |> add_lines(generate_line(start = c(-1, 1, 1), end = c(-1, 1, -1))) |> add_lines(generate_line(start = c(-1, 1, 1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(1, 1, -1), end = c(1, -1, -1))) |> add_lines(generate_line(start = c(1, 1, -1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(1, -1, -1), end = c(1, -1, 1))) |> add_lines(generate_line(start = c(1, -1, 1), end = c(1, 1, 1))) |> add_lines(generate_line(start = c(-1, 1, -1), end = c(1, 1, -1))) rasterize_lines(cube_outline,fov=40,lookfrom=c(1,2,10),lookat=c(0,0,0)) #Scale the cube uniformly translated_cube = color_lines(translate_lines(cube_outline,c(1,1,1)),"red") translated_cube2 = color_lines(translate_lines(cube_outline,c(-1,-1,-1)),"green") cube_outline |> add_lines(translated_cube) |> add_lines(translated_cube2) |> rasterize_lines(fov=40,lookfrom=c(1,2,10),lookat=c(0,0,0))
Translate Mesh
translate_mesh(mesh, position = c(0, 0, 0))translate_mesh(mesh, position = c(0, 0, 0))
mesh |
The mesh. |
position |
Default |
Translated mesh
#Translate a mesh in the Cornell box robj = obj_mesh(r_obj(), scale=150,angle=c(0,180,0)) generate_cornell_mesh() |> add_shape(translate_mesh(robj,c(400,100,155))) |> add_shape(translate_mesh(robj,c(555/2,200,555/2))) |> add_shape(translate_mesh(robj,c(155,300,400))) |> rasterize_scene(light_info=directional_light(direction=c(0.1,0.6,-1)))#Translate a mesh in the Cornell box robj = obj_mesh(r_obj(), scale=150,angle=c(0,180,0)) generate_cornell_mesh() |> add_shape(translate_mesh(robj,c(400,100,155))) |> add_shape(translate_mesh(robj,c(555/2,200,555/2))) |> add_shape(translate_mesh(robj,c(155,300,400))) |> rasterize_scene(light_info=directional_light(direction=c(0.1,0.6,-1)))
This function takes a mesh and validates it. The mesh should be a list with "shapes", "materials", "vertices", "texcoords", "normals", and "material_hashes" entries.
validate_mesh(mesh, validate_materials = TRUE)validate_mesh(mesh, validate_materials = TRUE)
mesh |
List. A mesh is a list as described above. |
validate_materials |
Default |
A mesh.
Each "shapes" entry should be a list with "mesh", "name", and "material" entries. Each "mesh" entry should have "indices", "tex_indices", "norm_indices", "material_ids", "has_vertex_tex", and "has_vertex_normals". The indices should not exceed the number of rows in their corresponding vertex/normal/texcoord data. There should be no NA/NaN values in the vertex/normal/texcoord data.
Each "materials" entry is expected to be a list with several entries with specific required lengths, as listed below:
| Attribute | Length | Type |
| diffuse | 3 | Numeric |
| ambient | 3 | Numeric |
| specular | 3 | Numeric |
| transmittance | 3 | Numeric |
| emission | 3 | Numeric |
| shininess | 1 | Numeric |
| ior | 1 | Numeric |
| dissolve | 1 | Numeric |
| illum | 1 | Numeric |
| diffuse_texname | 1 | Character |
| normal_texname | 1 | Character |
| bump_texname | 1 | Character |
| specular_texname | 1 | Character |
| ambient_texname | 1 | Character |
| emissive_texname | 1 | Character |
| diffuse_intensity | 1 | Numeric |
| bump_intensity | 1 | Numeric |
| specular_intensity | 1 | Numeric |
| emission_intensity | 1 | Numeric |
| ambient_intensity | 1 | Numeric |
| culling | 1 | Character |
| type | 1 | Character |
| translucent | 1 | Logical |
| toon_levels | 1 | Numeric |
| toon_outline_width | 1 | Numeric |
| toon_outline_color | 3 | Numeric |
| reflection_intensity | 1 | Numeric |
| reflection_sharpness | 1 | Numeric |
| two_sided | 1 | Logical |
Note: This materials validation only applies to the rayvertex package. Other renderers might choose to use their own information in the material list.
# validate a mesh mesh = validate_mesh(sphere_mesh())# validate a mesh mesh = validate_mesh(sphere_mesh())
Writes the current scene to a Wavefront OBJ file, with or without materials
write_scene_to_obj(scene, filename, materials = TRUE, fileext = ".obj")write_scene_to_obj(scene, filename, materials = TRUE, fileext = ".obj")
scene |
A rayvertex scene. |
filename |
The filename for the OBJ file. |
materials |
Default |
fileext |
Default |
None
tmpfile = tempfile(fileext = ".obj") write_scene_to_obj(generate_cornell_mesh(), tmpfile)tmpfile = tempfile(fileext = ".obj") write_scene_to_obj(generate_cornell_mesh(), tmpfile)
XY Rectangle 3D Model
xy_rect_mesh( position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), material = material_list() )xy_rect_mesh( position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), material = material_list() )
position |
Default |
scale |
Default |
angle |
Default |
pivot_point |
Default |
order_rotation |
Default |
material |
Default |
List describing the mesh.
generate_cornell_mesh() |> add_shape(xy_rect_mesh(position = c(555/2, 100, 555/2), scale=200, material = material_list(diffuse = "purple"),angle=c(0,180,0))) |> rasterize_scene(light_info = directional_light(c(0,0.5,-1))) #Rotate the plane and scale generate_cornell_mesh() |> add_shape(xy_rect_mesh(position = c(555/2, 100, 555/2), scale=c(200,100,1), angle=c(0,180,0), material = material_list(diffuse = "purple"))) |> rasterize_scene(light_info = directional_light(c(0,0.5,-1)))generate_cornell_mesh() |> add_shape(xy_rect_mesh(position = c(555/2, 100, 555/2), scale=200, material = material_list(diffuse = "purple"),angle=c(0,180,0))) |> rasterize_scene(light_info = directional_light(c(0,0.5,-1))) #Rotate the plane and scale generate_cornell_mesh() |> add_shape(xy_rect_mesh(position = c(555/2, 100, 555/2), scale=c(200,100,1), angle=c(0,180,0), material = material_list(diffuse = "purple"))) |> rasterize_scene(light_info = directional_light(c(0,0.5,-1)))
XZ Rectangle 3D Model
xz_rect_mesh( position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), material = material_list() )xz_rect_mesh( position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), material = material_list() )
position |
Default |
scale |
Default |
angle |
Default |
pivot_point |
Default |
order_rotation |
Default |
material |
Default |
List describing the mesh.
generate_cornell_mesh() |> add_shape(xz_rect_mesh(position = c(555/2, 100, 555/2), scale=200, material = material_list(diffuse = "purple"))) |> rasterize_scene(light_info = directional_light(c(0,0.5,-1))) #Rotate the plane and scale generate_cornell_mesh() |> add_shape(xz_rect_mesh(position = c(555/2, 100, 555/2), scale=c(200,1,100), angle=c(0,30,0), material = material_list(diffuse = "purple"))) |> rasterize_scene(light_info = directional_light(c(0,0.5,-1)))generate_cornell_mesh() |> add_shape(xz_rect_mesh(position = c(555/2, 100, 555/2), scale=200, material = material_list(diffuse = "purple"))) |> rasterize_scene(light_info = directional_light(c(0,0.5,-1))) #Rotate the plane and scale generate_cornell_mesh() |> add_shape(xz_rect_mesh(position = c(555/2, 100, 555/2), scale=c(200,1,100), angle=c(0,30,0), material = material_list(diffuse = "purple"))) |> rasterize_scene(light_info = directional_light(c(0,0.5,-1)))
YZ Rectangle 3D Model
yz_rect_mesh( position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), material = material_list() )yz_rect_mesh( position = c(0, 0, 0), scale = c(1, 1, 1), angle = c(0, 0, 0), pivot_point = c(0, 0, 0), order_rotation = c(1, 2, 3), material = material_list() )
position |
Default |
scale |
Default |
angle |
Default |
pivot_point |
Default |
order_rotation |
Default |
material |
Default |
List describing the mesh.
generate_cornell_mesh() |> add_shape(yz_rect_mesh(position = c(555/2, 100, 555/2), scale=c(200,1,200), angle=c(0,0,0), material = material_list(diffuse = "purple"))) |> rasterize_scene(light_info = directional_light(c(0,0.5,-1))) #Rotate and scale generate_cornell_mesh() |> add_shape(yz_rect_mesh(position = c(555/2, 100, 555/2), scale=c(300,1,200), angle=c(0,45,0), material = material_list(diffuse = "purple"))) |> rasterize_scene(light_info = directional_light(c(0,0.5,-1)))generate_cornell_mesh() |> add_shape(yz_rect_mesh(position = c(555/2, 100, 555/2), scale=c(200,1,200), angle=c(0,0,0), material = material_list(diffuse = "purple"))) |> rasterize_scene(light_info = directional_light(c(0,0.5,-1))) #Rotate and scale generate_cornell_mesh() |> add_shape(yz_rect_mesh(position = c(555/2, 100, 555/2), scale=c(300,1,200), angle=c(0,45,0), material = material_list(diffuse = "purple"))) |> rasterize_scene(light_info = directional_light(c(0,0.5,-1)))