### R Example: Constructing a Mesh and Applying UV Mapping (Direct Camera Mapping) Source: https://www.rayvertex.com/reference/add_plane_uv_mesh.html This example demonstrates applying UV mapping to a mesh with a direction vector pointing directly at the camera, using `add_plane_uv_mesh` and then rasterizing the scene. This results in a different texture orientation compared to the previous example. ```R #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)) #> Setting `lookat` to: c(0.00, 0.00, 0.00) ``` -------------------------------- ### Example: Constructing a Mesh Source: https://www.rayvertex.com/reference/displace_mesh.html This example demonstrates how to construct a mesh, likely as a prerequisite for using the `displace_mesh` function. It uses the 'volcano' dataset. ```R #Let's construct a mesh from the volcano dataset ``` -------------------------------- ### Load OBJ File Example Source: https://www.rayvertex.com/reference/read_obj.html Example of loading an OBJ file named 'arrow.txt' from the package's extdata directory. The result is stored in the 'sphere' object. ```r sphere = read_obj(system.file("extdata", "arrow.txt", package="rayvertex")) ``` -------------------------------- ### R Example: Constructing a Mesh and Applying UV Mapping (Carpet Orientation) Source: https://www.rayvertex.com/reference/add_plane_uv_mesh.html This example demonstrates constructing a mesh from the volcano dataset, creating a checkerboard texture, and applying it to the mesh using `add_plane_uv_mesh` with a specific direction to simulate a carpet-like mapping. It then rasterizes the scene. ```R #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) #> agg_record_1f4e4d2565c4 #> 2 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)) #> Setting `lookat` to: c(0.00, 0.00, 0.00) ``` -------------------------------- ### Basic Scene Rendering Example Source: https://www.rayvertex.com/reference/rasterize_scene.html This example demonstrates a basic usage of `rasterize_scene` by rendering a cube mesh. It customizes the camera's position (`lookfrom`) and the direction of the light source. ```R #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))) #> Setting `lookat` to: c(0.00, 0.00, 0.00) ``` -------------------------------- ### PLY Mesh Loading Example Source: https://www.rayvertex.com/reference/ply_mesh.html This example demonstrates how to load a PLY model, noting that no sample PLY models are included with the package and materials are not part of PLY files. ```R #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). ``` -------------------------------- ### Add Point Lights and Vary Color Source: https://www.rayvertex.com/reference/point_light.html Illustrates adding point lights with different colors to a scene. This example chains `point_light` calls with `add_light` to create a multi-colored lighting setup. ```R #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) #> Setting default values for Cornell box: lookfrom `c(278,278,-800)` lookat `c(278,278,0)` fov `40` . ``` -------------------------------- ### R Example: Write Scene to OBJ Source: https://www.rayvertex.com/reference/write_scene_to_obj.html Demonstrates saving a generated Cornell mesh scene to a temporary OBJ file. Ensure the 'rayvertex' package is installed and loaded. ```R tmpfile = tempfile(fileext = ".obj") write_scene_to_obj(generate_cornell_mesh(), tmpfile) ``` -------------------------------- ### Generate and Scale a Cube Outline Source: https://www.rayvertex.com/reference/scale_lines.html This example demonstrates generating a cube outline using `generate_line` and `add_lines`, then scaling it uniformly with `scale_lines` and visualizing both the original and scaled cube. ```R #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)) #> Setting `lookat` to: c(0.00, 0.00, 0.00) #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)) #> Setting `lookat` to: c(0.00, 0.00, 0.00) ``` -------------------------------- ### get_time Source: https://www.rayvertex.com/reference/get_time.html Retrieves the current time. The `init` parameter controls initial setup. ```APIDOC ## get_time ### Description Retrieves the current time. The `init` parameter controls initial setup. ### Parameters #### Arguments - **init** (logical) - Optional - If TRUE, performs initialization. ### Value Nothing ``` -------------------------------- ### R Example: Subdividing a Mesh Source: https://www.rayvertex.com/reference/subdivide_mesh.html Demonstrates how to subdivide a mesh using the `subdivide_mesh` function in R. This example adds a subdivided mesh to a scene and then rasterizes the scene. ```R #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) #> Setting `lookat` to: c(-0.00, 0.00, 0.00) ``` -------------------------------- ### Generate a Cube Outline Source: https://www.rayvertex.com/reference/add_lines.html This example demonstrates how to create a 3D cube outline by sequentially adding lines using `generate_line` and `add_lines`. The resulting line matrix is then visualized using `rasterize_lines`. ```R 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)) ``` -------------------------------- ### Create Sphere with Custom Material and Rasterize Scene Source: https://www.rayvertex.com/reference/material_list.html This example demonstrates creating a sphere mesh with a custom material defined using `material_list` and then rendering it in a scene with a directional light. ```R 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))) #> Setting `lookat` to: c(277.50, 277.50, 277.50) ``` -------------------------------- ### R Example: Modifying Multiple Shapes Source: https://www.rayvertex.com/reference/change_material.html Demonstrates how to apply `change_material` to multiple shapes within a scene, including changing diffuse color, transparency, and shader type. This example shows creating a sphere, adding it to a scene, and then modifying its material properties before rendering. ```R 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))) #> Setting default values for Cornell box: lookfrom `c(278,278,-800)` lookat `c(278,278,0)` fov `40` . ``` -------------------------------- ### Add Point Lights and Vary Intensity Source: https://www.rayvertex.com/reference/point_light.html Demonstrates adding multiple point lights to a scene and adjusting their intensity. This example shows how to chain `point_light` calls with `add_light` and then render the scene. ```R #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) #> Setting default values for Cornell box: lookfrom `c(278,278,-800)` lookat `c(278,278,0)` fov `40` . ``` -------------------------------- ### Apply Look-At Transform to a Mesh Source: https://www.rayvertex.com/reference/lookat_transform.html Example demonstrating how to create a sphere mesh, define camera parameters, generate a look-at transform matrix, and then apply it to the mesh for rendering. ```R 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 spiral of lines Source: https://www.rayvertex.com/reference/generate_line.html This example demonstrates how to create a spiral of lines by iteratively calling generate_line within a loop and adding them to a line matrix. It visualizes a 3D spiral. ```R # 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) # ``` -------------------------------- ### Basic Scene Rasterization with a Base Model Source: https://www.rayvertex.com/reference/rasterize_scene.html Renders a scene with a base model (a flattened, grey cube). This is a foundational example for scene rendering. ```R 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))) ``` -------------------------------- ### Construct Mesh from Volcano Dataset Source: https://www.rayvertex.com/reference/add_sphere_uv_mesh.html Example of constructing a mesh from the volcano dataset, which can then be used with the `add_sphere_uv_mesh` function. ```R #Let's construct a mesh from the volcano dataset ``` -------------------------------- ### Example: Center and Combine Meshes Source: https://www.rayvertex.com/reference/center_mesh.html Demonstrates centering the Cornell box and an OBJ mesh, then combining them into a single scene for rasterization. Adjusts scale and rotation for the OBJ mesh before centering. ```R #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))) ``` -------------------------------- ### Flip Mesh Orientation Example Source: https://www.rayvertex.com/reference/flip_orientation_mesh.html Demonstrates how to flip the orientation of a mesh and add it to a scene for visualization. Requires the `sphere_mesh`, `add_shape`, and `rasterize_scene` functions. ```R 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) #> Setting `lookat` to: c(0.00, 0.00, 0.00) ``` -------------------------------- ### Add a Point Light to the Scene Source: https://www.rayvertex.com/reference/add_light.html Manually specify a point light and add it to the scene. This example demonstrates setting up a basic point light for the Cornell Box. ```r 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) ``` -------------------------------- ### Generate and Rasterize a Line Cube Source: https://www.rayvertex.com/reference/rasterize_lines.html This example demonstrates how to generate a cube composed entirely of lines using `generate_line` and `add_lines`, and then rasterize it using `rasterize_lines`. It sets a field of view and camera position for the rendering. ```R #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)) ``` -------------------------------- ### Constructing a Mesh from Volcano Dataset Source: https://www.rayvertex.com/reference/construct_mesh.html Example demonstrating how to construct a mesh from the 'volcano' dataset by building vertex and index matrices, then calling construct_mesh. This snippet requires the 'volcano' dataset to be available. ```R #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)))) #> Setting `lookat` to: c(31.00, 144.50, 44.00) ``` -------------------------------- ### Generate a Cone Source: https://www.rayvertex.com/reference/cone_mesh.html Generates a basic cone and adds it to a scene. Requires scene generation and rasterization setup. ```R 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))) ``` -------------------------------- ### Add a cube to a Cornell box scene Source: https://www.rayvertex.com/reference/cube_mesh.html This example demonstrates adding a cube to a pre-generated Cornell box scene. It shows how to set the position and scale of the cube. ```R 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))) ``` -------------------------------- ### Non-Uniformly Scale a Cube Outline Source: https://www.rayvertex.com/reference/scale_lines.html This example shows how to scale a cube outline non-uniformly along each axis using `scale_lines` with a vector of scale factors, and then visualizes the result. ```R #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)) #> Setting `lookat` to: c(0.00, 0.00, 0.00) ``` -------------------------------- ### Generate a Shiny Sphere in the Cornell Box Source: https://www.rayvertex.com/reference/sphere_mesh.html Example of creating a sphere with a 'gold' diffuse material and 'phong' type in the Cornell box. Demonstrates material customization. ```R #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))) #> Setting default values for Cornell box: lookfrom `c(278,278,-800)` lookat `c(278,278,0)` fov `40` . ``` -------------------------------- ### Add Multiple Lights to the Scene Source: https://www.rayvertex.com/reference/add_light.html Demonstrates adding multiple directional and point lights to a scene using the add_light function. This allows for complex lighting setups. ```r 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) ``` -------------------------------- ### Example: Adding an OBJ Mesh to a Scene Source: https://www.rayvertex.com/reference/obj_mesh.html Demonstrates reading a 3D R mesh and adding an OBJ model to it with specified position, scale, and rotation. Requires the 'generate_cornell_mesh' and 'add_shape' functions. ```R #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))) #> Setting default values for Cornell box: lookfrom `c(278,278,-800)` lookat `c(278,278,0)` fov `40` . ``` -------------------------------- ### Render lines in a 3D scene with a mesh Source: https://www.rayvertex.com/reference/generate_line.html This example integrates generated lines into a 3D scene that also contains a mesh object. It demonstrates how to combine different geometric elements and lighting for visualization. ```R #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) ``` -------------------------------- ### Load and Render R 3D Model Source: https://www.rayvertex.com/reference/r_obj.html Loads the 3D R logo mesh and renders it with specified camera and lighting. Ensure the `raybevel` package is installed. ```r #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))) #> Setting `lookat` to: c(0.00, 0.00, 0.00) ``` -------------------------------- ### Add Point Lights and Vary Quadratic Falloff Term Source: https://www.rayvertex.com/reference/point_light.html Demonstrates adding point lights and adjusting the quadratic falloff term. This example highlights the impact of the `falloff_quad` parameter on light attenuation. ```R #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) #> Setting default values for Cornell box: lookfrom `c(278,278,-800)` lookat `c(278,278,0)` fov `40` . ``` -------------------------------- ### Rasterize Scaled Lines with Color Source: https://www.rayvertex.com/reference/rasterize_lines.html This example shows how to scale a line-based cube uniformly and color its lines red before rasterization. It then combines the original cube with the scaled, colored version for rendering. ```R #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)) ``` -------------------------------- ### Generate lines with changing colors Source: https://www.rayvertex.com/reference/generate_line.html This example shows how to generate lines with dynamically changing colors by mapping colors from a HSV color spectrum to each line segment in a spiral. It visualizes a colorful 3D spiral. ```R #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) # ``` -------------------------------- ### Add Point Lights and Vary Falloff Term Source: https://www.rayvertex.com/reference/point_light.html Shows how to add point lights and adjust the linear falloff term. This example demonstrates the effect of changing the `falloff` parameter on light intensity over distance. ```R #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) #> Setting default values for Cornell box: lookfrom `c(278,278,-800)` lookat `c(278,278,0)` fov `40` . ``` -------------------------------- ### Add labels with custom fonts and colors Source: https://www.rayvertex.com/reference/text3d_mesh.html This example demonstrates adding text labels with custom font families and colors, alongside a sphere mesh. It highlights the use of 'font' and 'font_color' arguments. ```R #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))) ``` -------------------------------- ### Create R 3D Model Object Source: https://www.rayvertex.com/reference/r_obj.html Generates the file path for the 3D R logo model. Set `simple_r` to TRUE to get a 3D R instead of the R logo. ```r r_obj(simple_r = FALSE) ``` -------------------------------- ### Create a room full of bee emojis Source: https://www.rayvertex.com/reference/text3d_mesh.html Similar to the previous example, this code generates 100 text meshes, but this time uses the bee emoji (\U1F41D) to fill the scene with yellow emojis. ```R #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))) ``` -------------------------------- ### Rasterize Non-Uniformly Scaled Lines Source: https://www.rayvertex.com/reference/rasterize_lines.html This example demonstrates rasterizing a line-based cube that has been scaled non-uniformly along different axes and colored red. A different field of view and camera position are used for this rendering. ```R #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)) ``` -------------------------------- ### Add Segment to Cornell Box Mesh Source: https://www.rayvertex.com/reference/segment_mesh.html Generates a Cornell box mesh and adds a segment to it. This example demonstrates basic usage with custom start and end points and radius. ```R #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))) ``` -------------------------------- ### Example: Setting Multiple Materials on a Cornell Box Source: https://www.rayvertex.com/reference/set_material.html Demonstrates how to apply different materials to various spheres within a scene using set_material. This includes setting diffuse colors, transparency, and using predefined material lists. ```R #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))) #> Setting default values for Cornell box: lookfrom `c(278,278,-800)` lookat `c(278,278,0)` fov `40` . ``` -------------------------------- ### arrow_mesh Function Source: https://www.rayvertex.com/reference/arrow_mesh.html Creates a 3D arrow mesh. You can define the arrow by its start and end points, or by a starting point and a direction vector. Various parameters control the arrow's dimensions and appearance. ```APIDOC ## arrow_mesh ### Description Creates a 3D arrow mesh object. ### Arguments - **start** (numeric vector, length 3) - Optional. Default `c(0, 0, 0)`. The base coordinates (x, y, z) of the arrow. - **end** (numeric vector, length 3) - Optional. Default `c(0, 1, 0)`. The tip coordinates (x, y, z) of the arrow. - **radius_top** (numeric) - Optional. Default `0.5`. The radius of the arrow's tip. - **radius_tail** (numeric) - Optional. Default `0.2`. The radius of the arrow's tail. - **tail_proportion** (numeric) - Optional. Default `0.5`. The proportion of the arrow's length that constitutes the tail. - **direction** (numeric vector, length 3) - Optional. Default `NA`. An alternative to `start` and `end`. Specifies the direction vector of the arrow. If used, the arrow will be centered at `start` and its length determined by the magnitude of this vector. - **from_center** (logical) - Optional. Default `TRUE`. If `direction` is used, setting this to `FALSE` makes `start` specify the bottom of the cone instead of the middle. - **material** (list) - Optional. Default `material_list()` (default values). Specifies the material properties of the arrow. ### Value Returns a list describing the arrow mesh. ``` -------------------------------- ### Change Start and End Points for Multiple Arrows Source: https://www.rayvertex.com/reference/arrow_mesh.html Demonstrates how to add multiple arrows to a scene by defining distinct start and end points for each, with different colors. This is useful for representing multiple vectors or positions. ```R #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))) ``` -------------------------------- ### Create a Point Light Source: https://www.rayvertex.com/reference/point_light.html Initializes a point light with default or specified parameters. Use this to define a light source in a 3D scene. ```R point_light( position = c(0, 0, 0), color = "white", intensity = 1, constant = 1, falloff = 1, falloff_quad = 1 ) ``` -------------------------------- ### Coloring a Cube Outline Source: https://www.rayvertex.com/reference/color_lines.html This example demonstrates how to generate a cube outline using a series of `generate_line` and `add_lines` calls, and then color the entire outline red using the `color_lines` function before rasterizing it. Ensure the necessary functions for line generation and rasterization are available. ```R 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() #> Setting `lookat` to: c(0.00, 0.00, 0.00) ```