diff options
author | nathan <nathansmith@disroot.org> | 2025-08-08 18:48:10 +0000 |
---|---|---|
committer | nathan <nathansmith@disroot.org> | 2025-08-08 18:48:10 +0000 |
commit | e5acfb329827802fe2259f91701dc8762c2573b6 (patch) | |
tree | 8d461b2d0e1864633341d066f783172790c62dc9 /assets/shaders/glsl100 | |
parent | 94ca30b13bffe1f02313b7fd32b2320e5c490fa5 (diff) | |
download | FindThings-e5acfb329827802fe2259f91701dc8762c2573b6.tar.gz FindThings-e5acfb329827802fe2259f91701dc8762c2573b6.tar.bz2 FindThings-e5acfb329827802fe2259f91701dc8762c2573b6.zip |
Mesh instancing test working
Diffstat (limited to 'assets/shaders/glsl100')
-rw-r--r-- | assets/shaders/glsl100/instancing.fs | 77 | ||||
-rw-r--r-- | assets/shaders/glsl100/instancing.vs | 36 |
2 files changed, 113 insertions, 0 deletions
diff --git a/assets/shaders/glsl100/instancing.fs b/assets/shaders/glsl100/instancing.fs new file mode 100644 index 0000000..596da0f --- /dev/null +++ b/assets/shaders/glsl100/instancing.fs @@ -0,0 +1,77 @@ +#version 100 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec3 fragPosition; +varying vec2 fragTexCoord; +varying vec4 fragColor; +varying vec3 fragNormal; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 colDiffuse; + +// NOTE: Add your custom variables here + +#define MAX_LIGHTS 4 +#define LIGHT_DIRECTIONAL 0 +#define LIGHT_POINT 1 + +struct Light { + int enabled; + int type; + vec3 position; + vec3 target; + vec4 color; +}; + +// Input lighting values +uniform Light lights[MAX_LIGHTS]; +uniform vec4 ambient; +uniform vec3 viewPos; + +void main() +{ + // Texel color fetching from texture sampler + vec4 texelColor = texture2D(texture0, fragTexCoord); + vec3 lightDot = vec3(0.0); + vec3 normal = normalize(fragNormal); + vec3 viewD = normalize(viewPos - fragPosition); + vec3 specular = vec3(0.0); + + vec4 tint = colDiffuse * fragColor; + + // NOTE: Implement here your fragment shader code + + for (int i = 0; i < MAX_LIGHTS; i++) + { + if (lights[i].enabled == 1) + { + vec3 light = vec3(0.0); + + if (lights[i].type == LIGHT_DIRECTIONAL) + { + light = -normalize(lights[i].target - lights[i].position); + } + + if (lights[i].type == LIGHT_POINT) + { + light = normalize(lights[i].position - fragPosition); + } + + float NdotL = max(dot(normal, light), 0.0); + lightDot += lights[i].color.rgb*NdotL; + + float specCo = 0.0; + if (NdotL > 0.0) specCo = pow(max(0.0, dot(viewD, reflect(-(light), normal))), 16.0); // 16 refers to shine + specular += specCo; + } + } + + vec4 finalColor = (texelColor*((tint + vec4(specular, 1.0))*vec4(lightDot, 1.0))); + finalColor += texelColor*(ambient/10.0); + + // Gamma correction + gl_FragColor = pow(finalColor, vec4(1.0/2.2)); +} diff --git a/assets/shaders/glsl100/instancing.vs b/assets/shaders/glsl100/instancing.vs new file mode 100644 index 0000000..59d7304 --- /dev/null +++ b/assets/shaders/glsl100/instancing.vs @@ -0,0 +1,36 @@ +#version 100 + +// Input vertex attributes +attribute vec3 vertexPosition; +attribute vec2 vertexTexCoord; +attribute vec3 vertexNormal; +attribute vec4 vertexColor; + +attribute mat4 instanceTransform; + +// Input uniform values +uniform mat4 mvp; +uniform mat4 matNormal; + +// Output vertex attributes (to fragment shader) +varying vec3 fragPosition; +varying vec2 fragTexCoord; +varying vec4 fragColor; +varying vec3 fragNormal; + +// NOTE: Add your custom variables here + +void main() +{ + // Compute MVP for current instance + mat4 mvpi = mvp*instanceTransform; + + // Send vertex attributes to fragment shader + fragPosition = vec3(mvpi*vec4(vertexPosition, 1.0)); + fragTexCoord = vertexTexCoord; + fragColor = vertexColor; + fragNormal = normalize(vec3(matNormal*vec4(vertexNormal, 1.0))); + + // Calculate final vertex position + gl_Position = mvpi*vec4(vertexPosition, 1.0); +} |