Skip to content

Commit

Permalink
Get simple texture working.
Browse files Browse the repository at this point in the history
  • Loading branch information
tychedelia committed Jan 31, 2024
1 parent e91ceff commit e5fa7cb
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 40 deletions.
14 changes: 12 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added bevy_nannou/assets/images/img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 34 additions & 8 deletions bevy_nannou/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use bevy::prelude::*;
use bevy_nannou_render::mesh::{Vertex, ViewMesh};
use bevy::prelude::shape::Cube;

use bevy_nannou_render::mesh::ViewMesh;

pub struct NannouPlugin;

Expand All @@ -12,29 +14,53 @@ impl Plugin for NannouPlugin {
}
}

fn setup(mut commands: Commands) {
fn setup(
mut commands: Commands,
assets: Res<AssetServer>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let mut mesh = ViewMesh::default();
let image = assets.load("images/img.png");
mesh.texture = Some(image);
mesh.extend_from_slices(
&[
Vec3::new(-512.0, -384.0, 0.0),
Vec3::new(-512.0, 384.0, 0.0),
Vec3::new(512.0, 384.0, 0.0),
Vec3::new(-512.0, -512.0, 0.0),
Vec3::new(512.0, -512.0, 0.0),
Vec3::new(-512.0, 512.0, 0.0),
Vec3::new(512.0, 512.0, 0.0),
],
&[1, 0, 2, 1, 2, 3],
&[Vec4::new(1.0, 0.0, 0.0, 1.0); 4],
&[
Vec2::new(0.0, 1.0),
Vec2::new(1.0, 1.0),
Vec2::new(0.0, 0.0),
Vec2::new(1.0, 0.0),
],
&[0, 1, 2],
&[Vec4::new(1.0, 0.0, 0.0, 1.0); 3],
&[Vec2::new(0.0, 0.0); 3],
);
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(0.0, 0.0, -10.0).looking_at(Vec3::ZERO, Vec3::Z),
projection: OrthographicProjection {
scale: 1.0,
..Default::default()
}
.into(),
..Default::default()
},
mesh,
));
commands.insert_resource(AmbientLight {
color: Color::WHITE,
brightness: 1.0,
});
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(Cube::new(10.0))),
material: materials.add(Color::rgb(1.0, 1.0, 1.0).into()),
transform: Transform::from_xyz(0.0, 0.0, 0.0),
..Default::default()
});
}

#[cfg(test)]
Expand Down
3 changes: 2 additions & 1 deletion bevy_nannou_render/src/mesh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub type MeshType =
#[derive(Component, Debug, Clone, ExtractComponent)]
pub struct ViewMesh {
mesh: MeshType,
pub texture: Option<Handle<Image>>,
}

impl ViewMesh {
Expand Down Expand Up @@ -183,7 +184,7 @@ impl ViewMesh {
impl Default for ViewMesh {
fn default() -> Self {
let mesh = Default::default();
ViewMesh { mesh }
ViewMesh { mesh, texture: None }
}
}

Expand Down
48 changes: 37 additions & 11 deletions bevy_nannou_render/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::hash::{Hash, Hasher};
use bevy::ecs::query::QueryItem;
use bevy::prelude::*;
use bevy::render::camera::ExtractedCamera;
use bevy::render::render_asset::RenderAssets;
use bevy::render::render_graph::{NodeRunError, RenderGraphContext, ViewNode};
use bevy::render::render_resource as wgpu;
use bevy::render::render_resource::{
Expand All @@ -19,7 +20,7 @@ use bevy::utils;

use crate::mesh::vertex::Point;
use crate::mesh::{TexCoords, ViewMesh};
use crate::{GlyphCache, NANNOU_SHADER_HANDLE, VertexMode, ViewUniformBindGroup};
use crate::{GlyphCache, mesh, NANNOU_SHADER_HANDLE, VertexMode, ViewUniformBindGroup};

#[derive(Resource)]
pub struct NannouPipeline {
Expand All @@ -28,7 +29,6 @@ pub struct NannouPipeline {
text_bind_group_layout: wgpu::BindGroupLayout,
text_bind_group: wgpu::BindGroup,
texture_samplers: HashMap<wgpu::SamplerId, wgpu::Sampler>,
// TODO: move to resource and support multiple textures.
texture_bind_group_layout: wgpu::BindGroupLayout,
texture_bind_group: wgpu::BindGroup,
output_color_format: wgpu::TextureFormat,
Expand Down Expand Up @@ -96,13 +96,13 @@ impl NannouPipeline {
offset: 0,
shader_location: 0,
}])
.add_vertex_buffer::<Color>(&[wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x3,
.add_vertex_buffer::<mesh::vertex::Color>(&[wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x4,
offset: 0,
shader_location: 1,
}])
.add_vertex_buffer::<TexCoords>(&[wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x3,
.add_vertex_buffer::<mesh::vertex::TexCoords>(&[wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2,
offset: 0,
shader_location: 2,
}])
Expand Down Expand Up @@ -148,7 +148,7 @@ impl NannouPipeline {
.build(device, layout)
}

fn create_texture_bind_group(
pub fn create_texture_bind_group(
device: &RenderDevice,
layout: &wgpu::BindGroupLayout,
sampler: &wgpu::Sampler,
Expand Down Expand Up @@ -364,15 +364,17 @@ impl ViewNode for NannouViewNode {

// Create render pass builder.
let render_pass_builder = bevy_nannou_wgpu::RenderPassBuilder::new()
.color_attachment(target.main_texture_view(), |color| color);
.color_attachment(target.main_texture_view(), |color|
color.load_op(wgpu::LoadOp::Load) // TODO: bg color
);

let render_device = render_context.render_device();

let vertex_usage = wgpu::BufferUsages::VERTEX;
let points_bytes = cast_slice(&mesh.points()[..]);
let colors_bytes = cast_slice(mesh.colors());
let tex_coords_bytes = cast_slice(mesh.tex_coords());
// let modes_bytes = vertex_modes_as_bytes(vertex_mode_buffer);
let modes_bytes = cast_slice(&[VertexMode::Texture as u32; 4]);
let indices_bytes = cast_slice(mesh.indices());
let point_buffer = render_device.create_buffer_with_data(&BufferInitDescriptor {
label: Some("nannou Renderer point_buffer"),
Expand All @@ -391,7 +393,7 @@ impl ViewNode for NannouViewNode {
});
let mode_buffer = render_device.create_buffer_with_data(&BufferInitDescriptor {
label: Some("nannou Renderer mode_buffer"),
contents: &[],
contents: modes_bytes,
usage: vertex_usage,
});
let index_buffer = render_device.create_buffer_with_data(&BufferInitDescriptor {
Expand All @@ -400,6 +402,30 @@ impl ViewNode for NannouViewNode {
usage: wgpu::BufferUsages::INDEX,
});

let texture_bind_group = match &mesh.texture {
Some(texture) => {
let images = world.resource::<RenderAssets<Image>>();
if let Some(gpu_image) = images.get(texture) {
let texture_bind_group_layout = NannouPipeline::create_texture_bind_group_layout(
render_device,
true,
wgpu::TextureSampleType::Float { filterable: true },
);
NannouPipeline::create_texture_bind_group(
render_device,
&texture_bind_group_layout,
&gpu_image.sampler,
&gpu_image.texture_view,
)
} else {
nannou_pipeline.texture_bind_group.clone()
}
}
None => {
nannou_pipeline.texture_bind_group.clone()
}
};

let mut render_pass = render_pass_builder.begin(render_context);
render_pass.set_render_pipeline(pipeline);

Expand All @@ -414,7 +440,7 @@ impl ViewNode for NannouViewNode {
let uniform_bind_group = world.resource::<ViewUniformBindGroup>();
render_pass.set_bind_group(0, &uniform_bind_group.bind_group, &[]);
render_pass.set_bind_group(1, &nannou_pipeline.text_bind_group, &[]);
render_pass.set_bind_group(2, &nannou_pipeline.texture_bind_group, &[]);
render_pass.set_bind_group(2, &texture_bind_group, &[]);

// Draw the mesh.
let indices = 0..mesh.indices().len() as u32;
Expand Down
3 changes: 1 addition & 2 deletions bevy_nannou_render/src/shaders/nannou.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ struct VertexOutput {
fn vertex(
input: VertexInput,
) -> VertexOutput {
let out_pos: vec4<f32> = view.projection * vec4<f32>(input.position, 1.0);
let out_pos: vec4<f32> = view.view_proj * vec4<f32>(input.position, 1.0);
return VertexOutput(input.color, input.tex_coords, input.mode, out_pos);

}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ FRAGMENT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
Expand Down
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ edition = "2018"

[dev-dependencies]
audrey = "0.3"
hotglsl = "0.2"
hotglsl = { git = "https://github.com/nannou-org/hotglsl", branch = "master" }
hrtf = "0.2"
nannou = { version ="0.19.0", path = "../nannou" }
nannou_audio = { version ="0.19.0", path = "../nannou_audio" }
Expand Down
19 changes: 4 additions & 15 deletions examples/draw/draw_textured_polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ fn main() {

struct Model {
window_id: window::Id,
texture_a: wgpu::Texture,
texture_b: wgpu::Texture,
texture: wgpu::Texture,
}

fn model(app: &App) -> Model {
Expand All @@ -16,11 +15,9 @@ fn model(app: &App) -> Model {
// Load the image from disk and upload it to a GPU texture.
let assets = app.assets_path().unwrap();
let img_path = assets.join("images").join("nature").join("nature_1.jpg");
let texture_a = wgpu::Texture::from_path(app, img_path).unwrap();
let img_path = assets.join("images").join("nature").join("nature_2.jpg");
let texture_b = wgpu::Texture::from_path(app, img_path).unwrap();
let texture = wgpu::Texture::from_path(app, img_path).unwrap();

Model { window_id, texture_a, texture_b }
Model { window_id, texture }
}

// Draw the state of your `Model` into the given `Frame` here.
Expand Down Expand Up @@ -58,17 +55,9 @@ fn view(app: &App, model: &Model, frame: Frame) {
let ellipse_side = win_rect.w().min(win_rect.h()) * 0.75;
draw.scale(ellipse_side)
.polygon()
.points_textured(&model.texture_a, points.clone())
.points_textured(&model.texture, points)
.rotate(app.time * 0.25);

// Scale the points up to half the window size.
let ellipse_side = win_rect.w().min(win_rect.h()) * 0.75;
draw.scale(ellipse_side)
.polygon()
.points_textured(&model.texture_b, points)
.rotate(app.time * 0.11);


// Draw to the frame!
draw.to_frame(app, &frame).unwrap();
}

0 comments on commit e5fa7cb

Please sign in to comment.