Skip to content

Instantly share code, notes, and snippets.

@ippan
Last active October 5, 2017 13:00
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 ippan/fce936e0dd9b2e9bce0ebc7ace191a71 to your computer and use it in GitHub Desktop.
Save ippan/fce936e0dd9b2e9bce0ebc7ace191a71 to your computer and use it in GitHub Desktop.
a simple earth shader in Godot
// author: PanPan
// random3 and simplex noise is come form
// https://thebookofshaders.com/edit.php#11/iching-03.frag
shader_type spatial;
vec3 random3(vec3 c)
{
float j = 4096.0 * sin(dot(c,vec3(17.0, 59.4, 15.0)));
vec3 r;
r.z = fract(512.0 * j);
j *= 0.125;
r.x = fract(512.0 * j);
j *= 0.125;
r.y = fract(512.0 * j);
return r - 0.5;
}
float simplex_noise(vec3 p)
{
vec3 s = floor(p + dot(p, vec3(0.3333333)));
vec3 x = p - s + dot(s, vec3(0.1666667));
vec3 e = step(vec3(0.0), x - x.yzx);
vec3 i1 = e * (1.0 - e.zxy);
vec3 i2 = 1.0 - e.zxy * (1.0 - e);
vec3 x1 = x - i1 + 0.1666667;
vec3 x2 = x - i2 + 2.0 * 0.1666667;
vec3 x3 = x - 1.0 + 3.0 * 0.1666667;
vec4 w, d;
w.x = dot(x, x);
w.y = dot(x1, x1);
w.z = dot(x2, x2);
w.w = dot(x3, x3);
w = max(0.6 - w, vec4(0.0));
d.x = dot(random3(s), x);
d.y = dot(random3(s + i1), x1);
d.z = dot(random3(s + i2), x2);
d.w = dot(random3(s + 1.0), x3);
w *= w;
w *= w;
d *= w;
return dot(d, vec4(52.0));
}
float height_map(in vec3 position)
{
return simplex_noise(position)
+ simplex_noise(2.0 * position) / 2.0
+ simplex_noise(4.0 * position) / 4.0
+ simplex_noise(8.0 * position) / 8.0
+ simplex_noise(16.0 * position) / 16.0
+ simplex_noise(32.0 * position) / 32.0;
}
void vertex()
{
// store model normal(not transformed) to color
COLOR = vec4(0.5 * (NORMAL + 1.0), 1.0);
}
vec3 sea(in float height)
{
float base_sea = clamp(-height, 0.0, 1.0);
base_sea = 1.0 * sign(base_sea) - base_sea;
float out_sea = 0.8 * pow(base_sea, 6.0);
return vec3(0.0, 0.0, clamp(out_sea, 0.0, 1.0));
}
vec3 land(in float height)
{
float base_land = clamp(height, 0.0, 1.0);
base_land += sign(base_land) * 0.1;
float out_land = 1.0 * pow(base_land, 3.0);
return vec3(0.0, out_land, 0.0);
}
vec3 cloud(in vec3 position, in float time)
{
float height = clamp(height_map(position * 3.0 + time / 20.0), 0.0, 1.0) * 0.5;
return vec3(height, height, height);
}
void fragment()
{
// use model normal to generate height map
float height = height_map(COLOR.xyz);
vec3 color = land(height);
color += sea(height);
color += cloud(COLOR.xyz, TIME);
ALBEDO = color;
}
@Osimedias
Copy link

wath godot version you use?

@ippan
Copy link
Author

ippan commented May 18, 2017

a custom build version [3.0 (98a329)]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment