Skip to content
This repository has been archived by the owner on Feb 1, 2025. It is now read-only.

Commit

Permalink
Colored volume (yet I think this is strange)
Browse files Browse the repository at this point in the history
  • Loading branch information
enigmahe committed Jun 24, 2024
1 parent 1146ba4 commit 528f395
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 72 deletions.
81 changes: 15 additions & 66 deletions bxdf/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ def __init__(self, elem: xet.Element):
# phase function
self.par = np.zeros(3, np.float32)
self.pdf = np.float32([1., 0., 0.])
self.mono2rgb = False

elem_to_query = {
"rgb": rgb_parse,
"float": lambda el: get(el, "value"),
"string": lambda el: self.setup_volume(get(el, "path", str)),
"transform": lambda el: self.assign_transform(*transform_parse(el))
"transform": lambda el: self.assign_transform(*transform_parse(el)),
"bool": lambda el: get(el, "value", bool)
}
if elem is not None:
type_name = elem.get("type")
Expand All @@ -93,6 +95,16 @@ def __init__(self, elem: xet.Element):
name = tag_elem.get("name")
if hasattr(self, name):
self.__setattr__(name, query_func(tag_elem))

if self.mono2rgb == True:
if self.channel == 3:
CONSOLE.log("Setting 'mono2rgb = True' is meanless for RGB volume. This setting is only meaningful when the volume is mono-chromatic.")
else:
CONSOLE.log("Mono-chromatic volume is converted to RGB volume.")
self.channel = 3
self.density_grid = GridVolume_np.make_colorful_volume(self.density_grid, self.xres, self.yres, self.zres)
else:
self.density_grid = np.concatenate([self.density_grid] * 3, axis = -1)

if self.scale is None:
self.scale = np.eye(3, dtype = np.float32)
Expand All @@ -119,8 +131,6 @@ def setup_volume(self, path:str):
CONSOLE.log(f"[yellow]Warning[/yellow]: FORCE_MONO_COLOR is True. This only makes sense when we are testing the code.")

density_grid = density_grid.reshape((self.zres, self.yres, self.xres, self.channel))
density_grid = GridVolume_np.make_colorful_volume(density_grid, self.xres, self.yres, self.zres)
self.channel = 3
return density_grid

@staticmethod
Expand Down Expand Up @@ -258,10 +268,7 @@ def transmittance(self, grid: ti.template(), ray_o: vec3, ray_d: vec3, max_t: fl
ray_o = self.inv_T @ (ray_o - self.trans)
ray_d = self.inv_T @ ray_d
if self._type:
if self._type == GridVolume_np.RGB: # single channel
transm = self.eval_tr_ratio_tracking_3d(grid, ray_o, ray_d, near_far)
else:
transm = self.eval_tr_ratio_tracking_3d(grid, ray_o, ray_d, near_far)
transm = self.eval_tr_ratio_tracking_3d(grid, ray_o, ray_d, near_far)
return transm

@ti.func
Expand All @@ -273,10 +280,7 @@ def sample_mfp(self, grid: ti.template(), ray_o: vec3, ray_d: vec3, max_t: float
ray_o = self.inv_T @ (ray_o - self.trans)
ray_d = self.inv_T @ ray_d
if self._type:
if self._type == 2: # single channel
transm = self.sample_distance_delta_tracking_3d(grid, ray_o, ray_d, near_far)
else:
transm = self.sample_distance_delta_tracking_3d(grid, ray_o, ray_d, near_far)
transm = self.sample_distance_delta_tracking_3d(grid, ray_o, ray_d, near_far)
return transm

@ti.func
Expand All @@ -288,61 +292,6 @@ def density_lookup_3d(self, grid: ti.template(), index: vec3, u_offset: vec3) ->
val = grid[idx[2], idx[1], idx[0]]
return val

@ti.func
def density_lookup(self, grid: ti.template(), index, u_offset: vec3) -> float:
""" Stochastic lookup of density (RGB volume) """
idx = ti.cast(ti.floor(index + (u_offset - 0.5)), int)
val = 0.0
if (idx >= 0).all() and (idx <= self.max_idxs).all():
val = grid[idx[2], idx[1], idx[0]]
# indexing pattern z, y, x
return val

@ti.func
def sample_distance_delta_tracking(self, grid: ti.template(), ray_ol: vec3, ray_dl: vec3, near_far: vec2) -> vec4:
""" Sample distance (mono-chromatic volume) via delta tracking
Note that there is no 'sampling PDF', since we are not going to use it anyway
"""
result = vec4([1, 1, 1, -1])
if self._type:
inv_maj = 1.0 / self.majorant[0]

t = near_far[0]
t -= ti.log(1.0 - ti.random(float)) * inv_maj
while t < near_far[1]:
d = self.density_lookup(grid, ray_ol + t * ray_dl, vec3([
ti.random(float), ti.random(float), ti.random(float)
]))
# Scatter upon real collision
if ti.random(float) < d * inv_maj:
result[:3] = self.albedo
result[3] = t
break
t -= ti.log(1.0 - ti.random(float)) * inv_maj
return result

@ti.func
def eval_tr_ratio_tracking(self, grid: ti.template(), ray_ol: vec3, ray_dl: vec3, near_far: vec2) -> vec3:
Tr = 1.0
if self._type:
inv_maj = 1.0 / self.majorant[0]

t = near_far[0]
while True:
t -= ti.log(1.0 - ti.random(float)) * inv_maj
if t >= near_far[1]: break
d = self.density_lookup(grid, ray_ol + t * ray_dl, vec3([
ti.random(float), ti.random(float), ti.random(float)
]))
Tr *= ti.max(0, 1.0 - d * inv_maj)
# Russian Roulette
if Tr < 0.1:
if ti.random(float) >= Tr:
Tr = 0.0
break
Tr = 1.0
return vec3([Tr, Tr, Tr])

@ti.func
def sample_new_rays(self, incid: vec3):
ret_spec = vec3([1, 1, 1])
Expand Down
8 changes: 2 additions & 6 deletions renderer/vpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,8 @@ def __init__(self,
if prop["volume"]:
CONSOLE.log("Loading Grid Volume ...")
volume = GridVolume_np(prop["volume"][0])
if volume.type_id == GridVolume_np.MONO:
self.density_grid = ti.field(float, volume.get_shape())
volume.density_grid = volume.density_grid.squeeze()
elif volume.type_id == GridVolume_np.RGB:
self.density_grid = ti.Vector.field(3, float, volume.get_shape())
else:
self.density_grid = ti.Vector.field(3, float, volume.get_shape())
if volume.type_id not in{ GridVolume_np.MONO, GridVolume_np.RGB}:
CONSOLE.log(f"Volume type '{volume.type_name}' is not supported.")
raise RuntimeError("Unsupported volume type.")
self.has_volume = True
Expand Down
1 change: 1 addition & 0 deletions scenes/cbox/cbox-rgbvol.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
<rgb name="albedo" value="#F8F8F8"/>
<rgb name="density_scaling" value="20.0"/>
<rgb name="par" value="0.7"/>
<bool name="mono2rgb" value="true"/>

<transform name="toWorld">
<translate x="0.6" y="0.0" z="1.3"/>
Expand Down

0 comments on commit 528f395

Please sign in to comment.