Skip to content

Commit

Permalink
gltf extras
Browse files Browse the repository at this point in the history
  • Loading branch information
arcadeperfect committed Aug 27, 2024
1 parent 78d300b commit c819504
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 24 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file added assets/.DS_Store
Binary file not shown.
Binary file added assets/temp/.DS_Store
Binary file not shown.
Binary file added assets/temp/torus_custom_property.bin
Binary file not shown.
120 changes: 120 additions & 0 deletions assets/temp/torus_custom_property.gltf
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
{
"asset":{
"generator":"Khronos glTF Blender I/O v4.1.63",
"version":"2.0"
},
"scene":0,
"scenes":[
{
"name":"Scene",
"nodes":[
0
]
}
],
"nodes":[
{
"extras":{
"selected_edges_json":"[[2643, 2644], [2644, 2645], [2676, 2709], [2676, 2677], [2677, 2678], [2645, 2678], [2709, 2710], [2710, 2711]]"
},
"mesh":0,
"name":"Torus"
}
],
"meshes":[
{
"name":"Torus",
"primitives":[
{
"attributes":{
"_INDEX":0,
"POSITION":1,
"NORMAL":2,
"TEXCOORD_0":3
},
"indices":4
}
]
}
],
"accessors":[
{
"bufferView":0,
"componentType":5126,
"count":11616,
"type":"SCALAR"
},
{
"bufferView":1,
"componentType":5126,
"count":11616,
"max":[
2.049999952316284,
0.7491505146026611,
2.049999952316284
],
"min":[
-2.049999952316284,
-0.7491505146026611,
-2.049999952316284
],
"type":"VEC3"
},
{
"bufferView":2,
"componentType":5126,
"count":11616,
"type":"VEC3"
},
{
"bufferView":3,
"componentType":5126,
"count":11616,
"type":"VEC2"
},
{
"bufferView":4,
"componentType":5123,
"count":17424,
"type":"SCALAR"
}
],
"bufferViews":[
{
"buffer":0,
"byteLength":46464,
"byteOffset":0,
"target":34962
},
{
"buffer":0,
"byteLength":139392,
"byteOffset":46464,
"target":34962
},
{
"buffer":0,
"byteLength":139392,
"byteOffset":185856,
"target":34962
},
{
"buffer":0,
"byteLength":92928,
"byteOffset":325248,
"target":34962
},
{
"buffer":0,
"byteLength":34848,
"byteOffset":418176,
"target":34963
}
],
"buffers":[
{
"byteLength":453024,
"uri":"torus_custom_property.bin"
}
]
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
'''
this script assigns a unique index to each vertex in the active object and stores it as a custom attribute.
this is we can be sure bevy and blender are using the same indices for the vertices.
'''


import bpy
import bmesh

Expand Down
60 changes: 47 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
use bevy::gltf::GltfExtras;
use bevy::{
animation::animate_targets, asset::AssetMetaCheck, core_pipeline::{bloom::BloomSettings, tonemapping::Tonemapping}, gltf::GltfPlugin, prelude::*, render::{
animation::animate_targets,
asset::AssetMetaCheck,
core_pipeline::{bloom::BloomSettings, tonemapping::Tonemapping},
gltf::GltfPlugin,
prelude::*,
render::{
mesh::{skinning::SkinnedMesh, MeshVertexAttribute},
render_resource::VertexFormat,
}, scene::SceneInstanceReady
},
scene::SceneInstanceReady,
};

use bevy_egui::{egui, EguiContexts, EguiPlugin};
use bevy_panorbit_camera::{PanOrbitCamera, PanOrbitCameraPlugin};
use fill_material::FillMaterial;
Expand All @@ -21,10 +29,11 @@ mod outline_material;

// const PATH: &str = "astro/scene.gltf";
const ASTROPATH: &str = "astro_custom/scene.gltf";
const FOXPATH: &str = "fox.glb";
// const FOXPATH: &str = "fox.glb";
// const PATH: &str = "sphere_flat.gltf";
// const PATH: &str = "sphere.gltf";
// const PATH: &str = "torus.gltf";
const TORUSPATH: &str = "temp/torus_custom_property.gltf";

// #[derive(Resource)]
// struct MyScene(Entity);
Expand Down Expand Up @@ -62,7 +71,7 @@ impl Default for ShaderSettings {

#[derive(Component)]
struct WireframeSettings {
gltf_path: Option<String>,
// gltf_path: Option<String>,
}

const ATTRIBUTE_INDEX: MeshVertexAttribute =
Expand All @@ -86,6 +95,8 @@ fn main() {
.add_systems(Update, process_scene)
.add_systems(Update, ui_system) // Add this line
// .add_systems(Update, ui_example_system) // Add this line
// .add_systems(Update, check_extras)
// .add_systems(Update, check_for_gltf_extras)
.run();
}

Expand Down Expand Up @@ -127,19 +138,34 @@ fn setup(
graph: graph.clone(),
});

let astro = commands
// let astro = commands
// .spawn((
// SceneBundle {
// scene: assets.load(GltfAssetLabel::Scene(0).from_asset(ASTROPATH)),
// transform: Transform::from_xyz(0.0, -1.2, 0.0)
// .with_rotation(Quat::from_rotation_y(0.0))
// .with_scale(Vec3::splat(1.)),
// ..default()
// },
// WireframeSettings {
// gltf_path: None,
// // gltf_path: Some(String::from(ASTROPATH)),

// },
// ))
// .id();

let torus = commands
.spawn((
SceneBundle {
scene: assets.load(GltfAssetLabel::Scene(0).from_asset(ASTROPATH)),
transform: Transform::from_xyz(0.0, -1.2, 0.0)
scene: assets.load(GltfAssetLabel::Scene(0).from_asset(TORUSPATH)),
transform: Transform::from_xyz(0.0, 0.0, 0.0)
.with_rotation(Quat::from_rotation_y(0.0))
.with_scale(Vec3::splat(1.)),
..default()
},
WireframeSettings {
// gltf_path: None,
gltf_path: Some(String::from(ASTROPATH)),


},
))
.id();
Expand All @@ -158,6 +184,8 @@ fn setup(
// .id();
}



fn process_scene(
mut commands: Commands,
mut events: EventReader<SceneInstanceReady>,
Expand All @@ -170,18 +198,24 @@ fn process_scene(
mut outline_materials: ResMut<Assets<OutlineMaterial>>, // Add FillMaterial resource
shader_settings: Res<ShaderSettings>,
processable_scenes: Query<&WireframeSettings>,
gltf_extras: Query<(Entity, &GltfExtras)>, // Modified this line
) {
for event in events.read() {
if !processable_scenes.contains(event.parent) {
continue;
}

if let Ok(wireframe_settings) = processable_scenes.get(event.parent) {
let use_json = wireframe_settings.gltf_path.is_some();
println!("Using JSON: {:?}", use_json);
// Print extras for the parent entity
if let Ok((_, extras)) = gltf_extras.get(event.parent) {
println!("Parent Extras: {:?}", extras);
}

// if event.parent == my_scene_entity.0 {
for entity in children.iter_descendants(event.parent) {
if let Ok((_, extras)) = gltf_extras.get(entity) {
println!("Child Entity Extras: {:?}", extras);
}

if let (Ok((entity, mesh_handle)), Ok(wireframe_settings)) =
(meshes.get(entity), processable_scenes.get(event.parent))
{
Expand Down
24 changes: 13 additions & 11 deletions src/mesh_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@ use crate::{
pub fn mesh_to_wireframe(mesh: &mut Mesh, settings: &WireframeSettings) -> Result<()> {
// apply_random_vertex_colors(mesh);

let line_list = if let Some(_) = &settings.gltf_path {
// Only call json_parse if gltf_path is Some
match json_parse(settings) {
Ok(parsed_data) => mesh.mesh_to_line_list_custom(parsed_data),
Err(e) => panic!("Failed to parse JSON: {}", e),
// Err(e) => return Err(anyhow::anyhow!("Failed to parse JSON: {}", e)),
}
} else {
info!("No JSON file provided, computing line list from mesh vertices");
mesh.mesh_to_line_list()
};
// let line_list = if let Some(_) = &settings.gltf_path {
// // Only call json_parse if gltf_path is Some
// match json_parse(settings) {
// Ok(parsed_data) => mesh.mesh_to_line_list_custom(parsed_data),
// Err(e) => panic!("Failed to parse JSON: {}", e),
// // Err(e) => return Err(anyhow::anyhow!("Failed to parse JSON: {}", e)),
// }
// } else {
// info!("No JSON file provided, computing line list from mesh vertices");
// mesh.mesh_to_line_list()
// };

let line_list = mesh.mesh_to_line_list();

let line_mesh = line_list_to_mesh(&line_list, mesh);
*mesh = line_mesh;
Expand Down

0 comments on commit c819504

Please sign in to comment.