Skip to content

Instantly share code, notes, and snippets.

@CaptainGPU
Created November 29, 2023 11:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CaptainGPU/d1ca6810e709dddd3aa445cfbf3478e7 to your computer and use it in GitHub Desktop.
Save CaptainGPU/d1ca6810e709dddd3aa445cfbf3478e7 to your computer and use it in GitHub Desktop.
IDI.glsl
#ifdef GL_ES
precision highp float;
#endif
uniform sampler2D u_texture_0;
uniform vec2 u_resolution;
uniform float u_time;
const float PI = 3.14159265358979323846264;
vec4 inTriangle(vec2 p0, vec2 p1, vec2 p2, vec2 p)
{
float a = 0.5*(-p1.y*p2.x + p0.y*(-p1.x + p2.x) + p0.x*(p1.y - p2.y) + p1.x*p2.y);
float s = 1.0/(2.0*a)*(p0.y*p2.x - p0.x*p2.y + (p2.y - p0.y)*p.x + (p0.x - p2.x)*p.y);
float t = 1.0/(2.0*a)*(p0.x*p1.y - p0.y*p1.x + (p0.y - p1.y)*p.x + (p1.x - p0.x)*p.y);
if (s > 0.0 && t > 0.0 && 1.0 - s - t > 0.0) {
return vec4(1.0,s,t,1.0-s-t);
} else {
return vec4(0.0,s,t,1.0-s-t);
}
}
void triangle(inout vec4 c, vec2 p, int shader, vec3 p0, vec3 p1, vec3 p2, vec2 t0, vec2 t1, vec2 t2)
{
float rx = u_time/3.;
float ry = u_time;
float rz = u_time;
float cx = cos(rx); float sx = sin(rx);
float cy = cos(ry); float sy = sin(ry);
float cz = cos(rz); float sz = sin(rz);
mat4 transform1 =
mat4(1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, -2.5, 1);
mat4 transform2 =
mat4(cz*cy, -sz*cy, sy, 0,
sz*cx + cz*sy*sx, cz*cx - sz*sy*sx, -cy*sx, 0,
sz*sx - cz*sy*cx,cz*sx + sz*sy*cx, cy*cx, 0,
0, 0, 0, 1);
float n = 1.0;
float f = 10.0;
float r = 1.0 * u_resolution.x / u_resolution.y;
float t = 1.0;
mat4 projection =
mat4(n/r, 0, 0, 0,
0, n/t, 0, 0,
0, 0, -(f+n)/(f-n), -1,
0, 0, -(2.0*f*n)/(f-n), 0);
vec4 pt0 = vec4(0,0,0,0);
vec4 pt1 = vec4(0,0,0,0);
vec4 pt2 = vec4(0,0,0,0);
pt0 = projection * transform1 * transform2 * vec4(p0,1);
pt1 = projection * transform1 * transform2 * vec4(p1,1);
pt2 = projection * transform1 * transform2 * vec4(p2,1);
vec4 test = inTriangle(pt0.xy / pt0.w, pt1.xy / pt1.w, pt2.xy / pt2.w, p);
if (test.x != 0.0) {
float z = ((pt1.z * test.y) / pt1.w +
(pt2.z * test.z) / pt2.w +
(pt0.z * test.w) / pt0.w) /
(test.y / pt1.w +
test.z / pt2.w +
test.w / pt0.w);
if (z < c.w) {
float tx = ((t1.x * test.y) / pt1.w +
(t2.x * test.z) / pt2.w +
(t0.x * test.w) / pt0.w) /
(test.y / pt1.w +
test.z / pt2.w +
test.w / pt0.w);
float ty = ((t1.y * test.y) / pt1.w +
(t2.y * test.z) / pt2.w +
(t0.y * test.w) / pt0.w) /
(test.y / pt1.w +
test.z / pt2.w +
test.w / pt0.w);
c.xyz = texture2D(u_texture_0, vec2(tx,ty)).xyz;
c.w=z;
}
}
}
vec4 pixel(vec2 p)
{
vec4 color = vec4(0,0,0,1000);
triangle(color,p,0,vec3(-1,-1,-1),vec3(1,-1,-1), vec3(-1,1,-1),vec2(0,0),vec2(1,0),vec2(0,1));
triangle(color,p,0,vec3(1,-1,-1),vec3(1,1,-1),vec3(-1,1,-1),vec2(1,0),vec2(1,1),vec2(0,1));
triangle(color,p,1,vec3(1,1,1),vec3(-1,1,1),vec3(1,-1,1),vec2(0,0),vec2(1,0),vec2(0,1));
triangle(color,p,1,vec3(-1,1,1),vec3(-1,-1,1),vec3(1,-1,1),vec2(1,0),vec2(1,1),vec2(0,1));
triangle(color,p,2,vec3(-1,1,-1),vec3(-1,1,1),vec3(-1,-1,-1),vec2(0,0),vec2(1,0),vec2(0,1));
triangle(color,p,2,vec3(-1,1,1),vec3(-1,-1,1),vec3(-1,-1,-1),vec2(1,0),vec2(1,1),vec2(0,1));
triangle(color,p,3,vec3(1,1,-1),vec3(1,1,1),vec3(1,-1,-1),vec2(0,0),vec2(1,0),vec2(0,1));
triangle(color,p,3,vec3(1,1,1),vec3(1,-1,1),vec3(1,-1,-1),vec2(1,0),vec2(1,1),vec2(0,1));
triangle(color,p,4,vec3(-1,1,-1),vec3(-1,1,1),vec3(1,1,-1),vec2(0,0),vec2(1,0),vec2(0,1));
triangle(color,p,4,vec3(-1,1,1),vec3(1,1,1),vec3(1,1,-1),vec2(1,0),vec2(1,1),vec2(0,1));
triangle(color,p,5,vec3(-1,-1,-1),vec3(-1,-1,1),vec3(1,-1,-1),vec2(0,0),vec2(1,0),vec2(0,1));
triangle(color,p,5,vec3(-1,-1,1),vec3(1,-1,1),vec3(1,-1,-1),vec2(1,0),vec2(1,1),vec2(0,1));
return color;
}
varying vec2 v_texcoord;
void main()
{
vec3 finalColor = vec3(.0);
vec2 uv = (gl_FragCoord.xy - .5 * u_resolution.xy) / u_resolution.y;
// Для моделей
//uv = v_texcoord - .5;
float f = uv.y / uv.x;
f = -atan(uv.x, uv.y);
f = (f / PI);
float r = length(uv);
vec2 st;
st.x = f * 2.0;
st.y = .3 /r;
st += .3 * u_time;
finalColor = texture2D(u_texture_0, st).xyz;
finalColor *= 1.3 * r;
uv = gl_FragCoord.xy / u_resolution.xy;
uv = uv * 2.0 - vec2(1.0,1.0);
float s = (cos(u_time) + 1.0) * .5;
uv *= 0.8 + s * 9.0;
vec4 cube = pixel(uv);
cube.xyz *= 1.0 - s;
cube.w /= 1000.0;
cube.w = clamp(.0, 1.0, cube.w);
finalColor = mix(cube.xyz, finalColor, cube.w);
gl_FragColor = vec4(finalColor, 1.);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment