Rendering the Sky, Sunsets, and Planets
535 points
• 6 days ago
• Article
Link
本文记录了在浏览器中构建真实大气散射着色器的一个月历程,灵感来自一张航天飞机 Endeavour 在日落时分拍摄的、地球高层大气色彩斑斓的照片。目标不是简单地绘制蓝色渐变背景,而是把天空模拟成光与空气分子、尘埃及其他颗粒在体积中相互作用的结果,采用像 raymarching 、 Rayleigh 和 Mie 散射以及臭氧吸收等技术。
作者首先用 raymarching 为大气着色器打下基础,采样大气密度并计算光学深度。具体做法包括用 Rayleigh 密度函数模拟随高度稀薄的大气分布,并应用 Beer's Law 计算透射率,即光穿过大气后剩余的强度。随后引入 Rayleigh 相位函数,考虑入射阳光与视线间的夹角,从而呈现出更真实的蓝天效果:较短波长的光散射更强。
为增强真实感,文章加入了 Mie 散射,用以模拟光与较大颗粒(如尘埃和气溶胶)的相互作用,在太阳周围形成朦胧的光晕;还加入了臭氧吸收,它会吸收部分波长的光,使天空颜色在太阳附近和黄昏时向更深的蓝紫色偏移。文中通过具体的密度与相位函数以及标高和散射系数等常数给出了这些效果的实现细节。
接着讨论光照部分,解释如何通过考虑从太阳到每个采样点路径上的光衰减来正确渲染日出和日落。实现方法是在主 raymarching 循环内嵌套一个"光线步进"循环,用于在光源方向上采样透射率,从而让着色器能准确表现一天之中天空颜色从正午明亮的蓝色到日落时温暖的橙红色的变化。
文章还把平面天空着色器改造为适用于三维行星周围大气的后期效果:通过屏幕空间 UV 和场景深度缓冲重建世界空间坐标,使 raymarching 能考虑场景几何,从而产生随距离变得更朦胧的大气雾效。作者还详述了为处理行星尺度启用对数深度缓冲的方法,并用光线与球体相交测试来确定大气壳层的起点和终点。
一个附加章节探讨了日食处理,介绍了如何实现太阳可见度函数,根据 moon 等天体的视半径和位置判定它们对太阳的遮挡程度。另一个附加示例展示了通过调整大气常数来模拟不同行星环境,例如 Mars 上尘埃弥漫的橙色大气及其独特的蓝色日落。最后,文章还介绍了一种受 Sebastian Hillaire 工作启发的更高性能的基于查找表(LUT)的方法:将昂贵的散射计算预先烘焙到透射率、天空视角和空气透视等 LUT 纹理中,然后在最终通道合成,从而相较于完全 raymarching 的实现获得显著的性能提升。
This article documents a month-long journey into building realistic atmospheric scattering shaders in the browser, inspired by a photo of the space shuttle Endeavour against the colorful layers of Earth's upper atmosphere at sunset. The goal was to move beyond simple blue gradient backgrounds and instead simulate the sky as the result of light interacting with air molecules, dust, and other particles in a volumetric way, using techniques like raymarching, Rayleigh and Mie scattering, and ozone absorption.
The author begins by laying the foundation for an atmosphere shader using raymarching to sample atmospheric density and calculate optical depth. This involves using the Rayleigh density function to model how air thins with altitude and applying Beer's Law to compute transmittance, which represents how much light survives traveling through the atmosphere. The Rayleigh phase function is then introduced to account for the angle between incoming sunlight and the view ray, resulting in a realistic blue sky where shorter wavelengths scatter more strongly.
To enhance realism, the article covers the addition of Mie scattering, which models light interaction with larger particles like dust and aerosols, creating a hazy glow around the sun. Ozone absorption is also incorporated, which removes certain wavelengths of light and shifts the sky's color toward deeper blues and purples, especially near the sun and during twilight. The implementation details for these effects are provided through specific density and phase functions, along with constants for scale heights and scattering coefficients.
The discussion then shifts to lighting, explaining how to properly render sunsets and sunrises by accounting for light attenuation along the path from the sun to each sample point. This is achieved by introducing a nested "lightmarch" loop within the main raymarching loop to sample transmittance in the direction of the light source. This allows the shader to accurately depict the changing colors of the sky throughout the day, from the bright blue of midday to the warm oranges and reds of sunset.
The article progresses to transforming the flat sky shader into a post-processing effect suitable for rendering atmospheres around 3D planets. This involves reconstructing world-space coordinates from screen-space UVs and the scene's depth buffer, allowing the raymarching to account for scene geometry and create atmospheric fog that makes distant objects appear hazier. The author also details enabling logarithmic depth buffers to handle the large scales involved in planetary rendering and using ray-sphere intersection tests to define the start and end points of the atmosphere shell.
A bonus section explores handling eclipses by implementing a sun visibility function that determines how much a celestial body like the moon obstructs the sun based on their apparent sizes and positions. Another bonus demonstrates how tweaking atmospheric constants can simulate different planetary environments, such as the dusty, orangy atmosphere of Mars with its distinctive blue sunsets. The article concludes with an exploration of a more performant LUT-based approach inspired by Sebastian Hillaire's work, which precomputes expensive scattering calculations into textures like transmittance, sky-view, and aerial perspective LUTs, then composes them in a final pass for a significant performance boost over the fully raymarched version.
41 comments • Comments Link
Sebastian Lague 的大气散射视频既有趣又富有启发性,对那些对程序化生成和图形编程感兴趣的人来说,常常成为他们进入这一领域的入口。 YouTube 的推荐算法随着时间显著影响了 Lague 的观看量,但在新冠疫情期间对小众话题兴趣的上升,也可能促成了他早期内容的病毒式传播。把 1993 年 Nishita 等人的开创性大气散射论文付诸实现仍然很有价值——那篇论文可读性强,其方法对现代实现依然具有参考意义。
当用 Rayleigh 和 Mie 散射方程从原本只是静态蓝色天空盒的场景中生成出令人信服的日落时,很多人都感受到一种深刻的顿悟:复杂的自然现象可以从相对简洁的数学模型中涌现。把大气散射和体积云渲染结合起来,能创造出令人惊叹的天空场景,尤其是在日落效果上,这一点在各种程序化太空与行星生成器中表现得尤为明显。该项目采用 MIT 许可证,使其对游戏开发者立即可用;在固定摄像机视角下,利用正弦函数就能轻松扩展出季节性太阳角度的变化。
相比之下,简单的基于梯度的天空渲染效果平淡无奇,物理基础的散射模型在质量上有明显优势。在 XNA 等业余游戏引擎中实现散射也能得到出乎意料的好效果:太阳的渲染会自然而然地从散射计算中显现出来,许多实现都是在教程系列和学术论文的指导下完成的。 SpaceEngine 是一个长期运行的项目,在天体渲染和天文模拟方面细节卓越,既包含大量真实天体也有程序化生成的天体目录;其模拟规模令人惊叹,覆盖了整个 Hipparcos 星表、已知的系外行星、数千个星系,以及超出可观测宇宙实际数量的程序化天体。
次表面散射的研究(如 Stanford 的 BSSRDF 论文)揭示了渲染牛奶等半透明材料所面临的独特挑战,这种复杂性在某种程度上与大气效果的难题相似。真实的天空渲染对天文馆和星图应用尤其重要,能显著增强教育与沉浸式体验。即便读者对底层数学理解有限,详尽的图形编程文章所呈现的视觉美感和工程思路仍然具有很强的启发性。
除了基于物理的散射模型外,Perez All-Weather 和 Preetham 等天空模型也提供了替代方案,不少开发者已将它们作为个人项目成功实现。总体而言,基于物理的渲染技术远胜于堆叠梯度等图形技巧,因为前者能产生更令人信服、更美丽且更贴近真实世界的结果。展示先进图形工作的个人网站常常激励他人改善自己的网络形象和设计能力。大气散射的优雅之处在于:相对紧凑的数学模型能够生成如此复杂且美丽的自然景观。
讨论中流露出对大气散射作为技术成就与艺术追求的深切赞赏,参与者在各种平台上分享了从 XNA 到现代浏览器的个人实现经验。大家普遍认为,基于物理的方法比简单的梯度方法带来显著优越性,而这一领域仍然足够开放,让业余开发者也能做出令人印象深刻的成果。对话还涉及图形编程的更广生态:包括影响内容创作者的算法变化、文档完善的开源项目的价值,以及从优雅数学模型中涌现出的复杂自然现象所带来的启发。 • Sebastian Lague's atmospheric scattering videos are highly entertaining and inspiring for those interested in procedural generation and graphics programming, with his work serving as a gateway into the field for many viewers.
• The YouTube algorithm significantly impacted Lague's view counts over time, though increased interest in niche topics during COVID may have also played a role in his earlier viral success.
• Implementing the foundational 1993 Nishita et al. paper on atmospheric scattering remains a rewarding experience, with its readable approach still relevant for modern implementations.
• The moment when Rayleigh and Mie scattering equations first produce a convincing sunset from a static blue skybox is described as a profound realization that complex natural phenomena can emerge from relatively simple mathematical models.
• Combining atmospheric scattering with volumetric cloud rendering creates stunning sky scenes, particularly for sunset effects, as demonstrated in procedural space/planet generators.
• The MIT license on this project makes it immediately practical for game developers, with fixed camera perspectives allowing for straightforward extension to seasonal sun angle variations using sinusoidal functions.
• Simple gradient-based sky rendering approaches can produce mediocre results compared to physics-based atmospheric scattering models, highlighting the dramatic quality difference that proper simulation achieves.
• Implementing scattering in hobbyist game engines like XNA can yield surprisingly good results, with sun rendering emerging naturally from the scattering calculations, often guided by tutorial series and academic papers.
• SpaceEngine stands out as a long-running project with exceptional attention to detail in atmospheric rendering and astronomical simulation, featuring an enormous catalog of real and procedurally generated celestial objects.
• The scale of SpaceEngine's simulation is remarkable, encompassing the entire Hipparcos catalog, known exoplanets, thousands of galaxies, and procedurally generated objects exceeding the observable universe's actual count.
• Subsurface scattering research, such as the Stanford BSSRDF paper, reveals how rendering translucent materials like milk presents unique challenges that parallel the complexity of atmospheric effects.
• Atmospheric scattering implementation is particularly relevant for planetarium and star map applications, where realistic sky rendering enhances the educational and immersive experience.
• Even readers with limited technical understanding can appreciate the visual beauty and craftsmanship of detailed graphics programming writeups, finding them inspiring despite not grasping the underlying mathematics.
• The Perez All-Weather and Preetham sky models represent alternative approaches to atmospheric rendering that some developers have successfully implemented as personal projects.
• Physics-based rendering techniques are strongly preferred over graphics hacks like stacked gradients, as they produce more convincing and beautiful results grounded in real-world phenomena.
• Personal websites showcasing advanced graphics work often inspire others to improve their own web presence and design skills.
• The elegance of atmospheric scattering lies in how relatively compact mathematical models can generate such visually complex and beautiful natural phenomena.
The discussion reveals a deep appreciation for atmospheric scattering as both a technical achievement and an artistic pursuit, with participants sharing personal experiences implementing these techniques across various platforms from XNA to modern web browsers. There's a strong consensus that physics-based approaches yield dramatically superior results compared to simpler gradient-based methods, and that the field remains accessible enough for hobbyist developers to achieve impressive results. The conversation also touches on the broader ecosystem of graphics programming, including algorithm changes affecting content creators, the value of well-documented open-source projects, and the inspirational power of seeing complex natural phenomena emerge from elegant mathematical models.