def conv(self, arguments: list = sys.argv[2:]): """Conversation between OBJ and NPZ file formats""" parser = argparse.ArgumentParser(prog='t3 conv', description=f"{self.conv.__doc__}") parser.add_argument( 'input', help='File name of the OBJ/NPZ model as input, e.g. monkey.obj') parser.add_argument( 'output', help='File name of the NPZ/OBJ model as output, e.g. monkey.npz') args = parser.parse_args(arguments) import taichi_three as t3 obj = t3.readobj(args.input) t3.writeobj(args.output, obj)
def info(self, arguments: list = sys.argv[2:]): """Display informations of an OBJ/NPZ model""" parser = argparse.ArgumentParser(prog='t3 info', description=f"{self.info.__doc__}") parser.add_argument( 'filename', help='File name of the OBJ/NPZ model to visualize, e.g. monkey.obj') args = parser.parse_args(arguments) import taichi_three as t3 obj = t3.readobj(args.filename) print('vertices:', len(obj['vp'])) print('normals:', len(obj['vn'])) print('texcoors:', len(obj['vt'])) print('maxpoly:', max(len(f) for f in obj['f'])) print('faces:', len(obj['f']))
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()
import taichi as ti import taichi_three as t3 import numpy as np ti.init(ti.cpu) scene = t3.Scene() model = t3.Model(t3.Mesh.from_obj(t3.readobj('assets/sphere.obj', scale=0.6))) model.add_texture('roughness', np.array([[0.4]])) model.add_texture('metallic', np.array([[0.8]])) scene.add_model(model) camera = t3.Camera(res=(512, 512), pos=[0, 0, -2], target=[0, 0, 0]) scene.add_camera(camera) light = t3.Light(dir=[0, 0, 1], color=[1.0, 1.0, 1.0]) scene.add_light(light) light2 = t3.Light(dir=[0, -1, 0], color=[1.0, 1.0, 1.0]) scene.add_light(light2) gui = ti.GUI('Model', camera.res) while gui.running: gui.running = not gui.get_event(ti.GUI.ESCAPE) camera.from_mouse(gui) scene.render() gui.set_image(camera.img) gui.show()
import taichi_three as t3 scene = t3.Scene() camera = t3.Camera() scene.add_camera(camera) light = t3.Light(dir=[-0.2, -0.6, 1.0]) scene.add_light(light) obj = t3.readobj('cube.obj', scale=0.6) model = t3.Model.from_obj(obj) scene.add_model(model) gui = t3.GUI('Shading Models') while gui.running: gui.get_event(None) camera.from_mouse(gui) scene.render() gui.set_image(camera.img) 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 numpy as np ti.init(ti.cpu) scene = t3.Scene() model1 = t3.Model(t3.Mesh.from_obj(t3.readobj('assets/torus.obj'))) model2 = t3.Model(t3.Mesh.from_obj(t3.readobj('assets/cube.obj', scale=0.5))) model = t3.ModelGroup([model1, model2]) scene.add_model(model) camera = t3.Camera(res=(600, 400)) scene.add_camera(camera) light = t3.Light([0.4, -1.5, -1.8]) scene.add_light(light) gui = ti.GUI('Camera', camera.res) while gui.running: gui.get_event(None) gui.running = not gui.is_pressed(ti.GUI.ESCAPE) model2.L2W[None] = t3.translate(0, ti.sin(t3.get_time()) * 0.6, 0) model.L2W[None] = t3.rotateZ(t3.get_time()) camera.from_mouse(gui) scene.render() gui.set_image(camera.img) gui.show()
import taichi as ti import taichi_three as t3 import numpy as np import time ti.init(ti.cpu) scene = t3.Scene() model = t3.Model(t3.Mesh.from_obj(t3.readobj('assets/torus.obj', scale=0.8))) scene.add_model(model) ball = t3.Model(t3.Mesh.from_obj(t3.readobj('assets/sphere.obj', scale=0.1))) ball.material = t3.Material( t3.BlinnPhong(emission=t3.Constant(t3.RGB(1.0, 1.0, 1.0)), )) scene.add_model(ball) camera = t3.Camera() camera.ctl = t3.CameraCtl(pos=[0, 1.8, 1.8]) scene.add_camera(camera) light = t3.PointLight(pos=[0, 1, 0]) scene.add_light(light) ambient = t3.AmbientLight(0.2) scene.add_light(ambient) 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) light.pos[None].y = ti.cos(time.time()) ball.L2W[None] = t3.translate(light.pos[None].value) scene.render() gui.set_image(camera.img)
import taichi as ti import taichi_three as t3 import numpy as np ti.init(ti.cpu) scene = t3.Scene() model = t3.Model(obj=t3.readobj('assets/torus.obj'), tex=ti.imread('assets/cloth.jpg')) camera = t3.Camera() scene.add_model(model) scene.add_camera(camera) light = t3.Light([0.4, -1.5, 1.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) gui.show()
import taichi as ti import taichi_three as t3 import numpy as np ti.init(ti.cpu) scene = t3.Scene() parts = t3.objunpackmtls(t3.readobj('assets/multimtl.obj', scale=0.8)) model1 = t3.Model(t3.Mesh.from_obj(parts[b'Material1'])) model2 = t3.Model(t3.Mesh.from_obj(parts[b'Material2'])) model1.material = t3.Material( t3.CookTorrance( # Up: gold color=t3.Constant(t3.RGB(1.0, 0.96, 0.88)), roughness=t3.Constant(0.2), metallic=t3.Constant(0.75), )) model2.material = t3.Material( t3.CookTorrance( # Down: cloth color=t3.Texture(ti.imread('assets/cloth.jpg')), roughness=t3.Constant(0.3), metallic=t3.Constant(0.0), )) scene.add_model(model1) scene.add_model(model2) camera = t3.Camera() camera.ctl = t3.CameraCtl(pos=[0.8, 0, 2.5]) scene.add_camera(camera) light = t3.Light([0, -0.5, -1], 0.9) scene.add_light(light) ambient = t3.AmbientLight(0.1) scene.add_light(ambient)
import taichi as ti import taichi_three as t3 import numpy as np res = 512, 512 ti.init(ti.cpu) scene = t3.Scene() cornell = t3.objunpackmtls(t3.readobj('assets/cornell.obj')) plane = t3.readobj('assets/plane.obj') model1 = t3.Model(t3.Mesh.from_obj(cornell[b'Material'])) model1.material = t3.Material( t3.IdealRT( specular=t3.Constant(0.0), diffuse=t3.Constant(1.0), emission=t3.Constant(0.0), diffuse_color=t3.Texture('assets/smallptwall.png'), )) scene.add_model(model1) model2 = t3.Model(t3.Mesh.from_obj(cornell[b'Material.001'])) model2.material = t3.Material( t3.IdealRT( specular=t3.Constant(0.7), diffuse=t3.Constant(1.0), emission=t3.Constant(0.0), )) scene.add_model(model2) light = t3.Model(t3.Mesh.from_obj(plane)) light.material = t3.Material( t3.IdealRT( specular=t3.Constant(0.0),
import taichi as ti import taichi_three as t3 import numpy as np ti.init(ti.cpu) scene = t3.Scene() obj = t3.readobj('assets/logo.obj', scale=0.8) t3.objbothface(obj) logo1 = t3.Model(t3.Mesh.from_obj(obj)) logo2 = t3.Model(t3.Mesh.from_obj(obj)) scene.add_model(logo1) scene.add_model(logo2) camera = t3.Camera() camera.ctl = t3.CameraCtl(pos=[-1, 1, 1]) scene.add_camera(camera) light = t3.Light([0.4, -1.5, 0.8]) scene.add_light(light) gui = ti.GUI('Taichi THREE', camera.res) while gui.running: gui.get_event(None) gui.running = not gui.is_pressed(ti.GUI.ESCAPE) camera.from_mouse(gui) t = t3.get_time() logo1.L2W[None] = t3.rotateY(t) logo2.L2W[None] = t3.rotateX(t3.pi - t) @ t3.rotateZ(t3.pi / 2) scene.render() gui.set_image(camera.img) gui.show()
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() if __name__ == '__main__': import taichi_three as t3 obj = t3.readobj('assets/sphere.obj') t3.objmknorm(obj) t3.objshow(obj, 'normal')
import taichi as ti import taichi_three as t3 import numpy as np ti.init(ti.cpu) scene = t3.Scene() model = t3.Model(t3.Mesh.from_obj(t3.readobj('assets/monkey.obj', scale=0.8))) scene.add_model(model) camera = t3.Camera(res=(256, 256)) camera.ctl = t3.CameraCtl(pos=[0, 0, 2.5], target=[0, 0, 0], up=[0, 1, 0]) scene.add_camera(camera) camera2 = t3.Camera() camera2.ctl = t3.CameraCtl(pos=[0, 1, -2], target=[0, 1, 0], up=[0, 1, 0]) scene.add_camera(camera2) light = t3.Light([0.4, -1.5, -0.8]) scene.add_light(light) camera.type = camera.ORTHO camera2.set_intrinsic(256, 256, 256, 256) gui = ti.GUI('Model', camera.res) gui2 = ti.GUI('Model2', camera2.res) while gui.running and gui2.running: gui.get_event(None) gui2.get_event(None) gui.running = not gui.is_pressed(ti.GUI.ESCAPE) gui2.running = not gui2.is_pressed(ti.GUI.ESCAPE)
import taichi as ti import taichi_three as t3 import numpy as np ti.init(ti.cpu) scene = t3.Scene() obj = t3.readobj('assets/cube.obj', scale=0.6) model = t3.Model(t3.Mesh.from_obj(obj)) model.material = t3.Material( t3.CookTorrance( color=t3.Texture(ti.imread('assets/cloth.jpg')), normal=t3.NormalMap( texture=t3.Texture(ti.imread('assets/normal.png'))), )) scene.add_model(model) camera = t3.Camera() camera.ctl = t3.CameraCtl(pos=[0, 1, 1.8]) scene.add_camera(camera) light = t3.Light([0.4, -0.8, -1.7]) scene.add_light(light) gui = ti.GUI('Normal map', 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) #gui.set_image(camera.fb['normal'].to_numpy() * 0.5 + 0.5) 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(obj=t3.readobj('assets/monkey.obj', scale=0.8)) scene.add_model(model) camera = t3.Camera() scene.add_camera(camera) light = t3.Light([0.4, -1.5, 1.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) #model.L2W.from_mouse(gui) scene.render() gui.set_image(camera.img) gui.show()
import taichi as ti import taichi_three as t3 import numpy as np ti.init(ti.cpu) scene = t3.Scene() cornell = t3.readobj('assets/cornell.obj') cube = t3.readobj('assets/plane.obj') model = t3.Model(t3.Mesh.from_obj(cornell)) scene.add_model(model) light = t3.PointLight(pos=[0.0, 3.9, 0.0], color=15.0) scene.add_light(light) camera = t3.Camera() camera.ctl = t3.CameraCtl(pos=[0, 2, 6], target=[0, 2, 0]) scene.add_camera_d(camera) original = t3.FrameBuffer(camera) mapped = t3.ImgUnaryOp(original, lambda x: 1 - ti.exp(-x)) scene.add_buffer(mapped) #light.L2W[None] = t3.translate(0, 3.9, 0) gui_ldr = ti.GUI('LDR', camera.res) gui_hdr = ti.GUI('HDR', camera.res) while gui_ldr.running and gui_hdr.running: gui_hdr.get_event(None) camera.from_mouse(gui_hdr) scene.render() gui_ldr.set_image(original.img) gui_ldr.show() gui_hdr.set_image(mapped.img) gui_hdr.show()
import taichi as ti import taichi_three as t3 import numpy as np ti.init(ti.cpu) scene = t3.Scene() obj = t3.readobj('assets/torus.obj', scale=0.8) model = t3.Model(t3.Mesh.from_obj(obj)) model.material = t3.Material( t3.CookTorrance(color=t3.Texture(ti.imread('assets/cloth.jpg')), )) scene.add_model(model) camera = t3.Camera() camera.ctl = t3.CameraCtl(pos=[0, 1, -1.8]) scene.add_camera(camera) light = t3.Light([0.4, -1.5, 1.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) gui.show()
import taichi as ti import taichi_three as t3 import numpy as np ti.init(ti.cpu) scene = t3.Scene() obj1 = t3.readobj('assets/torus.obj', scale=0.8) obj2 = t3.readobj('assets/cylinder.obj', scale=0.6) model1 = t3.Model(t3.Mesh.from_obj(obj1)) model2 = t3.Model(t3.Mesh.from_obj(obj2)) scene.add_model(model1) scene.add_model(model2) camera = t3.Camera() camera.ctl = t3.CameraCtl(pos=[1, 1, -1]) scene.add_camera(camera) light = t3.Light([0.4, -1.5, 1.8]) scene.add_shadow_camera(light.make_shadow_camera()) scene.add_light(light) gui = ti.GUI('Model', camera.res) gui2 = ti.GUI('Depth map', light.shadow.res) gui2.fps_limit = None while gui.running: gui.get_event(None) gui.running = not gui.is_pressed(ti.GUI.ESCAPE) camera.from_mouse(gui) model2.L2W[None] = t3.translate(0, 0.16 * ti.sin(gui.frame * 0.03), 0) scene.render_shadows() scene.render() gui.set_image(camera.img)
import taichi as ti import taichi_three as t3 import numpy as np ti.init(ti.cpu) scene = t3.Scene() texture = ti.imread("assets/cloth.jpg") model = t3.Model(obj=t3.readobj('assets/monkey.obj', scale=0.6), tex=texture) scene.add_model(model) camera = t3.Camera(res=(512, 512), pos=[0, 0, -2], target=[0, 0, 0]) scene.add_camera(camera) light = t3.Light(dir=[0, 0, 1], color=[1.0, 1.0, 1.0]) scene.add_light(light) light2 = t3.Light(dir=[0, -1, 0], color=[1.0, 1.0, 1.0]) scene.add_light(light2) gui = ti.GUI('Model', camera.res) while gui.running: gui.running = not gui.get_event(ti.GUI.ESCAPE) camera.from_mouse(gui) scene.render() gui.set_image(camera.img) 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.Mesh.from_obj(t3.readobj('assets/torus.obj', scale=0.8))) model.material = t3.Material( t3.CookTorrance( color=t3.Texture(ti.imread('assets/cloth.jpg')), roughness=t3.Texture(ti.imread('assets/pattern.jpg')), metallic=t3.Constant(0.5), )) scene.add_model(model) camera = t3.Camera() camera.ctl = t3.CameraCtl(pos=[0.8, 0, 2.5]) scene.add_camera(camera) light = t3.Light([0, -0.5, -1]) scene.add_light(light) ambient = t3.AmbientLight(0.3) scene.add_light(ambient) gui = ti.GUI('PBR demo', camera.res) while gui.running: gui.get_event(None) gui.running = not gui.is_pressed(ti.GUI.ESCAPE) camera.from_mouse(gui) model.L2W[None] = t3.rotateX(angle=t3.get_time()) scene.render() gui.set_image(camera.img)
import taichi as ti import taichi_three as t3 import numpy as np res = 512, 512 ti.init(ti.cpu) scene = t3.Scene() cornell = t3.readobj('assets/cornell.obj', orient='-xyz') cube = t3.readobj('assets/plane.obj') model = t3.Model(t3.Mesh.from_obj(cornell)) model.material = t3.Material( t3.IdealRT( diffuse=t3.Constant(1.0), emission=t3.Constant(0.0), )) scene.add_model(model) light = t3.Model(t3.Mesh.from_obj(cube)) light.material = t3.Material( t3.IdealRT( diffuse=t3.Constant(0.0), emission=t3.Constant(1.0), emission_color=t3.Constant(10.0), )) scene.add_model(light) camera = t3.RTCamera(res=res) camera.ctl = t3.CameraCtl(pos=[0, 2, 8], target=[0, 2, 0]) scene.add_camera(camera) accumator = t3.Accumator(camera.res) light.L2W[None] = t3.translate(0, 3.9, 0) @ t3.scale(0.25)
import taichi as ti import taichi_three as t3 import numpy as np import math, time ti.init(ti.cpu) scene = t3.Scene() monkey = t3.Model(obj=t3.readobj('assets/monkey.obj', scale=0.4)) torus = t3.Model(obj=t3.readobj('assets/torus.obj', scale=0.6)) scene.add_model(monkey) scene.add_model(torus) camera = t3.Camera() scene.add_camera(camera) light = t3.Light([0.4, -1.5, 1.8]) scene.add_light(light) gui = ti.GUI('Transform', camera.res) while gui.running: gui.get_event(None) gui.running = not gui.is_pressed(ti.GUI.ESCAPE) camera.from_mouse(gui) monkey.L2W.matrix[None] = t3.rotationZ(angle=time.time()) torus.L2W.offset[None] = [0, math.cos(time.time()) * 0.5, 0] scene.render() gui.set_image(camera.img) 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.Mesh.from_obj(t3.readobj('assets/orient.obj', scale=0.8))) scene.add_model(model) camera = t3.Camera() camera.ctl = t3.CameraCtl(pos=[0, 1, 1.8]) scene.add_camera(camera) light = t3.Light([0.4, -1.5, -1.8], 0.7) scene.add_light(light) ambient = t3.AmbientLight(0.3) scene.add_light(ambient) gui = ti.GUI('Orientation', 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) gui.show()
import taichi as ti import taichi_three as t3 import numpy as np ti.init(ti.cpu) scene = t3.Scene() obj = t3.readobj('assets/sphere.obj', scale=0.9) model = t3.Model(t3.Mesh.from_obj(obj)) model.material = t3.Material(t3.CookTorrance( roughness=t3.Uniform((), float), metallic=t3.Uniform((), float), )) scene.add_model(model) camera = t3.Camera() camera.ctl = t3.CameraCtl(pos=[0, 1, 1.8]) scene.add_camera(camera) light = t3.Light(dir=[0.9, -1.5, -1.3]) scene.add_light(light) gui = ti.GUI('Material Ball', camera.res) roughness = gui.slider('roughness', 0, 1, step=0.05) metallic = gui.slider('metallic', 0, 1, step=0.05) roughness.value = 0.3 metallic.value = 0.0 while gui.running: gui.get_event(None) gui.running = not gui.is_pressed(ti.GUI.ESCAPE) model.material.shader.params['roughness'].fill(roughness.value) model.material.shader.params['metallic'].fill(metallic.value) if any(x < 0.6 for x in gui.get_cursor_pos()):
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 import math, time ti.init(ti.cpu) scene = t3.Scene() monkey = t3.Model(t3.Mesh.from_obj(t3.readobj('assets/monkey.obj', scale=0.6))) torus = t3.Model(t3.Mesh.from_obj(t3.readobj('assets/torus.obj'))) scene.add_model(monkey) scene.add_model(torus) camera = t3.Camera() scene.add_camera(camera) light = t3.Light(dir=[0.4, -1.5, -1.8]) scene.add_light(light) gui = ti.GUI('Transform', camera.res) while gui.running: gui.get_event(None) gui.running = not gui.is_pressed(ti.GUI.ESCAPE) camera.from_mouse(gui) monkey.L2W[None] = t3.rotateZ(angle=time.time()) torus.L2W[None] = t3.translate(x=0, y=math.cos(time.time()) * 0.5, z=0) scene.render() gui.set_image(camera.img) gui.show()