forked from StrafesNET/strafe-project
Compare commits
7 Commits
physics-th
...
temp-spawn
Author | SHA1 | Date | |
---|---|---|---|
047f0038a5 | |||
f0479181f6 | |||
971aa9e287 | |||
6a79c4ec24 | |||
9025bea5ef | |||
6ed71073f6 | |||
91bfa70f05 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1 @@
|
|||||||
/target
|
/target
|
||||||
/textures
|
|
50
Cargo.lock
generated
50
Cargo.lock
generated
@ -834,29 +834,6 @@ dependencies = [
|
|||||||
"pkg-config",
|
"pkg-config",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "lazy-regex"
|
|
||||||
version = "3.0.2"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "e723bd417b2df60a0f6a2b6825f297ea04b245d4ba52b5a22cb679bdf58b05fa"
|
|
||||||
dependencies = [
|
|
||||||
"lazy-regex-proc_macros",
|
|
||||||
"once_cell",
|
|
||||||
"regex",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "lazy-regex-proc_macros"
|
|
||||||
version = "3.0.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "0f0a1d9139f0ee2e862e08a9c5d0ba0470f2aa21cd1e1aa1b1562f83116c725f"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"regex",
|
|
||||||
"syn 2.0.29",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lazy_static"
|
name = "lazy_static"
|
||||||
version = "1.4.0"
|
version = "1.4.0"
|
||||||
@ -1465,20 +1442,6 @@ dependencies = [
|
|||||||
"thiserror",
|
"thiserror",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "rbx_xml"
|
|
||||||
version = "0.13.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "4bc65b70827519fdc4ab47416d1085b912f087fadab9ed415471b6daba635574"
|
|
||||||
dependencies = [
|
|
||||||
"base64",
|
|
||||||
"log",
|
|
||||||
"rbx_dom_weak",
|
|
||||||
"rbx_reflection",
|
|
||||||
"rbx_reflection_database",
|
|
||||||
"xml-rs",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "redox_syscall"
|
name = "redox_syscall"
|
||||||
version = "0.3.5"
|
version = "0.3.5"
|
||||||
@ -1490,9 +1453,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "regex"
|
name = "regex"
|
||||||
version = "1.9.5"
|
version = "1.9.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "697061221ea1b4a94a624f67d0ae2bfe4e22b8a17b6a192afb11046542cc8c47"
|
checksum = "12de2eff854e5fa4b1295edd650e227e9d8fb0c9e90b12e7f36d6a6811791a29"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"aho-corasick",
|
"aho-corasick",
|
||||||
"memchr",
|
"memchr",
|
||||||
@ -1502,9 +1465,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "regex-automata"
|
name = "regex-automata"
|
||||||
version = "0.3.8"
|
version = "0.3.7"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c2f401f4955220693b56f8ec66ee9c78abffd8d1c4f23dc41a23839eb88f0795"
|
checksum = "49530408a136e16e5b486e883fbb6ba058e8e4e8ae6621a77b048b314336e629"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"aho-corasick",
|
"aho-corasick",
|
||||||
"memchr",
|
"memchr",
|
||||||
@ -1682,22 +1645,19 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "strafe-client"
|
name = "strafe-client"
|
||||||
version = "0.7.0"
|
version = "0.5.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-executor",
|
"async-executor",
|
||||||
"bytemuck",
|
"bytemuck",
|
||||||
"ddsfile",
|
"ddsfile",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
"glam",
|
"glam",
|
||||||
"lazy-regex",
|
|
||||||
"log",
|
"log",
|
||||||
"obj",
|
"obj",
|
||||||
"parking_lot",
|
|
||||||
"pollster",
|
"pollster",
|
||||||
"rbx_binary",
|
"rbx_binary",
|
||||||
"rbx_dom_weak",
|
"rbx_dom_weak",
|
||||||
"rbx_reflection_database",
|
"rbx_reflection_database",
|
||||||
"rbx_xml",
|
|
||||||
"wgpu",
|
"wgpu",
|
||||||
"winit",
|
"winit",
|
||||||
]
|
]
|
||||||
|
13
Cargo.toml
13
Cargo.toml
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "strafe-client"
|
name = "strafe-client"
|
||||||
version = "0.7.0"
|
version = "0.5.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
@ -11,19 +11,16 @@ bytemuck = { version = "1.13.1", features = ["derive"] }
|
|||||||
ddsfile = "0.5.1"
|
ddsfile = "0.5.1"
|
||||||
env_logger = "0.10.0"
|
env_logger = "0.10.0"
|
||||||
glam = "0.24.1"
|
glam = "0.24.1"
|
||||||
lazy-regex = "3.0.2"
|
|
||||||
log = "0.4.20"
|
log = "0.4.20"
|
||||||
obj = "0.10.2"
|
obj = "0.10.2"
|
||||||
parking_lot = "0.12.1"
|
|
||||||
pollster = "0.3.0"
|
pollster = "0.3.0"
|
||||||
rbx_binary = "0.7.1"
|
rbx_binary = "0.7.1"
|
||||||
rbx_dom_weak = "2.5.0"
|
rbx_dom_weak = "2.5.0"
|
||||||
rbx_reflection_database = "0.2.7"
|
rbx_reflection_database = "0.2.7"
|
||||||
rbx_xml = "0.13.1"
|
|
||||||
wgpu = "0.17.0"
|
wgpu = "0.17.0"
|
||||||
winit = "0.28.6"
|
winit = "0.28.6"
|
||||||
|
|
||||||
#[profile.release]
|
[profile.release]
|
||||||
#lto = true
|
lto = true
|
||||||
#strip = true
|
strip = true
|
||||||
#codegen-units = 1
|
codegen-units = 1
|
||||||
|
1032
src/body.rs
Normal file
1032
src/body.rs
Normal file
File diff suppressed because it is too large
Load Diff
@ -51,9 +51,8 @@ pub trait Example: 'static + Sized {
|
|||||||
device: &wgpu::Device,
|
device: &wgpu::Device,
|
||||||
queue: &wgpu::Queue,
|
queue: &wgpu::Queue,
|
||||||
);
|
);
|
||||||
fn update(&mut self, window: &winit::window::Window, device: &wgpu::Device, queue: &wgpu::Queue, event: WindowEvent);
|
fn update(&mut self, device: &wgpu::Device, event: WindowEvent);
|
||||||
fn device_event(&mut self, window: &winit::window::Window, event: DeviceEvent);
|
fn device_event(&mut self, event: DeviceEvent);
|
||||||
fn load_file(&mut self, path:std::path::PathBuf, device: &wgpu::Device, queue: &wgpu::Queue);
|
|
||||||
fn render(
|
fn render(
|
||||||
&mut self,
|
&mut self,
|
||||||
view: &wgpu::TextureView,
|
view: &wgpu::TextureView,
|
||||||
@ -180,41 +179,9 @@ async fn setup<E: Example>(title: &str) -> Setup {
|
|||||||
|
|
||||||
(size, surface)
|
(size, surface)
|
||||||
};
|
};
|
||||||
|
let adapter = wgpu::util::initialize_adapter_from_env_or_default(&instance, Some(&surface))
|
||||||
let adapter;
|
.await
|
||||||
|
.expect("No suitable GPU adapters found on the system!");
|
||||||
let optional_features = E::optional_features();
|
|
||||||
let required_features = E::required_features();
|
|
||||||
|
|
||||||
//no helper function smh gotta write it myself
|
|
||||||
let adapters = instance.enumerate_adapters(backends);
|
|
||||||
|
|
||||||
let mut chosen_adapter = None;
|
|
||||||
let mut chosen_adapter_score=0;
|
|
||||||
for adapter in adapters {
|
|
||||||
if !adapter.is_surface_supported(&surface) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let score=match adapter.get_info().device_type{
|
|
||||||
wgpu::DeviceType::IntegratedGpu=>3,
|
|
||||||
wgpu::DeviceType::DiscreteGpu=>4,
|
|
||||||
wgpu::DeviceType::VirtualGpu=>2,
|
|
||||||
wgpu::DeviceType::Other|wgpu::DeviceType::Cpu=>1,
|
|
||||||
};
|
|
||||||
|
|
||||||
let adapter_features = adapter.features();
|
|
||||||
if chosen_adapter_score<score&&adapter_features.contains(required_features) {
|
|
||||||
chosen_adapter_score=score;
|
|
||||||
chosen_adapter=Some(adapter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(maybe_chosen_adapter) = chosen_adapter{
|
|
||||||
adapter=maybe_chosen_adapter;
|
|
||||||
}else{
|
|
||||||
panic!("No suitable GPU adapters found on the system!");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
{
|
{
|
||||||
@ -222,6 +189,15 @@ async fn setup<E: Example>(title: &str) -> Setup {
|
|||||||
println!("Using {} ({:?})", adapter_info.name, adapter_info.backend);
|
println!("Using {} ({:?})", adapter_info.name, adapter_info.backend);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let optional_features = E::optional_features();
|
||||||
|
let required_features = E::required_features();
|
||||||
|
let adapter_features = adapter.features();
|
||||||
|
assert!(
|
||||||
|
adapter_features.contains(required_features),
|
||||||
|
"Adapter does not support required features for this example: {:?}",
|
||||||
|
required_features - adapter_features
|
||||||
|
);
|
||||||
|
|
||||||
let required_downlevel_capabilities = E::required_downlevel_capabilities();
|
let required_downlevel_capabilities = E::required_downlevel_capabilities();
|
||||||
let downlevel_capabilities = adapter.get_downlevel_capabilities();
|
let downlevel_capabilities = adapter.get_downlevel_capabilities();
|
||||||
assert!(
|
assert!(
|
||||||
@ -245,7 +221,7 @@ async fn setup<E: Example>(title: &str) -> Setup {
|
|||||||
.request_device(
|
.request_device(
|
||||||
&wgpu::DeviceDescriptor {
|
&wgpu::DeviceDescriptor {
|
||||||
label: None,
|
label: None,
|
||||||
features: (optional_features & adapter.features()) | required_features,
|
features: (optional_features & adapter_features) | required_features,
|
||||||
limits: needed_limits,
|
limits: needed_limits,
|
||||||
},
|
},
|
||||||
trace_dir.ok().as_ref().map(std::path::Path::new),
|
trace_dir.ok().as_ref().map(std::path::Path::new),
|
||||||
@ -359,7 +335,7 @@ fn start<E: Example>(
|
|||||||
WindowEvent::KeyboardInput {
|
WindowEvent::KeyboardInput {
|
||||||
input:
|
input:
|
||||||
event::KeyboardInput {
|
event::KeyboardInput {
|
||||||
virtual_keycode: Some(event::VirtualKeyCode::Scroll),
|
virtual_keycode: Some(event::VirtualKeyCode::R),
|
||||||
state: event::ElementState::Pressed,
|
state: event::ElementState::Pressed,
|
||||||
..
|
..
|
||||||
},
|
},
|
||||||
@ -368,14 +344,14 @@ fn start<E: Example>(
|
|||||||
println!("{:#?}", instance.generate_report());
|
println!("{:#?}", instance.generate_report());
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
example.update(&window,&device,&queue,event);
|
example.update(&device,event);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
event::Event::DeviceEvent {
|
event::Event::DeviceEvent {
|
||||||
event,
|
event,
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
example.device_event(&window,event);
|
example.device_event(event);
|
||||||
},
|
},
|
||||||
event::Event::RedrawRequested(_) => {
|
event::Event::RedrawRequested(_) => {
|
||||||
|
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct TimedInstruction<I> {
|
pub struct TimedInstruction<I> {
|
||||||
pub time: crate::physics::TIME,
|
pub time: crate::body::TIME,
|
||||||
pub instruction: I,
|
pub instruction: I,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait InstructionEmitter<I> {
|
pub trait InstructionEmitter<I> {
|
||||||
fn next_instruction(&self, time_limit:crate::physics::TIME) -> Option<TimedInstruction<I>>;
|
fn next_instruction(&self, time_limit:crate::body::TIME) -> Option<TimedInstruction<I>>;
|
||||||
}
|
}
|
||||||
pub trait InstructionConsumer<I> {
|
pub trait InstructionConsumer<I> {
|
||||||
fn process_instruction(&mut self, instruction:TimedInstruction<I>);
|
fn process_instruction(&mut self, instruction:TimedInstruction<I>);
|
||||||
@ -13,11 +13,11 @@ pub trait InstructionConsumer<I> {
|
|||||||
|
|
||||||
//PROPER PRIVATE FIELDS!!!
|
//PROPER PRIVATE FIELDS!!!
|
||||||
pub struct InstructionCollector<I> {
|
pub struct InstructionCollector<I> {
|
||||||
time: crate::physics::TIME,
|
time: crate::body::TIME,
|
||||||
instruction: Option<I>,
|
instruction: Option<I>,
|
||||||
}
|
}
|
||||||
impl<I> InstructionCollector<I> {
|
impl<I> InstructionCollector<I> {
|
||||||
pub fn new(time:crate::physics::TIME) -> Self {
|
pub fn new(time:crate::body::TIME) -> Self {
|
||||||
Self{
|
Self{
|
||||||
time,
|
time,
|
||||||
instruction:None
|
instruction:None
|
||||||
|
5
src/lib.rs
Normal file
5
src/lib.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
pub mod framework;
|
||||||
|
pub mod body;
|
||||||
|
pub mod zeroes;
|
||||||
|
pub mod instruction;
|
||||||
|
pub mod load_roblox;
|
@ -1,5 +1,3 @@
|
|||||||
use crate::primitives;
|
|
||||||
|
|
||||||
fn class_is_a(class: &str, superclass: &str) -> bool {
|
fn class_is_a(class: &str, superclass: &str) -> bool {
|
||||||
if class==superclass {
|
if class==superclass {
|
||||||
return true
|
return true
|
||||||
@ -12,458 +10,18 @@ fn class_is_a(class: &str, superclass: &str) -> bool {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
fn recursive_collect_superclass(objects: &mut std::vec::Vec<rbx_dom_weak::types::Ref>,dom: &rbx_dom_weak::WeakDom, instance: &rbx_dom_weak::Instance, superclass: &str){
|
pub fn get_objects<R: std::io::Read>(buf_thing: R, superclass: &str) -> Result<std::vec::Vec<rbx_dom_weak::Instance>, Box<dyn std::error::Error>> {
|
||||||
for &referent in instance.children() {
|
// Using buffered I/O is recommended with rbx_binary
|
||||||
if let Some(c) = dom.get_by_ref(referent) {
|
let dom = rbx_binary::from_reader(buf_thing)?;
|
||||||
if class_is_a(c.class.as_str(), superclass) {
|
|
||||||
objects.push(c.referent());//copy ref
|
let mut objects = std::vec::Vec::<rbx_dom_weak::Instance>::new();
|
||||||
}
|
//move matching instances into objects
|
||||||
recursive_collect_superclass(objects,dom,c,superclass);
|
let (_,mut instances) = dom.into_raw();
|
||||||
|
for (_,instance) in instances.drain() {
|
||||||
|
if class_is_a(instance.class.as_str(), superclass) {
|
||||||
|
objects.push(instance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
fn get_texture_refs(dom:&rbx_dom_weak::WeakDom) -> Vec<rbx_dom_weak::types::Ref>{
|
|
||||||
let mut objects = std::vec::Vec::new();
|
|
||||||
recursive_collect_superclass(&mut objects, dom, dom.root(),"Decal");
|
|
||||||
//get ids
|
|
||||||
//clear vec
|
|
||||||
//next class
|
|
||||||
objects
|
|
||||||
}
|
|
||||||
fn get_attributes(name:&str,can_collide:bool,velocity:glam::Vec3,force_intersecting:bool)->crate::model::CollisionAttributes{
|
|
||||||
let mut general=crate::model::GameMechanicAttributes::default();
|
|
||||||
let mut intersecting=crate::model::IntersectingAttributes::default();
|
|
||||||
let mut contacting=crate::model::ContactingAttributes::default();
|
|
||||||
match name{
|
|
||||||
//"Water"=>intersecting.water=Some(crate::model::IntersectingWater{density:1.0,drag:1.0}),
|
|
||||||
"Accelerator"=>intersecting.accelerator=Some(crate::model::IntersectingAccelerator{acceleration:velocity}),
|
|
||||||
"MapFinish"=>general.zone=Some(crate::model::GameMechanicZone{mode_id:0,behaviour:crate::model::ZoneBehaviour::Finish}),
|
|
||||||
"MapAnticheat"=>general.zone=Some(crate::model::GameMechanicZone{mode_id:0,behaviour:crate::model::ZoneBehaviour::Anitcheat}),
|
|
||||||
"Platform"=>general.stage_element=Some(crate::model::GameMechanicStageElement{
|
|
||||||
mode_id:0,
|
|
||||||
stage_id:0,
|
|
||||||
force:false,
|
|
||||||
behaviour:crate::model::StageElementBehaviour::Platform,
|
|
||||||
}),
|
|
||||||
other=>{
|
|
||||||
if let Some(captures)=lazy_regex::regex!(r"^(Force)?(Spawn|SpawnAt|Trigger|Teleport|Platform)(\d+)$")
|
|
||||||
.captures(other){
|
|
||||||
general.stage_element=Some(crate::model::GameMechanicStageElement{
|
|
||||||
mode_id:0,
|
|
||||||
stage_id:captures[3].parse::<u32>().unwrap(),
|
|
||||||
force:match captures.get(1){
|
|
||||||
Some(m)=>m.as_str()=="Force",
|
|
||||||
None=>false,
|
|
||||||
},
|
|
||||||
behaviour:match &captures[2]{
|
|
||||||
"Spawn"|"SpawnAt"=>crate::model::StageElementBehaviour::SpawnAt,
|
|
||||||
"Trigger"=>crate::model::StageElementBehaviour::Trigger,
|
|
||||||
"Teleport"=>crate::model::StageElementBehaviour::Teleport,
|
|
||||||
"Platform"=>crate::model::StageElementBehaviour::Platform,
|
|
||||||
_=>panic!("regex1[2] messed up bad"),
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}else if let Some(captures)=lazy_regex::regex!(r"^Bonus(Finish|Anticheat)(\d+)$")
|
|
||||||
.captures(other){
|
|
||||||
match &captures[1]{
|
|
||||||
"Finish"=>general.zone=Some(crate::model::GameMechanicZone{mode_id:captures[2].parse::<u32>().unwrap(),behaviour:crate::model::ZoneBehaviour::Finish}),
|
|
||||||
"Anticheat"=>general.zone=Some(crate::model::GameMechanicZone{mode_id:captures[2].parse::<u32>().unwrap(),behaviour:crate::model::ZoneBehaviour::Anitcheat}),
|
|
||||||
_=>panic!("regex2[1] messed up bad"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//need some way to skip this
|
|
||||||
if velocity!=glam::Vec3::ZERO{
|
|
||||||
general.booster=Some(crate::model::GameMechanicBooster{velocity});
|
|
||||||
}
|
|
||||||
match can_collide{
|
|
||||||
true=>{
|
|
||||||
match name{
|
|
||||||
//"Bounce"=>(),
|
|
||||||
"Surf"=>contacting.surf=Some(crate::model::ContactingSurf{}),
|
|
||||||
"Ladder"=>contacting.ladder=Some(crate::model::ContactingLadder{sticky:true}),
|
|
||||||
other=>{
|
|
||||||
//REGEX!!!!
|
|
||||||
//Jump#
|
|
||||||
//WormholeIn#
|
|
||||||
}
|
|
||||||
}
|
|
||||||
crate::model::CollisionAttributes::Contact{contacting,general}
|
|
||||||
},
|
|
||||||
false=>if force_intersecting
|
|
||||||
||general.jump_limit.is_some()
|
|
||||||
||general.booster.is_some()
|
|
||||||
||general.zone.is_some()
|
|
||||||
||general.stage_element.is_some()
|
|
||||||
||general.wormhole.is_some()
|
|
||||||
||intersecting.water.is_some()
|
|
||||||
||intersecting.accelerator.is_some()
|
|
||||||
{
|
|
||||||
crate::model::CollisionAttributes::Intersect{intersecting,general}
|
|
||||||
}else{
|
|
||||||
crate::model::CollisionAttributes::Decoration
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct RobloxAssetId(u64);
|
return Ok(objects)
|
||||||
struct RobloxAssetIdParseErr;
|
|
||||||
impl std::str::FromStr for RobloxAssetId {
|
|
||||||
type Err=RobloxAssetIdParseErr;
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err>{
|
|
||||||
let regman=lazy_regex::regex!(r"(\d+)$");
|
|
||||||
if let Some(captures) = regman.captures(s) {
|
|
||||||
if captures.len()==2{//captures[0] is all captures concatenated, and then each individual capture
|
|
||||||
if let Ok(id) = captures[0].parse::<u64>() {
|
|
||||||
return Ok(Self(id));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(RobloxAssetIdParseErr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#[derive(Clone,Copy,PartialEq)]
|
|
||||||
struct RobloxTextureTransform{
|
|
||||||
offset_u:f32,
|
|
||||||
offset_v:f32,
|
|
||||||
scale_u:f32,
|
|
||||||
scale_v:f32,
|
|
||||||
}
|
|
||||||
impl std::cmp::Eq for RobloxTextureTransform{}//????
|
|
||||||
impl std::default::Default for RobloxTextureTransform{
|
|
||||||
fn default() -> Self {
|
|
||||||
Self{offset_u:0.0,offset_v:0.0,scale_u:1.0,scale_v:1.0}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl std::hash::Hash for RobloxTextureTransform {
|
|
||||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
|
||||||
self.offset_u.to_ne_bytes().hash(state);
|
|
||||||
self.offset_v.to_ne_bytes().hash(state);
|
|
||||||
self.scale_u.to_ne_bytes().hash(state);
|
|
||||||
self.scale_v.to_ne_bytes().hash(state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#[derive(Clone,PartialEq)]
|
|
||||||
struct RobloxFaceTextureDescription{
|
|
||||||
texture:u32,
|
|
||||||
color:glam::Vec4,
|
|
||||||
transform:RobloxTextureTransform,
|
|
||||||
}
|
|
||||||
impl std::cmp::Eq for RobloxFaceTextureDescription{}//????
|
|
||||||
impl std::hash::Hash for RobloxFaceTextureDescription {
|
|
||||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
|
||||||
self.texture.hash(state);
|
|
||||||
self.transform.hash(state);
|
|
||||||
for &el in self.color.as_ref().iter() {
|
|
||||||
el.to_ne_bytes().hash(state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl RobloxFaceTextureDescription{
|
|
||||||
fn to_face_description(&self)->primitives::FaceDescription{
|
|
||||||
primitives::FaceDescription{
|
|
||||||
texture:Some(self.texture),
|
|
||||||
transform:glam::Affine2::from_translation(
|
|
||||||
glam::vec2(self.transform.offset_u,self.transform.offset_v)
|
|
||||||
)
|
|
||||||
*glam::Affine2::from_scale(
|
|
||||||
glam::vec2(self.transform.scale_u,self.transform.scale_v)
|
|
||||||
),
|
|
||||||
color:self.color,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
type RobloxPartDescription=[Option<RobloxFaceTextureDescription>;6];
|
|
||||||
type RobloxWedgeDescription=[Option<RobloxFaceTextureDescription>;5];
|
|
||||||
type RobloxCornerWedgeDescription=[Option<RobloxFaceTextureDescription>;4];
|
|
||||||
#[derive(Clone,Eq,Hash,PartialEq)]
|
|
||||||
enum RobloxBasePartDescription{
|
|
||||||
Sphere,
|
|
||||||
Part(RobloxPartDescription),
|
|
||||||
Cylinder,
|
|
||||||
Wedge(RobloxWedgeDescription),
|
|
||||||
CornerWedge(RobloxCornerWedgeDescription),
|
|
||||||
}
|
|
||||||
pub fn generate_indexed_models(dom:rbx_dom_weak::WeakDom) -> crate::model::IndexedModelInstances{
|
|
||||||
//IndexedModelInstances includes textures
|
|
||||||
let mut spawn_point=glam::Vec3::ZERO;
|
|
||||||
|
|
||||||
let mut indexed_models=Vec::new();
|
|
||||||
let mut model_id_from_description=std::collections::HashMap::<RobloxBasePartDescription,usize>::new();
|
|
||||||
|
|
||||||
let mut texture_id_from_asset_id=std::collections::HashMap::<u64,u32>::new();
|
|
||||||
let mut asset_id_from_texture_id=Vec::new();
|
|
||||||
|
|
||||||
let mut object_refs=Vec::new();
|
|
||||||
let mut temp_objects=Vec::new();
|
|
||||||
recursive_collect_superclass(&mut object_refs, &dom, dom.root(),"BasePart");
|
|
||||||
for object_ref in object_refs {
|
|
||||||
if let Some(object)=dom.get_by_ref(object_ref){
|
|
||||||
if let (
|
|
||||||
Some(rbx_dom_weak::types::Variant::CFrame(cf)),
|
|
||||||
Some(rbx_dom_weak::types::Variant::Vector3(size)),
|
|
||||||
Some(rbx_dom_weak::types::Variant::Vector3(velocity)),
|
|
||||||
Some(rbx_dom_weak::types::Variant::Float32(transparency)),
|
|
||||||
Some(rbx_dom_weak::types::Variant::Color3uint8(color3)),
|
|
||||||
Some(rbx_dom_weak::types::Variant::Bool(can_collide)),
|
|
||||||
) = (
|
|
||||||
object.properties.get("CFrame"),
|
|
||||||
object.properties.get("Size"),
|
|
||||||
object.properties.get("Velocity"),
|
|
||||||
object.properties.get("Transparency"),
|
|
||||||
object.properties.get("Color"),
|
|
||||||
object.properties.get("CanCollide"),
|
|
||||||
)
|
|
||||||
{
|
|
||||||
let model_transform=glam::Affine3A::from_translation(
|
|
||||||
glam::Vec3::new(cf.position.x,cf.position.y,cf.position.z)
|
|
||||||
)
|
|
||||||
* glam::Affine3A::from_mat3(
|
|
||||||
glam::Mat3::from_cols(
|
|
||||||
glam::Vec3::new(cf.orientation.x.x,cf.orientation.y.x,cf.orientation.z.x),
|
|
||||||
glam::Vec3::new(cf.orientation.x.y,cf.orientation.y.y,cf.orientation.z.y),
|
|
||||||
glam::Vec3::new(cf.orientation.x.z,cf.orientation.y.z,cf.orientation.z.z),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
* glam::Affine3A::from_scale(
|
|
||||||
glam::Vec3::new(size.x,size.y,size.z)/2.0
|
|
||||||
);
|
|
||||||
|
|
||||||
//push TempIndexedAttributes
|
|
||||||
let mut force_intersecting=false;
|
|
||||||
let mut temp_indexing_attributes=Vec::new();
|
|
||||||
if let Some(attr)=match &object.name[..]{
|
|
||||||
"MapStart"=>{
|
|
||||||
spawn_point=model_transform.transform_point3(glam::Vec3::ZERO)+glam::vec3(0.0,2.5,0.0);
|
|
||||||
Some(crate::model::TempIndexedAttributes::Start{mode_id:0})
|
|
||||||
},
|
|
||||||
"UnorderedCheckpoint"=>Some(crate::model::TempIndexedAttributes::UnorderedCheckpoint{mode_id:0}),
|
|
||||||
other=>{
|
|
||||||
let regman=lazy_regex::regex!(r"^(BonusStart|Spawn|ForceSpawn|OrderedCheckpoint)(\d+)$");
|
|
||||||
if let Some(captures) = regman.captures(other) {
|
|
||||||
match &captures[1]{
|
|
||||||
"BonusStart"=>Some(crate::model::TempIndexedAttributes::Start{mode_id:captures[2].parse::<u32>().unwrap()}),
|
|
||||||
"Spawn"|"ForceSpawn"=>Some(crate::model::TempIndexedAttributes::Spawn{mode_id:0,stage_id:captures[2].parse::<u32>().unwrap()}),
|
|
||||||
"OrderedCheckpoint"=>Some(crate::model::TempIndexedAttributes::OrderedCheckpoint{mode_id:0,checkpoint_id:captures[2].parse::<u32>().unwrap()}),
|
|
||||||
_=>None,
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}{
|
|
||||||
force_intersecting=true;
|
|
||||||
temp_indexing_attributes.push(attr);
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO: also detect "CylinderMesh" etc here
|
|
||||||
let shape=match &object.class[..]{
|
|
||||||
"Part"=>{
|
|
||||||
if let Some(rbx_dom_weak::types::Variant::Enum(shape))=object.properties.get("Shape"){
|
|
||||||
match shape.to_u32(){
|
|
||||||
0=>primitives::Primitives::Sphere,
|
|
||||||
1=>primitives::Primitives::Cube,
|
|
||||||
2=>primitives::Primitives::Cylinder,
|
|
||||||
3=>primitives::Primitives::Wedge,
|
|
||||||
4=>primitives::Primitives::CornerWedge,
|
|
||||||
_=>panic!("Funky roblox PartType={};",shape.to_u32()),
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
panic!("Part has no Shape!");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"WedgePart"=>primitives::Primitives::Wedge,
|
|
||||||
"CornerWedgePart"=>primitives::Primitives::CornerWedge,
|
|
||||||
_=>{
|
|
||||||
println!("Unsupported BasePart ClassName={}; defaulting to cube",object.class);
|
|
||||||
primitives::Primitives::Cube
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
//use the biggest one and cut it down later...
|
|
||||||
let mut part_texture_description:RobloxPartDescription=[None,None,None,None,None,None];
|
|
||||||
temp_objects.clear();
|
|
||||||
recursive_collect_superclass(&mut temp_objects, &dom, object,"Decal");
|
|
||||||
for &decal_ref in &temp_objects{
|
|
||||||
if let Some(decal)=dom.get_by_ref(decal_ref){
|
|
||||||
if let (
|
|
||||||
Some(rbx_dom_weak::types::Variant::Content(content)),
|
|
||||||
Some(rbx_dom_weak::types::Variant::Enum(normalid)),
|
|
||||||
Some(rbx_dom_weak::types::Variant::Color3(decal_color3)),
|
|
||||||
Some(rbx_dom_weak::types::Variant::Float32(decal_transparency)),
|
|
||||||
) = (
|
|
||||||
decal.properties.get("Texture"),
|
|
||||||
decal.properties.get("Face"),
|
|
||||||
decal.properties.get("Color3"),
|
|
||||||
decal.properties.get("Transparency"),
|
|
||||||
) {
|
|
||||||
if let Ok(asset_id)=content.clone().into_string().parse::<RobloxAssetId>(){
|
|
||||||
let texture_id=if let Some(&texture_id)=texture_id_from_asset_id.get(&asset_id.0){
|
|
||||||
texture_id
|
|
||||||
}else{
|
|
||||||
let texture_id=asset_id_from_texture_id.len() as u32;
|
|
||||||
texture_id_from_asset_id.insert(asset_id.0,texture_id);
|
|
||||||
asset_id_from_texture_id.push(asset_id.0);
|
|
||||||
texture_id
|
|
||||||
};
|
|
||||||
let normal_id=normalid.to_u32();
|
|
||||||
if normal_id<6{
|
|
||||||
let mut roblox_texture_transform=RobloxTextureTransform::default();
|
|
||||||
let mut roblox_texture_color=glam::Vec4::ONE;
|
|
||||||
if decal.class=="Texture"{
|
|
||||||
//generate tranform
|
|
||||||
if let (
|
|
||||||
Some(rbx_dom_weak::types::Variant::Float32(ox)),
|
|
||||||
Some(rbx_dom_weak::types::Variant::Float32(oy)),
|
|
||||||
Some(rbx_dom_weak::types::Variant::Float32(sx)),
|
|
||||||
Some(rbx_dom_weak::types::Variant::Float32(sy)),
|
|
||||||
) = (
|
|
||||||
decal.properties.get("OffsetStudsU"),
|
|
||||||
decal.properties.get("OffsetStudsV"),
|
|
||||||
decal.properties.get("StudsPerTileU"),
|
|
||||||
decal.properties.get("StudsPerTileV"),
|
|
||||||
)
|
|
||||||
{
|
|
||||||
let (size_u,size_v)=match normal_id{
|
|
||||||
0=>(size.z,size.y),//right
|
|
||||||
1=>(size.x,size.z),//top
|
|
||||||
2=>(size.x,size.y),//back
|
|
||||||
3=>(size.z,size.y),//left
|
|
||||||
4=>(size.x,size.z),//bottom
|
|
||||||
5=>(size.x,size.y),//front
|
|
||||||
_=>panic!("unreachable"),
|
|
||||||
};
|
|
||||||
roblox_texture_transform=RobloxTextureTransform{
|
|
||||||
offset_u:*ox/(*sx),offset_v:*oy/(*sy),
|
|
||||||
scale_u:size_u/(*sx),scale_v:size_v/(*sy),
|
|
||||||
};
|
|
||||||
roblox_texture_color=glam::vec4(decal_color3.r,decal_color3.g,decal_color3.b,1.0-*decal_transparency);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
part_texture_description[normal_id as usize]=Some(RobloxFaceTextureDescription{
|
|
||||||
texture:texture_id,
|
|
||||||
color:roblox_texture_color,
|
|
||||||
transform:roblox_texture_transform,
|
|
||||||
});
|
|
||||||
}else{
|
|
||||||
println!("NormalId={} unsupported for shape={:?}",normal_id,shape);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//obscure rust syntax "slice pattern"
|
|
||||||
let [
|
|
||||||
f0,//Cube::Right
|
|
||||||
f1,//Cube::Top
|
|
||||||
f2,//Cube::Back
|
|
||||||
f3,//Cube::Left
|
|
||||||
f4,//Cube::Bottom
|
|
||||||
f5,//Cube::Front
|
|
||||||
]=part_texture_description;
|
|
||||||
let basepart_texture_description=match shape{
|
|
||||||
primitives::Primitives::Sphere=>RobloxBasePartDescription::Sphere,
|
|
||||||
primitives::Primitives::Cube=>RobloxBasePartDescription::Part([f0,f1,f2,f3,f4,f5]),
|
|
||||||
primitives::Primitives::Cylinder=>RobloxBasePartDescription::Cylinder,
|
|
||||||
//use front face texture first and use top face texture as a fallback
|
|
||||||
primitives::Primitives::Wedge=>RobloxBasePartDescription::Wedge([
|
|
||||||
f0,//Cube::Right->Wedge::Right
|
|
||||||
if f5.is_some(){f5}else{f1},//Cube::Front|Cube::Top->Wedge::TopFront
|
|
||||||
f2,//Cube::Back->Wedge::Back
|
|
||||||
f3,//Cube::Left->Wedge::Left
|
|
||||||
f4,//Cube::Bottom->Wedge::Bottom
|
|
||||||
]),
|
|
||||||
primitives::Primitives::CornerWedge=>RobloxBasePartDescription::CornerWedge([
|
|
||||||
f0,//Cube::Right->CornerWedge::Right
|
|
||||||
f1,//Cube::Top->CornerWedge::Top
|
|
||||||
f4,//Cube::Bottom->CornerWedge::Bottom
|
|
||||||
f5,//Cube::Front->CornerWedge::Front
|
|
||||||
]),
|
|
||||||
};
|
|
||||||
//make new model if unit cube has not been created before
|
|
||||||
let model_id=if let Some(&model_id)=model_id_from_description.get(&basepart_texture_description){
|
|
||||||
//push to existing texture model
|
|
||||||
model_id
|
|
||||||
}else{
|
|
||||||
let model_id=indexed_models.len();
|
|
||||||
model_id_from_description.insert(basepart_texture_description.clone(),model_id);//borrow checker going crazy
|
|
||||||
indexed_models.push(match basepart_texture_description{
|
|
||||||
RobloxBasePartDescription::Sphere=>primitives::unit_sphere(),
|
|
||||||
RobloxBasePartDescription::Part(part_texture_description)=>{
|
|
||||||
let mut cube_face_description=primitives::CubeFaceDescription::new();
|
|
||||||
for (face_id,roblox_face_description) in part_texture_description.iter().enumerate(){
|
|
||||||
cube_face_description.insert(
|
|
||||||
match face_id{
|
|
||||||
0=>primitives::CubeFace::Right,
|
|
||||||
1=>primitives::CubeFace::Top,
|
|
||||||
2=>primitives::CubeFace::Back,
|
|
||||||
3=>primitives::CubeFace::Left,
|
|
||||||
4=>primitives::CubeFace::Bottom,
|
|
||||||
5=>primitives::CubeFace::Front,
|
|
||||||
_=>panic!("unreachable"),
|
|
||||||
},
|
|
||||||
match roblox_face_description{
|
|
||||||
Some(roblox_texture_transform)=>roblox_texture_transform.to_face_description(),
|
|
||||||
None=>primitives::FaceDescription::default(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
primitives::generate_partial_unit_cube(cube_face_description)
|
|
||||||
},
|
|
||||||
RobloxBasePartDescription::Cylinder=>primitives::unit_cylinder(),
|
|
||||||
RobloxBasePartDescription::Wedge(wedge_texture_description)=>{
|
|
||||||
let mut wedge_face_description=primitives::WedgeFaceDescription::new();
|
|
||||||
for (face_id,roblox_face_description) in wedge_texture_description.iter().enumerate(){
|
|
||||||
wedge_face_description.insert(
|
|
||||||
match face_id{
|
|
||||||
0=>primitives::WedgeFace::Right,
|
|
||||||
1=>primitives::WedgeFace::TopFront,
|
|
||||||
2=>primitives::WedgeFace::Back,
|
|
||||||
3=>primitives::WedgeFace::Left,
|
|
||||||
4=>primitives::WedgeFace::Bottom,
|
|
||||||
_=>panic!("unreachable"),
|
|
||||||
},
|
|
||||||
match roblox_face_description{
|
|
||||||
Some(roblox_texture_transform)=>roblox_texture_transform.to_face_description(),
|
|
||||||
None=>primitives::FaceDescription::default(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
primitives::generate_partial_unit_wedge(wedge_face_description)
|
|
||||||
},
|
|
||||||
RobloxBasePartDescription::CornerWedge(cornerwedge_texture_description)=>{
|
|
||||||
let mut cornerwedge_face_description=primitives::CornerWedgeFaceDescription::new();
|
|
||||||
for (face_id,roblox_face_description) in cornerwedge_texture_description.iter().enumerate(){
|
|
||||||
cornerwedge_face_description.insert(
|
|
||||||
match face_id{
|
|
||||||
0=>primitives::CornerWedgeFace::Top,
|
|
||||||
1=>primitives::CornerWedgeFace::Right,
|
|
||||||
2=>primitives::CornerWedgeFace::Bottom,
|
|
||||||
3=>primitives::CornerWedgeFace::Front,
|
|
||||||
_=>panic!("unreachable"),
|
|
||||||
},
|
|
||||||
match roblox_face_description{
|
|
||||||
Some(roblox_texture_transform)=>roblox_texture_transform.to_face_description(),
|
|
||||||
None=>primitives::FaceDescription::default(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
primitives::generate_partial_unit_cornerwedge(cornerwedge_face_description)
|
|
||||||
},
|
|
||||||
});
|
|
||||||
model_id
|
|
||||||
};
|
|
||||||
indexed_models[model_id].instances.push(crate::model::ModelInstance {
|
|
||||||
transform:model_transform,
|
|
||||||
color:glam::vec4(color3.r as f32/255f32, color3.g as f32/255f32, color3.b as f32/255f32, 1.0-*transparency),
|
|
||||||
attributes:get_attributes(&object.name,*can_collide,glam::vec3(velocity.x,velocity.y,velocity.z),force_intersecting),
|
|
||||||
temp_indexing:temp_indexing_attributes,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
crate::model::IndexedModelInstances{
|
|
||||||
textures:asset_id_from_texture_id.iter().map(|t|t.to_string()).collect(),
|
|
||||||
models:indexed_models,
|
|
||||||
spawn_point,
|
|
||||||
modes:Vec::new(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
1004
src/main.rs
1004
src/main.rs
File diff suppressed because it is too large
Load Diff
269
src/model.rs
269
src/model.rs
@ -1,269 +0,0 @@
|
|||||||
use bytemuck::{Pod, Zeroable};
|
|
||||||
#[derive(Clone, Copy, Pod, Zeroable)]
|
|
||||||
#[repr(C)]
|
|
||||||
pub struct Vertex {
|
|
||||||
pub pos: [f32; 3],
|
|
||||||
pub tex: [f32; 2],
|
|
||||||
pub normal: [f32; 3],
|
|
||||||
pub color: [f32; 4],
|
|
||||||
}
|
|
||||||
#[derive(Clone,Hash,PartialEq,Eq)]
|
|
||||||
pub struct IndexedVertex{
|
|
||||||
pub pos:u32,
|
|
||||||
pub tex:u32,
|
|
||||||
pub normal:u32,
|
|
||||||
pub color:u32,
|
|
||||||
}
|
|
||||||
pub struct IndexedPolygon{
|
|
||||||
pub vertices:Vec<u32>,
|
|
||||||
}
|
|
||||||
pub struct IndexedGroup{
|
|
||||||
pub texture:Option<u32>,//RenderPattern? material/texture/shader/flat color
|
|
||||||
pub polys:Vec<IndexedPolygon>,
|
|
||||||
}
|
|
||||||
pub struct IndexedModel{
|
|
||||||
pub unique_pos:Vec<[f32; 3]>,
|
|
||||||
pub unique_tex:Vec<[f32; 2]>,
|
|
||||||
pub unique_normal:Vec<[f32; 3]>,
|
|
||||||
pub unique_color:Vec<[f32; 4]>,
|
|
||||||
pub unique_vertices:Vec<IndexedVertex>,
|
|
||||||
pub groups: Vec<IndexedGroup>,
|
|
||||||
pub instances:Vec<ModelInstance>,
|
|
||||||
}
|
|
||||||
pub struct IndexedGroupFixedTexture{
|
|
||||||
pub polys:Vec<IndexedPolygon>,
|
|
||||||
}
|
|
||||||
pub struct IndexedModelSingleTexture{
|
|
||||||
pub unique_pos:Vec<[f32; 3]>,
|
|
||||||
pub unique_tex:Vec<[f32; 2]>,
|
|
||||||
pub unique_normal:Vec<[f32; 3]>,
|
|
||||||
pub unique_color:Vec<[f32; 4]>,
|
|
||||||
pub unique_vertices:Vec<IndexedVertex>,
|
|
||||||
pub texture:Option<u32>,//RenderPattern? material/texture/shader/flat color
|
|
||||||
pub groups: Vec<IndexedGroupFixedTexture>,
|
|
||||||
pub instances:Vec<ModelGraphicsInstance>,
|
|
||||||
}
|
|
||||||
pub struct ModelSingleTexture{
|
|
||||||
pub instances: Vec<ModelGraphicsInstance>,
|
|
||||||
pub vertices: Vec<Vertex>,
|
|
||||||
pub entities: Vec<Vec<u16>>,
|
|
||||||
pub texture: Option<u32>,
|
|
||||||
}
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct ModelGraphicsInstance{
|
|
||||||
pub transform:glam::Mat4,
|
|
||||||
pub normal_transform:glam::Mat4,
|
|
||||||
pub color:glam::Vec4,
|
|
||||||
}
|
|
||||||
pub struct ModelInstance{
|
|
||||||
//pub id:u64,//this does not actually help with map fixes resimulating bots, they must always be resimulated
|
|
||||||
pub transform:glam::Affine3A,
|
|
||||||
pub color:glam::Vec4,//transparency is in here
|
|
||||||
pub attributes:CollisionAttributes,
|
|
||||||
pub temp_indexing:Vec<TempIndexedAttributes>,
|
|
||||||
}
|
|
||||||
impl std::default::Default for ModelInstance{
|
|
||||||
fn default() -> Self {
|
|
||||||
Self{
|
|
||||||
color:glam::Vec4::ONE,
|
|
||||||
transform:Default::default(),
|
|
||||||
attributes:Default::default(),
|
|
||||||
temp_indexing:Default::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub struct IndexedModelInstances{
|
|
||||||
pub textures:Vec<String>,//RenderPattern
|
|
||||||
pub models:Vec<IndexedModel>,
|
|
||||||
//may make this into an object later.
|
|
||||||
pub modes:Vec<ModeDescription>,
|
|
||||||
pub spawn_point:glam::Vec3,
|
|
||||||
}
|
|
||||||
//stage description referencing flattened ids is spooky, but the map loading is meant to be deterministic.
|
|
||||||
pub struct ModeDescription{
|
|
||||||
pub start:u32,//start=model_id
|
|
||||||
pub spawns:Vec<u32>,//spawns[spawn_id]=model_id
|
|
||||||
pub ordered_checkpoints:Vec<u32>,//ordered_checkpoints[checkpoint_id]=model_id
|
|
||||||
pub unordered_checkpoints:Vec<u32>,//unordered_checkpoints[checkpoint_id]=model_id
|
|
||||||
pub spawn_from_stage_id:std::collections::HashMap::<u32,usize>,
|
|
||||||
pub ordered_checkpoint_from_checkpoint_id:std::collections::HashMap::<u32,usize>,
|
|
||||||
}
|
|
||||||
impl ModeDescription{
|
|
||||||
pub fn get_spawn_model_id(&self,stage_id:u32)->Option<&u32>{
|
|
||||||
if let Some(&spawn)=self.spawn_from_stage_id.get(&stage_id){
|
|
||||||
self.spawns.get(spawn)
|
|
||||||
}else{
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn get_ordered_checkpoint_model_id(&self,checkpoint_id:u32)->Option<&u32>{
|
|
||||||
if let Some(&checkpoint)=self.ordered_checkpoint_from_checkpoint_id.get(&checkpoint_id){
|
|
||||||
self.ordered_checkpoints.get(checkpoint)
|
|
||||||
}else{
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub enum TempIndexedAttributes{
|
|
||||||
Start{
|
|
||||||
mode_id:u32,
|
|
||||||
},
|
|
||||||
Spawn{
|
|
||||||
mode_id:u32,
|
|
||||||
stage_id:u32,
|
|
||||||
},
|
|
||||||
OrderedCheckpoint{
|
|
||||||
mode_id:u32,
|
|
||||||
checkpoint_id:u32,
|
|
||||||
},
|
|
||||||
UnorderedCheckpoint{
|
|
||||||
mode_id:u32,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
//you have this effect while in contact
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct ContactingSurf{}
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct ContactingLadder{
|
|
||||||
pub sticky:bool
|
|
||||||
}
|
|
||||||
//you have this effect while intersecting
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct IntersectingWater{
|
|
||||||
pub viscosity:i64,
|
|
||||||
pub density:i64,
|
|
||||||
pub current:glam::Vec3,
|
|
||||||
}
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct IntersectingAccelerator{
|
|
||||||
pub acceleration:glam::Vec3
|
|
||||||
}
|
|
||||||
//All models can be given these attributes
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct GameMechanicJumpLimit{
|
|
||||||
pub count:u32,
|
|
||||||
}
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct GameMechanicBooster{
|
|
||||||
pub velocity:glam::Vec3,
|
|
||||||
}
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub enum ZoneBehaviour{
|
|
||||||
//Start is indexed
|
|
||||||
//Checkpoints are indexed
|
|
||||||
Finish,
|
|
||||||
Anitcheat,
|
|
||||||
}
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct GameMechanicZone{
|
|
||||||
pub mode_id:u32,
|
|
||||||
pub behaviour:ZoneBehaviour,
|
|
||||||
}
|
|
||||||
// enum TrapCondition{
|
|
||||||
// FasterThan(i64),
|
|
||||||
// SlowerThan(i64),
|
|
||||||
// InRange(i64,i64),
|
|
||||||
// OutsideRange(i64,i64),
|
|
||||||
// }
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub enum StageElementBehaviour{
|
|
||||||
//Spawn,//The behaviour of stepping on a spawn setting the spawnid
|
|
||||||
SpawnAt,
|
|
||||||
Trigger,
|
|
||||||
Teleport,
|
|
||||||
Platform,
|
|
||||||
//Speedtrap(TrapCondition),//Acts as a trigger with a speed condition
|
|
||||||
}
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct GameMechanicStageElement{
|
|
||||||
pub mode_id:u32,
|
|
||||||
pub stage_id:u32,//which spawn to send to
|
|
||||||
pub force:bool,//allow setting to lower spawn id i.e. 7->3
|
|
||||||
pub behaviour:StageElementBehaviour
|
|
||||||
}
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct GameMechanicWormhole{//(position,angles)*=origin.transform.inverse()*destination.transform
|
|
||||||
pub model_id:u32,
|
|
||||||
}
|
|
||||||
#[derive(Default,Clone)]
|
|
||||||
pub struct GameMechanicAttributes{
|
|
||||||
pub jump_limit:Option<GameMechanicJumpLimit>,
|
|
||||||
pub booster:Option<GameMechanicBooster>,
|
|
||||||
pub zone:Option<GameMechanicZone>,
|
|
||||||
pub stage_element:Option<GameMechanicStageElement>,
|
|
||||||
pub wormhole:Option<GameMechanicWormhole>,//stage_element and wormhole are in conflict
|
|
||||||
}
|
|
||||||
#[derive(Default,Clone)]
|
|
||||||
pub struct ContactingAttributes{
|
|
||||||
pub elasticity:Option<u32>,//[1/2^32,1] 0=None (elasticity+1)/2^32
|
|
||||||
//friction?
|
|
||||||
pub surf:Option<ContactingSurf>,
|
|
||||||
pub ladder:Option<ContactingLadder>,
|
|
||||||
}
|
|
||||||
#[derive(Default,Clone)]
|
|
||||||
pub struct IntersectingAttributes{
|
|
||||||
pub water:Option<IntersectingWater>,
|
|
||||||
pub accelerator:Option<IntersectingAccelerator>,
|
|
||||||
}
|
|
||||||
//Spawn(u32) NO! spawns are indexed in the map header instead of marked with attibutes
|
|
||||||
pub enum CollisionAttributes{
|
|
||||||
Decoration,//visual only
|
|
||||||
Contact{//track whether you are contacting the object
|
|
||||||
contacting:ContactingAttributes,
|
|
||||||
general:GameMechanicAttributes,
|
|
||||||
},
|
|
||||||
Intersect{//track whether you are intersecting the object
|
|
||||||
intersecting:IntersectingAttributes,
|
|
||||||
general:GameMechanicAttributes,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
impl std::default::Default for CollisionAttributes{
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::Contact{
|
|
||||||
contacting:ContactingAttributes::default(),
|
|
||||||
general:GameMechanicAttributes::default()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn generate_indexed_model_list_from_obj(data:obj::ObjData,color:[f32;4]) -> Vec<IndexedModel>{
|
|
||||||
let mut unique_vertex_index = std::collections::HashMap::<obj::IndexTuple,u32>::new();
|
|
||||||
return data.objects.iter().map(|object|{
|
|
||||||
unique_vertex_index.clear();
|
|
||||||
let mut unique_vertices = Vec::new();
|
|
||||||
let groups = object.groups.iter().map(|group|{
|
|
||||||
IndexedGroup{
|
|
||||||
texture:None,
|
|
||||||
polys:group.polys.iter().map(|poly|{
|
|
||||||
IndexedPolygon{
|
|
||||||
vertices:poly.0.iter().map(|&tup|{
|
|
||||||
if let Some(&i)=unique_vertex_index.get(&tup){
|
|
||||||
i
|
|
||||||
}else{
|
|
||||||
let i=unique_vertices.len() as u32;
|
|
||||||
unique_vertices.push(IndexedVertex{
|
|
||||||
pos: tup.0 as u32,
|
|
||||||
tex: tup.1.unwrap() as u32,
|
|
||||||
normal: tup.2.unwrap() as u32,
|
|
||||||
color: 0,
|
|
||||||
});
|
|
||||||
unique_vertex_index.insert(tup,i);
|
|
||||||
i
|
|
||||||
}
|
|
||||||
}).collect()
|
|
||||||
}
|
|
||||||
}).collect()
|
|
||||||
}
|
|
||||||
}).collect();
|
|
||||||
IndexedModel{
|
|
||||||
unique_pos: data.position.clone(),
|
|
||||||
unique_tex: data.texture.clone(),
|
|
||||||
unique_normal: data.normal.clone(),
|
|
||||||
unique_color: vec![color],
|
|
||||||
unique_vertices,
|
|
||||||
groups,
|
|
||||||
instances:Vec::new(),
|
|
||||||
}
|
|
||||||
}).collect()
|
|
||||||
}
|
|
1358
src/physics.rs
1358
src/physics.rs
File diff suppressed because it is too large
Load Diff
@ -1,503 +0,0 @@
|
|||||||
use crate::model::{IndexedModel, IndexedPolygon, IndexedGroup, IndexedVertex};
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum Primitives{
|
|
||||||
Sphere,
|
|
||||||
Cube,
|
|
||||||
Cylinder,
|
|
||||||
Wedge,
|
|
||||||
CornerWedge,
|
|
||||||
}
|
|
||||||
#[derive(Hash,PartialEq,Eq)]
|
|
||||||
pub enum CubeFace{
|
|
||||||
Right,
|
|
||||||
Top,
|
|
||||||
Back,
|
|
||||||
Left,
|
|
||||||
Bottom,
|
|
||||||
Front,
|
|
||||||
}
|
|
||||||
const CUBE_DEFAULT_TEXTURE_COORDS:[[f32;2];4]=[[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0]];
|
|
||||||
const CUBE_DEFAULT_VERTICES:[[f32;3];8]=[
|
|
||||||
[-1.,-1., 1.],//0 left bottom back
|
|
||||||
[ 1.,-1., 1.],//1 right bottom back
|
|
||||||
[ 1., 1., 1.],//2 right top back
|
|
||||||
[-1., 1., 1.],//3 left top back
|
|
||||||
[-1., 1.,-1.],//4 left top front
|
|
||||||
[ 1., 1.,-1.],//5 right top front
|
|
||||||
[ 1.,-1.,-1.],//6 right bottom front
|
|
||||||
[-1.,-1.,-1.],//7 left bottom front
|
|
||||||
];
|
|
||||||
const CUBE_DEFAULT_NORMALS:[[f32;3];6]=[
|
|
||||||
[ 1., 0., 0.],//CubeFace::Right
|
|
||||||
[ 0., 1., 0.],//CubeFace::Top
|
|
||||||
[ 0., 0., 1.],//CubeFace::Back
|
|
||||||
[-1., 0., 0.],//CubeFace::Left
|
|
||||||
[ 0.,-1., 0.],//CubeFace::Bottom
|
|
||||||
[ 0., 0.,-1.],//CubeFace::Front
|
|
||||||
];
|
|
||||||
const CUBE_DEFAULT_POLYS:[[[u32;3];4];6]=[
|
|
||||||
// right (1, 0, 0)
|
|
||||||
[
|
|
||||||
[6,2,0],//[vertex,tex,norm]
|
|
||||||
[5,1,0],
|
|
||||||
[2,0,0],
|
|
||||||
[1,3,0],
|
|
||||||
],
|
|
||||||
// top (0, 1, 0)
|
|
||||||
[
|
|
||||||
[5,3,1],
|
|
||||||
[4,2,1],
|
|
||||||
[3,1,1],
|
|
||||||
[2,0,1],
|
|
||||||
],
|
|
||||||
// back (0, 0, 1)
|
|
||||||
[
|
|
||||||
[0,3,2],
|
|
||||||
[1,2,2],
|
|
||||||
[2,1,2],
|
|
||||||
[3,0,2],
|
|
||||||
],
|
|
||||||
// left (-1, 0, 0)
|
|
||||||
[
|
|
||||||
[0,2,3],
|
|
||||||
[3,1,3],
|
|
||||||
[4,0,3],
|
|
||||||
[7,3,3],
|
|
||||||
],
|
|
||||||
// bottom (0,-1, 0)
|
|
||||||
[
|
|
||||||
[1,1,4],
|
|
||||||
[0,0,4],
|
|
||||||
[7,3,4],
|
|
||||||
[6,2,4],
|
|
||||||
],
|
|
||||||
// front (0, 0,-1)
|
|
||||||
[
|
|
||||||
[4,1,5],
|
|
||||||
[5,0,5],
|
|
||||||
[6,3,5],
|
|
||||||
[7,2,5],
|
|
||||||
],
|
|
||||||
];
|
|
||||||
|
|
||||||
#[derive(Hash,PartialEq,Eq)]
|
|
||||||
pub enum WedgeFace{
|
|
||||||
Right,
|
|
||||||
TopFront,
|
|
||||||
Back,
|
|
||||||
Left,
|
|
||||||
Bottom,
|
|
||||||
}
|
|
||||||
const WEDGE_DEFAULT_NORMALS:[[f32;3];5]=[
|
|
||||||
[ 1., 0., 0.],//Wedge::Right
|
|
||||||
[ 0., 1.,-1.],//Wedge::TopFront
|
|
||||||
[ 0., 0., 1.],//Wedge::Back
|
|
||||||
[-1., 0., 0.],//Wedge::Left
|
|
||||||
[ 0.,-1., 0.],//Wedge::Bottom
|
|
||||||
];
|
|
||||||
/*
|
|
||||||
local cornerWedgeVerticies = {
|
|
||||||
Vector3.new(-1/2,-1/2,-1/2),7
|
|
||||||
Vector3.new(-1/2,-1/2, 1/2),0
|
|
||||||
Vector3.new( 1/2,-1/2,-1/2),6
|
|
||||||
Vector3.new( 1/2,-1/2, 1/2),1
|
|
||||||
Vector3.new( 1/2, 1/2,-1/2),5
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
#[derive(Hash,PartialEq,Eq)]
|
|
||||||
pub enum CornerWedgeFace{
|
|
||||||
Top,
|
|
||||||
Right,
|
|
||||||
Bottom,
|
|
||||||
Front,
|
|
||||||
}
|
|
||||||
const CORNERWEDGE_DEFAULT_NORMALS:[[f32;3];5]=[
|
|
||||||
[ 1., 0., 0.],//CornerWedge::Right
|
|
||||||
[ 0., 1., 1.],//CornerWedge::BackTop
|
|
||||||
[-1., 1., 0.],//CornerWedge::LeftTop
|
|
||||||
[ 0.,-1., 0.],//CornerWedge::Bottom
|
|
||||||
[ 0., 0.,-1.],//CornerWedge::Front
|
|
||||||
];
|
|
||||||
//HashMap fits this use case perfectly but feels like using a sledgehammer to drive a nail
|
|
||||||
pub fn unit_sphere()->crate::model::IndexedModel{
|
|
||||||
let mut indexed_model=crate::model::generate_indexed_model_list_from_obj(obj::ObjData::load_buf(&include_bytes!("../models/suzanne.obj")[..]).unwrap(),*glam::Vec4::ONE.as_ref()).remove(0);
|
|
||||||
for pos in indexed_model.unique_pos.iter_mut(){
|
|
||||||
pos[0]=pos[0]*0.5;
|
|
||||||
pos[1]=pos[1]*0.5;
|
|
||||||
pos[2]=pos[2]*0.5;
|
|
||||||
}
|
|
||||||
indexed_model
|
|
||||||
}
|
|
||||||
pub type CubeFaceDescription=std::collections::HashMap::<CubeFace,FaceDescription>;
|
|
||||||
pub fn unit_cube()->crate::model::IndexedModel{
|
|
||||||
let mut t=CubeFaceDescription::new();
|
|
||||||
t.insert(CubeFace::Right,FaceDescription::default());
|
|
||||||
t.insert(CubeFace::Top,FaceDescription::default());
|
|
||||||
t.insert(CubeFace::Back,FaceDescription::default());
|
|
||||||
t.insert(CubeFace::Left,FaceDescription::default());
|
|
||||||
t.insert(CubeFace::Bottom,FaceDescription::default());
|
|
||||||
t.insert(CubeFace::Front,FaceDescription::default());
|
|
||||||
generate_partial_unit_cube(t)
|
|
||||||
}
|
|
||||||
const TEAPOT_TRANSFORM:glam::Mat3=glam::mat3(glam::vec3(0.0,0.1,0.0),glam::vec3(-0.1,0.0,0.0),glam::vec3(0.0,0.0,0.1));
|
|
||||||
pub fn unit_cylinder()->crate::model::IndexedModel{
|
|
||||||
let mut indexed_model=crate::model::generate_indexed_model_list_from_obj(obj::ObjData::load_buf(&include_bytes!("../models/teapot.obj")[..]).unwrap(),*glam::Vec4::ONE.as_ref()).remove(0);
|
|
||||||
for pos in indexed_model.unique_pos.iter_mut(){
|
|
||||||
[pos[0],pos[1],pos[2]]=*(TEAPOT_TRANSFORM*glam::Vec3::from_array(*pos)).as_ref();
|
|
||||||
}
|
|
||||||
indexed_model
|
|
||||||
}
|
|
||||||
pub type WedgeFaceDescription=std::collections::HashMap::<WedgeFace,FaceDescription>;
|
|
||||||
pub fn unit_wedge()->crate::model::IndexedModel{
|
|
||||||
let mut t=WedgeFaceDescription::new();
|
|
||||||
t.insert(WedgeFace::Right,FaceDescription::default());
|
|
||||||
t.insert(WedgeFace::TopFront,FaceDescription::default());
|
|
||||||
t.insert(WedgeFace::Back,FaceDescription::default());
|
|
||||||
t.insert(WedgeFace::Left,FaceDescription::default());
|
|
||||||
t.insert(WedgeFace::Bottom,FaceDescription::default());
|
|
||||||
generate_partial_unit_wedge(t)
|
|
||||||
}
|
|
||||||
pub type CornerWedgeFaceDescription=std::collections::HashMap::<CornerWedgeFace,FaceDescription>;
|
|
||||||
pub fn unit_cornerwedge()->crate::model::IndexedModel{
|
|
||||||
let mut t=CornerWedgeFaceDescription::new();
|
|
||||||
t.insert(CornerWedgeFace::Right,FaceDescription::default());
|
|
||||||
t.insert(CornerWedgeFace::Top,FaceDescription::default());
|
|
||||||
t.insert(CornerWedgeFace::Bottom,FaceDescription::default());
|
|
||||||
t.insert(CornerWedgeFace::Front,FaceDescription::default());
|
|
||||||
generate_partial_unit_cornerwedge(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy,Clone)]
|
|
||||||
pub struct FaceDescription{
|
|
||||||
pub texture:Option<u32>,
|
|
||||||
pub transform:glam::Affine2,
|
|
||||||
pub color:glam::Vec4,
|
|
||||||
}
|
|
||||||
impl std::default::Default for FaceDescription{
|
|
||||||
fn default()->Self {
|
|
||||||
Self{
|
|
||||||
texture:None,
|
|
||||||
transform:glam::Affine2::IDENTITY,
|
|
||||||
color:glam::vec4(1.0,1.0,1.0,0.0),//zero alpha to hide the default texture
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl FaceDescription{
|
|
||||||
pub fn new(texture:u32,transform:glam::Affine2,color:glam::Vec4)->Self{
|
|
||||||
Self{texture:Some(texture),transform,color}
|
|
||||||
}
|
|
||||||
pub fn from_texture(texture:u32)->Self{
|
|
||||||
Self{
|
|
||||||
texture:Some(texture),
|
|
||||||
transform:glam::Affine2::IDENTITY,
|
|
||||||
color:glam::Vec4::ONE,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//TODO: it's probably better to use a shared vertex buffer between all primitives and use indexed rendering instead of generating a unique vertex buffer for each primitive.
|
|
||||||
//implementation: put all roblox primitives into one model.groups <- this won't work but I forget why
|
|
||||||
pub fn generate_partial_unit_cube(face_descriptions:CubeFaceDescription)->crate::model::IndexedModel{
|
|
||||||
let mut generated_pos=Vec::<[f32;3]>::new();
|
|
||||||
let mut generated_tex=Vec::new();
|
|
||||||
let mut generated_normal=Vec::new();
|
|
||||||
let mut generated_color=Vec::new();
|
|
||||||
let mut generated_vertices=Vec::new();
|
|
||||||
let mut groups=Vec::new();
|
|
||||||
let mut transforms=Vec::new();
|
|
||||||
//note that on a cube every vertex is guaranteed to be unique, so there's no need to hash them against existing vertices.
|
|
||||||
for (face,face_description) in face_descriptions.into_iter(){
|
|
||||||
//assume that scanning short lists is faster than hashing.
|
|
||||||
let transform_index=if let Some(transform_index)=transforms.iter().position(|&transform|transform==face_description.transform){
|
|
||||||
transform_index
|
|
||||||
}else{
|
|
||||||
//create new transform_index
|
|
||||||
let transform_index=transforms.len();
|
|
||||||
transforms.push(face_description.transform);
|
|
||||||
for tex in CUBE_DEFAULT_TEXTURE_COORDS{
|
|
||||||
generated_tex.push(*face_description.transform.transform_point2(glam::Vec2::from_array(tex)).as_ref());
|
|
||||||
}
|
|
||||||
transform_index
|
|
||||||
} as u32;
|
|
||||||
let color_index=if let Some(color_index)=generated_color.iter().position(|color|color==face_description.color.as_ref()){
|
|
||||||
color_index
|
|
||||||
}else{
|
|
||||||
//create new color_index
|
|
||||||
let color_index=generated_color.len();
|
|
||||||
generated_color.push(*face_description.color.as_ref());
|
|
||||||
color_index
|
|
||||||
} as u32;
|
|
||||||
let face_id=match face{
|
|
||||||
CubeFace::Right => 0,
|
|
||||||
CubeFace::Top => 1,
|
|
||||||
CubeFace::Back => 2,
|
|
||||||
CubeFace::Left => 3,
|
|
||||||
CubeFace::Bottom => 4,
|
|
||||||
CubeFace::Front => 5,
|
|
||||||
};
|
|
||||||
//always push normal
|
|
||||||
let normal_index=generated_normal.len() as u32;
|
|
||||||
generated_normal.push(CUBE_DEFAULT_NORMALS[face_id]);
|
|
||||||
//push vertices as they are needed
|
|
||||||
groups.push(IndexedGroup{
|
|
||||||
texture:face_description.texture,
|
|
||||||
polys:vec![IndexedPolygon{
|
|
||||||
vertices:CUBE_DEFAULT_POLYS[face_id].map(|tup|{
|
|
||||||
let pos=CUBE_DEFAULT_VERTICES[tup[0] as usize];
|
|
||||||
let pos_index=if let Some(pos_index)=generated_pos.iter().position(|&p|p==pos){
|
|
||||||
pos_index
|
|
||||||
}else{
|
|
||||||
//create new pos_index
|
|
||||||
let pos_index=generated_pos.len();
|
|
||||||
generated_pos.push(pos);
|
|
||||||
pos_index
|
|
||||||
} as u32;
|
|
||||||
//always push vertex
|
|
||||||
let vertex=IndexedVertex{
|
|
||||||
pos:pos_index,
|
|
||||||
tex:tup[1]+4*transform_index,
|
|
||||||
normal:normal_index,
|
|
||||||
color:color_index,
|
|
||||||
};
|
|
||||||
let vert_index=generated_vertices.len();
|
|
||||||
generated_vertices.push(vertex);
|
|
||||||
vert_index as u32
|
|
||||||
}).to_vec(),
|
|
||||||
}],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
IndexedModel{
|
|
||||||
unique_pos:generated_pos,
|
|
||||||
unique_tex:generated_tex,
|
|
||||||
unique_normal:generated_normal,
|
|
||||||
unique_color:generated_color,
|
|
||||||
unique_vertices:generated_vertices,
|
|
||||||
groups,
|
|
||||||
instances:Vec::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//don't think too hard about the copy paste because this is all going into the map tool eventually...
|
|
||||||
pub fn generate_partial_unit_wedge(face_descriptions:WedgeFaceDescription)->crate::model::IndexedModel{
|
|
||||||
let wedge_default_polys=vec![
|
|
||||||
// right (1, 0, 0)
|
|
||||||
vec![
|
|
||||||
[6,2,0],//[vertex,tex,norm]
|
|
||||||
[2,0,0],
|
|
||||||
[1,3,0],
|
|
||||||
],
|
|
||||||
// FrontTop (0, 1, -1)
|
|
||||||
vec![
|
|
||||||
[3,1,1],
|
|
||||||
[2,0,1],
|
|
||||||
[6,3,1],
|
|
||||||
[7,2,1],
|
|
||||||
],
|
|
||||||
// back (0, 0, 1)
|
|
||||||
vec![
|
|
||||||
[0,3,2],
|
|
||||||
[1,2,2],
|
|
||||||
[2,1,2],
|
|
||||||
[3,0,2],
|
|
||||||
],
|
|
||||||
// left (-1, 0, 0)
|
|
||||||
vec![
|
|
||||||
[0,2,3],
|
|
||||||
[3,1,3],
|
|
||||||
[7,3,3],
|
|
||||||
],
|
|
||||||
// bottom (0,-1, 0)
|
|
||||||
vec![
|
|
||||||
[1,1,4],
|
|
||||||
[0,0,4],
|
|
||||||
[7,3,4],
|
|
||||||
[6,2,4],
|
|
||||||
],
|
|
||||||
];
|
|
||||||
let mut generated_pos=Vec::<[f32;3]>::new();
|
|
||||||
let mut generated_tex=Vec::new();
|
|
||||||
let mut generated_normal=Vec::new();
|
|
||||||
let mut generated_color=Vec::new();
|
|
||||||
let mut generated_vertices=Vec::new();
|
|
||||||
let mut groups=Vec::new();
|
|
||||||
let mut transforms=Vec::new();
|
|
||||||
//note that on a cube every vertex is guaranteed to be unique, so there's no need to hash them against existing vertices.
|
|
||||||
for (face,face_description) in face_descriptions.into_iter(){
|
|
||||||
//assume that scanning short lists is faster than hashing.
|
|
||||||
let transform_index=if let Some(transform_index)=transforms.iter().position(|&transform|transform==face_description.transform){
|
|
||||||
transform_index
|
|
||||||
}else{
|
|
||||||
//create new transform_index
|
|
||||||
let transform_index=transforms.len();
|
|
||||||
transforms.push(face_description.transform);
|
|
||||||
for tex in CUBE_DEFAULT_TEXTURE_COORDS{
|
|
||||||
generated_tex.push(*face_description.transform.transform_point2(glam::Vec2::from_array(tex)).as_ref());
|
|
||||||
}
|
|
||||||
transform_index
|
|
||||||
} as u32;
|
|
||||||
let color_index=if let Some(color_index)=generated_color.iter().position(|color|color==face_description.color.as_ref()){
|
|
||||||
color_index
|
|
||||||
}else{
|
|
||||||
//create new color_index
|
|
||||||
let color_index=generated_color.len();
|
|
||||||
generated_color.push(*face_description.color.as_ref());
|
|
||||||
color_index
|
|
||||||
} as u32;
|
|
||||||
let face_id=match face{
|
|
||||||
WedgeFace::Right => 0,
|
|
||||||
WedgeFace::TopFront => 1,
|
|
||||||
WedgeFace::Back => 2,
|
|
||||||
WedgeFace::Left => 3,
|
|
||||||
WedgeFace::Bottom => 4,
|
|
||||||
};
|
|
||||||
//always push normal
|
|
||||||
let normal_index=generated_normal.len() as u32;
|
|
||||||
generated_normal.push(WEDGE_DEFAULT_NORMALS[face_id]);
|
|
||||||
//push vertices as they are needed
|
|
||||||
groups.push(IndexedGroup{
|
|
||||||
texture:face_description.texture,
|
|
||||||
polys:vec![IndexedPolygon{
|
|
||||||
vertices:wedge_default_polys[face_id].iter().map(|tup|{
|
|
||||||
let pos=CUBE_DEFAULT_VERTICES[tup[0] as usize];
|
|
||||||
let pos_index=if let Some(pos_index)=generated_pos.iter().position(|&p|p==pos){
|
|
||||||
pos_index
|
|
||||||
}else{
|
|
||||||
//create new pos_index
|
|
||||||
let pos_index=generated_pos.len();
|
|
||||||
generated_pos.push(pos);
|
|
||||||
pos_index
|
|
||||||
} as u32;
|
|
||||||
//always push vertex
|
|
||||||
let vertex=IndexedVertex{
|
|
||||||
pos:pos_index,
|
|
||||||
tex:tup[1]+4*transform_index,
|
|
||||||
normal:normal_index,
|
|
||||||
color:color_index,
|
|
||||||
};
|
|
||||||
let vert_index=generated_vertices.len();
|
|
||||||
generated_vertices.push(vertex);
|
|
||||||
vert_index as u32
|
|
||||||
}).collect(),
|
|
||||||
}],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
IndexedModel{
|
|
||||||
unique_pos:generated_pos,
|
|
||||||
unique_tex:generated_tex,
|
|
||||||
unique_normal:generated_normal,
|
|
||||||
unique_color:generated_color,
|
|
||||||
unique_vertices:generated_vertices,
|
|
||||||
groups,
|
|
||||||
instances:Vec::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn generate_partial_unit_cornerwedge(face_descriptions:CornerWedgeFaceDescription)->crate::model::IndexedModel{
|
|
||||||
let cornerwedge_default_polys=vec![
|
|
||||||
// right (1, 0, 0)
|
|
||||||
vec![
|
|
||||||
[6,2,0],//[vertex,tex,norm]
|
|
||||||
[5,1,0],
|
|
||||||
[1,3,0],
|
|
||||||
],
|
|
||||||
// BackTop (0, 1, 1)
|
|
||||||
vec![
|
|
||||||
[5,3,1],
|
|
||||||
[0,1,1],
|
|
||||||
[1,0,1],
|
|
||||||
],
|
|
||||||
// LeftTop (-1, 1, 0)
|
|
||||||
vec![
|
|
||||||
[5,3,2],
|
|
||||||
[7,2,2],
|
|
||||||
[0,1,2],
|
|
||||||
],
|
|
||||||
// bottom (0,-1, 0)
|
|
||||||
vec![
|
|
||||||
[1,1,3],
|
|
||||||
[0,0,3],
|
|
||||||
[7,3,3],
|
|
||||||
[6,2,3],
|
|
||||||
],
|
|
||||||
// front (0, 0,-1)
|
|
||||||
vec![
|
|
||||||
[5,0,4],
|
|
||||||
[6,3,4],
|
|
||||||
[7,2,4],
|
|
||||||
],
|
|
||||||
];
|
|
||||||
let mut generated_pos=Vec::<[f32;3]>::new();
|
|
||||||
let mut generated_tex=Vec::new();
|
|
||||||
let mut generated_normal=Vec::new();
|
|
||||||
let mut generated_color=Vec::new();
|
|
||||||
let mut generated_vertices=Vec::new();
|
|
||||||
let mut groups=Vec::new();
|
|
||||||
let mut transforms=Vec::new();
|
|
||||||
//note that on a cube every vertex is guaranteed to be unique, so there's no need to hash them against existing vertices.
|
|
||||||
for (face,face_description) in face_descriptions.into_iter(){
|
|
||||||
//assume that scanning short lists is faster than hashing.
|
|
||||||
let transform_index=if let Some(transform_index)=transforms.iter().position(|&transform|transform==face_description.transform){
|
|
||||||
transform_index
|
|
||||||
}else{
|
|
||||||
//create new transform_index
|
|
||||||
let transform_index=transforms.len();
|
|
||||||
transforms.push(face_description.transform);
|
|
||||||
for tex in CUBE_DEFAULT_TEXTURE_COORDS{
|
|
||||||
generated_tex.push(*face_description.transform.transform_point2(glam::Vec2::from_array(tex)).as_ref());
|
|
||||||
}
|
|
||||||
transform_index
|
|
||||||
} as u32;
|
|
||||||
let color_index=if let Some(color_index)=generated_color.iter().position(|color|color==face_description.color.as_ref()){
|
|
||||||
color_index
|
|
||||||
}else{
|
|
||||||
//create new color_index
|
|
||||||
let color_index=generated_color.len();
|
|
||||||
generated_color.push(*face_description.color.as_ref());
|
|
||||||
color_index
|
|
||||||
} as u32;
|
|
||||||
let face_id=match face{
|
|
||||||
CornerWedgeFace::Right => 0,
|
|
||||||
CornerWedgeFace::Top => 1,
|
|
||||||
CornerWedgeFace::Bottom => 2,
|
|
||||||
CornerWedgeFace::Front => 3,
|
|
||||||
};
|
|
||||||
//always push normal
|
|
||||||
let normal_index=generated_normal.len() as u32;
|
|
||||||
generated_normal.push(CORNERWEDGE_DEFAULT_NORMALS[face_id]);
|
|
||||||
//push vertices as they are needed
|
|
||||||
groups.push(IndexedGroup{
|
|
||||||
texture:face_description.texture,
|
|
||||||
polys:vec![IndexedPolygon{
|
|
||||||
vertices:cornerwedge_default_polys[face_id].iter().map(|tup|{
|
|
||||||
let pos=CUBE_DEFAULT_VERTICES[tup[0] as usize];
|
|
||||||
let pos_index=if let Some(pos_index)=generated_pos.iter().position(|&p|p==pos){
|
|
||||||
pos_index
|
|
||||||
}else{
|
|
||||||
//create new pos_index
|
|
||||||
let pos_index=generated_pos.len();
|
|
||||||
generated_pos.push(pos);
|
|
||||||
pos_index
|
|
||||||
} as u32;
|
|
||||||
//always push vertex
|
|
||||||
let vertex=IndexedVertex{
|
|
||||||
pos:pos_index,
|
|
||||||
tex:tup[1]+4*transform_index,
|
|
||||||
normal:normal_index,
|
|
||||||
color:color_index,
|
|
||||||
};
|
|
||||||
let vert_index=generated_vertices.len();
|
|
||||||
generated_vertices.push(vertex);
|
|
||||||
vert_index as u32
|
|
||||||
}).collect(),
|
|
||||||
}],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
IndexedModel{
|
|
||||||
unique_pos:generated_pos,
|
|
||||||
unique_tex:generated_tex,
|
|
||||||
unique_normal:generated_normal,
|
|
||||||
unique_color:generated_color,
|
|
||||||
unique_vertices:generated_vertices,
|
|
||||||
groups,
|
|
||||||
instances:Vec::new(),
|
|
||||||
}
|
|
||||||
}
|
|
@ -43,20 +43,19 @@ fn vs_sky(@builtin(vertex_index) vertex_index: u32) -> SkyOutput {
|
|||||||
|
|
||||||
struct ModelInstance{
|
struct ModelInstance{
|
||||||
transform:mat4x4<f32>,
|
transform:mat4x4<f32>,
|
||||||
normal_transform:mat4x4<f32>,
|
//texture_transform:mat3x3<f32>,
|
||||||
color:vec4<f32>,
|
color:vec4<f32>,
|
||||||
}
|
}
|
||||||
//my fancy idea is to create a megatexture for each model that includes all the textures each intance will need
|
//my fancy idea is to create a megatexture for each model that includes all the textures each intance will need
|
||||||
//the texture transform then maps the texture coordinates to the location of the specific texture
|
//the texture transform then maps the texture coordinates to the location of the specific texture
|
||||||
//group 1 is the model
|
//group 1 is the model
|
||||||
const MAX_MODEL_INSTANCES=4096;
|
@group(1)
|
||||||
@group(2)
|
|
||||||
@binding(0)
|
@binding(0)
|
||||||
var<uniform> model_instances: array<ModelInstance, MAX_MODEL_INSTANCES>;
|
var<storage> model_instances: array<ModelInstance>;
|
||||||
@group(2)
|
@group(1)
|
||||||
@binding(1)
|
@binding(1)
|
||||||
var model_texture: texture_2d<f32>;
|
var model_texture: texture_2d<f32>;
|
||||||
@group(2)
|
@group(1)
|
||||||
@binding(2)
|
@binding(2)
|
||||||
var model_sampler: sampler;
|
var model_sampler: sampler;
|
||||||
|
|
||||||
@ -66,7 +65,6 @@ struct EntityOutputTexture {
|
|||||||
@location(2) normal: vec3<f32>,
|
@location(2) normal: vec3<f32>,
|
||||||
@location(3) view: vec3<f32>,
|
@location(3) view: vec3<f32>,
|
||||||
@location(4) color: vec4<f32>,
|
@location(4) color: vec4<f32>,
|
||||||
@location(5) @interpolate(flat) model_color: vec4<f32>,
|
|
||||||
};
|
};
|
||||||
@vertex
|
@vertex
|
||||||
fn vs_entity_texture(
|
fn vs_entity_texture(
|
||||||
@ -78,26 +76,25 @@ fn vs_entity_texture(
|
|||||||
) -> EntityOutputTexture {
|
) -> EntityOutputTexture {
|
||||||
var position: vec4<f32> = model_instances[instance].transform * vec4<f32>(pos, 1.0);
|
var position: vec4<f32> = model_instances[instance].transform * vec4<f32>(pos, 1.0);
|
||||||
var result: EntityOutputTexture;
|
var result: EntityOutputTexture;
|
||||||
result.normal = (model_instances[instance].normal_transform * vec4<f32>(normal, 1.0)).xyz;
|
result.normal = (model_instances[instance].transform * vec4<f32>(normal, 0.0)).xyz;
|
||||||
result.texture = texture;
|
result.texture=texture;//(model_instances[instance].texture_transform * vec3<f32>(texture, 1.0)).xy;
|
||||||
result.color = color;
|
result.color=model_instances[instance].color * color;
|
||||||
result.model_color = model_instances[instance].color;
|
|
||||||
result.view = position.xyz - camera.cam_pos.xyz;
|
result.view = position.xyz - camera.cam_pos.xyz;
|
||||||
result.position = camera.proj * camera.view * position;
|
result.position = camera.proj * camera.view * position;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
//group 2 is the skybox texture
|
//group 2 is the skybox texture
|
||||||
@group(1)
|
@group(2)
|
||||||
@binding(0)
|
@binding(0)
|
||||||
var cube_texture: texture_cube<f32>;
|
var cube_texture: texture_cube<f32>;
|
||||||
@group(1)
|
@group(2)
|
||||||
@binding(1)
|
@binding(1)
|
||||||
var cube_sampler: sampler;
|
var cube_sampler: sampler;
|
||||||
|
|
||||||
@fragment
|
@fragment
|
||||||
fn fs_sky(vertex: SkyOutput) -> @location(0) vec4<f32> {
|
fn fs_sky(vertex: SkyOutput) -> @location(0) vec4<f32> {
|
||||||
return textureSample(cube_texture, cube_sampler, vertex.sampledir);
|
return textureSample(cube_texture, model_sampler, vertex.sampledir);
|
||||||
}
|
}
|
||||||
|
|
||||||
@fragment
|
@fragment
|
||||||
@ -109,5 +106,5 @@ fn fs_entity_texture(vertex: EntityOutputTexture) -> @location(0) vec4<f32> {
|
|||||||
|
|
||||||
let fragment_color = textureSample(model_texture, model_sampler, vertex.texture)*vertex.color;
|
let fragment_color = textureSample(model_texture, model_sampler, vertex.texture)*vertex.color;
|
||||||
let reflected_color = textureSample(cube_texture, cube_sampler, reflected).rgb;
|
let reflected_color = textureSample(cube_texture, cube_sampler, reflected).rgb;
|
||||||
return mix(vec4<f32>(vec3<f32>(0.05) + 0.2 * reflected_color,1.0),mix(vertex.model_color,vec4<f32>(fragment_color.rgb,1.0),fragment_color.a),1.0-pow(1.0-abs(d),2.0));
|
return mix(vec4<f32>(vec3<f32>(0.1) + 0.5 * reflected_color,1.0),fragment_color,1.0-pow(1.0-abs(d),2.0));
|
||||||
}
|
}
|
||||||
|
107
src/worker.rs
107
src/worker.rs
@ -1,107 +0,0 @@
|
|||||||
use std::thread;
|
|
||||||
use std::sync::{mpsc,Arc};
|
|
||||||
use parking_lot::Mutex;
|
|
||||||
|
|
||||||
//The goal here is to have a worker thread that parks itself when it runs out of work.
|
|
||||||
//The worker thread publishes the result of its work back to the worker object for every item in the work queue.
|
|
||||||
//The physics (target use case) knows when it has not changed the body, so not updating the value is also an option.
|
|
||||||
|
|
||||||
pub struct Worker<Task:Send,Value:Clone> {
|
|
||||||
sender: mpsc::Sender<Task>,
|
|
||||||
value:Arc<Mutex<Value>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Task:Send+'static,Value:Clone+Send+'static> Worker<Task,Value> {
|
|
||||||
pub fn new<F:FnMut(Task)->Value+Send+'static>(value:Value,mut f:F) -> Self {
|
|
||||||
let (sender, receiver) = mpsc::channel::<Task>();
|
|
||||||
let ret=Self {
|
|
||||||
sender,
|
|
||||||
value:Arc::new(Mutex::new(value)),
|
|
||||||
};
|
|
||||||
let value=ret.value.clone();
|
|
||||||
thread::spawn(move || {
|
|
||||||
loop {
|
|
||||||
match receiver.recv() {
|
|
||||||
Ok(task) => {
|
|
||||||
let v=f(task);//make sure function is evaluated before lock is acquired
|
|
||||||
*value.lock()=v;
|
|
||||||
}
|
|
||||||
Err(_) => {
|
|
||||||
println!("Worker stopping.",);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
ret
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn send(&self,task:Task)->Result<(), mpsc::SendError<Task>>{
|
|
||||||
self.sender.send(task)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn grab_clone(&self)->Value{
|
|
||||||
self.value.lock().clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct CompatWorker<Task,Value:Clone,F>{
|
|
||||||
data:std::marker::PhantomData<Task>,
|
|
||||||
f:F,
|
|
||||||
value:Value,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Task,Value:Clone,F:FnMut(Task)->Value> CompatWorker<Task,Value,F> {
|
|
||||||
pub fn new(value:Value,f:F) -> Self {
|
|
||||||
Self {
|
|
||||||
f,
|
|
||||||
value,
|
|
||||||
data:std::marker::PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn send(&mut self,task:Task)->Result<(),()>{
|
|
||||||
self.value=(self.f)(task);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn grab_clone(&self)->Value{
|
|
||||||
self.value.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]//How to run this test with printing: cargo test --release -- --nocapture
|
|
||||||
fn test_worker() {
|
|
||||||
println!("hiiiii");
|
|
||||||
// Create the worker thread
|
|
||||||
let worker = Worker::new(crate::physics::Body::with_pva(glam::Vec3::ZERO,glam::Vec3::ZERO,glam::Vec3::ZERO),
|
|
||||||
|_|crate::physics::Body::with_pva(glam::Vec3::ONE,glam::Vec3::ONE,glam::Vec3::ONE)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Send tasks to the worker
|
|
||||||
for i in 0..5 {
|
|
||||||
let task = crate::instruction::TimedInstruction{
|
|
||||||
time:0,
|
|
||||||
instruction:crate::physics::PhysicsInstruction::StrafeTick,
|
|
||||||
};
|
|
||||||
worker.send(task).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Optional: Signal the worker to stop (in a real-world scenario)
|
|
||||||
// sender.send("STOP".to_string()).unwrap();
|
|
||||||
|
|
||||||
// Sleep to allow the worker thread to finish processing
|
|
||||||
thread::sleep(std::time::Duration::from_secs(2));
|
|
||||||
|
|
||||||
// Send a new task
|
|
||||||
let task = crate::instruction::TimedInstruction{
|
|
||||||
time:0,
|
|
||||||
instruction:crate::physics::PhysicsInstruction::StrafeTick,
|
|
||||||
};
|
|
||||||
worker.send(task).unwrap();
|
|
||||||
|
|
||||||
println!("value={:?}",worker.grab_clone());
|
|
||||||
|
|
||||||
// wait long enough to see print from final task
|
|
||||||
thread::sleep(std::time::Duration::from_secs(1));
|
|
||||||
}
|
|
@ -1,5 +1,4 @@
|
|||||||
//find roots of polynomials
|
//find roots of polynomials
|
||||||
#[inline]
|
|
||||||
pub fn zeroes2(a0:f32,a1:f32,a2:f32) -> Vec<f32>{
|
pub fn zeroes2(a0:f32,a1:f32,a2:f32) -> Vec<f32>{
|
||||||
if a2==0f32{
|
if a2==0f32{
|
||||||
return zeroes1(a0, a1);
|
return zeroes1(a0, a1);
|
||||||
|
Reference in New Issue
Block a user