Load an obj file via a filepath. Currently only supports the diffuse texture with the `texture` argument. Note: light importance sampling currently not supported for this shape.

obj_model(
  filename,
  x = 0,
  y = 0,
  z = 0,
  scale_obj = 1,
  load_material = TRUE,
  load_textures = TRUE,
  load_normals = TRUE,
  vertex_colors = FALSE,
  calculate_consistent_normals = TRUE,
  subdivision_levels = 1,
  displacement_texture = "",
  displacement_intensity = 1,
  displacement_vector = FALSE,
  recalculate_normals = FALSE,
  importance_sample_lights = TRUE,
  material = diffuse(),
  angle = c(0, 0, 0),
  order_rotation = c(1, 2, 3),
  flipped = FALSE,
  scale = c(1, 1, 1)
)

Arguments

filename

Filename and path to the `obj` file. Can also be a `txt` file, if it's in the correct `obj` internally.

x

Default `0`. x-coordinate to offset the model.

y

Default `0`. y-coordinate to offset the model.

z

Default `0`. z-coordinate to offset the model.

scale_obj

Default `1`. Amount to scale the model. Use this to scale the object up or down on all axes, as it is more robust to numerical precision errors than the generic scale option.

load_material

Default `TRUE`. Whether to load the obj file material (MTL file). If material for faces aren't specified, the default material will be used (specified by the user in `material`).

load_textures

Default `TRUE`. If `load_material = TRUE`, whether to load textures in the MTL file (versus just using the colors specified for each material).

load_normals

Default `TRUE`. Whether to load the vertex normals if they exist in the OBJ file.

vertex_colors

Default `FALSE`. Set to `TRUE` if the OBJ file has vertex colors to apply them to the model.

calculate_consistent_normals

Default `TRUE`. Whether to calculate consistent vertex normals to prevent energy loss at edges.

subdivision_levels

Default `1`. Number of Loop subdivisions to be applied to the mesh.

displacement_texture

Default `""`. File path to the displacement texture. This texture is used to displace the vertices of the mesh based on the texture's pixel values.

displacement_intensity

Default `1`. Intensity of the displacement effect. Higher values result in greater displacement.

displacement_vector

Default `FALSE`. Whether to use vector displacement. If `TRUE`, the displacement texture is interpreted as providing a 3D displacement vector. Otherwise, the texture is interpreted as providing a scalar displacement.

recalculate_normals

Default `FALSE`. Whether to recalculate vertex normals based on the connecting face orientations. This can be used to compute normals for meshes lacking them or to calculate new normals after a displacement map has been applied to the mesh.

importance_sample_lights

Default `TRUE`. Whether to importance sample lights specified in the OBJ material (objects with a non-zero Ke MTL material).

material

Default diffuse.The material, called from one of the material functions diffuse, metal, or dielectric.

angle

Default `c(0, 0, 0)`. Angle of rotation around the x, y, and z axes, applied in the order specified in `order_rotation`.

order_rotation

Default `c(1, 2, 3)`. The order to apply the rotations, referring to "x", "y", and "z".

flipped

Default `FALSE`. Whether to flip the normals.

scale

Default `c(1, 1, 1)`. Scale transformation in the x, y, and z directions. If this is a single value, number, the object will be scaled uniformly. Note: emissive objects may not currently function correctly when scaled.

Value

Single row of a tibble describing the obj model in the scene.

Examples

#Load the included example R object file, by calling the r_obj() function. This
#returns the local file path to the `r.txt` obj file. The file extension is "txt" 
#due to package constraints, but the file contents are identical and it does not 
#affect the function.

if(run_documentation()) {
#Load the basic 3D R logo with the included materials
generate_ground(material = diffuse(checkercolor = "grey50")) %>%
  add_object(obj_model(y = 0.2, filename = rayrender::r_obj(),
                       scale_obj=3)) %>%
   add_object(sphere(z = 20, x = 20, y = 20, radius = 10,
                     material = light(intensity = 10))) %>%
  render_scene(parallel = TRUE, samples = 16, aperture = 0.05, 
               sample_method="sobol_blue",
               fov = 20, lookfrom = c(0, 2, 10))
}


if(run_documentation()) {
# Smooth a mesh by setting the number of subdivision levels 
generate_ground(material = diffuse(checkercolor = "grey50")) %>%
  add_object(obj_model(y = 0.2, filename = rayrender::r_obj(),
                       scale_obj=3, subdivision_levels = 3)) %>%
   add_object(sphere(z = 20, x = 20, y = 20, radius = 10,
                     material = light(intensity = 10))) %>%
  render_scene(parallel = TRUE, samples = 16, aperture = 0.05,
               sample_method="sobol_blue", 
               fov = 20, lookfrom = c(0, 2, 10))
}


if(run_documentation()) {
#Override the materials for each object
generate_ground(material = diffuse(checkercolor = "grey50")) %>%
  add_object(obj_model(y = 1.4, filename = rayrender::r_obj(), load_material = FALSE,
                       scale_obj = 1.8, angle=c(10,0,0),
                       material = microfacet(color = "gold", roughness = 0.05))) %>%
  add_object(obj_model(x = 0.9, y = 0, filename = rayrender::r_obj(), load_material = FALSE,
                       scale_obj = 1.8, angle=c(0,-20,0),
                       material = diffuse(color = "dodgerblue"))) %>%
  add_object(obj_model(x = -0.9, y = 0, filename = rayrender::r_obj() , load_material = FALSE,
                       scale_obj = 1.8, angle=c(0,20,0),
                       material = dielectric(attenuation = c(1,0.3,1), priority = 1,
                                             attenuation_intensity = 20))) %>%
  add_object(sphere(z = 20, x = 20, y = 20, radius = 10,
                    material = light(intensity = 10))) %>%
  render_scene(parallel = TRUE, samples = 16, aperture = 0.05, 
               sample_method="sobol_blue", lookat=c(0,0.5,0),
               fov = 22, lookfrom = c(0, 2, 10))

}