Skip to content

Commit

Permalink
Physics (#81)
Browse files Browse the repository at this point in the history
BEHOLD, IT'S PHYSIC-ING
  • Loading branch information
Speykious authored Feb 12, 2024
2 parents 7f52e39 + 5abd652 commit ea6425f
Show file tree
Hide file tree
Showing 19 changed files with 721 additions and 102 deletions.
74 changes: 36 additions & 38 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
Expand All @@ -26,35 +23,35 @@
{
"type": "lldb",
"request": "launch",
"name": "Debug example 'render_opengl'",
"name": "Debug example 'parse-inp'",
"cargo": {
"args": [
"build",
"--example=render_opengl",
"--example=parse-inp",
"--package=inox2d"
],
"filter": {
"name": "render_opengl",
"name": "parse-inp",
"kind": "example"
}
},
"args": ["../Aka.inp"],
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in example 'render_opengl'",
"name": "Debug unit tests in library 'inox2d-opengl'",
"cargo": {
"args": [
"test",
"--no-run",
"--example=render_opengl",
"--package=inox2d"
"--lib",
"--package=inox2d-opengl"
],
"filter": {
"name": "render_opengl",
"kind": "example"
"name": "inox2d-opengl",
"kind": "lib"
}
},
"args": [],
Expand All @@ -63,16 +60,17 @@
{
"type": "lldb",
"request": "launch",
"name": "Debug example 'render_wgpu'",
"name": "Debug unit tests in library 'inox2d-wgpu'",
"cargo": {
"args": [
"build",
"--example=render_wgpu",
"--package=inox2d"
"test",
"--no-run",
"--lib",
"--package=inox2d-wgpu"
],
"filter": {
"name": "render_wgpu",
"kind": "example"
"name": "inox2d-wgpu",
"kind": "lib"
}
},
"args": [],
Expand All @@ -81,35 +79,36 @@
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in example 'render_wgpu'",
"name": "Debug example 'render-opengl'",
"cargo": {
"args": [
"test",
"--no-run",
"--example=render_wgpu",
"--package=inox2d"
"build",
"--bin=render-opengl",
"--package=render-opengl"
],
"filter": {
"name": "render_wgpu",
"kind": "example"
"name": "render-opengl",
"kind": "bin"
}
},
"args": [],
"args": [
// "~/Documents/inochi-models/Aka.inp"
],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug example 'parse_inp'",
"name": "Debug example 'render-webgl'",
"cargo": {
"args": [
"build",
"--example=parse_inp",
"--package=inox2d"
"--bin=render-webgl",
"--package=render-webgl"
],
"filter": {
"name": "parse_inp",
"kind": "example"
"name": "render-webgl",
"kind": "bin"
}
},
"args": [],
Expand All @@ -118,17 +117,16 @@
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in example 'parse_inp'",
"name": "Debug example 'render-wgpu'",
"cargo": {
"args": [
"test",
"--no-run",
"--example=parse_inp",
"--package=inox2d"
"build",
"--bin=render-wgpu",
"--package=render-wgpu"
],
"filter": {
"name": "parse_inp",
"kind": "example"
"name": "render-wgpu",
"kind": "bin"
}
},
"args": [],
Expand Down
4 changes: 4 additions & 0 deletions examples/common/src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ impl ExampleSceneController {
}
}

pub fn dt(&self) -> f32 {
self.current_elapsed - self.prev_elapsed
}

pub fn current_elapsed(&self) -> f32 {
self.current_elapsed
}
Expand Down
4 changes: 2 additions & 2 deletions examples/render-opengl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ impl App for Inox2dOpenglExampleApp {
let puppet = &mut self.model.puppet;
puppet.begin_set_params();
let t = scene_ctrl.current_elapsed();
puppet.set_param("Head:: Yaw-Pitch", Vec2::new(t.cos(), t.sin()));
puppet.end_set_params();
puppet.set_named_param("Head:: Yaw-Pitch", Vec2::new(t.cos(), t.sin()));
puppet.end_set_params(scene_ctrl.dt());

renderer.render(puppet);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/render-webgl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> {
let mut puppet = puppet.borrow_mut();
puppet.begin_set_params();
let t = scene_ctrl.borrow().current_elapsed();
puppet.set_param("Head:: Yaw-Pitch", Vec2::new(t.cos(), t.sin()));
puppet.end_set_params();
puppet.set_named_param("Head:: Yaw-Pitch", Vec2::new(t.cos(), t.sin()));
puppet.end_set_params(scene_ctrl.borrow().dt());
}
renderer.borrow().render(&puppet.borrow());

Expand Down
4 changes: 4 additions & 0 deletions examples/render-webgl/src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ impl WasmSceneController {
}
}

pub fn dt(&self) -> f32 {
self.current_elapsed - self.prev_elapsed
}

pub fn current_elapsed(&self) -> f32 {
self.current_elapsed
}
Expand Down
4 changes: 2 additions & 2 deletions examples/render-wgpu/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ pub async fn run(model: Model) {

puppet.begin_set_params();
let t = scene_ctrl.current_elapsed();
puppet.set_param("Head:: Yaw-Pitch", vec2(t.cos(), t.sin()));
puppet.end_set_params();
puppet.set_named_param("Head:: Yaw-Pitch", vec2(t.cos(), t.sin()));
puppet.end_set_params(scene_ctrl.dt());

let output = surface.get_current_texture().unwrap();
let view = (output.texture).create_view(&wgpu::TextureViewDescriptor::default());
Expand Down
93 changes: 63 additions & 30 deletions inox2d/src/formats/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ use crate::nodes::node_data::{
BlendMode, Composite, Drawable, InoxData, Mask, MaskMode, Part, UnknownBlendModeError, UnknownMaskModeError,
};
use crate::nodes::node_tree::InoxNodeTree;
use crate::nodes::physics::SimplePhysics;
use crate::params::{AxisPoints, Binding, BindingValues, Param};
use crate::params::{AxisPoints, Binding, BindingValues, Param, ParamUuid};
use crate::physics::{ParamMapMode, SimplePhysics, SimplePhysicsProps, SimplePhysicsSystem};
use crate::puppet::{
Puppet, PuppetAllowedModification, PuppetAllowedRedistribution, PuppetAllowedUsers, PuppetMeta, PuppetPhysics,
PuppetUsageRights, UnknownPuppetAllowedModificationError, UnknownPuppetAllowedRedistributionError,
UnknownPuppetAllowedUsersError,
};
use crate::render::RenderCtx;
use crate::texture::TextureId;

use super::json::{JsonError, JsonObject, SerialExtend};
Expand Down Expand Up @@ -112,27 +111,30 @@ fn deserialize_part(obj: &JsonObject) -> InoxParseResult<Part> {
let (tex_albedo, tex_emissive, tex_bumpmap) = {
let textures = obj.get_list("textures")?;

let tex_albedo: usize = match textures.first().ok_or(InoxParseError::NoAlbedoTexture)?.as_number() {
let tex_albedo = match textures.first().ok_or(InoxParseError::NoAlbedoTexture)?.as_number() {
Some(val) => val
.try_into()
.map(TextureId)
.map_err(|_| InoxParseError::JsonError(JsonError::ParseIntError("0".to_owned()).nested("textures")))?,
None => return Err(InoxParseError::NoAlbedoTexture),
};

let tex_emissive: usize = match textures.get(1).and_then(JsonValue::as_number) {
let tex_emissive = match textures.get(1).and_then(JsonValue::as_number) {
Some(val) => (val.try_into())
// Map u32::MAX to nothing
.map(|val| if val == u32::MAX as usize { 0 } else { val })
.map(TextureId)
.map_err(|_| InoxParseError::JsonError(JsonError::ParseIntError("1".to_owned()).nested("textures")))?,
None => 0,
None => TextureId(0),
};

let tex_bumpmap: usize = match textures.get(2).and_then(JsonValue::as_number) {
let tex_bumpmap = match textures.get(2).and_then(JsonValue::as_number) {
Some(val) => (val.try_into())
// Map u32::MAX to nothing
.map(|val| if val == u32::MAX as usize { 0 } else { val })
.map(TextureId)
.map_err(|_| InoxParseError::JsonError(JsonError::ParseIntError("2".to_owned()).nested("textures")))?,
None => 0,
None => TextureId(0),
};

(tex_albedo, tex_emissive, tex_bumpmap)
Expand All @@ -141,9 +143,9 @@ fn deserialize_part(obj: &JsonObject) -> InoxParseResult<Part> {
Ok(Part {
draw_state: deserialize_drawable(obj)?,
mesh: vals("mesh", deserialize_mesh(&obj.get_object("mesh")?))?,
tex_albedo: TextureId(tex_albedo),
tex_emissive: TextureId(tex_emissive),
tex_bumpmap: TextureId(tex_bumpmap),
tex_albedo,
tex_emissive,
tex_bumpmap,
})
}

Expand All @@ -153,16 +155,48 @@ fn deserialize_composite(obj: &JsonObject) -> InoxParseResult<Composite> {
}

fn deserialize_simple_physics(obj: &JsonObject) -> InoxParseResult<SimplePhysics> {
let param = ParamUuid(obj.get_u32("param")?);

let system = match obj.get_str("model_type")? {
"Pendulum" => SimplePhysicsSystem::new_rigid_pendulum(),
"SpringPendulum" => SimplePhysicsSystem::new_spring_pendulum(),
_ => todo!(),
};
let map_mode = match obj.get_str("map_mode")? {
"angle_length" => ParamMapMode::AngleLength,
"XY" => ParamMapMode::XY,
a => todo!("{}", a),
};

let local_only = obj.get_bool("local_only").unwrap_or_default();
let gravity = obj.get_f32("gravity")?;
let length = obj.get_f32("length")?;
let frequency = obj.get_f32("frequency")?;
let angle_damping = obj.get_f32("angle_damping")?;
let length_damping = obj.get_f32("length_damping")?;

let output_scale = obj.get_vec2("output_scale")?;

Ok(SimplePhysics {
param: obj.get_u32("param")?,
model_type: obj.get_str("model_type")?.to_owned(),
map_mode: obj.get_str("map_mode")?.to_owned(),
gravity: obj.get_f32("gravity")?,
length: obj.get_f32("length")?,
frequency: obj.get_f32("frequency")?,
angle_damping: obj.get_f32("angle_damping")?,
length_damping: obj.get_f32("length_damping")?,
output_scale: obj.get_vec2("output_scale")?,
param,

system,
map_mode,

offset_props: SimplePhysicsProps::default(),
props: SimplePhysicsProps {
gravity,
length,
frequency,
angle_damping,
length_damping,
output_scale,
},

local_only,

anchor: Vec2::ZERO,
output: Vec2::ZERO,
})
}

Expand Down Expand Up @@ -274,15 +308,14 @@ pub fn deserialize_puppet_ext<T>(
"nodes",
deserialize_nodes(&obj.get_object("nodes")?, deserialize_node_custom),
)?;
let render_ctx = RenderCtx::new(&nodes);

Ok(Puppet {
meta: vals("meta", deserialize_puppet_meta(&obj.get_object("meta")?))?,
physics: vals("physics", deserialize_puppet_physics(&obj.get_object("physics")?))?,
nodes,
parameters: deserialize_params(obj.get_list("param")?),
render_ctx,
})

let meta = vals("meta", deserialize_puppet_meta(&obj.get_object("meta")?))?;

let physics = vals("physics", deserialize_puppet_physics(&obj.get_object("physics")?))?;

let parameters = deserialize_params(obj.get_list("param")?);

Ok(Puppet::new(meta, physics, nodes, parameters))
}

fn deserialize_params(vals: &[json::JsonValue]) -> HashMap<String, Param> {
Expand All @@ -296,7 +329,7 @@ fn deserialize_param(obj: &JsonObject) -> InoxParseResult<(String, Param)> {
Ok((
name.clone(),
Param {
uuid: obj.get_u32("uuid")?,
uuid: ParamUuid(obj.get_u32("uuid")?),
name,
is_vec2: obj.get_bool("is_vec2")?,
min: obj.get_vec2("min")?,
Expand Down
1 change: 1 addition & 0 deletions inox2d/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub mod mesh;
pub mod model;
pub mod nodes;
pub mod params;
pub mod physics;
pub mod puppet;
pub mod render;
pub mod texture;
1 change: 0 additions & 1 deletion inox2d/src/nodes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod node;
pub mod node_data;
pub mod node_tree;
pub mod physics;
2 changes: 1 addition & 1 deletion inox2d/src/nodes/node_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::mesh::Mesh;
use crate::texture::TextureId;

use super::node::InoxNodeUuid;
use super::physics::SimplePhysics;
use crate::physics::SimplePhysics;

#[derive(Debug, Clone)]
pub struct Composite {
Expand Down
14 changes: 0 additions & 14 deletions inox2d/src/nodes/physics.rs

This file was deleted.

Loading

0 comments on commit ea6425f

Please sign in to comment.