Skip to content

Instantly share code, notes, and snippets.

@neutrino84
Last active June 17, 2022 20:45
Show Gist options
  • Save neutrino84/8a6da26ffb91417c150c to your computer and use it in GitHub Desktop.
Save neutrino84/8a6da26ffb91417c150c to your computer and use it in GitHub Desktop.
GLSL Planet Shader
The MIT License (MIT)
Copyright (c) 2015 Solar Crusaders
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
precision lowp float;
uniform sampler2D channel0;
uniform float time;
varying vec2 vTextureCoord;
// rendering params
const float sphsize = 0.8; // planet size
const float dist = 0.08; // distance for glow and distortion
const float perturb = 0.3;// distortion amount of the flow around the planet
const float windspeed = 0.25; // speed of wind flow
const float glow = 3.2; // glow amount, mainly on hit side
const float start = 118.0;
const float steps = 194.0; // number of steps for the volumetric rendering
const float stepsize = 0.01;
const float brightness = 1.0;
float wind(vec3 p) {
float d = max(0.0, dist - max(0.0, length(p) - sphsize) / sphsize) / dist; // for distortion and glow area
float x = max(0.5, p.x * -2.); // to increase glow on left side
return length(p) * (d * glow * x)+ d * glow * x; // return the result with glow applied
}
void main() {
// get ray dir
vec2 uv = vTextureCoord - vec2(0.5, 0.5);
vec3 dir = vec3(uv, 1.0);
vec3 from = vec3(0., 0., -2.);
vec3 tex = vec3(0.);
float ttime = 0.4 * sin(time * 2.) + 0.6;
// volumetric rendering
float alpha = 0.0;
float v = 0.0;
float l = -0.0001;
float t = time * windspeed * 0.25;
for(float r=start; r<steps; r++) {
vec3 p = from + r * dir * stepsize;
if(length(p)-sphsize > 0.0) {
v += min(50., wind(p));
} else if(l < 0.0) {
alpha += 1.0;
v *= 2.;
l = 0.;
tex += pow(max(.2, dot(normalize(p), normalize(vec3(-0.75, 0.0, -1.75)))), 3.4) *
(0.0 + texture2D(channel0, uv * vec2(2., 2.) * (1.0 + p.z * 0.5) + vec2(t * 0.25, 0.5)).rgb * 1.5);
} else {
v += min(0.6, wind(p));
}
}
// average values and
// apply bright factor
v /= steps;
v *= brightness;
v *= pow(v, 1.4);
vec3 col = vec3(v/4., v/3., v/0.8);
col += tex * 1.5;
gl_FragColor = vec4(col, alpha);
}
precision lowp float;
attribute vec2 aVertexPosition;
attribute vec2 aTextureCoord;
attribute vec4 aColor;
uniform mat3 projectionMatrix;
varying vec2 vTextureCoord;
varying vec4 vColor;
void main(void) {
gl_Position = vec4((projectionMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0);
vTextureCoord = aTextureCoord;
vColor = vec4(aColor.rgb * aColor.a, aColor.a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment