Skip to main content

taichi.ui#

Taichi gui module for visualization.

This module contains a cpu based GUI system, a vulkan based GGUI system, and other helper utilities like adding widgets and exporting video files.

taichi.ui.ALT = Alt#
taichi.ui.BACKSPACE = BackSpace#
taichi.ui.CAPSLOCK = CapsLock#
taichi.ui.CTRL = Control#
class taichi.ui.Camera#

The Camera class.

You should also manually set the camera parameters like camera.position, camera.lookat, camera.up, etc. The default settings may not work for your scene.

Example:

>>> scene = ti.ui.Scene()  # assume you have a scene
>>>
>>> camera = ti.ui.Camera()
>>> camera.position(1, 1, 1)  # set camera position
>>> camera.lookat(0, 0, 0)  # set camera lookat
>>> camera.up(0, 1, 0)  # set camera up vector
>>> scene.set_camera(camera)
>>>
>>> # you can also control camera movement in a window
>>> window = ti.ui.Window("GGUI Camera", res=(640, 480), vsync=True)
>>> camera.track_user_inputs(window, movement_speed=0.03, hold_key=ti.ui.RMB)
bottom(self, bottom)#

Set the offset of the bottom clipping plane in camera frustum.

Parameters:

bottom (taichi.types.primitive_types) – offset of the bottom clipping plane.

Example:

>>> camera.bottom(1.0)
fov(self, fov)#

Set the camera fov angle (field of view) in degrees.

Parameters:

fov (taichi.types.primitive_types) – Angle in range (0, 180).

Example:

>>> camera.fov(45)
get_projection_matrix(self, aspect)#

Get the projection matrix(in row major) of the camera.

Parameters:

aspect (taichi.types.primitive_types) – aspect ratio of the camera

Example:

>>> camera.get_projection_matrix(1080/720)
get_view_matrix(self)#

Get the view matrix(in row major) of the camera.

Example:

>>> camera.get_view_matrix()
left(self, left)#

Set the offset of the left clipping plane in camera frustum.

Parameters:

left (taichi.types.primitive_types) – offset of the left clipping plane.

Example:

>>> camera.left(-1.0)
lookat(self, x, y, z)#

Set the camera lookat.

Parameters:

args (taichi.types.primitive_types) – 3D coordinates.

Example:

>>> camera.lookat(0, 0, 0)
position(self, x, y, z)#

Set the camera position.

Parameters:

args (taichi.types.primitive_types) – 3D coordinates.

Example:

>>> camera.position(1, 1, 1)
projection_mode(self, mode)#

Camera projection mode, 0 for perspective and 1 for orthogonal.

right(self, right)#

Set the offset of the right clipping plane in camera frustum.

Parameters:

right (taichi.types.primitive_types) – offset of the right clipping plane.

Example:

>>> camera.right(1.0)
top(self, top)#

Set the offset of the top clipping plane in camera frustum.

Parameters:

top (taichi.types.primitive_types) – offset of the top clipping plane.

Example:

>>> camera.top(-1.0)
track_user_inputs(self, window, movement_speed: float = 1.0, yaw_speed: float = 2.0, pitch_speed: float = 2.0, hold_key=None)#

Move the camera according to user inputs. Press w, s, a, d, e, q to move the camera formard, back, left, right, head up, head down, accordingly.

Parameters:
  • window (Window) – a windown instance.

  • movement_speed (primitive_types) – camera movement speed.

  • yaw_speed (primitive_types) – speed of changes in yaw angle.

  • pitch_speed (primitive_types) – speed of changes in pitch angle.

  • hold_key (ui) – User defined key for holding the camera movement.

up(self, x, y, z)#

Set the camera up vector.

Parameters:

args (taichi.types.primitive_types) – 3D coordinates.

Example:

>>> camera.up(0, 1, 0)
z_far(self, z_far)#

Set the offset of the far clipping plane in camera frustum.

Parameters:

far (taichi.types.primitive_types) – offset of the far clipping plane.

Example:

>>> camera.left(1000.0)
z_near(self, z_near)#

Set the offset of the near clipping plane in camera frustum.

Parameters:

near (taichi.types.primitive_types) – offset of the near clipping plane.

Example:

>>> camera.near(0.1)
class taichi.ui.Canvas(canvas)#

The Canvas class.

This is the context manager for managing drawing commands on a window. You should not instantiate this class directly via __init__, instead please call the get_canvas() method of Window.

circles(self, centers, radius, color=(0.5, 0.5, 0.5), per_vertex_color=None)#

Draw a set of 2D circles on this canvas.

Parameters:
  • centers – a taichi 2D Vector field, where each element indicate the 3D location of a vertex.

  • radius (Number) – radius of the circles in pixels.

  • color – a global color for the triangles as 3 floats representing RGB 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 circle.

contour(self, scalar_field, cmap_name='plasma', normalize=False)#

Plot a contour view of a scalar field.

The input scalar_field will be converted to a Numpy array first, and then plotted using Matplotlib’s colormap. Users can specify the color map through the cmap_name argument.

Parameters:
  • scalar_field (ti.field) – The scalar field being plotted. Must be 2D.

  • cmap_name (str, Optional) – The name of the color map in Matplotlib.

  • normalize (bool, Optional) – Display the normalized scalar field if set to True.

  • False. (Default is) –

lines(self, vertices, width, indices=None, color=(0.5, 0.5, 0.5), per_vertex_color=None)#

Draw a set of 2D lines on this canvas.

Parameters:
  • vertices – a taichi 2D Vector field, where each element indicate the 3D location of a vertex.

  • width (float) – width of the lines, relative to the height of the screen.

  • indices – a taichi int field of shape (2 * #lines), which indicate the vertex indices of the lines. If this is None, then it is assumed that the vertices are already arranged in lines order.

  • color – a global color for the triangles as 3 floats representing RGB 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 vertex.

scene(self, scene)#

Draw a 3D scene on the canvas

Parameters:

scene (Scene) – an instance of Scene.

set_background_color(self, color)#

Set the background color of this canvas.

Parameters:

color (tuple(float)) – RGB triple in the range [0, 1].

set_image(self, img)#

Set the content of this canvas to an img.

Parameters:

img (numpy.ndarray, MatrixField, Field, Texture) – the image to be shown.

triangles(self, vertices, color=(0.5, 0.5, 0.5), indices=None, per_vertex_color=None)#

Draw a set of 2D triangles on this canvas.

Parameters:
  • vertices – a taichi 2D Vector field, where each element indicate the 3D location of a vertex.

  • indices – a taichi int field of shape (3 * #triangles), which indicate the vertex indices of the triangles. If this is None, then it is assumed that the vertices are already arranged in triangles order.

  • color – a global color for the triangles as 3 floats representing RGB 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 vertex.

vector_field(self, vector_field, arrow_spacing=5, scale=0.1, width=0.002, color=(0, 0, 0))#

Draw a vector field on this canvas.

Parameters:
  • vector_field – The vector field to be plotted on the canvas.

  • arrow_spacing (int) – Spacing used when sample on the vector field.

  • scale (float) – Maximum vector length proportional to the canvas.

  • width (float) – Line width when drawing the arrow.

  • color (tuple[float]) – The RGB color of arrows.

taichi.ui.DOWN = Down#
taichi.ui.ESCAPE = Escape#
class taichi.ui.GUI(name='Taichi', res=512, background_color=0, show_gui=True, fullscreen=False, fast_gui=False)#

Taichi Graphical User Interface class.

Parameters:
  • name (str, optional) – The name of the GUI to be constructed. Default is ‘Taichi’.

  • res (Union[int, List[int]], optional) – The resolution of created GUI. Default is 512*512. If res is scalar, then width will be equal to height.

  • background_color (int, optional) – The background color of created GUI. Default is 0x000000.

  • show_gui (bool, optional) – Specify whether to render the GUI. Default is True.

  • fullscreen (bool, optional) – Specify whether to render the GUI in fullscreen mode. Default is False.

  • fast_gui (bool, optional) – Specify whether to use fast gui mode of Taichi. Default is False.

Returns:

The created taichi GUI object.

Return type:

GUI

class Event#

Class for holding a gui event.

An event is represented by:

  • type (PRESS, MOTION, RELEASE)

  • modifier (modifier keys like ctrl, shift, etc)

  • pos (mouse position)

  • key (event key)

  • delta (for holding mouse wheel)

class EventFilter(*e_filter)#

A set to store detected user events.

match(self, e)#

Check if a specified event e is among the detected events.

class WidgetValue(gui, wid)#

Class for maintaining id of gui widgets.

ALT = Alt#
BACKSPACE = BackSpace#
CAPSLOCK = Caps_Lock#
CTRL = Control#
DOWN = Down#
ESCAPE = Escape#
EXIT = WMClose#
LEFT = Left#
LMB = LMB#
MMB = MMB#
MOTION#
MOVE = Motion#
PRESS#
RELEASE#
RETURN = Return#
RIGHT = Right#
RMB = RMB#
SHIFT = Shift#
SPACE =#
TAB = Tab#
UP = Up#
WHEEL = Wheel#
arrow(self, orig, direction, radius=1, color=16777215, **kwargs)#

Draws a single arrow on canvas.

Parameters:
  • orig (List[Number]) – The position where arrow starts. Shape must be 2.

  • direction (List[Number]) – The direction where arrow points to. Shape must be 2.

  • radius (Number, optional) – The width of arrow. Default is 1.

  • color (int, optional) – The color of arrow. Default is 0xFFFFFF.

arrow_field(self, direction, radius=1, color=16777215, bound=0.5, **kwargs)#

Draw a field of arrows on canvas.

Parameters:
  • direction (np.array) – The pattern and direction of the field of arrows.

  • color (Union[int, np.array], optional) – The color or colors of arrows. Default is 0xFFFFFF.

  • bound (Number, optional) – The boundary of the field. Default is 0.5.

arrows(self, orig, direction, radius=1, color=16777215, **kwargs)#

Draw a list arrows on canvas.

Parameters:
  • orig (numpy.array) – The positions where arrows start.

  • direction (numpy.array) – The directions where arrows point to.

  • radius (Union[Number, np.array], optional) – The width of arrows. Default is 1.

  • color (Union[int, np.array], optional) – The color or colors of arrows. Default is 0xffffff.

button(self, text, event_name=None)#

Create a button object on canvas to be manipulated with.

Parameters:
  • text (str) – The title of button.

  • event_name (str, optional) – The event name associated with button. Default is WidgetButton_{text}

Returns:

The event name associated with created button.

circle(self, pos, color=16777215, radius=1)#

Draws a circle on canvas.

Parameters:
  • pos (Union[List[int], numpy.array]) – The position of the circle.

  • color (int, Optional) – The color of the circle. Default is 0xFFFFFF.

  • radius (Number, Optional) – The radius of the circle in pixel. Default is 1.

circles(self, pos, radius=1, color=16777215, palette=None, palette_indices=None)#

Draws a list of circles on canvas.

Parameters:
  • pos (numpy.array) – The positions of the circles.

  • radius (Union[Number, numpy.array], optional) – The radius of the circles in pixel. Can be either a number, which will be applied to all circles, or a 1D NumPy array of the same length as pos. The default is 1.

  • color (int, optional) – The color of the circles. Default is 0xFFFFFF.

  • palette (list[int], optional) – The List of colors from which to choose to draw. Default is None.

  • palette_indices (Union[list[int], ti.field, numpy.array], optional) – The List of indices that choose color from palette for each circle. Shape must match pos. Default is None.

clear(self, color=None)#

Clears the canvas with the color provided.

Parameters:

color (int, optional) – Specify the color to clear the canvas. Default is the background color of GUI.

close(self)#

Close this GUI.

Example:

>>> while gui.running:
>>>     if gui.get_event(gui.PRESS, ti.GUI.ESCAPE):
>>>         gui.close()
>>>     gui.show()
contour(self, scalar_field, normalize=False)#

Plot a contour view of a scalar field.

The input scalar_field will be converted to a Numpy array first, and then plotted by the Matplotlib colormap ‘Plasma’. Notice this method will automatically perform a bilinear interpolation on the field if the size of the field does not match with the GUI window size.

Parameters:
  • scalar_field (ti.field) – The scalar field being plotted.

  • normalize (bool, Optional) – Display the normalized scalar field if set to True.

  • False. (Default is) –

cook_image(self, img)#

Converts an img to range [0, 1] for display.

The input image is stored in a numpy.ndarray, if it’s dtype is int it will be rescaled and mapped into range [0, 1]. If the dtype is float it will be directly casted to 32-bit float type.

static get_bool_environ(key, default)#

Get an environment variable and cast it to bool.

Parameters:
  • key (str) – The environment variable key.

  • default (bool) – The default value.

Returns:

The environment variable value cast to bool. If the value is not found, directly return argument ‘default’.

get_cursor_pos(self)#

Returns the current position of mouse as a pair of floats in the range [0, 1] x [0, 1].

The origin of the coordinates system is located at the lower left corner, with +x direction points to the right, and +y direcntion points upward.

Returns:

The current position of mouse.

get_event(self, *e_filter)#

Checks if the specified event is triggered.

Parameters:

*e_filter (ti.GUI.EVENT) – The specific event to be checked.

Returns:

whether or not the specified event is triggered.

Return type:

bool

get_events(self, *e_filter)#

Gets a list of events that are triggered.

Parameters:

*e_filter (List[ti.GUI.EVENT]) – The type of events to be filtered.

Returns:

A list of events that are triggered.

Return type:

EVENT

get_image(self)#

Return the window content as an numpy.ndarray.

Returns:

The image data in numpy contiguous array type.

Return type:

numpy.array

get_key_event(self)#

Gets keyboard triggered event.

Returns:

The keyboard triggered event.

Return type:

EVENT

has_key_event(self)#

Check if there is any key event registered.

Returns:

whether or not there is any key event registered.

Return type:

bool

is_pressed(self, *keys)#

Checks if any key among a set of specified keys is pressed.

Parameters:

*keys (Union[str, List[str]]) – The keys to be listened to.

Returns:

whether or not any key among the specified keys is pressed.

Return type:

bool

label(self, text)#

Creates a label object on canvas.

Parameters:

text (str) – The title of label.

Returns:

The created label object.

Return type:

WidgetValue

line(self, begin, end, radius=1, color=16777215)#

Draws a single line on canvas.

Parameters:
  • begin (List[Number]) – The position of one end of line. Shape must be 2.

  • end (List[Number]) – The position of the other end of line. Shape must be 2.

  • radius (Number, optional) – The width of line. Default is 1.

  • color (int, optional) – The color of line. Default is 0xFFFFFF.

lines(self, begin, end, radius=1, color=16777215)#

Draw a list of lines on canvas.

Parameters:
  • begin (numpy.array) – The positions of one end of lines.

  • end (numpy.array) – The positions of the other end of lines.

  • radius (Union[Number, numpy.array], optional) – The width of lines. Can be either a single width or a list of width whose shape matches the shape of begin & end. Default is 1.

  • color (Union[int, numpy.array], optional) – The color or colors of lines. Can be either a single color or a list of colors whose shape matches the shape of begin & end. Default is 0xFFFFFF.

point_field(self, radius, color=16777215, bound=0.5)#

Draws a field of points on canvas.

Parameters:
  • radius (np.array) – The pattern and radius of the field of points.

  • color (Union[int, np.array], optional) – The color or colors of points. Default is 0xFFFFFF.

  • bound (Number, optional) – The boundary of the field. Default is 0.5.

rect(self, topleft, bottomright, radius=1, color=16777215)#

Draws a single rectangle on canvas.

Parameters:
  • topleft (List[Number]) – The position of the topleft corner of rectangle. Shape must be 2.

  • bottomright (List[Number]) – The position of the bottomright corner of rectangle. Shape must be 2.

  • radius (Number, optional) – The width of rectangle’s sides. Default is 1.

  • color (int, optional) – The color of rectangle. Default is 0xFFFFFF.

set_image(self, img)#

Sets an image to display on the window.

The image pixels are set from the values of img[i, j], where i indicates the horizontal coordinates (from left to right) and j the vertical coordinates (from bottom to top).

If the window size is (x, y), then img must be one of:
  • ti.field(shape=(x, y)), a gray-scale image

  • ti.field(shape=(x, y, 3)), where 3 is for (r, g, b) channels

  • ti.field(shape=(x, y, 2)), where 2 is for (r, g) channels

  • ti.Vector.field(3, shape=(x, y)) (r, g, b) channels on each component

  • ti.Vector.field(2, shape=(x, y)) (r, g) channels on each component

  • np.ndarray(shape=(x, y))

  • np.ndarray(shape=(x, y, 3))

  • np.ndarray(shape=(x, y, 2))

The data type of img must be one of:
  • uint8, range [0, 255]

  • uint16, range [0, 65535]

  • uint32, range [0, 4294967295]

  • float32, range [0, 1]

  • float64, range [0, 1]

Parameters:

img (Union[taichi.field, numpy.array]) – The color array representing the image to be drawn. Support greyscale, RG, RGB, and RGBA color representations. Its shape must match GUI resolution.

show(self, file=None)#

Shows the frame content in the gui window, or save the content to an image file.

Parameters:

file (str, optional) – output filename. The default is None, and the frame content is displayed in the gui window. If it’s a valid image filename the frame will be saved as the specified image.

slider(self, text, minimum, maximum, step=1)#

Creates a slider object on canvas to be manipulated with.

Parameters:
  • text (str) – The title of slider.

  • minimum (int, float) – The minimum value of slider.

  • maximum (int, float) – The maximum value of slider.

  • step (int, float) – The changing step of slider. Optional and default to 1.

Returns:

The created slider object.

Return type:

WidgetValue

text(self, content, pos, font_size=15, color=16777215)#

Draws texts on canvas.

Parameters:
  • content (str) – The text to be drawn on canvas.

  • pos (List[Number]) – The position where the text is to be put.

  • font_size (Number, optional) – The font size of the text.

  • color (int, optional) – The color of the text. Default is 0xFFFFFF.

triangle(self, a, b, c, color=16777215)#

Draws a single triangle on canvas.

Parameters:
  • a (List[Number]) – The position of the first point of triangle. Shape must be 2.

  • b (List[Number]) – The position of the second point of triangle. Shape must be 2.

  • c (List[Number]) – The position of the third point of triangle. Shape must be 2.

  • color (int, optional) – The color of the triangle. Default is 0xFFFFFF.

triangles(self, a, b, c, color=16777215)#

Draws a list of triangles on canvas.

Parameters:
  • a (numpy.array) – The positions of the first points of triangles.

  • b (numpy.array) – The positions of the second points of triangles.

  • c (numpy.array) – The positions of the third points of triangles.

  • color (Union[int, numpy.array], optional) – The color or colors of triangles. Can be either a single color or a list of colors whose shape matches the shape of a & b & c. Default is 0xFFFFFF.

vector_field(self, vector_field, arrow_spacing=5, color=16777215)#

Display a vector field on canvas.

Parameters:
  • vector_field (ti.Vector.field) – The vector field being displayed.

  • arrow_spacing (int, optional) – The spacing between vectors.

  • color (Union[int, np.array], optional) – The color of vectors.

class taichi.ui.Gui(gui)#

For declaring IMGUI components in a taichi.ui.Window created by the GGUI system.

Parameters:

gui – reference to a PyGui.

begin(self, name, x, y, width, height)#

Creates a subwindow that holds imgui widgets.

All widget function calls (e.g. text, button) after the begin and before the next end will describe the widgets within this subwindow.

Parameters:
  • x (float) – The x-coordinate (between 0 and 1) of the top-left corner of the subwindow, relative to the full window.

  • y (float) – The y-coordinate (between 0 and 1) of the top-left corner of the subwindow, relative to the full window.

  • width (float) – The width of the subwindow relative to the full window.

  • height (float) – The height of the subwindow relative to the full window.

button(self, text)#

Declares a button, and returns whether or not it had just been clicked.

Parameters:

text (str) – a line of text to be shown next to the button.

checkbox(self, text, old_value)#

Declares a checkbox, and returns whether or not it has been checked.

Parameters:
  • text (str) – a line of text to be shown next to the checkbox.

  • old_value (bool) – whether the checkbox is currently checked.

color_edit_3(self, text, old_value)#

Declares a color edit palate.

Parameters:
  • text (str) – a line of text to be shown next to the palate.

  • old_value (Tuple[float]) – the current value of the color, this should be a tuple of floats in [0,1] that indicates RGB values.

end(self)#

End the description of the current subwindow.

slider_float(self, text, old_value, minimum, maximum)#

Declares a slider, and returns its newest value.

Parameters:
  • text (str) – a line of text to be shown next to the slider

  • old_value (float) – the current value of the slider.

  • minimum (float) – the minimum value of the slider.

  • maximum (float) – the maximum value of the slider.

slider_int(self, text, old_value, minimum, maximum)#

Declares a slider, and returns its newest value.

Parameters:
  • text (str) – a line of text to be shown next to the slider

  • old_value (int) – the current value of the slider.

  • minimum (int) – the minimum value of the slider.

  • maximum (int) – the maximum value of the slider.

Returns:

the updated value of the slider.

Return type:

int

sub_window(self, name, x, y, width, height)#

Creating a context manager for subwindow.

Note

All args of this method should align with begin.

Parameters:
  • x (float) – The x-coordinate (between 0 and 1) of the top-left corner of the subwindow, relative to the full window.

  • y (float) – The y-coordinate (between 0 and 1) of the top-left corner of the subwindow, relative to the full window.

  • width (float) – The width of the subwindow relative to the full window.

  • height (float) – The height of the subwindow relative to the full window.

Example:

>>> with gui.sub_window(name, x, y, width, height) as g:
>>>     g.text("Hello, World!")
text(self, text, color=None)#

Declares a line of text.

taichi.ui.LEFT = Left#
taichi.ui.LMB = LMB#
taichi.ui.MMB = MMB#
taichi.ui.PRESS = Press#
taichi.ui.ProjectionMode#

Camera projection mode, 0 for perspective and 1 for orthogonal.

taichi.ui.RELEASE = Release#
taichi.ui.RETURN = Return#
taichi.ui.RIGHT = Right#
taichi.ui.RMB = RMB#
taichi.ui.SHIFT = Shift#
taichi.ui.SPACE =#
class taichi.ui.Scene#

The 3D scene class, which can contain meshes and particles, and can be rendered on a canvas.

ambient_light(self, color)#

Set the ambient color of this scene.

Example:

>>> scene = ti.ui.Scene()
>>> scene.ambient_light([0.2, 0.2, 0.2])
lines(self, vertices, width, indices=None, color=(0.5, 0.5, 0.5), 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].

Parameters:
  • vertices – a taichi 3D Vector field, where each element indicate the 3D location of points of lines.

  • width – the line width (maybe different on different systems).

  • indices – a taichi int field of shape (2 * #points), which indicate the points indices of the lines. If this is None, then it is assumed that the points are already arranged in lines order.

  • color – a global color of the mesh as 3 floats representing RGB 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 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.

mesh(self, vertices, indices=None, normals=None, color=(0.5, 0.5, 0.5), per_vertex_color=None, two_sided=False, vertex_offset: int = 0, vertex_count: int = None, index_offset: int = 0, index_count: int = None, show_wireframe: bool = False)#

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)

Parameters:
  • vertices – a taichi 3D Vector field, where each element indicate the 3D location of a vertex.

  • indices – a taichi int field of shape (3 * #triangles), which indicate the vertex indices of the triangles. If this is None, then it is assumed that the vertices are already arranged in triangles order.

  • normals – a taichi 3D Vector field, where each element indicate the normal of a vertex. If this is none, normals will be automatically inferred from vertex positions.

  • color – a global color of the mesh as 3 floats representing RGB 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 vertex.

  • two_sided (bool) – whether or not the triangles should be able to be seen from both sides.

  • 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.

  • 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 the number of vertices to draw.

  • show_wireframe (bool, optional) – turn on/off WareFrame mode.

mesh_instance(self, vertices, indices=None, normals=None, color=(0.5, 0.5, 0.5), per_vertex_color=None, two_sided=False, transforms=None, instance_offset: int = 0, instance_count: int = None, vertex_offset: int = 0, vertex_count: int = None, index_offset: int = 0, index_count: int = None, show_wireframe: bool = False)#

Declare mesh instances inside the scene.

If transforms is given, then according to the shape of transforms, it will draw mesh instances based on the transforms, and you can indicate which instance to draw first. 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)

Parameters:
  • vertices – a taichi 3D Vector field, where each element indicate the 3D location of a vertex.

  • indices – a taichi int field of shape (3 * #triangles), which indicate the vertex indices of the triangles. If this is None, then it is assumed that the vertices are already arranged in triangles order.

  • normals – a taichi 3D Vector field, where each element indicate the normal of a vertex. If this is none, normals will be automatically inferred from vertex positions.

  • color – a global color of the mesh as 3 floats representing RGB 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 vertex.

  • two_sided (bool) – whether or not the triangles should be able to be seen from both sides.

  • transforms (ti.Matrix.field, optional) – The Matrix must be 4x4 size with N instances, and data type should be ti.f32, ti.i32, ti.u32. If None, then it behaves like raw mesh (no copy).

  • instance_offset (int, optional) – Default value is 0 which means no offset to show mesh instances. Otherwise, the mesh instances will show from the instance_offset.

  • instance_count (int, optional) – The default value is None. If this parameter is not provided, instance_count = transforms.shape[0] - instance_offset.

  • 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.

  • 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 the number of indices to draw.

  • show_wireframe (bool, optional) – turn on/off WareFrame mode.

particles(self, centers, radius, color=(0.5, 0.5, 0.5), per_vertex_color=None, index_offset: int = 0, index_count: int = None)#

Declare a set of particles within the scene.

Parameters:
  • centers – a taichi 3D Vector field, where each element indicate the 3D location of the center of a triangle.

  • color – a global color for the particles as 3 floats representing RGB 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, optional) – the index of the first vertex to draw.

  • index_count (int, optional) – the number of vertices to draw.

point_light(self, pos, color)#

Set a point light in this scene.

Parameters:
  • pos (list, tuple, vector(3, float)) – 3D vector for light position.

  • color (list, tuple, vector(3, float)) – (r, g, b) triple for the color of the light, in the range [0, 1].

set_camera(self, camera)#

Set the camera for this scene.

Parameters:

camera (Camera) – A camera instance.

taichi.ui.TAB = Tab#
taichi.ui.UP = Up#
class taichi.ui.Window(name, res, vsync=False, show_window=True, fps_limit=1000, pos=(100, 100))#

The window class.

Parameters:
  • name (str) – Window title.

  • res (tuple[int]) – resolution (width, height) of the window, in pixels.

  • vsync (bool) – whether or not vertical sync should be enabled.

  • show_window (bool) – where or not display the window after initialization.

  • pos (tuple[int]) – position (left to right, up to bottom) of the window which origins from the left-top of your main screen, in pixels.

destroy(self)#

Destroy this window. The window will be unavailable then.

get_canvas(self)#

Returns a canvas handle. See :class`~taichi.ui.canvas.Canvas`

get_cursor_pos(self)#

Get current cursor position, in the range [0, 1] x [0, 1].

get_depth_buffer(self, depth)#
fetch the depth information of current scene to ti.ndarray/ti.field

(support copy from vulkan to cuda/cpu which is a faster version)

Parameters:

depth (ti.ndarray/ti.field) – [window_width, window_height] carries depth information.

get_depth_buffer_as_numpy(self)#

Get the depth information of current scene to numpy array.

Returns:

[width, height] with (0.0~1.0) float-format.

Return type:

2d numpy array

get_event(self, tag=None)#

Returns whether or not a event that matches tag has occurred.

If tag is None, then no filters are applied. If this function returns True, the event property of the window will be set to the corresponding event.

get_events(self, tag=None)#

Get the current list of unprocessed events.

Parameters:

tag (str) – A tag used for filtering events. If it is None, then all events are returned.

get_gui(self)#

Returns a IMGUI handle. See :class`~taichi.ui.ui.Gui`

get_image_buffer_as_numpy(self)#

Get the window content to numpy array.

Returns:

[width, height, channels] with (0.0~1.0) float-format color.

Return type:

3d numpy array

get_window_shape(self)#

Return the shape of window. :returns: (width, height) :rtype: tuple

is_pressed(self, *keys)#

Checks if any of a set of specified keys is pressed.

Parameters:

keys (list[constants]) – The keys to be matched.

Returns:

True if any key among keys is pressed, else False.

Return type:

bool

save_image(self, filename)#

Save the window content to an image file.

Parameters:

filename (str) – output filename.

show(self)#

Display this window.

taichi.ui.check_ggui_availability()#

Checks if the GGUI environment is available.

taichi.ui.hex_to_rgb(color)#

Converts hex color format to rgb color format.

Parameters:

color (int) – The hex representation of color.

Returns:

The rgb representation of color.

taichi.ui.rgb_to_hex(c)#

Converts rgb color format to hex color format.

Parameters:

c (List[int]) – The rgb representation of color.

Returns:

The hex representation of color.

Was this helpful?