8000 Minor changes by nobbele · Pull Request #1095 · ggez/ggez · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Minor changes #1095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/03_drawing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ struct MainState {
impl MainState {
/// Load images and create meshes.
fn new(ctx: &mut Context) -> GameResult<MainState> {
let image1 = graphics::Image::from_path(ctx, "/dragon1.png", true)?;
let image2 = graphics::Image::from_path(ctx, "/shot.png", true)?;
let image1 = graphics::Image::from_path(ctx, "/dragon1.png")?;
let image2 = graphics::Image::from_path(ctx, "/shot.png")?;

let mb = &mut graphics::MeshBuilder::new();
mb.rectangle(
Expand All @@ -29,7 +29,7 @@ impl MainState {
graphics::Color::new(1.0, 0.0, 0.0, 1.0),
)?;

let rock = graphics::Image::from_path(ctx, "/rock.png", true)?;
let rock = graphics::Image::from_path(ctx, "/rock.png")?;

let meshes = vec![
(None, build_mesh(ctx)?),
Expand Down
6 changes: 3 additions & 3 deletions examples/05_astroblasto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,9 @@ struct Assets {

impl Assets {
fn new(ctx: &mut Context) -> GameResult<Assets> {
let player_image = graphics::Image::from_path(ctx, "/player.png", true)?;
let shot_image = graphics::Image::from_path(ctx, "/shot.png", true)?;
let rock_image = graphics::Image::from_path(ctx, "/rock.png", true)?;
let player_image = graphics::Image::from_path(ctx, "/player.png")?;
let shot_image = graphics::Image::from_path(ctx, "/shot.png")?;
let rock_image = graphics::Image::from_path(ctx, "/rock.png")?;

let shot_sound = audio::Source::new(ctx, "/pew.ogg")?;
let hit_sound = audio::Source::new(ctx, "/boom.ogg")?;
Expand Down
2 changes: 1 addition & 1 deletion examples/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl MainState {
Color::WHITE,
)?;

let img = graphics::Image::from_path(ctx, "/player_sheet.png", true)?;
let img = graphics::Image::from_path(ctx, "/player_sheet.png")?;
let s = MainState {
ball,
spritesheet: img,
Expand Down
2 changes: 1 addition & 1 deletion examples/bunnymark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl GameState {
fn new(ctx: &mut Context) -> ggez::GameResult<GameState> {
// We just use the same RNG seed every time.
let mut rng = Rand32::new(12345);
let texture = Image::from_path(ctx, "/wabbit_alpha.png", true)?;
let texture = Image::from_path(ctx, "/wabbit_alpha.png")?;
let mut bunnies = Vec::with_capacity(INITIAL_BUNNIES);
let max_x = (WIDTH - texture.width() as u16) as f32;
let max_y = (HEIGHT - texture.height() as u16) as f32;
Expand Down
2 changes: 1 addition & 1 deletion examples/canvas_subframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct MainState {

impl MainState {
fn new(ctx: &mut Context) -> GameResult<MainState> {
let image = graphics::Image::from_path(ctx, "/tile.png", true).unwrap();
let image = graphics::Image::from_path(ctx, "/tile.png").unwrap();
let instances = graphics::InstanceArray::new(ctx, image, 150 * 150, false);
let canvas_image = graphics::ScreenImage::new(ctx, None, 1., 1., 1);
let draw_pt = Point2::new(0.0, 0.0);
Expand Down
2 changes: 1 addition & 1 deletion examples/graphics_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl MainState {
let s = MainState {
angle: 0.0,
zoom: 1.0,
image: graphics::Image::from_path(ctx, "/tile.png", true)?,
image: graphics::Image::from_path(ctx, "/tile.png")?,
window_settings: WindowSettings {
toggle_fullscreen: false,
is_fullscreen: false,
Expand Down
2 changes: 1 addition & 1 deletion examples/imageview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl MainState {
fn new(ctx: &mut Context) -> GameResult<MainState> {
ctx.fs.print_all();

let image = graphics::Image::from_path(ctx, "/dragon1.png", true)?;
let image = graphics::Image::from_path(ctx, "/dragon1.png")?;

let mut sound = audio::Source::new(ctx, "/sound.ogg")?;

Expand Down
2 changes: 1 addition & 1 deletion examples/instance_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct MainState {

impl MainState {
fn new(ctx: &mut Context) -> GameResult<MainState> {
let image = graphics::Image::from_path(ctx, "/tile.png", true)?;
let image = graphics::Image::from_path(ctx, "/tile.png")?;
let instances = graphics::InstanceArray::new(ctx, image, 150 * 150, false);
Ok(MainState { instances })
}
Expand Down
167 changes: 5 additions & 162 deletions examples/shadows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,50 +25,7 @@ struct Light {
/// and encoded in the red channel (it is halved because if the distance can be
/// greater than 1.0 - think bottom left to top right corner, that sqrt(1) and
/// will not get properly encoded).
const OCCLUSIONS_SHADER_SOURCE: &str = "
struct VertexOutput {
@builtin(position) position: vec4<f32>;
@location(0) uv: vec2<f32>;
@location(1) color: vec4<f32>;
};

struct Light {
light_color: vec4<f32>,
shadow_color: vec4<f32>,
pos: vec2<f32>,
screen_size: vec2<f32>,
glow: f32,
strength: f32,
}

@group(1) @binding(0)
var t: texture_2d<f32>;

@group(2) @binding(0)
var s: sampler;

@group(4) @binding(0)
var<uniform> light: Light;

@fragment
fn main(in: VertexOutput) -> @location(0) vec4<f32> {
var dist = 1.0;
var theta = in.uv.x * 6.28318530718;
var dir = vec2<f32>(cos(theta), sin(theta));
for (var i: i32 = 0; i < 1024; i = i + 1) {
var fi = f32(i);
var r = fi / 1024.0;
var rel = r * dir;
var p = clamp(light.pos + rel, vec2<f32>(0.0), vec2<f32>(1.0));
if (textureSample(t, s, p).a > 0.8) {
dist = distance(light.pos, p) * 0.5;
break;
}
}
var others = select(dist, 0.0, dist == 1.0);
return vec4<f32>(dist, others, others, 1.0);
}
";
const OCCLUSIONS_SHADER_SOURCE: &str = include_str!("../resources/occlusions.wgsl");

/// Shader for drawing shadows based on a 1D shadow map. It takes current
/// fragment coordinates and converts them to polar coordinates centered
Expand All @@ -77,58 +34,7 @@ fn main(in: VertexOutput) -> @location(0) vec4<f32> {
/// closest reported shadow, then the output is the shadow color, else it calculates some
/// shadow based on the distance from light source based on strength and glow
/// uniform parameters.
const SHADOWS_SHADER_SOURCE: &str = "
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) uv: vec2<f32>,
@location(1) color: vec4<f32>,
}

struct Light {
light_color: vec4<f32>,
shadow_color: vec4<f32>,
pos: vec2<f32>,
screen_size: vec2<f32>,
glow: f32,
strength: f32,
}

@group(1) @binding(0)
var t: texture_2d<f32>;

<<<<<<< HEAD
@group(1) @binding(1)
var s: sampler;

@group(0) binding(0)
=======
[[group(2), binding(0)]]
var s: sampler;

[[group(4), binding(0)]]
>>>>>>> ec2076b1421aec71939a0eafcbabc43ec5deb4b5
var<uniform> light: Light;

fn degrees(x: f32) -> f32 {
return x * 57.2957795130823208767981548141051703;
}

@fragment
fn main(in: VertexOutput) -> @location(0) vec4<f32> {
var rel = light.pos - in.uv;
var theta = atan2(rel.y, rel.x);
var ox = (theta + 3.1415926) / 6.2831853;
var r = length(rel);
var occl = 1.0 - step(r, textureSample(t, s, vec2<f32>(ox, 0.5)).r * 2.0);

var g = light.screen_size / light.screen_size.y;
var p = light.strength + light.glow;
var d = distance(g * in.uv, g * light.pos);
var intensity = 1.0 - clamp(p/(d*d), 0.0, 1.0);

return light.shadow_color * vec4<f32>(vec3<f32>(mix(intensity, 1.0, occl)), 1.0);
}
";
const SHADOWS_SHADER_SOURCE: &str = include_str!("../resources/shadows.wgsl");

/// Shader for drawing lights based on a 1D shadow map. It takes current
/// fragment coordinates and converts them to polar coordinates centered
Expand All @@ -138,70 +44,7 @@ fn main(in: VertexOutput) -> @location(0) vec4<f32> {
/// light based on the distance from light source based on strength and glow
/// uniform parameters. It is meant to be used additively for drawing multiple
/// lights.
const LIGHTS_SHADER_SOURCE: &str = "
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) uv: vec2<f32>,
@location(1) color: vec4<f32>,
}

struct Light {
light_color: vec4<f32>,
shadow_color: vec4<f32>,
pos: vec2<f32>,
screen_size: vec2<f32>,
glow: f32,
strength: f32,
}

@group(1) @binding(0)
var t: texture_2d<f32>;

<<<<<<< HEAD
@group(1) @binding(1)
var s: sampler;

@group(0) binding(0)
=======
[[group(2), binding(0)]]
var s: sampler;

[[group(4), binding(0)]]
>>>>>>> ec2076b1421aec71939a0eafcbabc43ec5deb4b5
var<uniform> light: Light;

fn degrees(x: f32) -> f32 {
return x * 57.2957795130823208767981548141051703;
}

@fragment
fn main(in: VertexOutput) -> @location(0) vec4<f32> {
var rel = light.pos - in.uv;
var theta = atan2(rel.y, rel.x);
var ox = (theta + 3.1415926) / 6.2831853;
var r = length(rel);
var occl = step(r, textureSample(t, s, vec2<f32>(ox, 0.5)).r * 2.0);

var g = light.screen_size / light.screen_size.y;
var p = light.strength + light.glow;
var d = distance(g * in.uv, g * light.pos);
var intensity = clamp(p/(d*d), 0.0, 0.6);

var blur = (2.5 / light.screen_size.x) * smoothStep(0.0, 1.0, r);
var sum = 0.0;
sum = sum + step(r, textureSample(t, s, vec2<f32>(ox - 4.0 * blur, 0.5)).r * 2.0) * 0.05;
sum = sum + step(r, textureSample(t, s, vec2<f32>(ox - 3.0 * blur, 0.5)).r * 2.0) * 0.09;
sum = sum + step(r, textureSample(t, s, vec2<f32>(ox - 2.0 * blur, 0.5)).r * 2.0) * 0.12;
sum = sum + step(r, textureSample(t, s, vec2<f32>(ox - 1.0 * blur, 0.5)).r * 2.0) * 0.15;
sum = sum + occl * 0.16;
sum = sum + step(r, textureSample(t, s, vec2<f32>(ox + 1.0 * blur, 0.5)).r * 2.0) * 0.15;
sum = sum + step(r, textureSample(t, s, vec2<f32>(ox + 2.0 * blur, 0.5)).r * 2.0) * 0.12;
sum = sum + step(r, textureSample(t, s, vec2<f32>(ox + 3.0 * blur, 0.5)).r * 2.0) * 0.09;
sum = sum + step(r, textureSample(t, s, vec2<f32>(ox + 4.0 * blur, 0.5)).r * 2.0) * 0.05;

return light.light_color * vec4<f32>(vec3<f32>(sum * intensity), 1.0);
}
";
const LIGHTS_SHADER_SOURCE: &str = include_str!("../resources/lights.wgsl");

struct MainState {
background: graphics::Image,
Expand Down Expand Up @@ -238,8 +81,8 @@ const LIGHT_GLOW_RATE: f32 = 0.9;

impl MainState {
fn new(ctx: &mut Context) -> GameResult<MainState> {
let background = graphics::Image::from_path(ctx, "/bg_top.png", true)?;
let tile = graphics::Image::from_path(ctx, "/tile.png", true)?;
let background = graphics::Image::from_path(ctx, "/bg_top.png")?;
let tile = graphics::Image::from_path(ctx, "/tile.png")?;

let screen_size = {
let size = ctx.gfx.drawable_size();
Expand Down
2 changes: 1 addition & 1 deletion examples/transforms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl MainState {
const GRID_POINT_RADIUS: f32 = 5.0;

fn new(ctx: &mut Context) -> GameResult<MainState> {
let angle = graphics::Image::from_path(ctx, "/angle.png", true)?;
let angle = graphics::Image::from_path(ctx, "/angle.png")?;
let gridmesh_builder = &mut graphics::MeshBuilder::new();
for x in 0..Self::GRID_SIZE {
for y in 0..Self::GRID_SIZE {
Expand Down
55 changes: 55 additions & 0 deletions resources/lights.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) uv: vec2<f32>,
@location(1) color: vec4<f32>,
}

struct Light {
light_color: vec4<f32>,
shadow_color: vec4<f32>,
pos: vec2<f32>,
screen_size: vec2<f32>,
glow: f32,
strength: f32,
}

@group(1) @binding(0)
var t: texture_2d<f32>;

@group(2) @binding(0)
var s: sampler;

@group(4) @binding(0)
var<uniform> light: Light;

fn degrees(x: f32) -> f32 {
return x * 57.2957795130823208767981548141051703;
}

@fragment
fn main(in: VertexOutput) -> @location(0) vec4<f32> {
var rel = light.pos - in.uv;
var theta = atan2(rel.y, rel.x);
var ox = (theta + 3.1415926) / 6.2831853;
var r = length(rel);
var occl = step(r, textureSample(t, s, vec2<f32>(ox, 0.5)).r * 2.0);

var g = light.screen_size / light.screen_size.y;
var p = light.strength + light.glow;
var d = distance(g * in.uv, g * light.pos);
var intensity = clamp(p / (d * d), 0.0, 0.6);

var blur = (2.5 / light.screen_size.x) * smoothstep(0.0, 1.0, r);
var sum = 0.0;
sum = sum + step(r, textureSample(t, s, vec2<f32>(ox - 4.0 * blur, 0.5)).r * 2.0) * 0.05;
sum = sum + step(r, textureSample(t, s, vec2<f32>(ox - 3.0 * blur, 0.5)).r * 2.0) * 0.09;
sum = sum + step(r, textureSample(t, s, vec2<f32>(ox - 2.0 * blur, 0.5)).r * 2.0) * 0.12;
sum = sum + step(r, textureSample(t, s, vec2<f32>(ox - 1.0 * blur, 0.5)).r * 2.0) * 0.15;
sum = sum + occl * 0.16;
sum = sum + step(r, textureSample(t, s, vec2<f32>(ox + 1.0 * blur, 0.5)).r * 2.0) * 0.15;
sum = sum + step(r, textureSample(t, s, vec2<f32>(ox + 2.0 * blur, 0.5)).r * 2.0) * 0.12;
sum = sum + step(r, textureSample(t, s, vec2<f32>(ox + 3.0 * blur, 0.5)).r * 2.0) * 0.09;
sum = sum + step(r, textureSample(t, s, vec2<f32>(ox + 4.0 * blur, 0.5)).r * 2.0) * 0.05;

return light.light_color * vec4<f32>(vec3<f32>(sum * intensity), 1.0);
}
42 changes: 42 additions & 0 deletions resources/occlusions.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) uv: vec2<f32>,
@location(1) color: vec4<f32>,
};

struct Light {
light_color: vec4<f32>,
shadow_color: vec4<f32>,
pos: vec2<f32>,
screen_size: vec2<f32>,
glow: f32,
strength: f32,
}

@group(1) @binding(0)
var t: texture_2d<f32>;

@group(2) @binding(0)
var s: sampler;

@group(4) @binding(0)
var<uniform> light: Light;

@fragment
fn main(in: VertexOutput) -> @location(0) vec4<f32> {
var dist = 1.0;
var theta = in.uv.x * 6.28318530718;
var dir = vec2<f32>(cos(theta), sin(theta));
for (var i: i32 = 0; i < 1024; i = i + 1) {
var fi = f32(i);
var r = fi / 1024.0;
var rel = r * dir;
var p = clamp(light.pos + rel, vec2<f32>(0.0), vec2<f32>(1.0));
if (textureSample(t, s, p).a > 0.8) {
dist = distance(light.pos, p) * 0.5;
break;
}
}
var others = select(dist, 0.0, dist == 1.0);
return vec4<f32>(dist, others, others, 1.0);
}
Loading
0