Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save companje/1bbbe8278f65aa192117240a9b513731 to your computer and use it in GitHub Desktop.
Save companje/1bbbe8278f65aa192117240a9b513731 to your computer and use it in GitHub Desktop.
WaterMars with Shader in Processing / Water Mars
PShader shader;
PImage colormap, heightmap, palette;
PShape shape;
void setup() {
size(1169, 1169, P3D);
shader = loadShader("watermars.glsl");
colormap = loadImage("mars-2k-noaa.jpg");
heightmap = loadImage("mars-4k-bump.png");
palette = loadImage("water.png");
sphereDetail(100);
fill(255);
shape = createShape(SPHERE, 500);
shape.setStroke(false);
shader.set("colormap",colormap);
shader.set("heightmap",heightmap);
shader.set("palette",palette);
}
void draw() {
ortho();
camera();
translate(width/2,height/2);
shader.set("progress",map(mouseX,0,width,0,1));
shader(shader);
shape(shape);
}
uniform sampler2D colormap;
uniform sampler2D heightmap;
uniform sampler2D palette;
uniform sampler2D normalmap;
uniform sampler2D borders;
// uniform sampler2D overlay;
uniform float progress;
varying vec4 vertTexCoord;
const float fade = 0.001; //25;
float map(float value, float min1, float max1, float min2, float max2) {
return min2 + (value - min1) * (max2 - min2) / (max1 - min1);
}
void main() {
vec4 terrainColor = texture2D(colormap, vertTexCoord.st);
vec4 heightColor = texture2D(heightmap, vertTexCoord.st);
// vec4 overlayColor = texture2D(overlay, vertTexCoord.st);
float elevation = (heightColor.r*256 + heightColor.g) / 256; //0..1
vec4 waterColor = texture2D(palette, vec2(map(elevation,0,progress,0,.85),0));
vec4 landColor = terrainColor;
float d = smoothstep(progress-fade, progress+fade, elevation);
vec4 color = mix(waterColor, landColor, d);
// grid
float gridResolution = .083333;
float gridLineWidth = 1;
vec2 distanceToLine = mod(vertTexCoord.st,gridResolution);
vec2 dx = abs(dFdx(vertTexCoord.st));
vec2 dy = abs(dFdy(vertTexCoord.st));
vec2 dF = vec2(max(dx.s, dy.s), max(dx.t, dy.t)) * gridLineWidth;
if (distanceToLine.x < dF.x || distanceToLine.y < dF.y) {
float a = .1;
color += vec4(a,a,a,0); //grid
}
// color = mix(color,overlayColor,overlayColor.a); //overlay
gl_FragColor = color;
}
@companje
Copy link
Author

companje commented Sep 7, 2023

water

@companje
Copy link
Author

companje commented Sep 7, 2023

mars-2k-noaa

@companje
Copy link
Author

companje commented Sep 7, 2023

@companje
Copy link
Author

companje commented Sep 7, 2023

Screenshot 2023-09-07 at 19 11 20

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