Skip to content

Commit

Permalink
Debug: Erroneous color.
Browse files Browse the repository at this point in the history
  • Loading branch information
Enigmatisms committed Jun 27, 2024
1 parent 928c979 commit b25daab
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 27 deletions.
56 changes: 47 additions & 9 deletions bxdf/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import numpy as np
import taichi as ti
import taichi.math as tm
import xml.etree.ElementTree as xet

from typing import Tuple
Expand Down Expand Up @@ -134,15 +135,33 @@ def setup_volume(self, path:str):

@staticmethod
def make_colorful_volume(density_grid: np.ndarray, xres: int, yres: int, zres: int):
z_coords = np.linspace(0, 4.0, zres, dtype = np.float32).reshape(-1, 1, 1, 1) + 0.1
y_coords = np.linspace(0, 0.5, yres, dtype = np.float32).reshape(1, -1, 1, 1) + 0.1
x_coords = np.linspace(0, 0.1, xres, dtype = np.float32).reshape(1, 1, -1, 1) + 0.01
density_grid = np.concatenate([
density_grid * z_coords,
density_grid * y_coords,
density_grid * x_coords,
density_grid,
density_grid,
density_grid
], axis = -1)
return density_grid

# 创建一个从0到1的渐变数组,表示x轴从左到右
half_x = zres // 3
grad_l = np.linspace(1, 0, half_x, dtype = np.float32) ** 0.25
grad_r = np.linspace(0, 1, zres - half_x, dtype = np.float32) ** 0.1

# 前半部分,从红色(1, 0, 0)到白色(1, 1, 1)
left_half = np.zeros((half_x, 3), dtype = np.float32)
left_half[:, 0] = 1 # 红色通道保持1
left_half[:, 1] = 1 - grad_l # 绿色通道从0到1
left_half[:, 2] = 1 - grad_l # 蓝色通道从0到1

# 后半部分,从白色(1, 1, 1)到蓝色(0, 0, 1)
right_half = np.zeros((zres - half_x, 3), dtype = np.float32)
right_half[:, 0] = 1 - grad_r # 红色通道从1到0
right_half[:, 1] = 1 - grad_r # 绿色通道从1到0
right_half[:, 2] = 1 # 蓝色通道保持1

# 将两个部分组合在一起
color_gradient = np.vstack((left_half, right_half))

return density_grid * color_gradient[:, None, None, :]

def get_shape(self) -> Tuple[int, int, int]:
return (self.zres, self.yres, self.xres)
Expand Down Expand Up @@ -298,6 +317,25 @@ 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_lerp_3d(self, grid: ti.template(), index: vec3, u_offset: vec3) -> vec3:
""" Stochastic lookup of density (mono-chromatic volume) """
coord = index + (u_offset - 0.5)
idx = ti.cast(ti.floor(coord), int)
coord -= idx
val = ZERO_V3
if (idx >= 0).all() and (idx <= self.max_idxs - 1).all():
v1 = tm.mix(grid[idx[2], idx[1], idx[0]], grid[idx[2], idx[1], idx[0] + 1], coord[0])
v2 = tm.mix(grid[idx[2] + 1, idx[1], idx[0]], grid[idx[2] + 1, idx[1], idx[0] + 1], coord[0])
v3 = tm.mix(grid[idx[2], idx[1] + 1, idx[0]], grid[idx[2], idx[1] + 1, idx[0] + 1], coord[0])
v4 = tm.mix(grid[idx[2] + 1, idx[1] + 1, idx[0]], grid[idx[2] + 1, idx[1] + 1, idx[0] + 1], coord[0])

v1 = tm.mix(v1, v2, coord[2])
v3 = tm.mix(v3, v4, coord[2])

val = tm.mix(v1, v3, coord[1])
return val

@ti.func
def sample_new_rays(self, incid: vec3):
ret_spec = vec3([1, 1, 1])
Expand Down Expand Up @@ -345,7 +383,7 @@ def sample_distance_delta_tracking_3d(self,

t = near_far[0] - ti.log(1.0 - ti.random(float)) * inv_maj
while t < near_far[1]:
d = self.density_lookup_3d(grid, ray_ol + t * ray_dl, vec3([
d = self.density_lookup_lerp_3d(grid, ray_ol + t * ray_dl, vec3([
ti.random(float), ti.random(float), ti.random(float)
]))
# Scatter upon real collision
Expand Down Expand Up @@ -404,7 +442,7 @@ def eval_tr_ratio_tracking_3d(self,

if t >= near_far[1]: break
# for mono-chromatic medium, this is 1
d = self.density_lookup_3d(grid, ray_ol + t * ray_dl, vec3([
d = self.density_lookup_lerp_3d(grid, ray_ol + t * ray_dl, vec3([
ti.random(float), ti.random(float), ti.random(float)
]))

Expand Down
14 changes: 7 additions & 7 deletions scenes/cbox/cbox-rgbvol.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@
</brdf>

<brdf type="phong" id="left_wall">
<rgb name="k_d" value="#DD2525"/>
<rgb name="k_d" value="#BDBDBD"/>
<rgb name="k_g" value="10.0"/>
<rgb name="k_s" value="#221313"/>
</brdf>

<brdf type="phong" id="right_wall">
<rgb name="k_d" value="#25DD25"/>
<rgb name="k_d" value="#BDBDBD"/>
<rgb name="k_g" value="10.0"/>
<rgb name="k_s" value="#132213"/>
</brdf>
Expand All @@ -69,7 +69,7 @@

<emitter type="area" id="area">
<rgb name="emission" value="50.0, 50.0, 50.0"/>
<rgb name="scaler" value="0.1"/>
<rgb name="scaler" value="0.12"/>
</emitter>

<shape type="obj">
Expand Down Expand Up @@ -114,14 +114,14 @@
<volume name="janga" type="mono" phase_type="hg">
<string name="density_grid" path="./scenes/volume/janga-smoke-264-136-136.vol"/>
<rgb name="albedo" value="#F8F8F8"/>
<rgb name="density_scaling" value="15.0"/>
<rgb name="density_scaling" value="25.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"/>
<rotate type="euler" r="0" p="0.0" y="0"/>
<scale x="0.03" y="0.03" z="0.03"/>
<translate x="0.35" y="-0.6" z="2.8"/>
<rotate type="euler" r="10" p="0.0" y="90"/>
<scale x="0.01" y="0.035" z="0.02"/>
</transform>
</volume>

Expand Down
23 changes: 12 additions & 11 deletions scenes/cbox/cbox-volgrid.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@
<float name="far_clip" value="2000"/>
<float name="fov" value="39.3077"/>
<integer name="sample_count" value="128"/>
<integer name="max_bounce" value="8"/>
<integer name="max_bounce" value="16"/>
<integer name="num_shadow_ray" value="1"/>

<integer name="start_t" value="1"/>
<integer name="end_t" value="100"/>
<integer name="start_s" value="0"/>
<integer name="end_s" value="100"/>

<integer name="rr_threshold" value="4"/>
<boolean name="use_rr" value="true"/> <!-- Whether to use Russian roulette ray termination -->
<integer name="rr_bounce_th" value="4"/>
<float name="rr_threshold" value="0.1"/>

<string name="accelerator" value="bvh"/>
<boolean name="use_rr" value="false"/> <!-- Whether to use Russian roulette ray termination -->
<boolean name="anti_alias" value="true"/>
<boolean name="stratified_sampling" value="true"/> <!-- TODO: stratified sampling only implemented for pixel sampling -->
<boolean name="use_mis" value="true"/> <!-- Whether to use multiple importance sampling -->
Expand Down Expand Up @@ -66,19 +67,19 @@
<rgb name="k_s" value="0.0"/>
</brdf>

<emitter type="area" id="area">
<!-- <emitter type="area" id="area">
<rgb name="emission" value="50.0, 50.0, 50.0"/>
<rgb name="scaler" value="0.1"/>
</emitter>
</emitter> -->

<emitter type="area" id="area_red">
<rgb name="emission" value="50.0, 5.0, 5.0"/>
<rgb name="scaler" value="0.1"/>
<rgb name="scaler" value="0.08"/>
</emitter>

<emitter type="area" id="area_green">
<rgb name="emission" value="5.0, 50.0, 5.0"/>
<rgb name="scaler" value="0.125"/>
<rgb name="scaler" value="0.08"/>
</emitter>

<emitter type="area" id="area_blue">
Expand All @@ -104,14 +105,14 @@
<ref type="emitter" id="area_blue"/>
</shape>

<shape type="obj">
<!-- <shape type="obj">
<string name="filename" value="../meshes/cornell/cbox_luminaire.obj"/>
<ref type="material" id="light"/>
<ref type="emitter" id="area"/>
<transform name="toWorld">
<translate x="0" y="-0.001" z="0"/>
</transform>
</shape>
</shape> -->

<shape type="obj">
<string name="filename" value="../meshes/cornell/cbox_floor.obj"/>
Expand Down Expand Up @@ -160,9 +161,9 @@
<volume name="janga" type="mono" phase_type="hg">
<string name="density_grid" path="./scenes/volume/janga-smoke-264-136-136.vol"/>
<rgb name="albedo" value="#F8F8F8"/>
<rgb name="density_scaling" value="20.0"/>
<rgb name="density_scaling" value="4.0"/>
<rgb name="par" value="0.7"/>
<bool name="mono2rgb" value="false"/>
<bool name="mono2rgb" value="true"/>

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

0 comments on commit b25daab

Please sign in to comment.