Shadertoy解析:Ray tracer with volumetric light 光线追踪+体积光

原地址https://www.shadertoy.com/view/4dsGRn

这个"反向光线追踪"+体积光的代码简单清晰,加注释总共也就300+行。本人也是初学者,除了1行不是完全理解,其他都完全搞懂了。在此与分享。

其实最基本的光线追踪和体积光并不难。不过,首先了解一下本代码中的光线追踪和体积光的基本算法。

"反向光线追踪":把屏幕空间的每一个像素转化到三维空间,并沿着视线方向发射大量视线束,并计算视线束碰到的场景点上的颜色c,使得color+=c(使用Blinn-Phong光照模型),可多次重复此步骤(即多次反向光线追踪/视线束反弹),使得color+=c,最终得到该像素的光线追踪颜色(起点函数/主函数:mainImage)

体积光:同上的大量视线束起点开始,将每个起点做一次随机偏移,并以步长累次采样,相加得像素的体积光颜色。每次采样看是否与光源有遮挡,有遮挡的话此点采样值为0,否则为此点照度*inscattering,inscattering是一个比较小的值(<1)。所以这里的体积光算法是不太物理,比较经验,要调参数的算法。并且,对于近处的物体,由于视线束逐步采样会由于碰到其停止,所以只要是近处的像素都会因为采样次数不足而比较暗。所以这个体积光算法只能具体情况具体应用啦。

最终颜色=光线追踪颜色+0.5*体积光颜色;(0.5比较经验)

原地址渲染的画面会有噪点,是体积光算法导致的,关掉体积光即可“丝滑”。反向光线追踪是不会出现噪点的,而正向光线追踪(光子从光源发出,被屏幕捕捉)是会的。

原代码可以改成多次反向光线追踪/视线束反弹版本,你可以看到"多次"版本的球中球中球有反射效果,而原地址的版本仅做了2次反弹,球中球中球是没有反射效果的。

最后,完整版。本人注释,小改成多次反弹的代码:

// Ray tracing with improvised volumetric shadows
// Attempting to make it windows friendly by reducing loopiness.
// Thanks to iq for fixing the sphere intersection code, sqrt(-n) is bad :)
// Matthijs De Smedt
// @anji_nl

const float ZMAX = 99999.0;
const float EPSILON = 0.001;
const int MAX_BOUNCES = 20; // For looping version
const int VOLUMETRIC_SAMPLES = 10;

//交点
struct Intersection
{
	vec3 p;
	//与向量束起点的距离
	float dist;
	
	vec3 n;
	vec3 diffuse;
	vec3 specular;
};
	
//向量束,用作视线束和体积光里的射线检测
struct Ray
{
	vec3 o;
	vec3 dir;
};
	
struct Light
{
	vec3 p;
	vec3 color;
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个插件允许您通过生成真正容积的程序束来大大改善场景的照明。 这是模拟聚灯和手电筒的密度,深度和音量的完美,简单而便宜的方法。 The simple and efficient volumetric lighting solution compatible with every platforms: Windows PC, Mac OS X, Linux, WebGL, iOS, Android, VR, AR, Consoles, Built-in/Legacy Render Pipeline, SRP (URP & HDRP)! The perfect, easy and cheap way to simulate density, depth and volume for your spotlights and flashlights, even on Mobile! It greatly improves the lighting of your scenes by automatically and efficiently generating truly volumetric procedural beams of light to render high quality light shafts effects. A production ready plugin proven by awesome released games showcasing it: - BONEWORKS released for high-end PC VR - Carly and the Reaperman released for Playstation 4 PSVR and high-end PC VR - Kingspray Graffiti released for high-end PC VR and Oculus Quest - Hexagroove released for Nintendo Switch - Covert released for Playstation 4 PSVR, Oculus Rift and Oculus Go Features: - Truly volumetric: works even if you are INSIDE the beam of light. - Incredibly easy to use and integrate / Import it instantly / Zero setup required. - In addition to the Built-in Legacy Render Pipeline, it fully supports the Universal Render Pipeline (URP) and the High Definition Pipeline (HDRP). - Optimized for VR: tested with high-end headsets (Oculus Rift, HTC Vive, Valve Index...) and standalone hardware (Oculus Go, Oculus Quest...), supports all Stereo Rendering Methods (Multi Pass, Single Pass and Single Pass Instanced or Multiview). - AR Ready: supports both Apple iOS ARKit and Google Android ARCore. - GPU Instancing & SRP Batcher: render and batch thousands of beams in 1 single drawcall. - Super FAST w/ low memory footprint: doesn't require any post-process, command buffers, nor compute shaders: works great even on low-performance platforms such as Mobiles and WebGL. - Procedural generation: everything is dynamically computed under the hood. - Add unlimited light beams everywhere: alternative solutions usually requi
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值