Removed old anchor code

master
Mark 2024-02-03 12:46:24 -08:00
parent 193feb5b92
commit 6b4c80ce0f
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
9 changed files with 48 additions and 206 deletions

View File

@ -1,51 +0,0 @@
// Given an anchored position and sprite dimensions,
// return the translation that should be applied on
// vertex coordinates
fn anchor(
anchor: u32, // Anchor index
position: vec2<f32>, // Anchored position
dim: vec2<f32>, // Sprite dimensions (width, height)
) -> vec2<f32> {
// TODO: remove, do in ui rust lib
var trans: vec2<f32> = vec2(0.0, 0.0);
let window_dim = (
vec2(global_data.window_size_w, global_data.window_size_h) /
global_data.window_scale
);
if anchor == 0u { // NW C (screen anchor, sprite anchor)
trans += vec2(-window_dim.x, window_dim.y) / 2.0; // origin
trans += vec2(0.0, 0.0) / 2.0; // offset
} else if anchor == 1u { // NW NW
trans += vec2(-window_dim.x, window_dim.y) / 2.0;
trans += vec2(dim.x, -dim.y) / 2.0;
} else if anchor == 2u { // NW NE
trans += vec2(-window_dim.x, window_dim.y) / 2.0;
trans += vec2(-dim.x, -dim.y) / 2.0;
} else if anchor == 3u { // NW SW
trans += vec2(-window_dim.x, window_dim.y) / 2.0;
trans += vec2(dim.x, dim.y) / 2.0;
} else if anchor == 4u { // NW SE
trans += vec2(-window_dim.x, window_dim.y) / 2.0;
trans += vec2(-dim.x, dim.y) / 2.0;
} else if anchor == 5u { // NE NE
trans += vec2(window_dim.x, window_dim.y) / 2.0;
trans += vec2(-dim.x, -dim.y) / 2.0;
} else if anchor == 6u { // C C
trans += vec2(0.0, 0.0) / 2.0;
trans += vec2(0.0, 0.0) / 2.0;
} else if anchor == 7u { // C NW
trans += vec2(0.0, 0.0) / 2.0;
trans += vec2(dim.x, -dim.y) / 2.0;
} else { // center / center as default, since it's the most visible variant.
trans += vec2(0.0, 0.0) / 2.0;
trans += vec2(0.0, 0.0) / 2.0;
}
trans += position;
// This renders correctly, but the offsets here are off by a factor of two.
// I'm not sure why... might be because WGPU screen coordinates are -1 to 1.
return (trans / window_dim) * 2.0;
}

View File

@ -1,12 +1,11 @@
// INCLUDE: global uniform header
struct InstanceInput {
@location(2) anchor: u32,
@location(3) position: vec2<f32>,
@location(4) diameter: f32,
@location(5) stroke: f32,
@location(6) angle: f32,
@location(7) color: vec4<f32>,
@location(2) position: vec2<f32>,
@location(3) diameter: f32,
@location(4) stroke: f32,
@location(5) angle: f32,
@location(6) color: vec4<f32>,
};
struct VertexInput {
@ -29,9 +28,6 @@ var texture_array: binding_array<texture_2d<f32>>;
@group(0) @binding(1)
var sampler_array: binding_array<sampler>;
// INCLUDE: anchor.wgsl
@vertex
fn vertex_main(
vertex: VertexInput,
@ -45,17 +41,18 @@ fn vertex_main(
out.color = instance.color;
out.angle = instance.angle;
// Center of this radial bar, in logical pixels,
// with (0, 0) at the center of the screen.
out.center = anchor(
instance.anchor,
instance.position,
vec2(instance.diameter, instance.diameter)
) / 2.0 * (
let window_dim = (
vec2(global_data.window_size_w, global_data.window_size_h) /
global_data.window_scale
);
// Center of this radial bar, in logical pixels,
// with (0, 0) at the center of the screen.
out.center = (instance.position / window_dim) * (
vec2(global_data.window_size_w, global_data.window_size_h) /
global_data.window_scale
);
// ^ slight correction, since anchor gives us a different result than we need here
return out;
}

View File

@ -1,14 +1,13 @@
// INCLUDE: global uniform header
struct InstanceInput {
@location(2) anchor: u32,
@location(3) position: vec2<f32>,
@location(4) angle: f32,
@location(5) dim: vec2<f32>,
@location(6) color_transform: vec4<f32>,
@location(7) texture_index: vec2<u32>,
@location(8) texture_fade: f32,
@location(9) mask_index: vec2<u32>,
@location(2) position: vec2<f32>,
@location(3) angle: f32,
@location(4) dim: vec2<f32>,
@location(5) color_transform: vec4<f32>,
@location(6) texture_index: vec2<u32>,
@location(7) texture_fade: f32,
@location(8) mask_index: vec2<u32>,
};
struct VertexInput {
@ -34,10 +33,6 @@ var texture_array: binding_array<texture_2d<f32>>;
@group(0) @binding(1)
var sampler_array: binding_array<sampler>;
// INCLUDE: anchor.wgsl
fn transform_vertex(
instance: InstanceInput,
vertex_position: vec3<f32>,
@ -57,24 +52,19 @@ fn transform_vertex(
vertex_position.y * scale
);
// Apply rotation (and adjust sprite angle, since sprites point north)
pos = mat2x2(
vec2(cos(instance.angle - 1.5708), sin(instance.angle - 1.5708)),
vec2(-sin(instance.angle - 1.5708), cos(instance.angle - 1.5708))
) * pos;
// Apply rotation (and adjust sprite angle, since sprites point north)
// Correct for screen aspect, preserving height
pos = vec2(
pos.x / global_data.window_aspect,
pos.y
);
pos = pos + anchor(
instance.anchor,
instance.position,
instance.dim
);
pos = pos + (instance.position / window_dim) * 2.0;
return vec4<f32>(pos, 0.0, 1.0);
}

View File

@ -10,7 +10,6 @@
mod globaluniform;
mod gpustate;
mod pipeline;
mod positionanchor;
mod renderinput;
mod renderstate;
mod shaderprocessor;
@ -20,7 +19,6 @@ mod ui;
mod vertexbuffer;
pub use gpustate::GPUState;
pub use positionanchor::PositionAnchor;
pub use renderinput::RenderInput;
use renderstate::*;

View File

@ -1,57 +0,0 @@
/// The location of a UI element, in one of a few
/// possible coordinate systems.
///
/// Positive Y always points up,
/// positive X always points right.
#[derive(Debug, Clone)]
pub enum PositionAnchor {
/// Position of this sprite's center,
/// relative to the nw corner of the window.
NwC,
/// Position of this sprite's nw corner,
/// relative to the nw corner of the window.
NwNw,
/// Position of this sprite's ne corner,
/// relative to the nw corner of the window.
NwNe,
/// Position of this sprite's sw corner,
/// relative to the nw corner of the window.
NwSw,
/// Position of this sprite's se corner,
/// relative to the nw corner of the window.
NwSe,
/// Position of this sprite's ne corner,
/// relative to the ne corner of the window.
NeNe,
/// Position of this sprite's center,
/// relative to the center of the window.
CC,
/// Position of this sprite's NW corner,
/// relative to the center of the window.
CNw,
}
// These offsets are implemented in wgsl shaders.
impl PositionAnchor {
/// Get the uint that represents this anchor mode in shaders
pub fn to_int(&self) -> u32 {
match self {
Self::NwC => 0,
Self::NwNw => 1,
Self::NwNe => 2,
Self::NwSw => 3,
Self::NwSe => 4,
Self::NeNe => 5,
Self::CC => 6,
Self::CNw => 7,
}
}
}

View File

@ -14,15 +14,5 @@ pub(crate) fn preprocess_shader(
&global_uniform.shader_header(global_uniform_group),
);
// Insert common functions
let shader = shader.replace(
"// INCLUDE: anchor.wgsl",
&include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/shaders/include/",
"anchor.wgsl"
)),
);
return shader;
}

View File

@ -2,10 +2,7 @@ use galactica_content::Content;
use std::f32::consts::TAU;
use super::super::api::Rect;
use crate::{
ui::api::Color, vertexbuffer::types::RadialBarInstance, PositionAnchor, RenderInput,
RenderState,
};
use crate::{ui::api::Color, vertexbuffer::types::RadialBarInstance, RenderInput, RenderState};
#[derive(Debug, Clone)]
pub struct RadialBar {
@ -39,7 +36,6 @@ impl RadialBar {
state.push_radialbar_buffer(RadialBarInstance {
position: [rect.pos.x, rect.pos.y],
anchor: PositionAnchor::CC.to_int(),
diameter: rect.dim.x.min(rect.dim.y),
stroke: self.stroke * input.ct.get_config().ui_scale,
color: self.color.as_array(),

View File

@ -53,7 +53,6 @@ impl Sprite {
let anim_state = self.anim.get_texture_idx();
state.push_ui_buffer(UiInstance {
anchor: crate::PositionAnchor::CC.to_int(),
position: rect.pos.into(),
angle: to_radians(90.0),
dim: rect.dim.into(),

View File

@ -143,10 +143,6 @@ impl BufferObject for ObjectInstance {
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct UiInstance {
/// How to interpret this object's coordinates.
/// this should be generated from an AnchoredUiPosition
pub anchor: u32,
/// Position of this object in logical pixels
pub position: [f32; 2],
@ -183,52 +179,46 @@ impl BufferObject for UiInstance {
// instance when the shader starts processing a new instance
step_mode: wgpu::VertexStepMode::Instance,
attributes: &[
// Anchor
wgpu::VertexAttribute {
offset: 0,
shader_location: 2,
format: wgpu::VertexFormat::Uint32,
},
// Position
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 1]>() as wgpu::BufferAddress,
shader_location: 3,
offset: mem::size_of::<[f32; 0]>() as wgpu::BufferAddress,
shader_location: 2,
format: wgpu::VertexFormat::Float32x2,
},
// Angle
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
shader_location: 4,
offset: mem::size_of::<[f32; 2]>() as wgpu::BufferAddress,
shader_location: 3,
format: wgpu::VertexFormat::Float32,
},
// Dim
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 4]>() as wgpu::BufferAddress,
shader_location: 5,
offset: mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
shader_location: 4,
format: wgpu::VertexFormat::Float32x2,
},
// Color
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 6]>() as wgpu::BufferAddress,
shader_location: 6,
offset: mem::size_of::<[f32; 5]>() as wgpu::BufferAddress,
shader_location: 5,
format: wgpu::VertexFormat::Float32x4,
},
// Texture
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 10]>() as wgpu::BufferAddress,
shader_location: 7,
offset: mem::size_of::<[f32; 9]>() as wgpu::BufferAddress,
shader_location: 6,
format: wgpu::VertexFormat::Uint32x2,
},
// Texture fade
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 12]>() as wgpu::BufferAddress,
shader_location: 8,
offset: mem::size_of::<[f32; 11]>() as wgpu::BufferAddress,
shader_location: 7,
format: wgpu::VertexFormat::Float32,
},
// Mask
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 13]>() as wgpu::BufferAddress,
shader_location: 9,
offset: mem::size_of::<[f32; 12]>() as wgpu::BufferAddress,
shader_location: 8,
format: wgpu::VertexFormat::Uint32x2,
},
],
@ -239,10 +229,6 @@ impl BufferObject for UiInstance {
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct RadialBarInstance {
/// How to interpret this object's coordinates.
/// this should be generated from an AnchoredUiPosition
pub anchor: u32,
/// Position of this object in logical pixels
pub position: [f32; 2],
@ -267,40 +253,34 @@ impl BufferObject for RadialBarInstance {
array_stride: Self::SIZE,
step_mode: wgpu::VertexStepMode::Instance,
attributes: &[
// Anchor
wgpu::VertexAttribute {
offset: 0,
shader_location: 2,
format: wgpu::VertexFormat::Uint32,
},
// Position
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 1]>() as wgpu::BufferAddress,
shader_location: 3,
offset: mem::size_of::<[f32; 0]>() as wgpu::BufferAddress,
shader_location: 2,
format: wgpu::VertexFormat::Float32x2,
},
// Diameter
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 2]>() as wgpu::BufferAddress,
shader_location: 3,
format: wgpu::VertexFormat::Float32,
},
// Width
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
shader_location: 4,
format: wgpu::VertexFormat::Float32,
},
// Width
// Angle
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 4]>() as wgpu::BufferAddress,
shader_location: 5,
format: wgpu::VertexFormat::Float32,
},
// Angle
// Color
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 5]>() as wgpu::BufferAddress,
shader_location: 6,
format: wgpu::VertexFormat::Float32,
},
// Color
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 6]>() as wgpu::BufferAddress,
shader_location: 7,
format: wgpu::VertexFormat::Float32x4,
},
],