美文网首页Unity Shader分享
Shader特效——实现“闪电”(转)

Shader特效——实现“闪电”(转)

作者: 树上的cat_ee3c | 来源:发表于2017-10-16 08:39 被阅读63次

效果图

片元着色器代码:

[cpp]view plaincopy

uniformfloattime_0_X;

uniform vec4 color;

uniform sampler3D Noise;

uniformfloatglowStrength;

uniformfloatheight;

uiformfloatglowFallOff;

uniformfloatspeed;

uniformfloatsampleDist;

uniformfloatambientGlow;

uniformfloatambientGlowHeightScale;

uniformfloatvertNoise

voidmain(void)

{

//vec2 texCoord = vec2(2.* (gl_FragCoord.x - 256.)/ 512.,

//2. * (gl_FragCoord.y - 256.)/ 512.);

vec2 texCoord = vec2(clamp(2.* (gl_FragCoord.x - 256.)/ 512., -1., 1.),

clamp(2.* (gl_FragCoord.y - 256.)/ 512., -1., 1.));

// 噪声采样的垂直位置

vec2 t = vec2(speed * time_0_X * .5871

- vertNoise * abs(texCoord.y), speed * time_0_X);

// 噪声采样的三个水平位置

floatxs0 = texCoord.x - sampleDist;

floatxs1 = texCoord.x;

floatxs2 = texCoord.x + sampleDist;

// 三次噪声采样

floatnoise0 = texture3D(Noise, vec3(xs0, t)).r;

floatnoise1 = texture3D(Noise, vec3(xs1, t)).r;

floatnoise2 = texture3D(Noise, vec3(xs2, t)).r;

// The position of the flash

floatmid0 = height * (noise0 * 2. - 1.) * (1. - xs0 * xs0);

floatmid1 = height * (noise1 * 2. - 1.) * (1. - xs1 * xs1);

floatmid2 = height * (noise2 * 2. - 1.) * (1. - xs2 * xs2);

// Distance to flash

floatdist0 = abs(texCoord.y - mid0);

floatdist1 = abs(texCoord.y - mid1);

floatdist2 = abs(texCoord.y - mid2);

// Glow according to distance to flash

floatglow = 1.0 - pow(0.25 * (dist0 + 2. * dist1 + dist2), glowFallOff);

// Add some ambient glow to get some power in the air feeling

floatambGlow = ambientGlow * (1. - texCoord.x * texCoord.x)

* (1.0 - abs(ambientGlowHeightScale * texCoord.y));

//vec4 result = ambGlow * color;

//vec4 result = glowStrength * glow * glow * color;

vec4 result = (glowStrength * glow * glow + ambGlow) * color;

gl_FragColor = result;

}

转自:http://blog.csdn.net/panda1234lee/article/details/52198637

相关文章

网友评论

    本文标题:Shader特效——实现“闪电”(转)

    本文链接:https://www.haomeiwen.com/subject/jxahuxtx.html