Skip to content

Instantly share code, notes, and snippets.

@companje
Last active September 7, 2023 17:19
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/7071ab45091926f941ddf809f9e98636 to your computer and use it in GitHub Desktop.
Save companje/7071ab45091926f941ddf809f9e98636 to your computer and use it in GitHub Desktop.
NormalMap Shader on Textured Sphere in Processing / Processing.org
PShader shader;
PImage colormap, normalmap;
PShape shape;
void setup() {
size(1169, 1169, P3D);
shader = loadShader("frag-shader.glsl", "vert-shader.glsl");
colormap = loadImage("colormaps/mercury.jpg");
normalmap = loadImage("normalmaps/mercury.jpg");
sphereDetail(100);
fill(255);
shape = createShape(SPHERE, 500);
shape.setStroke(false);
shader.set("colormap", colormap);
shader.set("normalmap", normalmap);
}
void draw() {
float bumpDetail = map(mouseX,0,width,0,8);
background(0);
ortho();
translate(width/2,height/2);
shader.set("bumpDetail",bumpDetail);
shader(shader);
rotateY(frameCount/100.);
shape(shape);
text("bumpDetail:" + bumpDetail,0,20);
}
varying vec4 vertColor;
varying vec4 vertTexCoord;
varying vec3 lightDir;
uniform sampler2D colormap;
uniform sampler2D normalmap;
uniform float bumpDetail;
void main() {
vec4 color = texture2D(colormap, vertTexCoord.st);
//normalmap
vec4 normalColor = texture2D(normalmap, vertTexCoord.st);
vec3 normal = normalize(normalColor.rgb * 2. - 1.);
float normalDotLight = max(dot(normal, normalize(lightDir)), 0.);
float intensity = pow(normalDotLight,bumpDetail);
if (intensity!=0.) color.rgb *= intensity;
else color = vec4(1,0,0,1);
gl_FragColor = color;
}
uniform mat4 modelview;
uniform mat4 transform;
uniform mat3 normalMatrix;
uniform mat4 texMatrix;
uniform vec4 lightPosition;
uniform vec3 lightNormal;
attribute vec4 position;
attribute vec4 color;
attribute vec3 normal;
attribute vec2 texCoord;
varying vec4 vertColor;
varying vec3 ecNormal;
varying vec3 lightDir;
varying vec4 vertTexCoord;
void main() {
gl_Position = transform * position;
vec3 ecPosition = vec3(modelview * position);
ecNormal = normalize(normalMatrix * normal);
lightDir = normalize(lightPosition.xyz - ecPosition);
vertColor = color;
vertTexCoord = texMatrix * vec4(texCoord, 1.0, 1.0);
}
@companje
Copy link
Author

companje commented Sep 7, 2023

earth:
earth

mercury:
mercury

mars:
mars

moon:
moon

@companje
Copy link
Author

companje commented Sep 7, 2023

earth:
earth

mercury:
mercury

mars:
mars

moon:
moon

@companje
Copy link
Author

companje commented Sep 7, 2023

mars

moon

earth

mercury

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