Skip to content

Instantly share code, notes, and snippets.

@murphy-slaw
Created August 14, 2018 02:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save murphy-slaw/a8d1344b7fdb0de2fd5adab97c376428 to your computer and use it in GitHub Desktop.
Save murphy-slaw/a8d1344b7fdb0de2fd5adab97c376428 to your computer and use it in GitHub Desktop.
shader_type canvas_item;
/**
Maps object texture onto a sphere and applies a spherical normal
based on a simulated light.
Adapted from
https://www.raywenderlich.com/2323-opengl-es-pixel-shaders-tutorial
and
https://gamedev.stackexchange.com/a/9385
**/
void fragment() {
float pi = 3.14159;
float x = (UV.x - 0.5) * 2.0;
float y = (UV.y - 0.5) * 2.0;
float r = sqrt(x * x + y * y);
float d = bool(r) ? asin(r)/r : 0.0;
float x2 = d * x;
float y2 = d * y;
float rot_speed = 16.0; // smaller values rotate faster
float x3 = mod(x2 / (4.0 * pi) + 0.5 + TIME / rot_speed, 1.0);
float y3 = y2 / (2.0 * pi) + 0.5;
//simulated light vector
vec3 cLight = normalize(vec3(-0.5, -0.5, 1.0));
vec2 center = vec2(0.5, 0.5);
float radius = 0.5;
vec2 position = UV - center;
float z = sqrt(radius * radius - position.x * position.x - position.y * position.y);
vec3 normal = normalize(vec3(position.x, position.y, z));
if (length(position) > radius) { //discard pixels outside the sphere
discard;
} else {
float diffuse = max(0.0, dot(normal, cLight));
vec4 sample = texture(TEXTURE,vec2(x3,y3)); //grab sphere-mapped pixel from texture
COLOR = vec4(vec3(diffuse), 1.0) * sample;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment