funny-animated-gif-from-pro.../shaders/fragment.glsl

68 lines
2.2 KiB
Text
Raw Normal View History

/*
Copyright (C) 2021 hiimgoodpack
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2021-02-01 23:07:15 +02:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2021-02-01 23:07:15 +02:00
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2021-01-29 04:49:11 +02:00
#version 330 core
2021-02-01 23:07:15 +02:00
in vec2 vFragCoord;
2021-02-04 21:16:27 +02:00
in vec2 vTexCoord;
2021-02-01 23:07:15 +02:00
uniform sampler2D aTexture;
2021-02-04 21:16:27 +02:00
uniform float aHorizontalOffset;
// Set to 1 or -1 to flip Y
uniform float aTexCoordYScale;
// Adds the Z value to a vec2, calculating it assuming
// it's a sphere with a radius of 2
vec3 getZInSphere(vec2 v) {
const float radius = 2;
return vec3(v, sqrt(radius-dot(v,v)));
}
2021-01-29 04:49:11 +02:00
void main() {
2021-02-01 23:07:15 +02:00
float distanceFromCenterSquared = dot(vFragCoord, vFragCoord);
// alpha = 0 if fragment is outside circle
// alpha = 1 if fragment is inside circle
float alpha = distanceFromCenterSquared > 1 ? 0 : 1;
2021-02-04 21:16:27 +02:00
vec3 normal;
{
// Form a triangle to approximate the normal or something like that
const float offset = 0.001;
vec3 p0 = getZInSphere(vFragCoord + vec2(0, offset));
vec3 p1 = getZInSphere(vFragCoord + vec2(offset, -offset));
vec3 p2 = getZInSphere(vFragCoord + vec2(-offset, -offset));
// Now calculate the normal of the triangle
vec3 a = p0 - p1;
vec3 b = p2 - p1;
normal = normalize(cross(a, b));
}
const float pi = radians(180);
// Based off of https://stackoverflow.com/questions/41957890/how-to-properly-map-a-2d-image-texture-to-an-icosphere
vec2 texCoord = vec2(
atan(normal.x, normal.z) * (1.0f/pi),
asin(-normal.y) * (2.0f/pi) * aTexCoordYScale
) + vec2(0.5f+aHorizontalOffset, 0.5f);
2021-02-04 21:16:27 +02:00
// Some equation which seems to work good enough
float specular = (dot(normal, normalize(vec3(-1, 2, 0.5f))) - 0.25f) * 1.3f + 0.1f;
specular = specular*specular * sign(specular);
2021-02-04 21:16:27 +02:00
gl_FragColor = vec4(texture(aTexture, texCoord).rgb + (vec3(1,1,1) * specular), alpha);
2021-01-29 04:49:11 +02:00
}