Skip to content

Instantly share code, notes, and snippets.

@miketamis
Created March 23, 2023 21:53
Show Gist options
  • Save miketamis/da73117ba8022793662d3c7a50a40da0 to your computer and use it in GitHub Desktop.
Save miketamis/da73117ba8022793662d3c7a50a40da0 to your computer and use it in GitHub Desktop.
/*{
"CATEGORIES": [
"XXX"
],
"CREDIT": "",
"INPUTS": [
{
"DEFAULT": 0,
"NAME": "width",
"TYPE": "float"
},
{
"DEFAULT": [
0,
0
],
"MAX": [
1,
1
],
"MIN": [
0,
0
],
"NAME": "offset",
"TYPE": "point2D"
},
{
"DEFAULT": [
1,
1,
1,
1
],
"NAME": "color1",
"TYPE": "color"
},
{
"DEFAULT": [
0,
0,
0,
1
],
"NAME": "color2",
"TYPE": "color"
},
{
"DEFAULT": [
0.5,
0.5
],
"MAX": [
1,
1
],
"MIN": [
0,
0
],
"NAME": "splitPos",
"TYPE": "point2D"
},
{
"MAX": 1,
"NAME": "lol",
"TYPE": "float"
},
{
"NAME": "color3",
"TYPE": "color"
}
],
"ISFVSN": "2"
}
*/
struct Camera {
vec3 Obs;
vec3 View;
vec3 Up;
vec3 Horiz;
float H;
float W;
float z;
};
struct Ray {
vec3 Origin;
vec3 Dir;
};
Camera camera(in vec3 Obs, in vec3 LookAt, in float aperture) {
Camera C;
C.Obs = Obs;
C.View = normalize(LookAt - Obs);
C.Horiz = normalize(cross(vec3(0.0, 0.0, 1.0), C.View));
C.Up = cross(C.View, C.Horiz);
C.W = float(RENDERSIZE.x);
C.H = float(RENDERSIZE.y);
C.z = (C.H/2.0) / tan((aperture * 3.1415 / 180.0) / 2.0);
return C;
}
struct Sphere {
vec3 Center;
float R;
};
bool intersect_sphere(in Ray R, in Sphere S, out float t) {
vec3 CO = R.Origin - S.Center;
float a = dot(R.Dir, R.Dir);
float b = 2.0*dot(R.Dir, CO);
float c = dot(CO, CO) - S.R*S.R;
float delta = b*b - 4.0*a*c;
if(delta < 0.0) {
return false;
}
t = (-b-sqrt(delta)) / (2.0*a);
return true;
}
Ray launch(in Camera C, in vec2 XY) {
return Ray(
C.Obs,
C.z*C.View+(XY.x-C.W/2.0)*C.Horiz+(XY.y-C.H/2.0)*C.Up
);
}
void main() {
// determine if we are on an even or odd line
// math goes like..
// mod(((coord+offset) / width),2)
vec4 out_color = color2;
float size = width * RENDERSIZE.x;
Camera C = camera(
vec3(2.0, 2.0, 1.5),
vec3(0.5, 0.5, 0.5),
50.0
);
float pos = lol * 10.0 - 8.0;
float outw = 0.5 + ((sin(lol*10.0) ));
Ray R = launch(C, vec2(gl_FragCoord.x, gl_FragCoord.y));
Sphere S = Sphere(vec3(0.0, pos, 0.0), 0.5);
Sphere S2 = Sphere(vec3(outw, pos, 0.0), 0.25);
Sphere S3 = Sphere(vec3(-outw, pos, 0), 0.25);
Sphere S4 = Sphere(vec3(0, pos, outw), 0.25);
Sphere S5 = Sphere(vec3(0, pos, -outw), 0.25);
float t;
if( intersect_sphere(R,S,t) ) {
out_color = color1;
}
if(intersect_sphere(R,S2,t) || intersect_sphere(R,S3,t) || intersect_sphere(R,S4,t) || intersect_sphere(R,S5,t) ) {
out_color *= color3;
}
gl_FragColor = out_color;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment