realityplayground-of/zoneb/bin/data/shaders/particles/update.frag

242 lines
7 KiB
GLSL
Raw Normal View History

2020-10-20 12:02:10 +00:00
#extension GL_ARB_explicit_attrib_location : enable
// ping pong inputs
uniform sampler2DRect particles0;
uniform sampler2DRect particles1;
uniform sampler2DRect particles2;
2020-10-20 15:17:41 +00:00
uniform sampler2DRect particles3;
2020-10-31 18:47:59 +00:00
uniform sampler2DRect particles4;
2020-10-20 12:02:10 +00:00
uniform sampler2DRect u_depth;
uniform sampler2DRect u_world;
uniform sampler2DRect u_v4l2cam;
uniform ivec2 uFrameSize;
uniform ivec2 uDepthFrameSize;
uniform vec3 mouse;
uniform float radiusSquared;
uniform float elapsed;
2020-10-31 19:24:50 +00:00
uniform float u_time;
2020-10-20 12:02:10 +00:00
2020-10-20 15:17:41 +00:00
uniform vec3 uHottest0;
uniform vec3 uHottest1;
uniform vec3 uHottest3d0;
uniform vec3 uHottest3d1;
2020-10-20 15:17:41 +00:00
2020-10-20 12:02:10 +00:00
in vec2 texCoordVarying;
layout(location = 0) out vec4 posOut;
layout(location = 1) out vec4 velOut;
layout(location = 2) out vec4 misOut;
2020-10-20 15:17:41 +00:00
layout(location = 3) out vec4 tarOut;
2020-10-31 18:47:59 +00:00
layout(location = 4) out vec4 prevPosOut;
2020-10-20 12:02:10 +00:00
2020-10-20 12:50:26 +00:00
vec3 random3(vec3 c) {
float j = 4096.0*sin(dot(c,vec3(17.0, 59.4, 15.0)));
vec3 r;
r.z = fract(512.0*j);
j *= .125;
r.x = fract(512.0*j);
j *= .125;
r.y = fract(512.0*j);
return r-0.5;
}
/* skew constants for 3d simplex functions */
const float F3 = 0.3333333;
const float G3 = 0.1666667;
/* 3d simplex noise */
float simplex3d(vec3 p) {
/* 1. find current tetrahedron T and it's four vertices */
/* s, s+i1, s+i2, s+1.0 - absolute skewed (integer) coordinates of T vertices */
/* x, x1, x2, x3 - unskewed coordinates of p relative to each of T vertices*/
/* calculate s and x */
vec3 s = floor(p + dot(p, vec3(F3)));
vec3 x = p - s + dot(s, vec3(G3));
/* calculate i1 and i2 */
vec3 e = step(vec3(0.0), x - x.yzx);
vec3 i1 = e*(1.0 - e.zxy);
vec3 i2 = 1.0 - e.zxy*(1.0 - e);
/* x1, x2, x3 */
vec3 x1 = x - i1 + G3;
vec3 x2 = x - i2 + 2.0*G3;
vec3 x3 = x - 1.0 + 3.0*G3;
/* 2. find four surflets and store them in d */
vec4 w, d;
/* calculate surflet weights */
w.x = dot(x, x);
w.y = dot(x1, x1);
w.z = dot(x2, x2);
w.w = dot(x3, x3);
/* w fades from 0.6 at the center of the surflet to 0.0 at the margin */
w = max(0.6 - w, 0.0);
/* calculate surflet components */
d.x = dot(random3(s), x);
d.y = dot(random3(s + i1), x1);
d.z = dot(random3(s + i2), x2);
d.w = dot(random3(s + 1.0), x3);
/* multiply d by w^4 */
w *= w;
w *= w;
d *= w;
/* 3. return the sum of the four surflets */
return dot(d, vec4(52.0));
}
/* const matrices for 3d rotation */
const mat3 rot1 = mat3(-0.37, 0.36, 0.85,-0.14,-0.93, 0.34,0.92, 0.01,0.4);
const mat3 rot2 = mat3(-0.55,-0.39, 0.74, 0.33,-0.91,-0.24,0.77, 0.12,0.63);
const mat3 rot3 = mat3(-0.71, 0.52,-0.47,-0.08,-0.72,-0.68,-0.7,-0.45,0.56);
/* directional artifacts can be reduced by rotating each octave */
float simplex3d_fractal(vec3 m) {
return 0.5333333*simplex3d(m*rot1)
+0.2666667*simplex3d(2.0*m*rot2)
+0.1333333*simplex3d(4.0*m*rot3)
+0.0666667*simplex3d(8.0*m);
}
2020-10-20 12:02:10 +00:00
void main()
{
2020-10-20 15:17:41 +00:00
vec3 pos = texture(particles0, texCoordVarying.st).xyz;
vec3 vel = texture(particles1, texCoordVarying.st).xyz;
vec3 misc = texture(particles2, texCoordVarying.st).xyz;
vec4 target = texture(particles3, texCoordVarying.st).xyzw;
2020-10-31 18:47:59 +00:00
vec3 prevPos = pos;//texture(particles4, texCoordVarying.st).xyz;
2020-10-20 15:17:41 +00:00
float fraction = 8;
float thermo = texture2D(u_v4l2cam, texCoordVarying.st/fraction).r;
2020-10-20 12:02:10 +00:00
vec2 subPix = mod(texCoordVarying.st / fraction, 1.0);
2020-10-31 16:42:54 +00:00
vec2 basePix = texCoordVarying.st/fraction - subPix;
float depth00 = texture(u_depth, basePix + vec2(0, 0)).x;
float depth01 = texture(u_depth, basePix + vec2(0, 1)).x;
float depth10 = texture(u_depth, basePix + vec2(1, 0)).x;
float depth11 = texture(u_depth, basePix + vec2(1, 1)).x;
vec4 ray00 = texture(u_world, basePix + vec2(0, 0));
vec4 ray01 = texture(u_world, basePix + vec2(0, 1));
vec4 ray10 = texture(u_world, basePix + vec2(1, 0));
vec4 ray11 = texture(u_world, basePix + vec2(1, 1));
float depth0 = mix(depth00, depth10, subPix.x);
float depth1 = mix(depth01, depth11, subPix.x);
vec4 ray0 = mix(ray00, ray10, subPix.x);
vec4 ray1 = mix(ray01, ray11, subPix.x);
float depth = mix(depth0, depth1, subPix.y);
vec4 ray = mix(ray0, ray1, subPix.y);
float vValid = (depth00 != 0 && depth01 != 0 && depth10 != 0 && depth11 != 0 && ray.x != 0 && ray.y != 0) ? 1 : 0;
2020-10-20 12:02:10 +00:00
vec4 posWorld = vec4(1);
posWorld.z = -depth * 65535.0; // Remap to float range.
posWorld.x = ray.x * posWorld.z;
posWorld.y = ray.y * posWorld.z;
2020-10-31 16:54:52 +00:00
posWorld.xyz += random3(posWorld.xyz) * 0.5;
2020-10-20 12:02:10 +00:00
2020-10-20 12:55:48 +00:00
float age = misc.y;
2020-10-21 10:05:45 +00:00
if(age > 1) { // reached target
2020-10-20 15:17:41 +00:00
target.w = 0;
2020-10-21 10:05:45 +00:00
if(vValid == 1) { // rebirth if point is valid
2020-10-20 15:17:41 +00:00
age = 0;
}
}
2020-10-21 10:05:45 +00:00
if(vValid == 1 && target.w == 0) { // point valid && no target -> move to the point
2020-10-20 12:50:26 +00:00
pos = posWorld.xyz;
2020-10-31 18:47:59 +00:00
// prevPos = posWorld.xyz;// + vec3(1,0,0);//random3(posWorld.xyz) * 0.5;
2020-10-20 12:55:48 +00:00
age = 0;
}
2020-10-21 10:05:45 +00:00
else if (target.w > 0) { // targeted
2020-10-31 16:42:54 +00:00
age += 0.002;
2020-10-20 17:26:29 +00:00
thermo = misc.x;
}
2020-10-21 10:05:45 +00:00
else { // wandering
2020-10-31 19:24:50 +00:00
age += 01.02;
2020-10-20 12:55:48 +00:00
thermo = misc.x;
2020-10-20 12:50:26 +00:00
}
2020-10-20 12:02:10 +00:00
2020-10-31 16:42:54 +00:00
if(vValid == 1 && target.w > 0 && length(target.xy - pos.xy) < 100) { // arrived
age += 0.01;
2020-10-31 16:42:54 +00:00
}
2020-10-20 17:26:29 +00:00
2020-10-21 10:05:45 +00:00
// wandering
2020-10-20 12:50:26 +00:00
vec3 force = vec3(0,0,0);
2020-10-31 16:42:54 +00:00
if(vValid == 1 && target.w == 0) {
float th = 3.1415 * 4 * simplex3d_fractal(vec3(pos.xyz * 0.001));
float phi = 3.1415 * simplex3d_fractal(pos.xyz * 0.002);
force.x += cos(th) * cos(phi);
force.y += sin(th) * cos(phi);
force.z += sin(phi);
force *= 100;
}
else {
float th = 3.1415 * 4 * simplex3d_fractal(vec3(pos.xyz * 0.01));
float phi = 3.1415 * simplex3d_fractal(pos.xyz * 0.02);
force.x += cos(th) * cos(phi);
force.y += sin(th) * cos(phi);
force.z += sin(phi);
force *= 100;
}
2020-10-20 15:17:41 +00:00
2020-10-21 10:05:45 +00:00
if(age < 0.001) { // targetting
2020-10-20 15:17:41 +00:00
target.w = 0;
2020-10-31 19:24:50 +00:00
if(random3(pos + vec3(u_time, 0, 0)).x > 2*pow(length(texCoordVarying.st/fraction - uHottest0.st)/25,0.5)) {
2020-10-31 10:49:35 +00:00
vec2 h = uHottest1.st;
vec4 hray = texture(u_world, h);
float hdepth = -depth * 65535.0;
if(hdepth == 0) hdepth = -0.01 * 65535.0;
vec3 H = vec3(hray.xy * hdepth, hdepth);
2020-10-31 16:54:52 +00:00
target.xyz = H + random3(pos) * 3;
2020-10-20 15:17:41 +00:00
target.w = 1;
}
2020-10-31 19:24:50 +00:00
else if(random3(pos + vec3(u_time, 0, 0)).x > 2*pow(length(texCoordVarying.st/fraction - uHottest1.st)/25,0.5)) {
2020-10-31 10:49:35 +00:00
vec2 h = uHottest0.st;
vec4 hray = texture(u_world, h);
float hdepth = -depth * 65535.0;
if(hdepth == 0) hdepth = -0.01 * 65535.0;
vec3 H = vec3(hray.xy * hdepth, hdepth);
2020-10-31 16:54:52 +00:00
target.xyz = H + random3(pos) * 3;
2020-10-20 15:17:41 +00:00
target.w = 1;
}
}
2020-10-21 10:05:45 +00:00
if(target.w > 0) { // go to target
2020-10-20 17:26:29 +00:00
float th = atan(-pos.y + target.y, -pos.x + target.x);
float phi = atan(-pos.z + target.z, length(target.xy-pos.xy));
th += 3.1415 / 1 * simplex3d_fractal(vec3(pos.xyz * 0.01) + vec3(u_time, 0, 0));
2020-10-31 16:54:52 +00:00
phi += 3.1415 / 1 * simplex3d_fractal(pos.xyz * 0.01);
2020-10-20 17:26:29 +00:00
force.x = cos(th) * cos(phi);
force.y = sin(th) * cos(phi);
force.z = sin(phi);
2020-10-31 19:24:50 +00:00
force *= 100 + 200 * simplex3d_fractal(vec3(texCoordVarying, 0.0));
2020-10-20 15:17:41 +00:00
}
2020-10-20 12:02:10 +00:00
// damping
2020-10-20 12:50:26 +00:00
vel *= 0.95;
2020-10-20 12:02:10 +00:00
2020-10-20 15:17:41 +00:00
// accelerate
vel += elapsed * force;
2020-10-20 12:02:10 +00:00
// move
2020-10-31 18:47:59 +00:00
prevPos = pos;
2020-10-20 12:02:10 +00:00
pos += elapsed * vel;
posOut = vec4(pos, 1.0);
velOut = vec4(vel, 0.0);
2020-10-20 12:55:48 +00:00
misOut = vec4(thermo, age, 0, 0);
2020-10-20 15:17:41 +00:00
tarOut = vec4(target);
2020-10-31 18:47:59 +00:00
prevPosOut = vec4(prevPos, 1.0);
2020-10-20 12:02:10 +00:00
}