Skip to content

Commit

Permalink
[gui] Support rendering lines from a part of VBO (taichi-dev#5495)
Browse files Browse the repository at this point in the history
* add extra args to support users to draw part of lines they want

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update python/taichi/ui/scene.py

Co-authored-by: YuZhang <YuCrazing@users.noreply.github.com>

* fix notes format for docs using

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update python/taichi/ui/scene.py

Co-authored-by: Vissidarte-Herman <93570324+Vissidarte-Herman@users.noreply.github.com>

* Update python/taichi/ui/scene.py

Co-authored-by: Vissidarte-Herman <93570324+Vissidarte-Herman@users.noreply.github.com>

* Update python/taichi/ui/scene.py

Co-authored-by: Vissidarte-Herman <93570324+Vissidarte-Herman@users.noreply.github.com>

* Update python/taichi/ui/scene.py

Co-authored-by: Vissidarte-Herman <93570324+Vissidarte-Herman@users.noreply.github.com>

* imporve the notes of functions in Scene

* skip some tests

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* test is no problem, github runner goes wrong

* improve notes format

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: YuZhang <YuCrazing@users.noreply.github.com>
Co-authored-by: Vissidarte-Herman <93570324+Vissidarte-Herman@users.noreply.github.com>
  • Loading branch information
4 people committed Jul 26, 2022
1 parent d768799 commit 0e02562
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 16 deletions.
71 changes: 58 additions & 13 deletions python/taichi/ui/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,18 @@ def lines(self,
width,
indices=None,
color=(0.5, 0.5, 0.5),
per_vertex_color=None):
per_vertex_color=None,
vertex_offset: int = 0,
vertex_count: int = None,
index_offset: int = 0,
index_count: int = None):
"""Declare multi-lines inside the scene.
Note that under current situation, for example, there you have 4 vertices,
vertices.shape[0] is 4. So there will be 2 lines, the first line's two points
are vertices[0] and vertices[1], and the second line's two points are
vertices[2] and vertices[3].
Args:
vertices: a taichi 3D Vector field, where each element indicate the
3D location of points of lines.
Expand All @@ -106,7 +116,39 @@ def lines(self,
If `per_vertex_color` is provided, this is ignored.
per_vertex_color (Tuple[float]): a taichi 3D vector field, where each
element indicate the RGB color of the line.
vertex_offset (int, optional):
if 'indices' is provided, this refers to the value added to the vertex
index before indexing into the vertex buffer, else this refers to the
index of the first vertex to draw.
vertex_count (int, optional):
only available when `indices` is not provided, which is the number
of vertices to draw. There are 2 cases that we will change your
`vertex_count`. [1] If the `vertex_count` is an odd number, then we
will change it to `vertex_count` - 1. [2] If `vertex_offset` plus
`vertex_count` greater than vertices.shape[0], then we will reduce
`vertex_count` to no more than vertices.shape[0].
index_offset (int, optional):
Only available when `indices` is provided, which is the base index
within the index buffer.
index_count (int, optional):
Only available when `indices` is provided, which is the number
of vertices to draw.
"""
if vertex_count is None:
vertex_count = vertices.shape[0]
if index_count is None:
if indices is None:
index_count = vertex_count
else:
index_count = indices.shape[0]
if vertex_count % 2:
print(
"Warning! Odd drawing count will be cut to neast even number")
vertex_count -= 1
if vertex_count + vertex_offset > vertices.shape[0]:
print("Warning! Drawing count greater than shape will be cut")
vertex_count = vertices.shape[0] - vertex_offset
vertex_count -= vertex_count % 2
vbo = get_vbo_field(vertices)
copy_vertices_to_vbo(vbo, vertices)
has_per_vertex_color = per_vertex_color is not None
Expand All @@ -115,7 +157,8 @@ def lines(self,
vbo_info = get_field_info(vbo)
indices_info = get_field_info(indices)
self.scene.lines(vbo_info, indices_info, has_per_vertex_color, color,
width)
width, index_count, index_offset, vertex_count,
vertex_offset)

def mesh(self,
vertices,
Expand All @@ -129,9 +172,11 @@ def mesh(self,
index_offset: int = 0,
index_count: int = None):
"""Declare a mesh inside the scene.
if you indicate the index_offset and index_count, the normals will also
be sliced by the args, and the shading resultes will not be affected.
(It is equal to make a part of the mesh visible)
Args:
vertices: a taichi 3D Vector field, where each element indicate the
3D location of a vertex.
Expand All @@ -147,18 +192,18 @@ def mesh(self,
element indicate the RGB color of a vertex.
two_sided (bool): whether or not the triangles should be able to be
seen from both sides.
vertex_offset: int type(ohterwise float type will be floored to int),
if 'indices' is provided, this means the value added to the vertex
index before indexing into the vertex buffer, else this means the
vertex_offset (int, optional):
if 'indices' is provided, this refers to the value added to the vertex
index before indexing into the vertex buffer, else this refers to the
index of the first vertex to draw.
vertex_count: int type(ohterwise float type will be floored to int),
only avaliable when `indices` is not provided, which is the number
vertex_count (int, optional):
only available when `indices` is not provided, which is the number
of vertices to draw.
index_offset: int type(ohterwise float type will be floored to int),
only avaliable when `indices` is provided, which is the base index
index_offset (int, optional):
only available when `indices` is provided, which is the base index
within the index buffer.
index_count: int type(ohterwise float type will be floored to int),
only avaliable when `indices` is provided, which is the the number
index_count (int, optional):
only available when `indices` is provided, which is the the number
of vertices to draw.
"""
vbo = get_vbo_field(vertices)
Expand Down Expand Up @@ -199,9 +244,9 @@ def particles(self,
values. If `per_vertex_color` is provided, this is ignored.
per_vertex_color (Tuple[float]): a taichi 3D vector field, where each
element indicate the RGB color of a particle.
index_offset: int type(ohterwise float type will be floored to int),
index_offset (int, optional):
the index of the first vertex to draw.
index_count: int type(ohterwise float type will be floored to int),
index_count (int, optional):
the number of vertices to draw.
"""
vbo = get_vbo_field(centers)
Expand Down
11 changes: 10 additions & 1 deletion taichi/python/export_ggui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,20 @@ struct PyScene {
FieldInfo indices,
bool has_per_vertex_color,
py::tuple color_,
float width) {
float width,
float draw_index_count,
float draw_first_index,
float draw_vertex_count,
float draw_first_vertex) {
RenderableInfo renderable_info;
renderable_info.vbo = vbo;
renderable_info.indices = indices;
renderable_info.has_per_vertex_color = has_per_vertex_color;
renderable_info.has_user_customized_draw = true;
renderable_info.draw_index_count = (int)draw_index_count;
renderable_info.draw_first_index = (int)draw_first_index;
renderable_info.draw_vertex_count = (int)draw_vertex_count;
renderable_info.draw_first_vertex = (int)draw_first_vertex;

SceneLinesInfo info;
info.renderable_info = renderable_info;
Expand Down
6 changes: 4 additions & 2 deletions taichi/ui/backends/vulkan/renderables/scene_lines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ void SceneLines::record_this_frame_commands(CommandList *command_list) {
command_list->set_line_width(curr_width_);

if (indexed_) {
command_list->draw_indexed(config_.indices_count, 0, 0);
command_list->draw_indexed(config_.draw_index_count,
config_.draw_first_vertex,
config_.draw_first_index);
} else {
command_list->draw(config_.vertices_count, 0);
command_list->draw(config_.draw_vertex_count, config_.draw_first_vertex);
}
}

Expand Down

0 comments on commit 0e02562

Please sign in to comment.