generated from TheBevyFlock/bevy_new_2d
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from bevy-pirates-15/ryn-changes
Ryn changes
- Loading branch information
Showing
16 changed files
with
216 additions
and
25 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#version 450 | ||
|
||
layout(location = 0) in vec2 frag_uv; | ||
layout(location = 1) in vec3 world_position; | ||
|
||
layout(location = 0) out vec4 out_color; | ||
|
||
layout(set = 2, binding = 0) uniform Material { | ||
vec4 material_color; | ||
}; | ||
|
||
layout(set = 2, binding = 1) uniform LightCount { | ||
uint light_count; | ||
}; | ||
|
||
layout(set = 2, binding = 2) buffer Lights { | ||
vec3 lights[]; | ||
}; | ||
|
||
void main() { | ||
bool discard_fragment = false; | ||
|
||
for (uint i = 0u; i < light_count; i++) { | ||
vec2 light_pos = lights[i].xy; | ||
float light_radius = lights[i].z; | ||
float lr_squared = light_radius * light_radius; | ||
vec2 diff = world_position.xy - light_pos; | ||
float distancesquared = dot(diff, diff); | ||
if (distancesquared < lr_squared) { | ||
discard_fragment = true; | ||
break; | ||
} | ||
} | ||
|
||
if (discard_fragment) { | ||
discard; | ||
} | ||
|
||
out_color = material_color; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#import bevy_sprite::{ | ||
mesh2d_vertex_output::VertexOutput, | ||
} | ||
|
||
@group(2) @binding(0) var<uniform> material_color: vec4<f32>; | ||
@group(2) @binding(1) var<uniform> light_count: u32; | ||
@group(2) @binding(2) var<uniform> lights: array<vec3<f32>, 64>; | ||
|
||
|
||
@fragment | ||
fn fragment( | ||
in: VertexOutput | ||
) -> @location(0) vec4<f32> { | ||
|
||
var world_pos = in.world_position; | ||
var discard_fragment = false; | ||
|
||
for (var i = 0u; i < light_count; i = i + 1u) { | ||
let light_pos = lights[i].xy; | ||
let light_radius = lights[i].z; | ||
let lr_squared = light_radius * light_radius; | ||
let diff = world_pos.xy - light_pos; | ||
let distancesquared = dot(diff, diff); | ||
if (distancesquared < lr_squared) { | ||
discard_fragment = true; | ||
break; | ||
} | ||
} | ||
|
||
if (discard_fragment) { | ||
discard; | ||
} | ||
|
||
return material_color; | ||
} | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#version 450 | ||
|
||
layout(location = 0) in vec3 in_position; | ||
layout(location = 1) in vec2 in_uv; | ||
|
||
layout(location = 0) out vec2 frag_uv; | ||
layout(location = 1) out vec3 world_position; | ||
|
||
layout(set = 0, binding = 0) uniform CameraData { | ||
mat4 view_proj; | ||
mat4 model; | ||
}; | ||
|
||
void main() { | ||
frag_uv = in_uv; | ||
world_position = (model * vec4(in_position, 1.0)).xyz; | ||
gl_Position = view_proj * vec4(in_position, 1.0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use bevy::app::Update; | ||
use bevy::asset::{Asset, Assets}; | ||
use bevy::color::LinearRgba; | ||
use bevy::math::Vec3; | ||
use bevy::prelude::{Component, GlobalTransform, Query, ResMut, TypePath}; | ||
use bevy::render::render_resource::{AsBindGroup, ShaderRef}; | ||
use bevy::sprite::{Material2d, Material2dPlugin}; | ||
|
||
pub(super) fn plugin(app: &mut bevy::app::App) { | ||
app.add_plugins(Material2dPlugin::<LightMaterial>::default()); | ||
app.add_systems(Update, update_light_material); | ||
} | ||
|
||
#[derive(Component)] | ||
pub struct GameLight { | ||
pub radius: f32, | ||
#[allow(dead_code)] | ||
pub priority: u32, | ||
} | ||
|
||
#[derive(AsBindGroup, TypePath, Debug, Clone, Asset)] | ||
pub struct LightMaterial { | ||
#[uniform(0)] | ||
pub color: LinearRgba, | ||
#[uniform(1)] | ||
pub light_count: u32, | ||
#[uniform(2)] | ||
pub lights: [Vec3; 64], | ||
} | ||
|
||
impl Material2d for LightMaterial { | ||
fn fragment_shader() -> ShaderRef { | ||
"light_shader.wgsl".into() | ||
} | ||
} | ||
|
||
fn update_light_material( | ||
mut materials: ResMut<Assets<LightMaterial>>, | ||
light_sources: Query<(&GameLight, &GlobalTransform)>, | ||
) { | ||
let mut lights = [Vec3::ZERO; 64]; | ||
let mut light_count = 0; | ||
|
||
for (light, transform) in light_sources.iter() { | ||
lights[light_count] = transform.translation().truncate().extend(light.radius); | ||
light_count += 1; | ||
} | ||
|
||
for (_, material) in materials.iter_mut() { | ||
material.light_count = light_count as u32; | ||
material.lights.copy_from_slice(&lights); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.