def objshow(obj, visual='color', res=(512, 512), ortho=False, showball=False, lightdir=[0.4, -1.5, 0.8]): import taichi_three as t3 t3.reset() scene = t3.Scene() model = t3.Model.from_obj(obj) scene.add_model(model) if showball: ball = t3.Model.from_obj(t3.readobj('assets/sphere.obj', scale=0.6)) scene.add_model(ball) camera = t3.Camera(res=res) if visual != 'color': dim = 3 if visual == 'idepth': dim = 0 if visual == 'texcoor': dim = 2 camera.fb.add_buffer('normal', dim) if ortho: camera.type = camera.ORTHO scene.add_camera(camera) light = t3.Light(dir=lightdir) scene.add_light(light) gui = t3.GUI('Model', camera.res) while gui.running: gui.get_event(None) gui.running = not gui.is_pressed(gui.ESCAPE) camera.from_mouse(gui) if showball: ball.L2W.offset[None] = t3.Vector([1.75, -1.75, 0.0]) scene.render() if visual == 'normal': gui.set_image(camera.fb['normal'].to_numpy() * 0.5 + 0.5) elif visual == 'color': gui.set_image(camera.img) else: gui.set_image(camera.fb[visual].to_numpy()) gui.show()
def show(self, arguments: list = sys.argv[2:]): """Visualize an OBJ/NPZ model using Taichi THREE""" parser = argparse.ArgumentParser(prog='t3 show', description=f"{self.show.__doc__}") parser.add_argument( 'filename', help='File name of the OBJ/NPZ model to visualize, e.g. monkey.obj') parser.add_argument('-s', '--scale', default=0.75, type=float, help='Specify a scale parameter') parser.add_argument('-u', '--resx', default=512, type=int, help='Specify window width') parser.add_argument('-v', '--resy', default=512, type=int, help='Specify window height') parser.add_argument('-A', '--ambient', default=0, type=float, help='Specify ambient light strength') parser.add_argument('-o', '--ortho', action='store_true', help='Display in orthogonal mode') parser.add_argument('-l', '--lowp', action='store_true', help='Shade faces by interpolation') parser.add_argument('-x', '--flipx', action='store_true', help='Flip X axis of model when display') parser.add_argument('-y', '--flipy', action='store_true', help='Flip Y axis of model when display') parser.add_argument('-z', '--flipz', action='store_true', help='Flip Z axis of model when display') parser.add_argument('-f', '--flipface', action='store_true', help='Flip face culling direction') parser.add_argument('-F', '--flipnorm', action='store_true', help='Flip face normal direction') parser.add_argument('-b', '--bothface', action='store_true', help='Including both face, no culling') parser.add_argument('-N', '--renorm', action='store_true', help='Reset normal vectors to flat') parser.add_argument('-S', '--showhints', action='store_true', help='Show information about pixel under cursor') parser.add_argument('-T', '--taa', default=True, action='store_true', help='Enable temporal anti-aliasing') parser.add_argument('-t', '--texture', type=str, help='Path to texture to bind') parser.add_argument('-n', '--normtex', type=str, help='Path to normal map to bind') parser.add_argument('-m', '--metallic', type=str, help='Path to metallic map to bind') parser.add_argument('-r', '--roughness', type=str, help='Path to roughness map to bind') parser.add_argument('-a', '--arch', default='cpu', type=str, help='Backend to use for rendering') args = parser.parse_args(arguments) import taichi as ti import taichi_three as t3 import numpy as np ti.init(getattr(ti, args.arch)) scene = t3.Scene() obj = t3.readobj(args.filename, scale=args.scale if args.scale != 0 else 1) t3.objflipaxis(obj, args.flipx, args.flipy, args.flipz) if args.scale == 0: t3.objautoscale(obj) if args.flipface: t3.objflipface(obj) if args.flipnorm: t3.objflipnorm(obj) if args.renorm: t3.objmknorm(obj) if args.bothface: t3.objbothface(obj) model = (t3.ModelLow if args.lowp else t3.Model).from_obj(obj) if args.texture is not None: model.add_texture('color', ti.imread(args.texture)) if args.normtex is not None: model.add_texture('normal', ti.imread(args.normtex)) if args.metallic is not None: model.add_texture('metallic', ti.imread(args.metallic)) if args.roughness is not None: model.add_texture('roughness', ti.imread(args.roughness)) scene.add_model(model) camera = t3.Camera(res=(args.resx, args.resy), taa=args.taa) if args.showhints: camera.fb.add_buffer('pos', 3) camera.fb.add_buffer('texcoor', 2) camera.fb.add_buffer('normal', 3) if args.ortho: camera.type = camera.ORTHO scene.add_camera(camera) if args.ambient: light = t3.AmbientLight(args.ambient) else: light = t3.Light([0.4, -1.5, 0.8]) scene.add_light(light) gui = ti.GUI('Model', camera.res) while gui.running: gui.get_event(None) gui.running = not gui.is_pressed(ti.GUI.ESCAPE) camera.from_mouse(gui) scene.render() gui.set_image(camera.img) if args.showhints: coor = gui.get_cursor_pos() pos = camera.fb.fetchpixelinfo('pos', coor) color = camera.fb.fetchpixelinfo('img', coor) texcoor = camera.fb.fetchpixelinfo('texcoor', coor) normal = camera.fb.fetchpixelinfo('normal', coor) gui.text(f'color: [{color.x:.2f} {color.y:.2f} {color.z:.2f}]; pos: [{pos.x:+.2f} {pos.y:+.2f} {pos.z:+.2f}]', (0, 1)) gui.text(f'texcoor: [{texcoor.x:.2f} {texcoor.y:.2f}]; normal: [{normal.x:+.2f} {normal.y:+.2f} {normal.z:+.2f}]', (0, 1 - 16 / camera.res[1])) gui.show()
import taichi as ti import taichi_three as t3 import numpy as np ti.init(ti.cpu) scene = t3.Scene() model = t3.Model(t3.readobj('assets/monkey.obj', scale=0.6)) #model = t3.Model(t3.readobj('assets/torus.obj', scale=0.6)) scene.add_model(model) scene.set_light_dir([0.4, -1.5, -1.8]) gui = ti.GUI('Model', scene.res) while gui.running: gui.running = not gui.get_event(ti.GUI.ESCAPE) #scene.camera.from_mouse(gui) model.L2W.from_mouse(gui) scene.render() gui.set_image(scene.img) gui.show()
import taichi as ti import taichi_three as t3 import time, math ti.init(ti.opengl) n = 2 r = t3.Scene((640, 480)) pos = ti.Vector(3, ti.f32, n) radius = ti.var(ti.f32, n) r.add_ball(pos, radius) r.opt.is_normal_map = True gui = ti.GUI('Ball', r.res) while gui.running: gui.running = not gui.get_event(ti.GUI.ESCAPE) radius[0] = math.sin(time.time()) * 0.25 + 0.5 radius[1] = math.cos(time.time() * 1.3) * 0.1 + 0.2 pos[1] = [0.5, 0.5, 0.0] r.render() gui.set_image(r.img) gui.show()
import taichi as ti import taichi_glsl as ts import taichi_three as t3 from math import cos, sin, tan, pi, tau from time import time ti.init(ti.opengl) N = 12 dt = 0.01 scene = t3.Scene((640, 640)) pos = ti.Vector(3, ti.f32, N) vel = ti.Vector(3, ti.f32, N) radius = ti.var(ti.f32, N) bound = ti.Vector(3, ti.f32, ()) scene.add_ball(pos, radius) scene.set_light_dir([1, 1, -1]) @ti.kernel def init(): for i in pos: pos[i] = ts.randNDRange(ts.vec3(-1), ts.vec3(1)) vel[i] = ts.randNDRange(ts.vec3(-1.1), ts.vec3(1.1)) radius[i] = ts.randRange(0.1, 0.2) @ti.func def interact(i, j):