tylermorganwall / rayrender

A pathtracer for R. Build and render complex scenes and 3D data visualizations directly from R
http://www.rayrender.net
622 stars 43 forks source link

Observing Straight Down `camera_up` axis Is Slow Black Death #7

Closed brodieG closed 5 years ago

brodieG commented 5 years ago

I'm continuing to have a blast playing with this package. Awesome (in the original sense) stuff.

One small seeming glitch I've run into. If you sight straight down the Y-axis (or whatever the camera_up axis is) rayrender very slowly renders dark inky blackness. My completely uneducated guess is that there is a NaN or some such arising from an angle calculation relative to the camera_up vector.

library(rayrender)
# A plane illuminated by a single light

scn <- add_object(
  xz_rect(),
  sphere(y=6, material = lambertian(lightintensity = 10, implicit_sample=TRUE))
)
#> Warning in lambertian(lightintensity = 10, implicit_sample = TRUE):
#> lambertian() deprecated--use diffuse() instead.
# blackness

render_scene(
  scn, width=100, height=100, samples=100,
  lookfrom=c(0, 5, 0)
)

# works

render_scene(
  scn, width=100, height=100, samples=100,
  lookfrom=c(0, 5, 0.0001)
)

# works

render_scene(
  scn, width=100, height=100, samples=100,
  lookfrom=c(0, 5, 0), lookat=c(0, 0, 0.0001)
)

# recreate scene, but rotated about x-axis 

scn <- add_object(
  xy_rect(),
  sphere(y=0, z = 6, material = lambertian(lightintensity = 10, implicit_sample=TRUE))
)
#> Warning in lambertian(lightintensity = 10, implicit_sample = TRUE):
#> lambertian() deprecated--use diffuse() instead.
# works!

render_scene(
  scn, width=100, height=100, samples=100,
  lookfrom=c(0, 0, 5), lookat=c(0, 0, 0)
)

# Fails if we change camera_up to be same axis we're looking down

render_scene(
  scn, width=100, height=100, samples=100,
  lookfrom=c(0, 0, 5), lookat=c(0, 0, 0), camera_up=c(0, 0, 1)
)

Created on 2019-11-05 by the reprex package (v0.3.0)

Obviously this has a trivial work-around so absolutely no rush in fixing this on my account.

tylermorganwall commented 5 years ago

Glad you’re enjoying it! Yep, this is a bug related to constructing an orthonormal basis for the camera plane—it takes the cross product between the camera_up vector and the look direction vector to build the camera plane. When those two are parallel, the resulting cross product is zero so no plane is formed.

I’ll probably just end up throwing an error for these cases since the camera is poorly defined otherwise. An easy solution is just to slightly perturb the camera_up direction in an orthogonal direction (e.g. ‘camera_up = c(0, 1, 0.001)’) or do what you did and perturb the look direction.

tylermorganwall commented 5 years ago

Turns out I fixed this (in the sense of throwing an error) a couple weeks ago (see bff50a3e85c677edd65e00fbb2fdfb7e0f63444d) and just never pushed to master!