Skip to content

Commit

Permalink
fixed procedural meshes serialization
Browse files Browse the repository at this point in the history
- fixes #657
  • Loading branch information
mrDIMAS committed Jun 22, 2024
1 parent 6b1508d commit 643a3cc
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 39 deletions.
2 changes: 2 additions & 0 deletions fyrox-impl/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ use std::{

use crate::plugin::dynamic::DynamicPlugin;
use crate::plugin::{DynamicPluginState, PluginContainer};
use crate::scene::mesh::surface::SurfaceData;
use fyrox_core::futures::future::join_all;
use fyrox_core::notify;
use fyrox_core::notify::{EventKind, RecursiveMode, Watcher};
Expand Down Expand Up @@ -1210,6 +1211,7 @@ pub(crate) fn initialize_resource_manager_loaders(
state.constructors_container.add::<Material>();
state.constructors_container.add::<Font>();
state.constructors_container.add::<UserInterface>();
state.constructors_container.add::<SurfaceData>();

let loaders = &mut state.loaders;
loaders.set(model_loader);
Expand Down
2 changes: 1 addition & 1 deletion fyrox-impl/src/renderer/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ impl RenderDataBundleStorageTrait for RenderDataBundleStorage {
// Create temporary surface data (valid for one frame).
let data = SurfaceResource::new_ok(
ResourceKind::Embedded,
SurfaceData::new(vertex_buffer, triangle_buffer, true),
SurfaceData::new(vertex_buffer, triangle_buffer),
);

self.bundle_map.insert(key, self.bundles.len());
Expand Down
1 change: 0 additions & 1 deletion fyrox-impl/src/renderer/light/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ impl DeferredLightRenderer {
TriangleDefinition([20, 21, 22]),
TriangleDefinition([20, 22, 23]),
]),
true,
),
GeometryBufferKind::StaticDraw,
state,
Expand Down
8 changes: 4 additions & 4 deletions fyrox-impl/src/resource/fbx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ enum FbxMeshBuilder {
impl FbxMeshBuilder {
fn build(self) -> SurfaceData {
match self {
FbxMeshBuilder::Static(builder) => SurfaceData::from_raw_mesh(builder.build(), false),
FbxMeshBuilder::Animated(builder) => SurfaceData::from_raw_mesh(builder.build(), false),
FbxMeshBuilder::Static(builder) => SurfaceData::from_raw_mesh(builder.build()),
FbxMeshBuilder::Animated(builder) => SurfaceData::from_raw_mesh(builder.build()),
}
}
}
Expand Down Expand Up @@ -279,7 +279,7 @@ async fn create_surfaces(
surface_data.blend_shapes_container =
make_blend_shapes_container(&surface_data.vertex_buffer, data.blend_shapes);
let mut surface = Surface::new(SurfaceResource::new_ok(
ResourceKind::Embedded,
ResourceKind::External(model_path.to_path_buf()),
surface_data,
));
surface.vertex_weights = data.skin_data;
Expand All @@ -291,7 +291,7 @@ async fn create_surfaces(
surface_data.blend_shapes_container =
make_blend_shapes_container(&surface_data.vertex_buffer, data.blend_shapes);
let mut surface = Surface::new(SurfaceResource::new_ok(
ResourceKind::Embedded,
ResourceKind::External(model_path.to_path_buf()),
surface_data,
));
surface.vertex_weights = data.skin_data;
Expand Down
2 changes: 0 additions & 2 deletions fyrox-impl/src/scene/mesh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ impl RenderDataBundleStorageTrait for BatchContainer {
VertexBuffer::new_with_layout(layout, 0, BytesStorage::with_capacity(4096))
.unwrap(),
TriangleBuffer::new(Vec::with_capacity(4096)),
false,
),
),
material: material.clone(),
Expand Down Expand Up @@ -238,7 +237,6 @@ impl RenderDataBundleStorageTrait for BatchContainer {
SurfaceData::new(
src_data.vertex_buffer.clone_empty(4096),
TriangleBuffer::new(Vec::with_capacity(4096)),
false,
),
),
material: material.clone(),
Expand Down
38 changes: 8 additions & 30 deletions fyrox-impl/src/scene/mesh/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,6 @@ pub struct SurfaceData {
pub geometry_buffer: TriangleBuffer,
/// A container for blend shapes.
pub blend_shapes_container: Option<BlendShapesContainer>,
// If true - indicates that surface was generated and does not have reference
// resource. Procedural data will be serialized.
is_embedded: bool,
#[reflect(hidden)]
pub(crate) cache_index: Arc<AtomicIndex>,
}
Expand Down Expand Up @@ -229,15 +226,12 @@ impl ResourceData for SurfaceData {
}

impl SurfaceData {
/// Creates new data source using given vertices and indices. `is_procedural` flags affects serialization - when it
/// is `true` the content of the vertex and triangle buffers will be serialized. It is useful if you need to save
/// surfaces with procedural content.
pub fn new(vertex_buffer: VertexBuffer, triangles: TriangleBuffer, is_embedded: bool) -> Self {
/// Creates new data source using given vertices and indices.
pub fn new(vertex_buffer: VertexBuffer, triangles: TriangleBuffer) -> Self {
Self {
vertex_buffer,
geometry_buffer: triangles,
blend_shapes_container: None,
is_embedded,
cache_index: Arc::new(AtomicIndex::unassigned()),
}
}
Expand Down Expand Up @@ -273,15 +267,14 @@ impl SurfaceData {

/// Converts raw mesh into "renderable" mesh. It is useful to build procedural meshes. See [`RawMesh`] docs for more
/// info.
pub fn from_raw_mesh<T>(raw: RawMesh<T>, is_embedded: bool) -> Self
pub fn from_raw_mesh<T>(raw: RawMesh<T>) -> Self
where
T: VertexTrait,
{
Self {
vertex_buffer: VertexBuffer::new(raw.vertices.len(), raw.vertices).unwrap(),
geometry_buffer: TriangleBuffer::new(raw.triangles),
blend_shapes_container: Default::default(),
is_embedded,
cache_index: Arc::new(AtomicIndex::unassigned()),
}
}
Expand Down Expand Up @@ -407,7 +400,6 @@ impl SurfaceData {
Self::new(
VertexBuffer::new(vertices.len(), vertices).unwrap(),
TriangleBuffer::new(triangles),
true,
)
}

Expand Down Expand Up @@ -446,7 +438,6 @@ impl SurfaceData {
Self::new(
VertexBuffer::new(vertices.len(), vertices).unwrap(),
TriangleBuffer::new(triangles),
true,
)
}

Expand Down Expand Up @@ -485,7 +476,6 @@ impl SurfaceData {
TriangleDefinition([0, 1, 2]),
TriangleDefinition([0, 2, 3]),
]),
true,
);
data.calculate_tangents().unwrap();
data.transform_geometry(transform).unwrap();
Expand Down Expand Up @@ -590,7 +580,7 @@ impl SurfaceData {
}
}

let mut data = Self::from_raw_mesh(builder.build(), true);
let mut data = Self::from_raw_mesh(builder.build());
data.calculate_tangents().unwrap();
data.transform_geometry(transform).unwrap();
data
Expand Down Expand Up @@ -668,7 +658,7 @@ impl SurfaceData {
));
}

let mut data = Self::from_raw_mesh(builder.build(), true);
let mut data = Self::from_raw_mesh(builder.build());
data.calculate_tangents().unwrap();
data.transform_geometry(transform).unwrap();
data
Expand Down Expand Up @@ -725,7 +715,6 @@ impl SurfaceData {
let mut data = Self::new(
VertexBuffer::new(vertices.len(), vertices).unwrap(),
TriangleBuffer::new(triangles),
true,
);
data.calculate_tangents().unwrap();
data.transform_geometry(transform).unwrap();
Expand Down Expand Up @@ -840,7 +829,7 @@ impl SurfaceData {
));
}

let mut data = Self::from_raw_mesh(builder.build(), true);
let mut data = Self::from_raw_mesh(builder.build());
data.calculate_tangents().unwrap();
data.transform_geometry(transform).unwrap();
data
Expand Down Expand Up @@ -1019,7 +1008,6 @@ impl SurfaceData {
let mut data = Self::new(
VertexBuffer::new(vertices.len(), vertices).unwrap(),
TriangleBuffer::new(triangles),
true,
);
data.calculate_tangents().unwrap();
data.transform_geometry(&transform).unwrap();
Expand All @@ -1040,24 +1028,14 @@ impl SurfaceData {
self.geometry_buffer.modify().clear();
self.vertex_buffer.modify().clear();
}

/// Marks surface's content as procedural (created from code) or not. Content of procedural surfaces will
/// be serialized. It is useful if you need to save procedural surfaces to disk or some other storage.
pub fn set_embedded(&mut self, is_embedded: bool) {
self.is_embedded = is_embedded;
}
}

impl Visit for SurfaceData {
fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
let mut region = visitor.enter_region(name)?;

self.is_embedded.visit("IsProcedural", &mut region)?;

if self.is_embedded {
self.vertex_buffer.visit("VertexBuffer", &mut region)?;
self.geometry_buffer.visit("GeometryBuffer", &mut region)?
}
self.vertex_buffer.visit("VertexBuffer", &mut region)?;
self.geometry_buffer.visit("GeometryBuffer", &mut region)?;

Ok(())
}
Expand Down
1 change: 0 additions & 1 deletion fyrox-impl/src/scene/terrain/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ impl TerrainGeometry {
let mut surface_data = SurfaceData::new(
VertexBuffer::new::<StaticVertex>(0, vec![]).unwrap(),
TriangleBuffer::default(),
false,
);

let mut vertex_buffer_mut = surface_data.vertex_buffer.modify();
Expand Down

0 comments on commit 643a3cc

Please sign in to comment.