Skip to content

Commit

Permalink
Add reader utility back
Browse files Browse the repository at this point in the history
  • Loading branch information
David Harvey-Macaulay committed Jun 3, 2024
1 parent b523986 commit fcc59f5
Show file tree
Hide file tree
Showing 29 changed files with 2,602 additions and 1,249 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ features = ["jpeg", "png"]
optional = true
version = "0.25"

[features]=
[features]
default = ["import", "utils"]
allow_empty_texture = []
extensions = []
Expand Down
142 changes: 142 additions & 0 deletions examples/Box.gltf
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
{
"asset": {
"generator": "COLLADA2GLTF",
"version": "2.0"
},
"scene": 0,
"scenes": [
{
"nodes": [
0
]
}
],
"nodes": [
{
"children": [
1
],
"matrix": [
1.0,
0.0,
0.0,
0.0,
0.0,
0.0,
-1.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
0.0,
0.0,
1.0
]
},
{
"mesh": 0
}
],
"meshes": [
{
"primitives": [
{
"attributes": {
"NORMAL": 1,
"POSITION": 2
},
"indices": 0,
"mode": 4,
"material": 0
}
],
"name": "Mesh"
}
],
"accessors": [
{
"bufferView": 0,
"byteOffset": 0,
"componentType": 5123,
"count": 36,
"max": [
23
],
"min": [
0
],
"type": "SCALAR"
},
{
"bufferView": 1,
"byteOffset": 0,
"componentType": 5126,
"count": 24,
"max": [
1.0,
1.0,
1.0
],
"min": [
-1.0,
-1.0,
-1.0
],
"type": "VEC3"
},
{
"bufferView": 1,
"byteOffset": 288,
"componentType": 5126,
"count": 24,
"max": [
0.5,
0.5,
0.5
],
"min": [
-0.5,
-0.5,
-0.5
],
"type": "VEC3"
}
],
"materials": [
{
"pbrMetallicRoughness": {
"baseColorFactor": [
0.800000011920929,
0.0,
0.0,
1.0
],
"metallicFactor": 0.0
},
"name": "Red"
}
],
"bufferViews": [
{
"buffer": 0,
"byteOffset": 576,
"byteLength": 72,
"target": 34963
},
{
"buffer": 0,
"byteOffset": 0,
"byteLength": 576,
"byteStride": 12,
"target": 34962
}
],
"buffers": [
{
"byteLength": 648,
"uri": "Box0.bin"
}
]
}
Binary file added examples/Box0.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/export/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn export(output: Output) {
buffer,
length: USize64::from(buffer_length),
offset: USize64(0),
stride: Some(gltf::buffer::Stride(mem::size_of::<Vertex>())),
stride: Some(gltf::buffer::Stride(mem::size_of::<Vertex>() as u8)),
name: None,
target: Some(gltf::buffer::Target::ArrayBuffer),
extras: Default::default(),
Expand Down
61 changes: 18 additions & 43 deletions gltf-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,6 @@ use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use syn::{parse_quote, DeriveInput};

#[proc_macro_derive(Validate, attributes(gltf))]
pub fn derive_validate(input: TokenStream) -> TokenStream {
expand(&syn::parse_macro_input!(input as DeriveInput)).into()
}

struct ValidateHook(pub syn::Ident);

impl syn::parse::Parse for ValidateHook {
fn parse(input: syn::parse::ParseStream<'_>) -> syn::parse::Result<Self> {
let tag = input.parse::<syn::Ident>()?;
if tag == "validate_hook" {
let _eq = input.parse::<syn::Token![=]>()?;
let literal = input.parse::<syn::LitStr>()?;
let ident = syn::Ident::new(&literal.value(), tag.span());
Ok(ValidateHook(ident))
} else {
panic!("unrecognized gltf attribute");
}
}
}

/// Provided `struct` attributes.
enum StructAttribute {
/// Identifies a indexable data structure.
Expand All @@ -42,6 +21,9 @@ enum StructAttribute {
/// extra `fn index(&self) -> usize` function defined in their
/// generated reader type.
Indexed,

/// A hook for extra validation steps.
ValidateHook(syn::Ident),
}

/// Provided attributes for named `struct` fields.
Expand All @@ -61,6 +43,12 @@ impl syn::parse::Parse for StructAttribute {
let tag = input.parse::<syn::Ident>()?;
match tag.to_string().as_str() {
"indexed" => Ok(Self::Indexed),
"validate_hook" => {
let _eq = input.parse::<syn::Token![=]>()?;
let literal = input.parse::<syn::LitStr>()?;
let ident = syn::Ident::new(&literal.value(), tag.span());
Ok(Self::ValidateHook(ident))
}
unrecognized => {
panic!("gltf({unrecognized}) is not a recognized `struct` attribute")
}
Expand Down Expand Up @@ -400,6 +388,7 @@ fn wrap_indexed(attributes: &[syn::Attribute]) -> TokenStream2 {
}
}
}
StructAttribute::ValidateHook(_) => {}
}
}
}
Expand Down Expand Up @@ -598,32 +587,18 @@ pub fn derive_validate(input: TokenStream) -> TokenStream {
expand_validate(&syn::parse_macro_input!(input as DeriveInput)).into()
}

struct ValidateHook(pub syn::Ident);

impl syn::parse::Parse for ValidateHook {
fn parse(input: syn::parse::ParseStream<'_>) -> syn::parse::Result<Self> {
let tag = input.parse::<syn::Ident>()?;
if tag == "validate_hook" {
let _eq = input.parse::<syn::Token![=]>()?;
let literal = input.parse::<syn::LitStr>()?;
let ident = syn::Ident::new(&literal.value(), tag.span());
Ok(ValidateHook(ident))
} else {
panic!("unrecognized gltf attribute");
}
}
}

fn expand_validate(ast: &DeriveInput) -> proc_macro2::TokenStream2 {
fn expand_validate(ast: &DeriveInput) -> TokenStream2 {
let mut validate_hook = quote! {};
for attr in &ast.attrs {
if attr.path().is_ident("gltf") {
let ValidateHook(ident) = attr
.parse_args::<ValidateHook>()
let parsed_attr = attr
.parse_args::<StructAttribute>()
.expect("failed to parse attribute");
validate_hook = quote! {
#ident(self, _root, _path, _report);
};
if let StructAttribute::ValidateHook(ident) = parsed_attr {
validate_hook = quote! {
#ident(self, _root, _path, _report);
};
}
}
}

Expand Down
Loading

0 comments on commit fcc59f5

Please sign in to comment.