Basic flashlight

This commit is contained in:
2025-08-31 22:08:18 -05:00
parent a1fede92f3
commit d7363921ad
6 changed files with 113 additions and 39 deletions

73
shaders/fog.fs Normal file
View File

@@ -0,0 +1,73 @@
#version 330 core
in vec2 vTexCoord;
in vec3 vWorldPos;
uniform sampler2D texture0;
// --- Soft bubble (kept) ---
uniform vec3 u_playerPos;
uniform float u_inner;
uniform float u_outer;
uniform float u_minShadow;
// --- Flashlight cone + distance ---
uniform vec3 u_spotDir; // normalized camera forward
uniform float u_spotCos; // cos(half-angle)
uniform float u_spotSoft; // base penumbra width in cos-space
uniform float u_lightRange; // bright zone distance (>0)
uniform float u_lightFade; // fade width beyond range
// --- NEW: decouple brightness + widen with distance ---
uniform float u_intensity; // flashlight peak brightness (independent of range)
uniform float u_beamSpread; // additional penumbra growth per unit distance (0..~0.5)
// Alpha cut (trees, grass)
uniform float u_alphaCutoff;
out vec4 finalColor;
// Helpers
float remap01(float x, float a, float b) {
return clamp((x - a) / max(b - a, 1e-5), 0.0, 1.0);
}
float invsq(float d, float r) {
float s = d / max(r, 1e-4);
return 1.0 / (1.0 + s*s);
}
void main() {
vec4 texel = texture(texture0, vTexCoord);
if (texel.a < u_alphaCutoff) discard;
float dist = length(vWorldPos - u_playerPos);
// (A) Soft ambient bubble around the player (unchanged)
float bubble = 1.0 - remap01(dist, u_inner, u_outer); // 1 near, 0 far
float ambient = mix(1.0 - clamp(u_minShadow,0.0,1.0), 0.0, 1.0 - bubble);
// (B) Flashlight cone with penumbra + distance drop + widening
vec3 L = normalize(vWorldPos - u_playerPos);
float cosTheta = dot(normalize(u_spotDir), L);
// Widen penumbra with distance so long ranges don't look like a laser
float distNorm = dist / max(u_lightRange, 1e-4);
float softWide = clamp(u_spotSoft + u_beamSpread * distNorm, 0.0, 0.8);
float innerEdge = u_spotCos;
float outerEdge = clamp(u_spotCos - softWide, -1.0, 1.0);
float spotA = smoothstep(outerEdge, innerEdge, cosTheta);
spotA *= spotA; // mild hotspot
// Distance attenuation: inverse-square * soft fade band
float band = 1.0 - remap01(dist, u_lightRange, u_lightRange + u_lightFade);
float lightD = invsq(dist, u_lightRange) * band;
// Decoupled brightness
float flashlight = u_intensity * spotA * lightD;
// Compose
float light = clamp(ambient + flashlight, 0.0, 1.0);
finalColor = vec4(texel.rgb * light, texel.a);
}

16
shaders/fog.vs Normal file
View File

@@ -0,0 +1,16 @@
#version 330
in vec3 vertexPosition;
in vec2 vertexTexCoord;
uniform mat4 matModel, matView, matProjection;
out vec3 vWorldPos;
out vec2 vTexCoord;
void main()
{
vec4 world = matModel * vec4(vertexPosition, 1.0);
vWorldPos = world.xyz;
vTexCoord = vertexTexCoord;
gl_Position = matProjection * matView * world;
}

30
shaders/skybox.fs Normal file
View File

@@ -0,0 +1,30 @@
#version 330
// Input vertex attributes (from vertex shader)
in vec3 fragPosition;
// Input uniform values
uniform samplerCube environmentMap;
uniform bool vflipped;
uniform bool doGamma;
// Output fragment color
out vec4 finalColor;
void main()
{
// Fetch color from texture map
vec3 color = vec3(0.0);
if (vflipped) color = texture(environmentMap, vec3(fragPosition.x, -fragPosition.y, fragPosition.z)).rgb;
else color = texture(environmentMap, fragPosition).rgb;
if (doGamma)// Apply gamma correction
{
color = color/(color + vec3(1.0));
color = pow(color, vec3(1.0/2.2));
}
// Calculate final fragment color
finalColor = vec4(color, 1.0);
}

24
shaders/skybox.vs Normal file
View File

@@ -0,0 +1,24 @@
#version 330
// Input vertex attributes
in vec3 vertexPosition;
// Input uniform values
uniform mat4 matProjection;
uniform mat4 matView;
// Output vertex attributes (to fragment shader)
out vec3 fragPosition;
void main()
{
// Calculate fragment position based on model transformations
fragPosition = vertexPosition;
// Remove translation from the view matrix
mat4 rotView = mat4(mat3(matView));
vec4 clipPos = matProjection*rotView*vec4(vertexPosition, 1.0);
// Calculate final vertex position
gl_Position = clipPos;
}