等离子效果
Plasma Effect

原始链接: https://www.4rknova.com/blog/2016/11/01/plasma

## 等离子体效果总结 “等离子体效果”是 1980-90 年代demoscene图形的标志,它使用出奇简单的数学公式生成迷人的流动图案。其工作原理是将多个正弦和余弦波函数结合起来,这些波函数具有不同的频率和相位。这些波的干涉会产生建设性和破坏性影响,从而产生该效果的动态峰值和谷值。 最初,早期硬件(如 Commodore 64 和 Amiga)的限制需要预先计算查找表。现代 GPU 轻松处理实时计算,从而实现更复杂、更高分辨率的等离子体视觉效果。 核心公式涉及基于屏幕坐标和时间来动画化波形图案,然后将结果值映射到颜色渐变(通常使用余弦颜色调色板来实现平滑过渡)。高级实现会添加镜面高光,使用导数计算来模拟反射表面并增强深度。这种效果仍然是程序化图形和波干涉之美的流行演示。

黑客新闻 新 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 等离子体效应 (4rknova.com) 11 分,by todsacerdoti 2 小时前 | 隐藏 | 过去 | 收藏 | 讨论 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请YC | 联系 搜索:
相关文章

原文

The plasma effect is one of the most iconic visual effects from the demoscene era of the 1980s and 1990s. Named for its resemblance to flowing plasma or aurora-like patterns, this effect creates organic, continuously moving patterns using surprisingly simple mathematics. Despite its simplicity, the visual result is captivating and has remained a favorite demonstration of procedural graphics techniques.

The effect relies on combining multiple sinusoidal wave patterns to create complex interference patterns. By animating these patterns over time and mapping them to color gradients, we achieve the characteristic flowing, pulsing appearance.

On early hardware like the Commodore 64 and Amiga, plasma effects often used pre-calculated lookup tables to avoid expensive real-time sine calculations. Modern GPUs can compute these effects in real-time with minimal performance impact, allowing for more complex variations and higher resolutions.

At its heart, the plasma effect combines multiple sine and cosine functions with different frequencies and phases. The basic approach involves:

  1. Generating wave patterns: Create 2D wave patterns using sine and cosine functions based on screen coordinates
  2. Combining waves: Add or multiply multiple wave patterns to create interference effects
  3. Time animation: Offset the wave patterns over time to create motion
  4. Color mapping: Map the resulting values to a color gradient

The typical formula structure looks like:

value = sin(x + time) + cos(y + time) + sin(distance + time)

Where different combinations of x, y, distance, and time create different visual patterns.

The beauty of the plasma effect comes from wave interference. When multiple sinusoidal patterns overlap, they create complex patterns through constructive and destructive interference:

  • Constructive interference: Waves align and amplify each other
  • Destructive interference: Waves cancel each other out

This creates the characteristic peaks and valleys that define the plasma’s flowing appearance.

The numerical output of the combined sine waves is then mapped to colors. A common approach uses the cosine color palette technique, which provides smooth, continuous color gradients by treating the wave output as input to RGB color channels with different phase offsets.

Modern implementations can add specular highlights by analyzing the gradient of the color field. This creates the illusion of a reflective surface and adds depth to the otherwise flat 2D pattern.

The specular component is typically implemented using derivatives (dFdx and dFdy in GLSL) to detect rapid color changes, treating these as “surface normals” for lighting calculations.

Below is my implementation of the above in GLSL along with a live preview of the effect.

// by Nikos Papadopoulos, 4rknova / 2016
// Specular highlights contributed by Shane

#define SPECULAR
#define SCALE 1.0

void mainImage(out vec4 col, in vec2 pc)
{
	float time = iTime;
    vec2 a = vec2(iResolution.x /iResolution.y, 1);
    vec2 c = SCALE * pc.xy / iResolution.xy * a * 4. + time * .3;

	float k = .1 + cos(c.y + sin(.148 - time)) + 2.4 * time;
	float w = .9 + sin(c.x + cos(.628 + time)) - 0.7 * time;
	float d = length(c);
	float s = 7. * cos(d+w) * sin(k+w);
	
	col = vec4(.5 + .5 * cos(s + vec3(.2, .5, .9)), 1);
    
    #ifdef SPECULAR
    col *= vec4(1, .7, .4, 1) 
        *  pow(max(normalize(vec3(length(dFdx(col)), 
               length(dFdy(col)), .5/iResolution.y)).z, 0.), 2.)
        + .75; 
    #endif
}
联系我们 contact @ memedata.com