I was searching for a fire shader, …Ended up with these HD shaders but with 1FPS , so…
Take care!
http://http.developer.nvidia.com/GPUGems/gpugems_ch38.html
…
Main program to test shaders. (some includes touches)
-- Use this function to perform your initial setup
displayMode(FULLSCREEN)
function setup()
parameter.watch("1/DeltaTime")
m = mesh()
m.texture = image(1,1) --allTextures[Texture]
m.shader = shader("Documents:FireG")
-- m.shader = shader(getshader())
rIdx = m:addRect(0, 0, 0, 0)
m:setRect(rIdx, WIDTH/2, HEIGHT/2, WIDTH, HEIGHT)
m.shader.iGlobalTime = ElapsedTime
m.shader.iMouse = vec4(0,0,0,0)
m.shader.iResolution = vec3(WIDTH,HEIGHT,0)
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
-- This sets the line thickness
strokeWidth(5)
-- Here we set up the rect texture and size
--m.texture = allTextures[Texture]
--local cw,ch = spriteSize(allTextures[Texture])
--m:setRect(rIdx, WIDTH/2, HEIGHT/2, cw, ch)
-- Configure out custom uniforms for the ripple shader
m.shader.iGlobalTime = ElapsedTime
m.shader.iMouse = vec4(CurrentTouch.x, CurrentTouch.y,0,0)
-- Draw the mesh
m:draw()
end
function getshader()
return [[
uniform mat4 modelViewProjection;
//This is the current mesh vertex position, color and tex coord
// Set automatically
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;
//This is an output variable that will be passed to the fragment shader
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
void main()
{
//Pass the mesh color to the fragment shader
vColor = color;
vTexCoord = texCoord;
//Multiply the vertex position by our combined transform
gl_Position = modelViewProjection * position;
}
]],
[[
uniform lowp vec3 iResolution;
uniform lowp float iGlobalTime;
uniform lowp vec4 iMouse;
PASTE SHADER CODE HERE
]]
end
Now the shaders, first realistic fire shader, the slowest:
//
// A basic fragment shader
//
//Default precision qualifier
uniform highp vec3 iResolution;
uniform highp float iGlobalTime;
uniform highp vec4 iMouse;
precision highp float;
//This represents the current texture on the mesh
mat3 rotm;
mat3 rot(vec3 v, float angle)
{
float c = cos(angle);
float s = sin(angle);
return mat3(c + (1.0 - c) * v.x * v.x, (1.0 - c) * v.x * v.y - s * v.z, (1.0 - c) * v.x * v.z + s * v.y,
(1.0 - c) * v.x * v.y + s * v.z, c + (1.0 - c) * v.y * v.y, (1.0 - c) * v.y * v.z - s * v.x,
(1.0 - c) * v.x * v.z - s * v.y, (1.0 - c) * v.y * v.z + s * v.x, c + (1.0 - c) * v.z * v.z
);
}
vec2 dragonkifs(vec3 p) {
float otrap=1000.;
float l=0.;
for (int i=0; i<20; i++) {
p.y*=-sign(p.x);
p.x=abs(p.x);
p=p*rotm*1.18-vec3(1.2);
l=length(p);
if (l>100.) otrap=0.;
otrap=min(otrap,l);
}
return vec2(l*pow(1.18,float(-20))*.15,clamp(otrap*.3,0.,1.));
}
void main(void)
{
float time = iGlobalTime*.5;
vec2 coord = gl_FragCoord.xy / iResolution.xy *2. - vec2(1.);
coord.y *= iResolution.y / iResolution.x;
vec3 from = 16.*normalize(vec3(sin(time),cos(time),.5+sin(time)));
vec3 up=vec3(1,sin(time*2.)*.5,1);
vec3 edir = normalize(-from);
vec3 udir = normalize(up-dot(edir,up)*edir);
vec3 rdir = normalize(cross(edir,udir));
vec3 dir=normalize((coord.x*rdir+coord.y*udir)*.9+edir);
vec3 col=vec3(0.);
float dist=0.;
rotm=rot(normalize(vec3(1.)),time*3.);
for (int r=0; r<120; r++) {
vec3 p=from+dist*dir;
vec2 d=dragonkifs(p);
dist+=max(0.02,abs(d.x));
if (dist>20.) break;
col+=vec3(1.+d.y*1.8,.5+d.y,d.y*.5)*.125*sign(d.y);
}
col=col*length(col)*0.001;
gl_FragColor = vec4(col,1.0);
}
Another fire shader, faster but not enought, it also can be used to create an ice fx changing the iResolution field in “y”
uniform highp vec3 iResolution;
uniform highp float iGlobalTime;
uniform highp vec4 iMouse;
precision highp float;
float rand(vec2 n) {
return fract(sin(dot(n, vec2(12.9898, 4.1414))) * 43758.5453);
}
float noise(vec2 n) {
const vec2 d = vec2(0.0, 1.0);
vec2 b = floor(n), f = smoothstep(vec2(0.0), vec2(1.0), fract(n));
return mix(mix(rand(b), rand(b + d.yx), f.x), mix(rand(b + d.xy), rand(b + d.yy), f.x), f.y);
}
float fbm(vec2 n) {
float total = 0.0, amplitude = 1.0;
for (int i = 0; i < 7; i++) {
total += noise(n) * amplitude;
n += n;
amplitude *= 0.5;
}
return total;
}
void main() {
const vec3 c1 = vec3(0.1, 0.0, 0.0);
const vec3 c2 = vec3(0.7, 0.0, 0.0);
const vec3 c3 = vec3(0.2, 0.0, 0.0);
const vec3 c4 = vec3(1.0, 0.9, 0.0);
const vec3 c5 = vec3(0.1);
const vec3 c6 = vec3(0.9);
vec2 p = gl_FragCoord.xy * 8.0 / iResolution.xx;
float q = fbm(p - iGlobalTime * 0.1);
vec2 r = vec2(fbm(p + q + iGlobalTime * 0.7 - p.x - p.y), fbm(p + q - iGlobalTime * 0.4));
vec3 c = mix(c1, c2, fbm(p + r)) + mix(c3, c4, r.x) - mix(c5, c6, r.y);
gl_FragColor = vec4(c * cos(1.57 * gl_FragCoord.y / iResolution.y), 1.0);
}
Screenshots and videos
http://www.dropbox.com/sh/j9nlh9zschj2ku6/-h-WPRpkCF