diff options
Diffstat (limited to 'src')
27 files changed, 473 insertions, 0 deletions
diff --git a/src/helpers.js b/src/helpers.js new file mode 100644 index 0000000..20b86f4 --- /dev/null +++ b/src/helpers.js @@ -0,0 +1,13 @@ +let resized = false; + +export async function js_track_resized_setup() { + window.addEventListener("resize", () => { + resized = true; + }); +}; + +export function js_poll_resized() { + let ret = resized; + resized = false; + return ret; +} diff --git a/src/index.css b/src/index.css new file mode 100644 index 0000000..80de9e4 --- /dev/null +++ b/src/index.css @@ -0,0 +1,46 @@ +html { + /* Remove touch delay: */ + touch-action: manipulation; +} + +body { + /* Light mode background color for what is not covered by the egui canvas, + or where the egui canvas is translucent. */ + background: #000000; +} + +/* Allow canvas to fill entire web page: */ +html, +body { + overflow: hidden; + margin: 0 !important; + padding: 0 !important; + height: 100%; + width: 100%; +} + +/* Position canvas in center-top: */ +canvas { + margin-right: auto; + margin-left: auto; + display: block; + position: absolute; + top: 0%; + left: 50%; + transform: translate(-50%, 0%); + color: #000000; +} + +.centered { + margin-right: auto; + margin-left: auto; + display: block; + position: absolute; + top: 50%; + left: 50%; + transform: translate(-50%, -50%); + font-size: 24px; + font-family: Ubuntu-Light, Helvetica, sans-serif; + text-align: center; + image-transform: pixelated; +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..de8c271 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,10 @@ +mod newton; + +#[cfg(target_arch = "wasm32")] +use wasm_bindgen::prelude::*; + +#[cfg(target_arch = "wasm32")] +#[wasm_bindgen] +pub async fn main_js() { + teleia::run(240, 160, newton::client::Game::new).await; +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..92a527b --- /dev/null +++ b/src/main.rs @@ -0,0 +1,35 @@ +mod newton; + +#[cfg(not(target_arch = "wasm32"))] +use clap::{command, Command}; + +#[cfg(target_arch = "wasm32")] +pub fn main() {} // dummy main, real wasm32 main is lib::main_js + +#[cfg(not(target_arch = "wasm32"))] +#[tokio::main] +pub async fn main() { + let matches = command!() + .propagate_version(true) + .subcommand_required(true) + .arg_required_else_help(true) + .subcommand( + Command::new("overlay") + .about("Run the LCOLONQ model renderer / overlay") + ) + .subcommand( + Command::new("server") + .about("Run the LCOLONQ online websocket server") + ) + .get_matches(); + match matches.subcommand() { + Some(("overlay", _cm)) => { + teleia::run("LCOLONQ", 1920, 1080, newton::overlay::Overlay::new).await; + }, + Some(("server", _cm)) => { + env_logger::Builder::new().filter(None, log::LevelFilter::Info).init(); + log::info!("starting LCOLONQ server..."); + }, + _ => unreachable!("no subcommand"), + } +} diff --git a/src/newton.rs b/src/newton.rs new file mode 100644 index 0000000..3599211 --- /dev/null +++ b/src/newton.rs @@ -0,0 +1,8 @@ +#[cfg(target_arch = "wasm32")] +pub mod client; + +#[cfg(not(target_arch = "wasm32"))] +pub mod overlay; + +#[cfg(not(target_arch = "wasm32"))] +pub mod server; diff --git a/src/newton/client.rs b/src/newton/client.rs new file mode 100644 index 0000000..69ac6b3 --- /dev/null +++ b/src/newton/client.rs @@ -0,0 +1,48 @@ +#![allow(dead_code, unused_variables)] +mod assets; + +use std::collections::HashMap; +use teleia::*; + +pub struct Game { + assets: assets::Assets, +} + +impl Game { + pub async fn new(ctx: &context::Context) -> Self { + Self { + assets: assets::Assets::new(ctx), + } + } +} + +impl teleia::state::Game for Game { + fn initialize_audio(&self, ctx: &context::Context, st: &state::State, actx: &audio::Context) -> HashMap<String, audio::Audio> { + HashMap::from_iter(vec![ + ("test".to_owned(), audio::Audio::new(&actx, include_bytes!("client/assets/audio/test.wav"))), + ]) + } + fn finish_title(&mut self, _st: &mut state::State) {} + fn mouse_press(&mut self, _ctx: &context::Context, _st: &mut state::State) {} + fn mouse_move(&mut self, _ctx: &context::Context, _st: &mut state::State, _x: i32, _y: i32) {} + fn update(&mut self, ctx: &context::Context, _st: &mut state::State) -> Option<()> { + Some(()) + } + fn render(&mut self, ctx: &context::Context, st: &mut state::State) -> Option<()> { + ctx.clear(); + self.assets.font.render_text( + ctx, + &glam::Vec2::new(0.0, 0.0), + "hello computer", + ); + st.bind_2d(ctx, &self.assets.shader_flat); + self.assets.texture_test.bind(ctx); + self.assets.shader_flat.set_position_2d( + ctx, + &glam::Vec2::new(40.0, 40.0), + &glam::Vec2::new(16.0, 16.0), + ); + self.assets.mesh_square.render(ctx); + Some(()) + } +} diff --git a/src/newton/client/assets.rs b/src/newton/client/assets.rs new file mode 100644 index 0000000..742f9ea --- /dev/null +++ b/src/newton/client/assets.rs @@ -0,0 +1,23 @@ +use teleia::*; + +pub struct Assets { + pub font: font::Bitmap, + pub shader_flat: shader::Shader, + pub mesh_square: mesh::Mesh, + pub texture_test: texture::Texture, +} + +impl Assets { + pub fn new(ctx: &context::Context) -> Self { + Self { + font: font::Bitmap::new(ctx), + shader_flat: shader::Shader::new( + ctx, + include_str!("assets/shaders/flat/vert.glsl"), + include_str!("assets/shaders/flat/frag.glsl"), + ), + mesh_square: mesh::Mesh::from_obj(ctx, include_bytes!("assets/meshes/square.obj")), + texture_test: texture::Texture::new(ctx, include_bytes!("assets/textures/test.png")), + } + } +} diff --git a/src/newton/client/assets/audio/test.wav b/src/newton/client/assets/audio/test.wav Binary files differnew file mode 100644 index 0000000..0eabe85 --- /dev/null +++ b/src/newton/client/assets/audio/test.wav diff --git a/src/newton/client/assets/meshes/square.obj b/src/newton/client/assets/meshes/square.obj new file mode 100644 index 0000000..7328a6c --- /dev/null +++ b/src/newton/client/assets/meshes/square.obj @@ -0,0 +1,15 @@ +# Blender 3.6.2 +# www.blender.org +o Cube +v -1.000000 -1.000000 0.000000 +v 1.000000 -1.000000 0.000000 +v -1.000000 1.000000 0.000000 +v 1.000000 1.000000 0.000000 +vn -0.0000 -0.0000 -1.0000 +vt 0.0 1.0 +vt 1.0 0.0 +vt 0.0 0.0 +vt 1.0 1.0 +s 0 +f 3/1/1 2/2/1 1/3/1 +f 3/1/1 4/4/1 2/2/1 diff --git a/src/newton/client/assets/shaders/flat/frag.glsl b/src/newton/client/assets/shaders/flat/frag.glsl new file mode 100644 index 0000000..7006a2b --- /dev/null +++ b/src/newton/client/assets/shaders/flat/frag.glsl @@ -0,0 +1,11 @@ +uniform sampler2D texture_data; + +void main() +{ + vec2 tcfull = vec2(vertex_texcoord.x, 1.0 - vertex_texcoord.y); + vec4 texel = texture(texture_data, tcfull); + if (texel.a != 1.0) { + discard; + } + frag_color = texel; +} diff --git a/src/newton/client/assets/shaders/flat/vert.glsl b/src/newton/client/assets/shaders/flat/vert.glsl new file mode 100644 index 0000000..e324f7e --- /dev/null +++ b/src/newton/client/assets/shaders/flat/vert.glsl @@ -0,0 +1,4 @@ +void main() +{ + default_main(); +}
\ No newline at end of file diff --git a/src/newton/client/assets/textures/test.png b/src/newton/client/assets/textures/test.png Binary files differnew file mode 100644 index 0000000..1f1edca --- /dev/null +++ b/src/newton/client/assets/textures/test.png diff --git a/src/newton/overlay.rs b/src/newton/overlay.rs new file mode 100644 index 0000000..33b96cb --- /dev/null +++ b/src/newton/overlay.rs @@ -0,0 +1,76 @@ +#![allow(dead_code, unused_variables)] +mod assets; +mod terminal; + +use std::collections::HashMap; +use teleia::*; + +pub struct Overlay { + assets: assets::Assets, + model: scene::Scene, + model_fb: framebuffer::Framebuffer, + terminal: terminal::Terminal, +} + +impl Overlay { + pub async fn new(ctx: &context::Context) -> Self { + Self { + assets: assets::Assets::new(ctx), + model: scene::Scene::from_gltf(ctx, include_bytes!("overlay/assets/scenes/lcolonq.vrm")), + model_fb: framebuffer::Framebuffer::new( + ctx, + &glam::Vec2::new(terminal::WIDTH as _, terminal::HEIGHT as _), + &glam::Vec2::ZERO + ), + terminal: terminal::Terminal::new(ctx), + } + } +} + +impl teleia::state::Game for Overlay { + fn initialize_audio(&self, ctx: &context::Context, st: &state::State, actx: &audio::Context) -> HashMap<String, audio::Audio> { + HashMap::new() + } + fn finish_title(&mut self, _st: &mut state::State) {} + fn mouse_press(&mut self, _ctx: &context::Context, _st: &mut state::State) {} + fn mouse_move(&mut self, _ctx: &context::Context, _st: &mut state::State, _x: i32, _y: i32) {} + fn update(&mut self, ctx: &context::Context, st: &mut state::State) -> Option<()> { + st.projection = glam::Mat4::perspective_lh( + std::f32::consts::PI / 4.0, + terminal::WIDTH as f32 / terminal::HEIGHT as f32, + 0.1, 10.0 + ); + st.move_camera( + ctx, + &glam::Vec3::new(0.0, 0.0, 1.0), + &glam::Vec3::new(0.0, 0.0, -1.0), + &glam::Vec3::new(0.0, 1.0, 0.0), + ); + // if let Some(n) = self.model.nodes_by_name.get("J_Bip_C_Neck").and_then(|i| self.model.nodes.get_mut(*i)) { + // n.transform *= glam::Mat4::from_rotation_y(0.05); + // } + Some(()) + } + fn render(&mut self, ctx: &context::Context, st: &mut state::State) -> Option<()> { + self.model_fb.bind(ctx); + ctx.clear_color(glam::Vec4::new(0.0, 0.0, 0.0, 1.0)); + ctx.clear(); + st.bind_3d(ctx, &self.assets.shader_scene); + self.assets.shader_scene.set_position_3d( + ctx, + &glam::Mat4::from_translation( + glam::Vec3::new(0.0, -1.6, 0.5), + ), + ); + self.model.render(ctx, &self.assets.shader_scene); + st.render_framebuffer.bind(ctx); + // self.model_fb.blit( + // ctx, &st.render_framebuffer, + // &glam::Vec2::new(ctx.render_width / 2.0 - 512.0, ctx.render_height / 2.0 - 512.0), + // &glam::Vec2::new(1024.0, 1024.0) + // ); + self.terminal.update(ctx, &self.model_fb); + self.terminal.render(ctx, &glam::Vec2::new(400.0, 200.0)); + Some(()) + } +} diff --git a/src/newton/overlay/assets.rs b/src/newton/overlay/assets.rs new file mode 100644 index 0000000..9a0cefc --- /dev/null +++ b/src/newton/overlay/assets.rs @@ -0,0 +1,27 @@ +use teleia::*; + +pub struct Assets { + pub font: font::Bitmap, + pub shader_flat: shader::Shader, + pub shader_scene: shader::Shader, + pub mesh_square: mesh::Mesh, +} + +impl Assets { + pub fn new(ctx: &context::Context) -> Self { + Self { + font: font::Bitmap::new(ctx), + shader_flat: shader::Shader::new( + ctx, + include_str!("assets/shaders/flat/vert.glsl"), + include_str!("assets/shaders/flat/frag.glsl"), + ), + shader_scene: shader::Shader::new( + ctx, + include_str!("assets/shaders/scene/vert.glsl"), + include_str!("assets/shaders/scene/frag.glsl") + ), + mesh_square: mesh::Mesh::from_obj(ctx, include_bytes!("assets/meshes/square.obj")), + } + } +} diff --git a/src/newton/overlay/assets/audio/test.wav b/src/newton/overlay/assets/audio/test.wav Binary files differnew file mode 100644 index 0000000..0eabe85 --- /dev/null +++ b/src/newton/overlay/assets/audio/test.wav diff --git a/src/newton/overlay/assets/fonts/iosevka-comfy-regular.ttf b/src/newton/overlay/assets/fonts/iosevka-comfy-regular.ttf Binary files differnew file mode 100644 index 0000000..b474adc --- /dev/null +++ b/src/newton/overlay/assets/fonts/iosevka-comfy-regular.ttf diff --git a/src/newton/overlay/assets/fonts/terminus.png b/src/newton/overlay/assets/fonts/terminus.png Binary files differnew file mode 100644 index 0000000..287810b --- /dev/null +++ b/src/newton/overlay/assets/fonts/terminus.png diff --git a/src/newton/overlay/assets/fonts/terminus.ttf b/src/newton/overlay/assets/fonts/terminus.ttf Binary files differnew file mode 100644 index 0000000..d125e63 --- /dev/null +++ b/src/newton/overlay/assets/fonts/terminus.ttf diff --git a/src/newton/overlay/assets/meshes/square.obj b/src/newton/overlay/assets/meshes/square.obj new file mode 100644 index 0000000..7328a6c --- /dev/null +++ b/src/newton/overlay/assets/meshes/square.obj @@ -0,0 +1,15 @@ +# Blender 3.6.2 +# www.blender.org +o Cube +v -1.000000 -1.000000 0.000000 +v 1.000000 -1.000000 0.000000 +v -1.000000 1.000000 0.000000 +v 1.000000 1.000000 0.000000 +vn -0.0000 -0.0000 -1.0000 +vt 0.0 1.0 +vt 1.0 0.0 +vt 0.0 0.0 +vt 1.0 1.0 +s 0 +f 3/1/1 2/2/1 1/3/1 +f 3/1/1 4/4/1 2/2/1 diff --git a/src/newton/overlay/assets/scenes/lcolonq.vrm b/src/newton/overlay/assets/scenes/lcolonq.vrm Binary files differnew file mode 100644 index 0000000..49fa4a4 --- /dev/null +++ b/src/newton/overlay/assets/scenes/lcolonq.vrm diff --git a/src/newton/overlay/assets/shaders/flat/frag.glsl b/src/newton/overlay/assets/shaders/flat/frag.glsl new file mode 100644 index 0000000..7006a2b --- /dev/null +++ b/src/newton/overlay/assets/shaders/flat/frag.glsl @@ -0,0 +1,11 @@ +uniform sampler2D texture_data; + +void main() +{ + vec2 tcfull = vec2(vertex_texcoord.x, 1.0 - vertex_texcoord.y); + vec4 texel = texture(texture_data, tcfull); + if (texel.a != 1.0) { + discard; + } + frag_color = texel; +} diff --git a/src/newton/overlay/assets/shaders/flat/vert.glsl b/src/newton/overlay/assets/shaders/flat/vert.glsl new file mode 100644 index 0000000..e324f7e --- /dev/null +++ b/src/newton/overlay/assets/shaders/flat/vert.glsl @@ -0,0 +1,4 @@ +void main() +{ + default_main(); +}
\ No newline at end of file diff --git a/src/newton/overlay/assets/shaders/scene/frag.glsl b/src/newton/overlay/assets/shaders/scene/frag.glsl new file mode 100644 index 0000000..81e08b9 --- /dev/null +++ b/src/newton/overlay/assets/shaders/scene/frag.glsl @@ -0,0 +1,11 @@ +uniform sampler2D texture_data; + +void main() +{ + vec4 texel = texture(texture_data, vertex_texcoord); + if (texel.a != 1.0) { + discard; + } + + frag_color = vec4(texel.rgb, texel.a); +} diff --git a/src/newton/overlay/assets/shaders/scene/vert.glsl b/src/newton/overlay/assets/shaders/scene/vert.glsl new file mode 100644 index 0000000..64f400c --- /dev/null +++ b/src/newton/overlay/assets/shaders/scene/vert.glsl @@ -0,0 +1,18 @@ +in vec4 joint; +in vec4 weight; + +uniform mat4 joint_matrices[128]; + +void main() +{ + vertex_texcoord = texcoord; + vertex_normal = (normal_matrix * vec4(normal, 1.0)).xyz; + mat4 skin + = weight.x * joint_matrices[int(joint.x)] + + weight.y * joint_matrices[int(joint.y)] + + weight.z * joint_matrices[int(joint.z)] + + weight.w * joint_matrices[int(joint.w)]; + vec3 pos = (position * skin * vec4(vertex, 1.0)).xyz; + vertex_view_vector = camera_pos - pos; + gl_Position = projection * view * vec4(pos, 1.0); +} diff --git a/src/newton/overlay/assets/textures/test.png b/src/newton/overlay/assets/textures/test.png Binary files differnew file mode 100644 index 0000000..1f1edca --- /dev/null +++ b/src/newton/overlay/assets/textures/test.png diff --git a/src/newton/overlay/terminal.rs b/src/newton/overlay/terminal.rs new file mode 100644 index 0000000..3371789 --- /dev/null +++ b/src/newton/overlay/terminal.rs @@ -0,0 +1,98 @@ +use teleia::*; + +pub const WIDTH: usize = 64; +pub const HEIGHT: usize = 64; + +#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq)] +pub struct Pos { + pub x: i32, pub y: i32, +} +impl Pos { + pub fn new(x: i32, y: i32) -> Self { Self { x, y } } +} +impl std::ops::Add for Pos { + type Output = Pos; + fn add(self, rhs: Self) -> Self { Self {x: self.x + rhs.x, y: self.y + rhs.y } } +} + +pub struct Layer<T> { + pub data: [T; WIDTH * HEIGHT], +} +impl<T> Layer<T> { + pub fn new() -> Self where T: Default { + Self { + data: [(); WIDTH * HEIGHT].map(|_| T::default()), + } + } + pub fn get(&self, p: Pos) -> Option<&T> { + if p.x < 0 || p.x >= WIDTH as _ || p.y < 0 || p.y >= HEIGHT as _ { return None } + let idx = p.x as usize + p.y as usize * WIDTH; + Some(&self.data[idx]) + } +} +impl Layer<glam::Vec3> { + pub fn from_framebuffer(&mut self, ctx: &context::Context, fb: &framebuffer::Framebuffer) { + fb.get_pixels(ctx, &mut self.data); + } +} + +#[derive(Debug, Clone)] +pub struct CharPair { + pub first: char, + pub second: Option<char>, +} +impl Default for CharPair { + fn default() -> Self { + Self { + first: 'a', + second: Some('b'), + } + } +} + +pub struct Terminal { + pub font: font::Bitmap, + pub base_color: Layer<glam::Vec3>, + pub set_color: Layer<glam::Vec3>, + pub set_char: Layer<CharPair>, +} +impl Terminal { + pub fn new(ctx: &context::Context) -> Self { + Self { + font: font::Bitmap::from_image(ctx, 6, 12, 96, 72, include_bytes!("assets/fonts/terminus.png")), + base_color: Layer::new(), + set_color: Layer::new(), + set_char: Layer::new(), + } + } + pub fn get_color(&self, pos: Pos) -> glam::Vec3 { + if let Some(c) = self.base_color.get(pos) { + c.clone() + } else { + glam::Vec3::new(1.0, 1.0, 1.0) + } + } + pub fn update(&mut self, ctx: &context::Context, fb: &framebuffer::Framebuffer) { + self.base_color.from_framebuffer(ctx, fb); + } + pub fn render(&self, ctx: &context::Context, pos: &glam::Vec2) { + let mut s = String::new(); + let mut colors = Vec::new(); + for row in 0..64 { + for col in 0..64 { + let pos = Pos::new(col, row); + let new = if let Some(p) = self.set_char.get(pos) { + format!("{}{}", p.first, if let Some(snd) = p.second { snd } else { ' ' }) + } else { + String::from(" ") + }; + s += &new; + let c = self.get_color(pos); + colors.push(c); colors.push(c); + } + s += "\n"; + colors.push(glam::Vec3::new(1.0, 1.0, 1.0)); + } + self.font.render_text_helper(ctx, pos, &s, &colors); + } +} diff --git a/src/newton/server.rs b/src/newton/server.rs new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/newton/server.rs |
