Skip to content

Instantly share code, notes, and snippets.

@xeolabs
Created February 26, 2021 09:44
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 xeolabs/9d3c99a22014a50665ff0d4afa73b366 to your computer and use it in GitHub Desktop.
Save xeolabs/9d3c99a22014a50665ff0d4afa73b366 to your computer and use it in GitHub Desktop.
// Batched geometry drawing vertex shader
precision mediump float;
precision mediump int;
uniform int renderPass;
attribute vec3 position;
attribute vec3 normal;
attribute vec4 color;
attribute vec4 flags;
attribute vec4 flags2;
uniform mat4 worldMatrix;
uniform mat4 worldNormalMatrix;
uniform mat4 viewMatrix;
uniform mat4 projMatrix;
uniform mat4 viewNormalMatrix;
uniform mat4 positionsDecodeMatrix;
uniform vec4 lightAmbient;
uniform vec4 lightColor1;
uniform vec3 lightDir1;
uniform vec4 lightColor2;
uniform vec3 lightDir2;
vec3 octDecode(vec2 oct) {
vec3 v = vec3(oct.xy, 1.0 - abs(oct.x) - abs(oct.y));
if (v.z < 0.0) {
v.xy = (1.0 - abs(v.yx)) * vec2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0);
}
return normalize(v);
}
varying vec4 vColor;
void main(void) {
if (int(flags.x) ! = renderPass) {
gl_Position = vec4(0.0, 0.0, 0.0, 0.0);
}
else {
vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0));
vec4 viewPosition = viewMatrix * worldPosition;
vec4 worldNormal = worldNormalMatrix * vec4(octDecode(normal.xy), 0.0);
vec3 viewNormal = normalize((viewNormalMatrix * worldNormal).xyz);
vec3 reflectedColor = vec3(0.0, 0.0, 0.0);
vec3 viewLightDir = vec3(0.0, 0.0, -1.0);
float lambertian = 1.0;
viewLightDir = normalize((viewMatrix * vec4(lightDir1, 0.0)).xyz);
lambertian = max(dot(-viewNormal, viewLightDir), 0.0);
reflectedColor += lambertian * (lightColor1.rgb * lightColor1.a);
viewLightDir = normalize((viewMatrix * vec4(lightDir2, 0.0)).xyz);
lambertian = max(dot(-viewNormal, viewLightDir), 0.0);
reflectedColor += lambertian * (lightColor2.rgb * lightColor2.a);
vec3 rgb = (vec3(float(color.r) / 255.0, float(color.g) / 255.0, float(color.b) / 255.0));
vColor = vec4((lightAmbient.rgb * lightAmbient.a * rgb) + (reflectedColor * rgb), float(color.a) / 255.0);
vec4 clipPos = projMatrix * viewPosition;
gl_Position = clipPos;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment