glsl实现燃烧的火焰canvas动画效果

代码语言:html

所属分类:动画

代码描述:glsl实现燃烧的火焰canvas动画效果,GLSL(OpenGL着色语言OpenGL Shading Language)语法跟C语言很类似,在可编程管线中我们必须要纯手写顶点和片源着色器,这里就要求必须使用GLSL,自行编译,链接,使用

代码标签: 火焰 canvas 动画 效果

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">

<style>
body {
    overflow:hidden;
    margin:0;
}
</style>

</head>
<body translate="no">
<canvas id="canvas"></canvas>
<script type="glsl">
precision highp float;
uniform float time;      
uniform vec2 resolution;  
    
// taken from https://www.shadertoy.com/view/4tlSzl
    
vec3 firePalette(float i) {
    float T = 1400. + 1300.*i; 
    vec3 L = vec3(7.4, 5.6, 4.4); 
    L = pow(L,vec3(5.0)) * (exp(1.43876719683e5/(T*L))-1.0);
    return 1.0-exp(-5e8/L); 
}    

vec3 hash33(vec3 p) { 
    float n = sin(dot(p, vec3(7, 157, 113)));    
    return fract(vec3(2097152, 262144, 32768)*n); 
}

float voronoi(vec3 p) {
	vec3 b, r, g = floor(p);
	p = fract(p); 
	float d = 1.; 
	for(int j = -1; j <= 1; j++) {
	    for(int i = -1; i <= 1; i++) {
		    b = vec3(i, j, -1);
		    r = b - p + hash33(g+b);
		    d = min(d, dot(r,r));
		    b.z = 0.0;
		    r = b - p + hash33(g+b);
		    d = min(d, dot(r,r));
		    b.z = 1.;
		    r = b - p + hash33(g+b);
		    d = min(d, dot(r,r));	
	    }
	}
	return d;
}

float noiseLayers(in vec3 p) {
    vec3 t = vec3(0., 0., p.z+time); 
    float tot = 0., sum = 0., amp = 1.; 
    for (int i = 0; i < 6; i++) {
        tot += voronoi(p + t) * amp; 
        p *= 2.0; 
        t *= 1.5; 
        sum += amp; 
        amp *= 0.5; 
    }
    return tot/sum; 
}
      
void main(void) {
    vec2 uv = (gl_FragCoord.xy - resolution.xy*0.5)/ resolution.y;
    vec3 rd = normalize(vec3(uv.x, uv.y, 3.1415/8.));
    float c = noiseLayers(rd*4.); 
    vec3 col = firePalette(c);
    gl_FragColor = vec4(sqrt(col), 1.);  
}
</script>
.........完整代码请登录后点击上方下载按钮下载查看

网友评论0