ggui可以画不同粒径的颗粒吗

scene.particles函数中的radius好像不支持ti.Field,请问ggui有画不同粒径大小颗粒的方法嘛

文档里似乎是支持的:Conduct Physical Simulation | Taichi Docs

但是我往里面传ti.template会报错,只有传float的时候才能正常显示

你方便贴一个比较小的复现代码嘛?

import taichi as ti
ti.init(arch=ti.gpu)

rad_max = 0.005
rad_min = 0.005
size = 1.

domain = ti.Vector([size, size, size])
particle_num = 100
particle = ti.Vector.field(3, dtype=ti.f32, shape=particle_num)
radius = ti.field(ti.f32, shape=particle_num)

@ti.kernel
def fills():
    for i in range(particle_num):
        particle[i] = rad_max + (size - 2. * rad_max) * ti.Vector([ti.random(), ti.random(), ti.random()])
        radius[i] = rad_min + (rad_max - rad_min) * ti.random()

fills()

window = ti.ui.Window("Test", (1024, 1024, 1024), show_window=True)
camera = ti.ui.Camera()
camera.position(0.5, -1, 0.5)
camera.up(0, 1, 0)
camera.lookat(0, 1, 0)
camera.fov(70)
scene = ti.ui.Scene()
scene.set_camera(camera)
canvas = window.get_canvas()

while window.running:
    camera.track_user_inputs(window, movement_speed=0.3, hold_key=ti.ui.LMB)
    scene.set_camera(camera)
    scene.point_light((0.5, 0.5, 1.), color=(1., 1., 1.))
    scene.particles(particle, radius, color=(0.5, 0.5, 0.5))
    canvas.set_background_color((0, 0, 0))
    canvas.scene(scene)

    window.show()

ERROR:

[Taichi] version 1.6.0, llvm 15.0.4, commit d4013266, linux, python 3.8.10
[Taichi] Starting on arch=cuda
Traceback (most recent call last):
  File "ggui.py", line 35, in <module>
    scene.particles(particle, radius, color=(0.5, 0.5, 0.5))
  File "/home/eleven/.local/lib/python3.8/site-packages/taichi/ui/scene.py", line 358, in particles
    self.scene.particles(vbo_info, has_per_vertex_color, color, radius,
TypeError: particles(): incompatible function arguments. The following argument types are supported:
    1. (self: taichi._lib.core.taichi_python.PyScene, arg0: taichi::ui::FieldInfo, arg1: bool, arg2: tuple, arg3: float, arg4: float, arg5: float) -> None

Invoked with: <taichi._lib.core.taichi_python.PyScene object at 0x7fcf60199570>, <taichi._lib.core.taichi_python.FieldInfo object at 0x7fcf67b15470>, False, (0.5, 0.5, 0.5), <ti.field>, 100, 0

lao yi lao

哦我明白你想做什么了,传一个raius的ti.field确实是不支持的。scene.particles()里面的radius必须是一个float值。如果你想给不同粒子画不同的半径,可能只能手动拆开然后多次调用scene.particles()这样

诶我发现最近taichi加了这个支持,用per_vertex_radius,你可以看一下下面这个例子:

N = 10
particles_pos = ti.Vector.field(3, dtype=ti.f32, shape=N)
particles_col = ti.Vector.field(3, dtype=ti.f32, shape=N)
particles_radii = ti.field(dtype=ti.f32, shape=N)

@ti.kernel
def init_points_pos(points: ti.template()):
    for i in range(points.shape[0]):
        points[i] = [i for j in ti.static(range(3))]

@ti.kernel
def init_points_col(points: ti.template()):
    for i in range(points.shape[0]):
        points[i] = [(i + 1) / N, 0.5, (i + 1) / N]

@ti.kernel
def init_points_radii(radii: ti.template()):
    for i in range(radii.shape[0]):
        radii[i] = (i + 1) * 0.05

init_points_pos(particles_pos)
init_points_radii(particles_radii)
init_points_col(particles_col)

window = ti.ui.Window("Test", (768, 768), show_window=False)
canvas = window.get_canvas()
scene = window.get_scene()
camera = ti.ui.Camera()
camera.position(0, 5, -10)
camera.lookat(3, 3, 1)

def render():
    scene.set_camera(camera)
    scene.ambient_light((0.8, 0.8, 0.8))
    scene.point_light(pos=(0.5, 1.5, 1.5), color=(1, 1, 1))

    scene.particles(
        particles_pos,
        color=(0.68, 0.26, 0.19),
        radius=0.5,
        per_vertex_color=particles_col,
        per_vertex_radius=particles_radii,
        index_offset=2,
        index_count=6,
    )
    canvas.scene(scene)

for _ in range(RENDER_REPEAT):
    render()
    window.get_image_buffer_as_numpy()

render()
verify_image(window.get_image_buffer_as_numpy(), "test_draw_part_of_particles_per_vertex_rad_and_col")
window.destroy()

你需要装一个taichi nightly:

pip install -i https://pypi.taichi.graphics/simple/ taichi-nightly

好的谢谢