Compare commits

..

No commits in common. "381dd4a38359bb178a0b918aa6bf9bf0a6cdedeb" and "a85a0f818873b9968a863a6f849d35deccf6b37e" have entirely different histories.

13 changed files with 76 additions and 271 deletions

View File

@ -4,7 +4,7 @@
- Sound system
- Particles, impact effects
- Debris on ship death
- Radar: ship size, planets and suns
- Radar: dynamic colors, size, planets and suns
----------------------------------

2
assets

@ -1 +1 @@
Subproject commit 62fd7fc297a2631691b588b898694e220d58cf78
Subproject commit 91377416617d049acec68bb5a17647dcac7acd0e

View File

@ -1,9 +1,7 @@
[faction."player"]
display_name = "Player"
relationship.enemy = "hostile"
color = [0.0, 1.0, 0.0]
[faction."enemy"]
display_name = "Enemy"
relationship.player = "hostile"
color = [1.0, 0.0, 0.0]

View File

@ -44,11 +44,8 @@ pub const CONTENT_ROOT: &'static str = "./content";
/// Root directory of game textures
pub const TEXTURE_ROOT: &'static str = "./assets/render";
/// We can draw at most this many object sprites on the screen.
pub const OBJECT_SPRITE_INSTANCE_LIMIT: u64 = 500;
/// We can draw at most this many ui sprites on the screen.
pub const UI_SPRITE_INSTANCE_LIMIT: u64 = 100;
/// We can draw at most this many sprites on the screen.
pub const SPRITE_INSTANCE_LIMIT: u64 = 500;
/// Must be small enough to fit in an i32
pub const STARFIELD_SPRITE_INSTANCE_LIMIT: u64 = STARFIELD_COUNT * 24;
pub const STARFIELD_INSTANCE_LIMIT: u64 = STARFIELD_COUNT * 24;

View File

@ -14,7 +14,6 @@ pub(crate) mod syntax {
#[derive(Debug, Deserialize)]
pub struct Faction {
pub display_name: String,
pub color: [f32; 3],
pub relationship: HashMap<String, super::Relationship>,
}
}
@ -46,10 +45,6 @@ pub struct Faction {
/// The name of this faction
pub name: String,
/// This faction's color.
/// Format is RGB, with each color between 0 and 1.
pub color: [f32; 3],
/// This faction's handle
pub handle: FactionHandle,
@ -103,7 +98,6 @@ impl crate::Build for Faction {
name: faction_name.to_owned(),
handle: h,
relationships,
color: faction.color,
});
}

View File

@ -1,75 +0,0 @@
struct InstanceInput {
@location(2) transform_matrix_0: vec4<f32>,
@location(3) transform_matrix_1: vec4<f32>,
@location(4) transform_matrix_2: vec4<f32>,
@location(5) transform_matrix_3: vec4<f32>,
@location(6) color_transform: vec4<f32>,
@location(7) texture_idx: u32,
};
struct VertexInput {
@location(0) position: vec3<f32>,
@location(1) texture_coords: vec2<f32>,
}
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) texture_coords: vec2<f32>,
@location(1) index: u32,
@location(2) color_transform: vec4<f32>,
}
@group(1) @binding(0)
var<uniform> global: GlobalUniform;
struct GlobalUniform {
camera_position: vec2<f32>,
camera_zoom: vec2<f32>,
camera_zoom_limits: vec2<f32>,
window_size: vec2<f32>,
window_aspect: vec2<f32>,
starfield_texture: vec2<u32>,
starfield_tile_size: vec2<f32>,
starfield_size_limits: vec2<f32>,
};
@group(0) @binding(0)
var texture_array: binding_array<texture_2d<f32>>;
@group(0) @binding(1)
var sampler_array: binding_array<sampler>;
@vertex
fn vertex_main(
vertex: VertexInput,
instance: InstanceInput,
) -> VertexOutput {
let transform = mat4x4<f32>(
instance.transform_matrix_0,
instance.transform_matrix_1,
instance.transform_matrix_2,
instance.transform_matrix_3,
);
var out: VertexOutput;
out.position = transform * vec4<f32>(vertex.position, 1.0);
out.texture_coords = vertex.texture_coords;
out.index = instance.texture_idx;
out.color_transform = instance.color_transform;
return out;
}
@fragment
fn fragment_main(in: VertexOutput) -> @location(0) vec4<f32> {
return textureSampleLevel(
texture_array[in.index],
sampler_array[0],
in.texture_coords,
0.0
).rgba * in.color_transform;
}

View File

@ -15,7 +15,7 @@ use crate::{
texturearray::TextureArray,
vertexbuffer::{
consts::{SPRITE_INDICES, SPRITE_VERTICES},
types::{ObjectInstance, StarfieldInstance, TexturedVertex, UiInstance},
types::{SpriteInstance, StarfieldInstance, TexturedVertex},
VertexBuffer,
},
ObjectSprite, UiSprite, OPENGL_TO_WGPU_MATRIX,
@ -37,9 +37,8 @@ pub struct GPUState {
window_aspect: f32,
object_pipeline: wgpu::RenderPipeline,
sprite_pipeline: wgpu::RenderPipeline,
starfield_pipeline: wgpu::RenderPipeline,
ui_pipeline: wgpu::RenderPipeline,
starfield: Starfield,
texture_array: TextureArray,
@ -48,9 +47,8 @@ pub struct GPUState {
}
struct VertexBuffers {
object: Rc<VertexBuffer>,
sprite: Rc<VertexBuffer>,
starfield: Rc<VertexBuffer>,
ui: Rc<VertexBuffer>,
}
impl GPUState {
@ -119,12 +117,12 @@ impl GPUState {
}
let vertex_buffers = VertexBuffers {
object: Rc::new(VertexBuffer::new::<TexturedVertex, ObjectInstance>(
"objecte",
sprite: Rc::new(VertexBuffer::new::<TexturedVertex, SpriteInstance>(
"sprite",
&device,
Some(SPRITE_VERTICES),
Some(SPRITE_INDICES),
galactica_constants::OBJECT_SPRITE_INSTANCE_LIMIT,
galactica_constants::SPRITE_INSTANCE_LIMIT,
)),
starfield: Rc::new(VertexBuffer::new::<TexturedVertex, StarfieldInstance>(
@ -132,15 +130,7 @@ impl GPUState {
&device,
Some(SPRITE_VERTICES),
Some(SPRITE_INDICES),
galactica_constants::STARFIELD_SPRITE_INSTANCE_LIMIT,
)),
ui: Rc::new(VertexBuffer::new::<TexturedVertex, UiInstance>(
"ui",
&device,
Some(SPRITE_VERTICES),
Some(SPRITE_INDICES),
galactica_constants::UI_SPRITE_INSTANCE_LIMIT,
galactica_constants::STARFIELD_INSTANCE_LIMIT,
)),
};
@ -155,15 +145,15 @@ impl GPUState {
];
// Create render pipelines
let object_pipeline = PipelineBuilder::new("object", &device)
let sprite_pipeline = PipelineBuilder::new("sprite", &device)
.set_shader(include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/shaders/",
"object.wgsl"
"sprite.wgsl"
)))
.set_format(config.format)
.set_triangle(true)
.set_vertex_buffer(&vertex_buffers.object)
.set_vertex_buffer(&vertex_buffers.sprite)
.set_bind_group_layouts(bind_group_layouts)
.build();
@ -179,18 +169,6 @@ impl GPUState {
.set_bind_group_layouts(bind_group_layouts)
.build();
let ui_pipeline = PipelineBuilder::new("ui", &device)
.set_shader(include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/shaders/",
"ui.wgsl"
)))
.set_format(config.format)
.set_triangle(true)
.set_vertex_buffer(&vertex_buffers.ui)
.set_bind_group_layouts(bind_group_layouts)
.build();
let mut starfield = Starfield::new();
starfield.regenerate();
@ -204,9 +182,8 @@ impl GPUState {
window_size,
window_aspect,
object_pipeline,
sprite_pipeline,
starfield_pipeline,
ui_pipeline,
starfield,
texture_array,
@ -234,13 +211,13 @@ impl GPUState {
self.update_starfield_buffer()
}
/// Create a ObjectInstance for an object and add it to `instances`.
/// Create a SpriteInstance for an object and add it to `instances`.
/// Also handles child sprites.
fn push_object_sprite(
&self,
camera_zoom: f32,
camera_pos: Point2<f32>,
instances: &mut Vec<ObjectInstance>,
instances: &mut Vec<SpriteInstance>,
clip_ne: Point2<f32>,
clip_sw: Point2<f32>,
s: &ObjectSprite,
@ -314,7 +291,7 @@ impl GPUState {
let t =
OPENGL_TO_WGPU_MATRIX * translate * screen_aspect * rotate * sprite_aspect_and_scale;
instances.push(ObjectInstance {
instances.push(SpriteInstance {
transform: t.into(),
texture_index: texture.index,
});
@ -332,7 +309,7 @@ impl GPUState {
fn push_object_subsprite(
&self,
camera_zoom: f32,
instances: &mut Vec<ObjectInstance>,
instances: &mut Vec<SpriteInstance>,
s: &ObjectSubSprite,
parent_pos: Point2<f32>,
parent_angle: Deg<f32>,
@ -364,14 +341,14 @@ impl GPUState {
* protate * translate
* rotate * sprite_aspect_and_scale;
instances.push(ObjectInstance {
instances.push(SpriteInstance {
transform: t.into(),
texture_index: texture.index,
});
}
/// Create a ObjectInstance for a ui sprite and add it to `instances`
fn push_ui_sprite(&self, instances: &mut Vec<UiInstance>, s: &UiSprite) {
/// Create a SpriteInstance for a ui sprite and add it to `instances`
fn push_ui_sprite(&self, instances: &mut Vec<SpriteInstance>, s: &UiSprite) {
let logical_size: LogicalSize<f32> =
self.window_size.to_logical(self.window.scale_factor());
@ -417,24 +394,24 @@ impl GPUState {
});
let screen_aspect = Matrix4::from_nonuniform_scale(1.0 / self.window_aspect, 1.0, 1.0);
instances.push(UiInstance {
instances.push(SpriteInstance {
transform: (OPENGL_TO_WGPU_MATRIX * translate * screen_aspect * rotate * scale).into(),
texture_index: texture.index,
color: s.color.unwrap_or([1.0, 1.0, 1.0, 1.0]),
});
}
/// Make an instance for all the game's sprites
/// (Objects and UI)
/// This will Will panic if any X_SPRITE_INSTANCE_LIMIT is exceeded.
fn update_sprite_instances(
/// Make a SpriteInstance for each of the game's visible sprites.
/// Will panic if SPRITE_INSTANCE_LIMIT is exceeded.
///
/// This is only called inside self.render()
fn make_sprite_instances(
&self,
camera_zoom: f32,
camera_pos: Point2<f32>,
objects: &Vec<ObjectSprite>,
ui: &Vec<UiSprite>,
) -> (usize, usize) {
let mut object_instances: Vec<ObjectInstance> = Vec::new();
) -> Vec<SpriteInstance> {
let mut instances: Vec<SpriteInstance> = Vec::new();
// Game coordinates (relative to camera) of ne and sw corners of screen.
// Used to skip off-screen sprites.
@ -442,45 +419,20 @@ impl GPUState {
let clip_sw = Point2::from((self.window_aspect, -1.0)) * camera_zoom;
for s in objects {
self.push_object_sprite(
camera_zoom,
camera_pos,
&mut object_instances,
clip_ne,
clip_sw,
s,
);
self.push_object_sprite(camera_zoom, camera_pos, &mut instances, clip_ne, clip_sw, s);
}
for s in ui {
self.push_ui_sprite(&mut instances, s);
}
// Enforce sprite limit
if object_instances.len() as u64 > galactica_constants::OBJECT_SPRITE_INSTANCE_LIMIT {
if instances.len() as u64 > galactica_constants::SPRITE_INSTANCE_LIMIT {
// TODO: no panic, handle this better.
panic!("Sprite limit exceeded!")
}
self.queue.write_buffer(
&self.vertex_buffers.object.instances,
0,
bytemuck::cast_slice(&object_instances),
);
let mut ui_instances: Vec<UiInstance> = Vec::new();
for s in ui {
self.push_ui_sprite(&mut ui_instances, s);
}
if ui_instances.len() as u64 > galactica_constants::UI_SPRITE_INSTANCE_LIMIT {
panic!("Ui sprite limit exceeded!")
}
self.queue.write_buffer(
&self.vertex_buffers.ui.instances,
0,
bytemuck::cast_slice(&ui_instances),
);
return (object_instances.len(), ui_instances.len());
return instances;
}
/// Make a StarfieldInstance for each star that needs to be drawn.
@ -558,8 +510,13 @@ impl GPUState {
);
// Create sprite instances
let (n_object, n_ui) =
self.update_sprite_instances(camera_zoom, camera_pos, object_sprites, ui_sprites);
let sprite_instances =
self.make_sprite_instances(camera_zoom, camera_pos, object_sprites, ui_sprites);
self.queue.write_buffer(
&self.vertex_buffers.sprite.instances,
0,
bytemuck::cast_slice(&sprite_instances),
);
// These should match the indices in each shader,
// and should each have a corresponding bind group layout.
@ -576,14 +533,13 @@ impl GPUState {
);
// Sprite pipeline
self.vertex_buffers.object.set_in_pass(&mut render_pass);
render_pass.set_pipeline(&self.object_pipeline);
render_pass.draw_indexed(0..SPRITE_INDICES.len() as u32, 0, 0..n_object as _);
// Ui pipeline
self.vertex_buffers.ui.set_in_pass(&mut render_pass);
render_pass.set_pipeline(&self.ui_pipeline);
render_pass.draw_indexed(0..SPRITE_INDICES.len() as u32, 0, 0..n_ui as _);
self.vertex_buffers.sprite.set_in_pass(&mut render_pass);
render_pass.set_pipeline(&self.sprite_pipeline);
render_pass.draw_indexed(
0..SPRITE_INDICES.len() as u32,
0,
0..sprite_instances.len() as _,
);
// begin_render_pass borrows encoder mutably, so we can't call finish()
// without dropping this variable.

View File

@ -38,10 +38,6 @@ pub struct UiSprite {
/// This object's position, in logical (dpi-adjusted) pixels
pub pos: AnchoredUiPosition,
/// This sprite's color will be multiplied by this value.
/// If this is None, color will not be changed.
pub color: Option<[f32; 4]>,
/// The size of this sprite, in logical (dpi-adjusted) pixels
pub dimensions: Point2<f32>,

View File

@ -94,7 +94,7 @@ impl Starfield {
while ((nw_tile.x * 2 + 1)
* (nw_tile.y * 2 + 1)
* galactica_constants::STARFIELD_COUNT as i32)
> galactica_constants::STARFIELD_SPRITE_INSTANCE_LIMIT as i32
> galactica_constants::STARFIELD_INSTANCE_LIMIT as i32
{
nw_tile -= Vector2::from((1, 1));
}
@ -119,7 +119,7 @@ impl Starfield {
}
// Enforce starfield limit
if instances.len() as u64 > galactica_constants::STARFIELD_SPRITE_INSTANCE_LIMIT {
if instances.len() as u64 > galactica_constants::STARFIELD_INSTANCE_LIMIT {
unreachable!("Starfield limit exceeded!")
}
self.instance_count = instances.len() as u32;

View File

@ -81,18 +81,19 @@ impl BufferObject for StarfieldInstance {
}
}
// Represents a sprite instance in WGSL
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct ObjectInstance {
pub struct SpriteInstance {
/// Extra transformations this sprite
/// (rotation, etc)
pub transform: [[f32; 4]; 4],
/// What texture to use for this sprite
// What texture to use for this sprite
pub texture_index: u32,
}
impl BufferObject for ObjectInstance {
impl BufferObject for SpriteInstance {
fn layout() -> wgpu::VertexBufferLayout<'static> {
wgpu::VertexBufferLayout {
array_stride: Self::SIZE,
@ -132,66 +133,3 @@ impl BufferObject for ObjectInstance {
}
}
}
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct UiInstance {
/// Extra transformations this sprite
/// (rotation, etc)
pub transform: [[f32; 4]; 4],
/// This lets us color ui sprites dynamically.
/// Each fragment's color is multiplied by this value.
/// Fill this array with ones if no recoloring should be done.
pub color: [f32; 4],
/// What texture to use for this sprite
pub texture_index: u32,
}
impl BufferObject for UiInstance {
fn layout() -> wgpu::VertexBufferLayout<'static> {
wgpu::VertexBufferLayout {
array_stride: Self::SIZE,
// Switch to a step mode of Vertex to Instance.
// This means that our shaders will only change to use the next
// instance when the shader starts processing a new instance
step_mode: wgpu::VertexStepMode::Instance,
attributes: &[
// 4 arrays = 1 4x4 matrix
wgpu::VertexAttribute {
offset: 0,
shader_location: 2,
format: wgpu::VertexFormat::Float32x4,
},
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 4]>() as wgpu::BufferAddress,
shader_location: 3,
format: wgpu::VertexFormat::Float32x4,
},
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 8]>() as wgpu::BufferAddress,
shader_location: 4,
format: wgpu::VertexFormat::Float32x4,
},
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 12]>() as wgpu::BufferAddress,
shader_location: 5,
format: wgpu::VertexFormat::Float32x4,
},
// Color
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 16]>() as wgpu::BufferAddress,
shader_location: 6,
format: wgpu::VertexFormat::Float32x4,
},
// Texture
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 20]>() as wgpu::BufferAddress,
shader_location: 7,
format: wgpu::VertexFormat::Uint32,
},
],
}
}
}

View File

@ -24,7 +24,6 @@ pub fn build_radar(
y: radar_size,
},
angle: Deg(0.0),
color: None,
});
// Draw viewport frame
@ -34,7 +33,7 @@ pub fn build_radar(
};
let m = d.magnitude() / radar_range;
let d = d / radar_range * (radar_size / 2.0);
let color = Some([0.5, 0.5, 0.5, 1.0]);
println!("{:?}", d);
if m < 0.8 {
let texture = ct.get_texture_handle("ui::radarframe");
let dimensions = Point2 {
@ -49,7 +48,6 @@ pub fn build_radar(
}),
dimensions,
angle: Deg(0.0),
color,
});
out.push(UiSprite {
@ -60,7 +58,6 @@ pub fn build_radar(
}),
dimensions,
angle: Deg(90.0),
color,
});
out.push(UiSprite {
@ -71,7 +68,6 @@ pub fn build_radar(
}),
dimensions,
angle: Deg(180.0),
color,
});
out.push(UiSprite {
@ -82,7 +78,6 @@ pub fn build_radar(
}),
dimensions,
angle: Deg(270.0),
color,
});
}
@ -93,13 +88,23 @@ pub fn build_radar(
let p = util::rigidbody_position(r);
let d = p - pr;
let m = d.magnitude() / radar_range;
if m < 0.8 {
let angle: Deg<f32> = util::rigidbody_rotation(r)
.angle(Vector2 { x: 0.0, y: 1.0 })
.into();
let f = ct.get_faction(s.ship.faction).color;
let f = [f[0], f[1], f[2], 1.0];
if s.physics_handle == *player {
out.push(UiSprite {
texture,
pos: AnchoredUiPosition::NwC(Point2 {
x: radar_size / 2.0 + 10.0,
y: radar_size / -2.0 - 10.0,
}),
dimensions: Point2 {
x: texture.aspect,
y: 1.0,
} * 5.0f32.min((0.8 - m) * 50.0),
angle: -angle,
});
} else if m < 0.8 {
out.push(UiSprite {
texture,
pos: AnchoredUiPosition::NwC(
@ -113,7 +118,6 @@ pub fn build_radar(
y: 1.0,
} * 5.0f32.min((0.8 - m) * 50.0),
angle: -angle,
color: Some(f),
});
}
}

View File

@ -49,9 +49,6 @@ impl Game {
let s = object::Ship::new(
&ct,
content::ShipHandle { index: 0 },
// This method of specifying factions is non-deterministic,
// but that's ok since this is for debug.
// TODO: fix
content::FactionHandle { index: 0 },
object::OutfitSet::new(ss),
);