taichi-dev / taichi_three

A soft renderer based on Taichi (work in progress)
https://t3.142857.red/
MIT License
221 stars 23 forks source link

the intuition behind Voxelizer #32

Open charlotte12l opened 3 years ago

charlotte12l commented 3 years ago

Thanks for your package! I find Voxelizer is really helpful, but I can't quite understand the algorithm ... Is there any reference for this method? Thanks!

archibate commented 3 years ago

Glad it's useful to you! Here's how it work: Step 1: use B-spline (the same as mpm88 used) to spread particles to 3x3x3 neighbours of the 3D grid. Step 2: Gaussian blur the 3D grid alone X-axis for radius. Step 3: Gaussian blur the 3D grid alone Y-axis for radius. Step 4: Gaussian blur the 3D grid alone Z-axis for radius. The total effect of step 2+3+4 will spread the 3D grid values further to RxRxR rather than just 3x3x3. We use three step for XYZ axis separately so that we only need to sample 3*R, otherwise we will sample over an area of R*R*R.

charlotte12l commented 3 years ago

Thanks for your explanation! I'll cite your package in my course project lol

I'm also curious about the marching cube algorithm. I read some blogs about the marching cube algorithm, and it is originally designed for implicit functions, the vertex is either positive or negative so we can generate mesh based on cube vertex values. However, I'm wondering in your implementation, do you still check the vertice is positive or negative, or you check it is zero or non-zero? I think because it is voxelizing from particle positions, the vertices will never be negative?

(Sorry for so many questions, I did read your code carefully but I didn't quite understand it because I'm a new beginner .. )

archibate commented 3 years ago

it is originally designed for implicit functions

By implicit functions you mean f(x, y, z), right? Then my f(x, y, z) = V[x, y, z], where V is the voxel data.

check the vertice is positive or negative

Their implementation checks f > 0 or f <= 0, my implementation checks f > 1 or f <= 1.

dodydharma commented 3 years ago

Hi @archibate , I tried tp run mciso_mpm3d.py , but i got errors, would u mind helping?

Traceback (most recent call last): File "mciso_mpm3d.py", line 129, in <module> voxel.voxelize(mciso.m, x) File "/usr/local/lib/python3.6/dist-packages/taichi/lang/kernel.py", line 603, in __call__ return self._primal(self._kernel_owner, *args, **kwargs) File "/usr/local/lib/python3.6/dist-packages/taichi/lang/kernel.py", line 499, in __call__ self.materialize(key=key, args=args, arg_features=arg_features) File "/usr/local/lib/python3.6/dist-packages/taichi/lang/kernel.py", line 368, in materialize taichi_kernel = taichi_kernel.define(taichi_ast_generator) File "/usr/local/lib/python3.6/dist-packages/taichi/lang/kernel.py", line 365, in taichi_ast_generator compiled() File "/usr/local/lib/python3.6/dist-packages/taichi_three/mciso.py", line 251, in voxelize p = (pos[i] - self.pmin) / (self.pmax - self.pmin) File "/usr/local/lib/python3.6/dist-packages/taichi/lang/common_ops.py", line 25, in __sub__ return ti.sub(self, other) File "/usr/local/lib/python3.6/dist-packages/taichi/lang/ops.py", line 74, in wrapped return a.element_wise_binary(imp_foo, b) File "/usr/local/lib/python3.6/dist-packages/taichi_glsl/hack.py", line 30, in _new_element_wise_binary return _old_element_wise_binary(self, foo, other) File "/usr/local/lib/python3.6/dist-packages/taichi/lang/matrix.py", line 132, in element_wise_binary assert self.m == other.m and self.n == other.n, f"Dimension mismatch between shapes ({self.n}, {self.m}), ({other.n}, {other.m})" AssertionError: Dimension mismatch between shapes (2, 1), (3, 1)

archibate commented 3 years ago

Hi @archibate , I tried tp run mciso_mpm3d.py , but i got errors, would u mind helping?

Thanks for reporting! This is likely a bug in early version of taichi_glsl. Does pip install -U taichi_glsl taichi_three solve the issue?

dodydharma commented 3 years ago

Thanks @archibate.
Btw, I am using scatter model to render particle without using the marching Cube. I would like to draw each particle with different color, based on each particle velocity. But it seems, currently I could only draw one color for all particle using the same material (CMIIW). Do you have tips, what is the best way to do that with the current library.

archibate commented 3 years ago

Thanks @archibate. Btw, I am using scatter model to render particle without using the marching Cube.

Sounds like a good choice! Snows are to be feel like particles rather than stuck together which is the result of mciso.

I would like to draw each particle with different color, based on each particle velocity. But it seems, currently I could only draw one color for all particle using the same material (CMIIW). Do you have tips, what is the best way to do that with the current library.

Solutions are:

  1. If you only have finite colors, consider use multiple t3.ScatterModels, each with a different material:
    
    model1 = t3.ScatterModel()
    model1.material = t3.Material(t3.CookTorrance(color=t3.Constant(t3.RGB(1, 0, 0))))  # red
    scene.add_model(model1)

model2 = t3.ScatterModel() model2.material = t3.Material(t3.CookTorrance(color=t3.Constant(t3.RGB(0, 1, 0)))) # green scene.add_model(model2)

...

model1.pos.from_numpy(pos_desired_for_red) model2.pos.from_numpy(pos_desired_for_green) ...



2. If you need a different color for each particle, I'd happy to modify the library a little bit to support per-vertex colors :) After that, just clone and install this updated library will work on your end.
dodydharma commented 3 years ago
  1. If you need a different color for each particle, I'd happy to modify the library a little bit to support per-vertex colors :) After that, just clone and install this updated library will work on your end.

Wow :), Thanks alot., that good news, can't wait for that. I need to draw per-vertex, to draw eachparticle based their own dynamic attribute (velocity, positon, etc... )

archibate commented 3 years ago
  1. If you need a different color for each particle, I'd happy to modify the library a little bit to support per-vertex colors :) After that, just clone and install this updated library will work on your end.

Wow :), Thanks alot., that good news, can't wait for that. I need to draw per-vertex, to draw eachparticle based their own dynamic attribute (velocity, positon, etc... )

Hi, I've just pushed commit 0aea61a2827d068533b60884b6be59483fc5a5a5 to master branch, would you clone it to install it locally? The usage example of using vertex colors can be found in examples/ball_simulation.py.

archibate commented 3 years ago

Hi, I've merged some huge refactors in this project recently and the API has been changed to make it easier to use and maintain. If you needs the old API, clone from the legacy branch. Otherwise if you'd like try out the new API:

import taichi as ti
import numpy as np
import tina  # previously called taichi_three

ti.init(ti.gpu)

scene = tina.Scene()

pars = tina.SimpleParticles()
material = tina.BlinnPhong()
scene.add_object(pars, material)

gui = ti.GUI('particles')

pos = np.random.rand(1024, 3) * 2 - 1
pars.set_particles(pos)  # you can directly specify particle positions from numpy array with variable length, like gui.circles does
radius = np.random.rand(1024) * 0.1 + 0.1
pars.set_particle_radii(radius)  # radii can still bee customized per-vertex
color = np.random.rand(1024, 3) * 0.8 + 0.2
pars.set_particle_colors(color)  # colors can be customized per-vertex too

while gui.running:
    scene.input(gui)
    scene.render()
    gui.set_image(scene.img)
    gui.show()

Hope it helps. Please let me know if you like this change :) The new API can be installed with pip install taichi-tina.

haizi32 commented 3 years ago

出现这个问题咋办,AttributeError: module 'taichi' has no attribute 'TaichiOperations'

archibate commented 3 years ago

更新了,最新的master应该没有这个错误 或者把taichi降级到0.7.8,命令:pip install taichi==0.7.8