Shamelessly stolen light code

This commit is contained in:
crumblingstatue 2023-04-06 19:21:20 +02:00
parent d1df7cd472
commit 45af69faa0
2 changed files with 81 additions and 15 deletions

View file

@ -1,16 +1,44 @@
uniform sampler2D texture; uniform sampler2D texture;
uniform float ambient; uniform vec2 resolution;
uniform vec2 mouse;
uniform float time;
uniform bool has_texture; uniform bool has_texture;
void main() // The RGB values are the ambient light color
{ // and the alpha is the ambient intensity
// lookup the pixel in the texture uniform vec4 ambientData;
vec4 pixel = texture2D(texture, gl_TexCoord[0].xy); // The RGB values are the light color
// and the alpha is the light intensity
uniform vec4 lightData;
// Maximum radius of the light
uniform float lightSize;
// multiply it by the color void main() {
vec4 col = gl_Color; // Light's position
if (has_texture) { vec2 position = mouse/resolution.xx;
col = gl_Color * pixel;
// Makes the light change its size slightly to make a fire effect
float maxDistance = lightSize + 0.015*sin(time);
// Gets the distance from the light's position and the fragment coord
float distance = distance(gl_FragCoord.xy/resolution.xx, position);
// Calculates the amount of light for the fragment
float value = 1.0 - smoothstep(-0.2, maxDistance, distance);
// Gets the original color from the texture
vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);
if (!has_texture) {
pixel = gl_Color;
} }
gl_FragColor = vec4(col.x * ambient, col.y * ambient, col.z * ambient, col.w);
// Applies the ambient light to the original pixel color
vec3 ambient = pixel.rgb * ambientData.rgb * ambientData.a;
// Calculates the light color for the pixel
vec3 light = lightData.rgb * lightData.a * clamp(value, 0.0, 1.0);
// Applies the light to the pixel
vec3 intensity = ambient + light;
vec3 final = pixel.rgb * intensity;
gl_FragColor = vec4(final, 1.0);
} }

View file

@ -1,8 +1,13 @@
mod player; mod player;
use sfml::graphics::{ use sfml::{
graphics::{
glsl::{Vec2, Vec4},
Color, Rect, RectangleShape, RenderStates, RenderTarget, RenderWindow, Shape, Sprite, Color, Rect, RectangleShape, RenderStates, RenderTarget, RenderWindow, Shape, Sprite,
Transformable, Transformable,
},
system::Clock,
SfBox,
}; };
use crate::{ use crate::{
@ -25,6 +30,7 @@ pub struct GameState {
pub prev_biome: Biome, pub prev_biome: Biome,
pub worldgen: Worldgen, pub worldgen: Worldgen,
pub ambient_light: f32, pub ambient_light: f32,
pub clock: SfBox<Clock>,
} }
#[derive(PartialEq, Eq, Clone, Copy)] #[derive(PartialEq, Eq, Clone, Copy)]
@ -77,8 +83,39 @@ impl GameState {
rw.draw(&rect_sh); rw.draw(&rect_sh);
} }
pub fn render_pre_step(&mut self, res: &mut Res) { pub fn render_pre_step(&mut self, res: &mut Res) {
res.lighting_shader.set_uniform_current_texture("texture");
res.lighting_shader res.lighting_shader
.set_uniform_float("ambient", self.ambient_light); .set_uniform_float("time", self.clock.elapsed_time().as_seconds() * 10.0);
res.lighting_shader.set_uniform_vec2(
"mouse",
Vec2::new(
NATIVE_RESOLUTION.w as f32 / 2.0,
NATIVE_RESOLUTION.h as f32 / 2.0,
),
);
res.lighting_shader.set_uniform_vec2(
"resolution",
Vec2::new(NATIVE_RESOLUTION.w as f32, NATIVE_RESOLUTION.h as f32),
);
res.lighting_shader.set_uniform_vec4(
"lightData",
Vec4 {
x: 1.0,
y: 0.8,
z: 0.2,
w: 2.0,
},
);
res.lighting_shader.set_uniform_vec4(
"ambientData",
Vec4 {
x: 0.3,
y: 0.3,
z: 0.8,
w: 0.3,
},
);
res.lighting_shader.set_uniform_float("lightSize", 0.3);
} }
} }
@ -113,6 +150,7 @@ impl Default for GameState {
prev_biome: Biome::Surface, prev_biome: Biome::Surface,
worldgen: Worldgen::default(), worldgen: Worldgen::default(),
ambient_light: 1.0, ambient_light: 1.0,
clock: Clock::start(),
} }
} }
} }