Implement side-by-side view

This commit is contained in:
Simon Gellis 2024-12-01 15:52:11 -05:00
parent 2e5d5e140f
commit 761b434108
3 changed files with 59 additions and 6 deletions

View File

@ -57,7 +57,7 @@ fn fs_lefteye(in: VertexOutput) -> @location(0) vec4<f32> {
@fragment @fragment
fn fs_righteye(in: VertexOutput) -> @location(0) vec4<f32> { fn fs_righteye(in: VertexOutput) -> @location(0) vec4<f32> {
let brt = textureSample(u_texture, u_sampler, in.tex_coords); let brt = textureSample(u_texture, u_sampler, in.tex_coords);
return colors.right * brt[1]; return colors.left * brt[1];
} }
@fragment @fragment
@ -65,3 +65,15 @@ fn fs_anaglyph(in: VertexOutput) -> @location(0) vec4<f32> {
let brt = textureSample(u_texture, u_sampler, in.tex_coords); let brt = textureSample(u_texture, u_sampler, in.tex_coords);
return colors.left * brt[0] + colors.right * brt[1]; return colors.left * brt[0] + colors.right * brt[1];
} }
@fragment
fn fs_sidebyside(in: VertexOutput) -> @location(0) vec4<f32> {
var point = in.tex_coords;
point.x = (point.x * 2.0) % 1.0;
let brt = textureSample(u_texture, u_sampler, point);
if in.tex_coords.x < 0.5 {
return colors.left * brt[0];
} else {
return colors.left * brt[1];
};
}

View File

@ -90,7 +90,10 @@ impl GameWindow {
for scale in 1..=4 { for scale in 1..=4 {
let label = format!("x{scale}"); let label = format!("x{scale}");
let scale = scale as f32; let scale = scale as f32;
let dims = (384.0 * scale, 224.0 * scale + 22.0).into(); let dims = {
let Vec2 { x, y } = self.display_mode.proportions();
Vec2::new(x * scale, y * scale + 22.0)
};
if ui if ui
.selectable_button((current_dims - dims).length() < 1.0, label) .selectable_button((current_dims - dims).length() < 1.0, label)
.clicked() .clicked()
@ -102,6 +105,7 @@ impl GameWindow {
}); });
ui.menu_button("Display Mode", |ui| { ui.menu_button("Display Mode", |ui| {
let old_proportions = self.display_mode.proportions();
if ui if ui
.selectable_option(&mut self.display_mode, DisplayMode::Anaglyph, "Anaglyph") .selectable_option(&mut self.display_mode, DisplayMode::Anaglyph, "Anaglyph")
.clicked() .clicked()
@ -120,6 +124,25 @@ impl GameWindow {
{ {
ui.close_menu(); ui.close_menu();
} }
if ui
.selectable_option(
&mut self.display_mode,
DisplayMode::SideBySide,
"Side by Side",
)
.clicked()
{
ui.close_menu();
}
let new_proportions = self.display_mode.proportions();
let scale = new_proportions / old_proportions;
if scale != Vec2::new(1.0, 1.0) {
let current_dims = ctx.input(|i| i.viewport().inner_rect.unwrap());
let current_dims = current_dims.max - current_dims.min;
ctx.send_viewport_cmd(ViewportCommand::InnerSize(current_dims * scale));
}
}); });
ui.menu_button("Colors", |ui| { ui.menu_button("Colors", |ui| {
for preset in COLOR_PRESETS { for preset in COLOR_PRESETS {
@ -183,9 +206,10 @@ impl AppWindow for GameWindow {
} }
fn initial_viewport(&self) -> ViewportBuilder { fn initial_viewport(&self) -> ViewportBuilder {
let dimensions = self.display_mode.proportions() + Vec2::new(0.0, 22.0);
ViewportBuilder::default() ViewportBuilder::default()
.with_title("Shrooms VB") .with_title("Shrooms VB")
.with_inner_size((384.0, 246.0)) .with_inner_size(dimensions)
} }
fn show(&mut self, ctx: &Context) { fn show(&mut self, ctx: &Context) {

View File

@ -1,6 +1,6 @@
use std::{collections::HashMap, sync::Arc}; use std::{collections::HashMap, sync::Arc};
use egui::{Color32, Rgba, Widget}; use egui::{Color32, Rgba, Vec2, Widget};
use wgpu::{util::DeviceExt as _, BindGroup, BindGroupLayout, Buffer, RenderPipeline}; use wgpu::{util::DeviceExt as _, BindGroup, BindGroupLayout, Buffer, RenderPipeline};
use crate::graphics::TextureSink; use crate::graphics::TextureSink;
@ -48,7 +48,7 @@ impl GameScreen {
], ],
}); });
let shader = device.create_shader_module(wgpu::include_wgsl!("../anaglyph.wgsl")); let shader = device.create_shader_module(wgpu::include_wgsl!("../game.wgsl"));
let render_pipeline_layout = let render_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("render pipeline layout"), label: Some("render pipeline layout"),
@ -100,6 +100,10 @@ impl GameScreen {
render_pipelines.insert(DisplayMode::Anaglyph, create_render_pipeline("fs_anaglyph")); render_pipelines.insert(DisplayMode::Anaglyph, create_render_pipeline("fs_anaglyph"));
render_pipelines.insert(DisplayMode::LeftEye, create_render_pipeline("fs_lefteye")); render_pipelines.insert(DisplayMode::LeftEye, create_render_pipeline("fs_lefteye"));
render_pipelines.insert(DisplayMode::RightEye, create_render_pipeline("fs_righteye")); render_pipelines.insert(DisplayMode::RightEye, create_render_pipeline("fs_righteye"));
render_pipelines.insert(
DisplayMode::SideBySide,
create_render_pipeline("fs_sidebyside"),
);
render_state render_state
.renderer .renderer
@ -217,7 +221,10 @@ impl egui_wgpu::CallbackTrait for GameScreenCallback {
let top = viewport.top_px as f32; let top = viewport.top_px as f32;
let width = viewport.width_px as f32; let width = viewport.width_px as f32;
let height = viewport.height_px as f32; let height = viewport.height_px as f32;
let aspect_ratio = 384.0 / 224.0; let aspect_ratio = {
let proportions = self.display_mode.proportions();
proportions.x / proportions.y
};
let w = width.min(height * aspect_ratio); let w = width.min(height * aspect_ratio);
let h = height.min(width / aspect_ratio); let h = height.min(width / aspect_ratio);
let x = left + (width - w) / 2.0; let x = left + (width - w) / 2.0;
@ -258,4 +265,14 @@ pub enum DisplayMode {
Anaglyph, Anaglyph,
LeftEye, LeftEye,
RightEye, RightEye,
SideBySide,
}
impl DisplayMode {
pub fn proportions(self) -> Vec2 {
match self {
Self::SideBySide => Vec2::new(768.0, 224.0),
_ => Vec2::new(384.0, 224.0),
}
}
} }