An implementation of Ray Tracing in One Weekend using Taichi

Overview

又一个Taichi语言的Ray Tracer

背景简介

这个Ray Tracer基本上是照搬了Peter Shirley的第一本小书Ray Tracing in One Weekend,在我写的时候参考的是Version 3.2.3这个版本。应该比其他中文博客删改了不少内容。果然Peter Shirley不是标题党,我在国庆期间用了2号、3号两天就写完了。期间还参考了moranzcw大佬一年前的左右 (主要是抄了几个物体的定义)及其帖子下其他人的代码,在此表示感谢。

Taichi语言依附于Python,而我自己本身对Python不是那么的熟悉,更不用提Taichi了。Taichi语言的开发者带简化很多快速数值计算及并行开发的操作,但同时也增添了很多限制(比如我不知道class如何复制导致hit_record无法实现)。再加上python对OOP编程的不完全支持,我写的程序很不优雅,同时也没做足优化。

开发环境:Surface Book 2(15inch)上,使用Python 3.7.8+Taichi v0.8.1

这次写的ray tracer算是运用了太极图形课前两节除了元编程的其他所有内容,把该封装的都封装起来了。

原来以为是个小程序,所以写在了一个文件里,后来也懒得改了

与原书不同的是优化了random_in_unit_sphere()这个用来生成在单位球内随机一点的函数,在我的电脑上跑时(18fps+)与moranzcw大佬的程序(9fps+)相比大概能快一倍。(估计拥有更高配置显卡的大佬们能跑到实时吧)为此特地写了另一个程序来不同随机函数的可视化分布,顺便尝鲜了一下GGUI。

之后加入了一些键鼠操作和录制功能

除此之外还生成了一张书的封面图。不知道为什么原书中22*22个小球不能在我的电脑上生成,经过漫长的等待后,等我的只有被占用了11/15.9GB的内存,和因为使用虚拟内存而跑的飞快的硬盘,当然最后还是闪退了。

成功效果展示

封面图(有小球卡在大球里了 😂 )

Cover

Cornell Box的效果

Cornell Box 1 Cornell Box 2

来个动图
Cornell Box Gif

这是使用错误的随机函数导致的效果

Wrong Cornell Box Gif

使用方法

点击窗口来切换视点
j,k键调整水平视角
s键截图
r键开启/终止录制

整体结构

-LICENSE
-|ouput             如果开始录制的话,视频和动图会储存在这儿
-|img               示例图片
-README.MD
-raytracing.py      本体,生成一个Cornell Box
-dots_on_sphere.py  对随机化程序的可视化

运行方式

应该都安装好taichi了吧

python3 raytracing.py

还存在的问题

  • 如何优雅的ODOP编程,解决类的复制与传递
  • ti.random() 如何设置随机种子
  • 如何声明@ti.kernel中最外层循环串行
  • 如何手动释放内存

TODO(就写写,不一定会码)

  • 渲染一张封面图
  • 增加显式光源,渲染一个Cornell Box
  • 添加其他一些模型
  • 添加材质,贴图
  • 加入BVH
  • 加入三维模型.obj的读取与渲染,渲染一只Standford Rabbit
  • 增加键鼠交互
  • 跟进下一本书的内容
  • 在学习了之后的课程后,使用更多Taichi的特性
You might also like...
A one place destination to check whatever is trending on the top social and news websites at present.
A one place destination to check whatever is trending on the top social and news websites at present.

UpTrend A one place destination to check whatever is trending on the top social and news websites at present. Explore the docs » View Demo · Report Bu

 [Cython] Vs [Python] Which one is Faster ?
[Cython] Vs [Python] Which one is Faster ?

[Cython] Vs [Python] ? Attractive Contrast :) Mission : Which one is Faster ? Comparing of Execution runtime for [Selection_sort] with Time Complexity

Providing a working, flexible, easier and faster installer than the one officially provided by Arch Linux
Providing a working, flexible, easier and faster installer than the one officially provided by Arch Linux

Purpose The purpose is to bring more people to Arch Linux by providing a working, flexible, easier and faster installer than the one officially provid

A place where one-off ideas/partial projects can live comfortably

A place to post ideas, partial projects, or anything else that doesn't necessarily warrant its own repo, from my mind to the web.

Automatically deletes Capital One Eno virtual cards for when you've made a couple too many.

eno-delete Automatically deletes Capital One Eno virtual cards for when you've made a couple too many. Warning: Program will delete ALL virtual cards

A web-based chat application that enables multiple users to interact with one another
A web-based chat application that enables multiple users to interact with one another

A web-based chat application that enables multiple users to interact with one another, in the same chat room or different ones according to their choosing.

Tips that improve your life in one way or another

Tips that improve your life in one way or another. This software downloads life tips from reddit.com/r/LifeProTips and tweet the most upvoted tips on Twitter.

Python script which synchronizes the replica-directoty with the original-one.

directories_synchronizer Python script which synchronizes the replica-directoty with the original-one. Automatically detects all changes when script i

Make dbt docs and Apache Superset talk to one another
Make dbt docs and Apache Superset talk to one another

dbt-superset-lineage Make dbt docs and Apache Superset talk to one another Why do I need something like this? Odds are rather high that you use dbt to

Comments
  • 优秀作业中奖~快来联系我们哦

    优秀作业中奖~快来联系我们哦

    Hi 0xrabbyte 恭喜你在本周太极图形课的直播抽奖环节中奖!快来将收件地址发给小编,太极周边将会快马加鞭向你飞奔而来~ 小编的邮箱是:[email protected] 快递预计将会在下周五前发放完毕哦~ 再次感谢你对图形课的支持,也为你的优秀作业点赞👍

    太极图形课小编 2021.11.12

    opened by taichiCourse01 0
  • TypeError: unsupported operand type(s) for -: 'SNodeHostAccess' and 'float'

    TypeError: unsupported operand type(s) for -: 'SNodeHostAccess' and 'float'

    [Taichi] version 0.8.1, llvm 10.0.0, commit cc2dd342, win, python 3.8.13 [Taichi] Starting on arch=cuda 2527 Generated Objects Traceback (most recent call last): File "E:/files/作业/计算机图形学/TinyRayTracer-Taichi/raytracing.py", line 970, in build_bvh_tree(0, world.objects) File "E:/files/作业/计算机图形学/TinyRayTracer-Taichi/raytracing.py", line 508, in build_bvh_tree build_bvh_tree(bvh_tree[u].left, objs[:mid]) File "E:/files/作业/计算机图形学/TinyRayTracer-Taichi/raytracing.py", line 508, in build_bvh_tree build_bvh_tree(bvh_tree[u].left, objs[:mid]) File "E:/files/作业/计算机图形学/TinyRayTracer-Taichi/raytracing.py", line 508, in build_bvh_tree build_bvh_tree(bvh_tree[u].left, objs[:mid]) [Previous line repeated 9 more times] File "E:/files/作业/计算机图形学/TinyRayTracer-Taichi/raytracing.py", line 512, in build_bvh_tree is_hit, box_a = get_bounding_box(objs_id[bvh_tree[u].left], bvh_tree[u].time0, bvh_tree[u].time1) File "E:/files/作业/计算机图形学/TinyRayTracer-Taichi/raytracing.py", line 313, in get_bounding_box return sphere(sphere_centers[objs_ind[id]], sphere_radius[objs_ind[id]], File "D:\software\Anaconda\envs\master\lib\site-packages\taichi\lang\kernel_impl.py", line 99, in decorated return fun.call(*args) File "D:\software\Anaconda\envs\master\lib\site-packages\taichi\lang\kernel_impl.py", line 135, in call return self.func(*args) File "E:/files/作业/计算机图形学/TinyRayTracer-Taichi/raytracing.py", line 584, in bounding_box return True, AABB(self.center - ti.Vector([self.radius, self.radius, self.radius]), File "D:\software\Anaconda\envs\master\lib\site-packages\taichi\lang\common_ops.py", line 24, in sub return ti.sub(self, other) File "D:\software\Anaconda\envs\master\lib\site-packages\taichi\lang\ops.py", line 74, in wrapped return a.element_wise_binary(imp_foo, b) File "D:\software\Anaconda\envs\master\lib\site-packages\taichi\lang\matrix.py", line 159, in element_wise_binary ret.entries[i] = foo(self.entries[i], other.entries[i]) File "D:\software\Anaconda\envs\master\lib\site-packages\taichi\lang\ops.py", line 63, in imp_foo return foo(x, y) File "D:\software\Anaconda\envs\master\lib\site-packages\taichi\lang\ops.py", line 441, in sub return _binary_operation(_ti_core.expr_sub, _bt_ops_mod.sub, a, b) File "D:\software\Anaconda\envs\master\lib\site-packages\taichi\lang\ops.py", line 176, in _binary_operation return python_op(a, b) TypeError: unsupported operand type(s) for -: 'SNodeHostAccess' and 'float'

    opened by 13253591602 1
Owner
张皓
张皓
Tracing and Observability with OpenFaaS

Tracing and Observability with OpenFaaS Today we will walk through how to add OpenTracing or OpenTelemetry with Grafana's Tempo. For this walk-through

Lucas Roesler 8 Nov 17, 2022
Ahmed Hossam 12 Oct 17, 2022
A Pythonic Data Catalog powered by Ray that brings exabyte-level scalability and fast, ACID-compliant, change-data-capture to your big data workloads.

DeltaCAT DeltaCAT is a Pythonic Data Catalog powered by Ray. Its data storage model allows you to define and manage fast, scalable, ACID-compliant dat

null 45 Oct 15, 2022
We are building an open database of COVID-19 cases with chest X-ray or CT images.

?? Note: please do not claim diagnostic performance of a model without a clinical study! This is not a kaggle competition dataset. Please read this pa

Joseph Paul Cohen 2.9k Dec 30, 2022
One Ansible Module for using LINE notify API to send notification. It can be required in the collection list.

Ansible Collection - hazel_shen.line_notify Documentation for the collection. ansible-galaxy collection install hazel_shen.line_notify --ignore-certs

Hazel Shen 4 Jul 19, 2021
Schemdule is a tiny tool using script as schema to schedule one day and remind you to do something during a day.

Schemdule is a tiny tool using script as schema to schedule one day and remind you to do something during a day. Platform Python Install Use pip: pip

StardustDL 4 Sep 13, 2021
One destination for all the developer's learning resources.

DevResources One destination for all the developer's learning resources. Find all of your learning resources under one roof and add your own. Live ✨ Y

Gaurav Sharma 33 Oct 21, 2022
AMTIO aka All My Tools in One

AMTIO AMTIO aka All My Tools In One. I plan to put a bunch of my tools in this one repo since im too lazy to make one big tool. Installation git clone

osintcat 3 Jul 29, 2021
One line Brainfuck interpreter in Python

One line Brainfuck interpreter in Python

null 16 Dec 21, 2022
Fixes your Microphone Level to one specific value.

MicLeveler Fixes your Microphone Level to one specific value. Intention A friend of mine has the problem that some programs are setting his microphone

Moritz Timpe 2 Oct 14, 2021