Use image effect instead of screen quad in image example
@@ -16,6 +16,7 @@ pub async fn run() {
})
.unwrap();
let context = window.gl();
+ let mut image_effect = ImageEffect::new(&context, include_str!("shader.frag")).unwrap();
// Source: https://polyhaven.com/
let mut loaded = if let Ok(loaded) =
@@ -33,15 +34,8 @@ pub async fn run() {
let mut gui = GUI::new(&context);
- let mut image_viewer = Gm::new(
- ScreenQuad::new(&context),
- ImageMaterial {
- image,
- tone_mapping: 1.0,
- },
- );
-
// main loop
+ let mut tone_mapping = 1.0;
let mut texture_transform_scale = 1.0;
let mut texture_transform_x = 0.0;
let mut texture_transform_y = 0.0;
@@ -56,10 +50,7 @@ pub async fn run() {
use three_d::egui::*;
SidePanel::right("side_panel").show(gui_context, |ui| {
ui.heading("Debug Panel");
- ui.add(
- Slider::new(&mut image_viewer.material.tone_mapping, 0.0..=50.0)
- .text("Tone mapping"),
- );
+ ui.add(Slider::new(&mut tone_mapping, 0.0..=50.0).text("Tone mapping"));
ui.add(
Slider::new(&mut texture_transform_scale, 0.0..=10.0)
.text("Texture transform scale"),
@@ -77,7 +68,7 @@ pub async fn run() {
},
);
- image_viewer.geometry.set_texture_transform(
+ image_effect.set_texture_transform(
Mat3::from_scale(texture_transform_scale)
* Mat3::from_translation(vec2(texture_transform_x, texture_transform_y)),
);
@@ -87,38 +78,13 @@ pub async fn run() {
frame_input.viewport.height,
);
- frame_input
- .screen()
- .clear(ClearState::default())
- .render(&camera2d(viewport), &image_viewer, &[])
- .write(|| {
+ frame_input.screen().clear(ClearState::default()).write(|| {
+ image_effect.use_texture("image", &image);
+ image_effect.use_uniform("parameter", tone_mapping);
+ image_effect.apply(RenderStates::default(), viewport);
gui.render();
});
FrameOutput::default()
});
}
-
-struct ImageMaterial {
- pub image: Texture2D,
- pub tone_mapping: f32,
-}
-
-impl Material for ImageMaterial {
- fn fragment_shader_source(&self, _use_vertex_colors: bool, _lights: &[&dyn Light]) -> String {
- include_str!("shader.frag").to_owned()
- }
-
- fn use_uniforms(&self, program: &Program, _camera: &Camera, _lights: &[&dyn Light]) {
- program.use_texture("image", &self.image);
- program.use_uniform("parameter", self.tone_mapping);
- }
-
- fn render_states(&self) -> RenderStates {
- RenderStates::default()
- }
-
- fn material_type(&self) -> MaterialType {
- MaterialType::Opaque
- }
-}
