From c232c1c9a4a1dd2938cb787294ceff46dca8bb83 Mon Sep 17 00:00:00 2001 From: Jakob Hellermann Date: Mon, 28 Feb 2022 18:36:57 +0100 Subject: [PATCH 1/3] add screenspace texture shader example --- Cargo.toml | 4 + .../custom_material_screenspace_texture.wgsl | 13 ++ examples/README.md | 1 + .../shader_material_screenspace_texture.rs | 164 ++++++++++++++++++ 4 files changed, 182 insertions(+) create mode 100644 assets/shaders/custom_material_screenspace_texture.wgsl create mode 100644 examples/shader/shader_material_screenspace_texture.rs diff --git a/Cargo.toml b/Cargo.toml index f7a7a95f2eec2..f7ca276c55460 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -467,6 +467,10 @@ path = "examples/shader/shader_defs.rs" name = "shader_material" path = "examples/shader/shader_material.rs" +[[example]] +name = "shader_material_screenspace_texture" +path = "examples/shader/shader_material_screenspace_texture.rs" + [[example]] name = "shader_material_glsl" path = "examples/shader/shader_material_glsl.rs" diff --git a/assets/shaders/custom_material_screenspace_texture.wgsl b/assets/shaders/custom_material_screenspace_texture.wgsl new file mode 100644 index 0000000000000..a1fa6998f20d2 --- /dev/null +++ b/assets/shaders/custom_material_screenspace_texture.wgsl @@ -0,0 +1,13 @@ +#import bevy_pbr::mesh_view_bind_group + +[[group(1), binding(0)]] +var texture: texture_2d; +[[group(1), binding(1)]] +var texture_sampler: sampler; + +[[stage(fragment)]] +fn fragment([[builtin(position)]] position: vec4) -> [[location(0)]] vec4 { + let uv = position.xy / vec2(view.width, view.height); + let color = textureSample(texture, texture_sampler, uv); + return color; +} diff --git a/examples/README.md b/examples/README.md index c73f654043b32..b80fc95a66cc8 100644 --- a/examples/README.md +++ b/examples/README.md @@ -226,6 +226,7 @@ Example | File | Description --- | --- | --- `custom_vertex_attribute` | [`shader/custom_vertex_attribute.rs`](./shader/custom_vertex_attribute.rs) | Illustrates creating a custom shader material that reads a mesh's custom vertex attribute. `shader_material` | [`shader/shader_material.rs`](./shader/shader_material.rs) | Illustrates creating a custom material and a shader that uses it +`shader_material_screenspace_uv` | [`shader/shader_material_screenspace_uv.rs`](./shader/shader_material_screenspace_uv.rs) | A custom shader sampling a texture with view-indepdenant UV coordinates `shader_material_glsl` | [`shader/shader_material_glsl.rs`](./shader/shader_material_glsl.rs) | A custom shader using the GLSL shading language. `shader_instancing` | [`shader/shader_instancing.rs`](./shader/shader_instancing.rs) | A custom shader showing off rendering a mesh multiple times in one draw call. `animate_shader` | [`shader/animate_shader.rs`](./shader/animate_shader.rs) | Shows how to pass changing data like the time since startup into a shader. diff --git a/examples/shader/shader_material_screenspace_texture.rs b/examples/shader/shader_material_screenspace_texture.rs new file mode 100644 index 0000000000000..6c0c7a9364554 --- /dev/null +++ b/examples/shader/shader_material_screenspace_texture.rs @@ -0,0 +1,164 @@ +use bevy::{ + ecs::system::{lifetimeless::SRes, SystemParamItem}, + pbr::MaterialPipeline, + prelude::*, + reflect::TypeUuid, + render::{ + render_asset::{PrepareAssetError, RenderAsset, RenderAssets}, + render_resource::{ + BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout, + BindGroupLayoutDescriptor, BindGroupLayoutEntry, BindingResource, BindingType, + SamplerBindingType, ShaderStages, TextureSampleType, TextureViewDimension, + }, + renderer::RenderDevice, + }, +}; + +fn main() { + App::new() + .add_plugins(DefaultPlugins) + .add_plugin(MaterialPlugin::::default()) + .add_startup_system(setup) + .add_system(rotate_camera) + .run(); +} + +#[derive(Component)] +struct MainCamera; + +fn setup( + mut commands: Commands, + asset_server: Res, + mut meshes: ResMut>, + mut custom_materials: ResMut>, + mut standard_materials: ResMut>, +) { + commands.spawn_bundle(PbrBundle { + mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })), + material: standard_materials.add(Color::rgb(0.3, 0.5, 0.3).into()), + ..Default::default() + }); + commands.spawn_bundle(PointLightBundle { + transform: Transform::from_xyz(4.0, 8.0, 4.0), + ..Default::default() + }); + commands.spawn_bundle(PerspectiveCameraBundle { + transform: Transform::from_xyz(0.0, 2.5, 1.0).looking_at(Vec3::default(), Vec3::Y), + ..Default::default() + }); + + commands.spawn().insert_bundle(MaterialMeshBundle { + mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), + transform: Transform::from_xyz(0.0, 0.5, 0.0), + material: custom_materials.add(CustomMaterial { + texture: asset_server.load( + "models/FlightHelmet/FlightHelmet_Materials_LensesMat_OcclusionRoughMetal.png", + ), + }), + ..Default::default() + }); + + // camera + commands + .spawn_bundle(PerspectiveCameraBundle { + transform: Transform::from_xyz(4.0, 2.5, 4.0).looking_at(Vec3::ZERO, Vec3::Y), + ..Default::default() + }) + .insert(MainCamera); +} + +fn rotate_camera(mut camera: Query<&mut Transform, With>, time: Res