From bc21beeb368d553e0857842736ecb86bc67cb12a Mon Sep 17 00:00:00 2001 From: Matthew Deville Date: Sun, 31 Aug 2025 02:05:01 +0200 Subject: [PATCH] wip --- assets/shaders/custom_material.frag | 14 ++++++++++++++ assets/shaders/custom_material.vert | 12 ++++++++++++ src/main.rs | 26 +++++++++++++------------- 3 files changed, 39 insertions(+), 13 deletions(-) create mode 100644 assets/shaders/custom_material.frag create mode 100644 assets/shaders/custom_material.vert diff --git a/assets/shaders/custom_material.frag b/assets/shaders/custom_material.frag new file mode 100644 index 0000000..b4f4df1 --- /dev/null +++ b/assets/shaders/custom_material.frag @@ -0,0 +1,14 @@ +#version 450 + +layout(location = 0) in vec2 v_uv; +layout(location = 0) out vec4 o_color; + +layout(set = 0, binding = 0) uniform MaterialUniform { + vec4 color; +} material; + +void main() { + // simple uv-based gradient multiplied by uniform color + vec3 grad = vec3(v_uv, 0.5); + o_color = material.color * vec4(grad, 1.0); +} diff --git a/assets/shaders/custom_material.vert b/assets/shaders/custom_material.vert new file mode 100644 index 0000000..547cac3 --- /dev/null +++ b/assets/shaders/custom_material.vert @@ -0,0 +1,12 @@ +#version 450 + +// Minimal passthrough vertex shader for a fullscreen plane. +layout(location = 0) in vec3 Vertex_Position; +layout(location = 1) in vec2 Vertex_Uv; + +layout(location = 0) out vec2 v_uv; + +void main() { + v_uv = Vertex_Uv; + gl_Position = vec4(Vertex_Position, 1.0); +} diff --git a/src/main.rs b/src/main.rs index d0ce2ce..2b2e248 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,9 @@ //! A shader that uses the GLSL shading language. use bevy::{ - prelude::*, reflect::TypePath, render::render_resource::AsBindGroup, render::render_resource::ShaderRef, + prelude::*, + reflect::TypePath, + render::render_resource::{AsBindGroup, ShaderRef}, }; /// This example uses shader source files from the assets subdirectory @@ -10,34 +12,32 @@ const FRAGMENT_SHADER_ASSET_PATH: &str = "shaders/custom_material.frag"; fn main() { App::new() - .add_plugins((DefaultPlugins, MaterialPlugin::::default())) + .add_plugins(DefaultPlugins) + .add_plugins(MaterialPlugin::::default()) .add_systems(Startup, setup) .run(); } -/// set up a simple 3D scene +/// set up a simple 2D-screen-like surface fn setup( mut commands: Commands, mut meshes: ResMut>, mut materials: ResMut>, - asset_server: Res, ) { - // cube + // Fullscreen flat surface: use a very large, thin cuboid so it behaves like a plane commands.spawn(( Mesh3d(meshes.add(Cuboid::default())), MeshMaterial3d(materials.add(CustomMaterial { - color: LinearRgba::BLUE, + color: LinearRgba::new(0.2, 0.6, 0.9, 1.0), color_texture: None, - alpha_mode: AlphaMode::Blend, + alpha_mode: AlphaMode::Opaque, })), - Transform::from_xyz(0.0, 0.5, 0.0), + // Scale X/Y large to cover the camera view, keep Z very small to make it effectively a plane + Transform::from_scale(Vec3::new(2000.0, 2000.0, 0.01)), )); - // camera - commands.spawn(( - Camera3d::default(), - Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y), - )); + // 2D camera so the scene renders as a flat screen + commands.spawn((Camera2d::default(), Transform::default())); } // This is the struct that will be passed to your shader