Skip to content

Commit

Permalink
Fix meshasset not saving card height offset, consolidate density
Browse files Browse the repository at this point in the history
  • Loading branch information
TokisanGames committed Dec 31, 2024
1 parent e82107a commit 7ea2191
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 30 deletions.
4 changes: 2 additions & 2 deletions doc/api/class_terrain3dmeshasset.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Properties
+-------------------------------------------------+-------------------------------------------------------------------------------+-------------------+
| GeometryInstance3D.ShadowCastingSetting | :ref:`cast_shadows<class_Terrain3DMeshAsset_property_cast_shadows>` | ``1`` |
+-------------------------------------------------+-------------------------------------------------------------------------------+-------------------+
| ``float`` | :ref:`density<class_Terrain3DMeshAsset_property_density>` | ``-1.0`` |
| ``float`` | :ref:`density<class_Terrain3DMeshAsset_property_density>` | ``10.0`` |
+-------------------------------------------------+-------------------------------------------------------------------------------+-------------------+
| ``int`` | :ref:`generated_faces<class_Terrain3DMeshAsset_property_generated_faces>` | ``2`` |
+-------------------------------------------------+-------------------------------------------------------------------------------+-------------------+
Expand Down Expand Up @@ -205,7 +205,7 @@ Tells the renderer how to cast shadows from this mesh asset onto the terrain and

.. rst-class:: classref-property

``float`` **density** = ``-1.0`` :ref:`🔗<class_Terrain3DMeshAsset_property_density>`
``float`` **density** = ``10.0`` :ref:`🔗<class_Terrain3DMeshAsset_property_density>`

.. rst-class:: classref-property-setget

Expand Down
2 changes: 1 addition & 1 deletion doc/doc_classes/Terrain3DMeshAsset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<member name="cast_shadows" type="int" setter="set_cast_shadows" getter="get_cast_shadows" enum="GeometryInstance3D.ShadowCastingSetting" default="1">
Tells the renderer how to cast shadows from this mesh asset onto the terrain and other objects. This sets [code skip-lint]GeometryInstance3D.cast_shadow[/code] on all MultiMeshInstances used by this mesh.
</member>
<member name="density" type="float" setter="set_density" getter="get_density" default="-1.0">
<member name="density" type="float" setter="set_density" getter="get_density" default="10.0">
Density is used to set the approximate default spacing between instances based on the size of the mesh. When painting meshes on the terrain, mesh density is multiplied by brush strength.
This value is not tied to any real world unit. It is calculated as [code skip-lint]10.f / mesh-&gt;get_aabb().get_volume()[/code], then clamped to a sane range. If the calculated amount is inappropriate, increase or decrease it here.
</member>
Expand Down
2 changes: 1 addition & 1 deletion project/addons/terrain_3d/src/asset_dock.gd
Original file line number Diff line number Diff line change
Expand Up @@ -792,8 +792,8 @@ class ListEntry extends VBoxContainer:
var ma := Terrain3DMeshAsset.new()
if resource is Terrain3DMeshAsset:
ma.id = resource.id
ma.set_scene_file(res)
set_edited_resource(ma, false)
ma.set_scene_file(res)
resource = ma
elif res is Terrain3DMeshAsset and type == Terrain3DAssets.TYPE_MESH:
if resource is Terrain3DMeshAsset:
Expand Down
34 changes: 11 additions & 23 deletions src/terrain_3d_mesh_asset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ void Terrain3DMeshAsset::_set_generated_type(const GenType p_type) {
LOG(DEBUG, "Generating card mesh");
_meshes.push_back(_get_generated_mesh());
_set_material_override(_get_material());
_height_offset = 0.5f;
_generated_faces = 2;
_relative_density = 10.f;
_calculated_density = 10.f;
}
}

Expand Down Expand Up @@ -158,8 +154,7 @@ void Terrain3DMeshAsset::clear() {
_cast_shadows = GeometryInstance3D::SHADOW_CASTING_SETTING_ON;
_generated_faces = 2.f;
_generated_size = Vector2(1.f, 1.f);
_relative_density = -1.f;
_calculated_density = -1.f;
_density = 10.f;
_packed_scene.unref();
_material_override.unref();
_set_generated_type(TYPE_TEXTURE_CARD);
Expand Down Expand Up @@ -187,19 +182,7 @@ void Terrain3DMeshAsset::set_height_offset(const real_t p_offset) {

void Terrain3DMeshAsset::set_density(const real_t p_density) {
LOG(INFO, "Setting mesh density: ", p_density);
if (p_density < 0) {
_relative_density = _calculated_density;
} else {
_relative_density = CLAMP(p_density, 0.01f, 10.f);
}
}

real_t Terrain3DMeshAsset::get_density() const {
if (_relative_density > 0) {
return _relative_density;
} else {
return _calculated_density;
}
_density = CLAMP(p_density, 0.01f, 10.f);
}

void Terrain3DMeshAsset::set_visibility_range(const real_t p_visibility_range) {
Expand Down Expand Up @@ -260,30 +243,33 @@ void Terrain3DMeshAsset::set_scene_file(const Ref<PackedScene> &p_scene_file) {
}
if (_meshes.size() > 0) {
Ref<Mesh> mesh = _meshes[0];
_calculated_density = CLAMP(10.f / mesh->get_aabb().get_volume(), 0.01f, 10.0f);
_relative_density = _calculated_density;
LOG(DEBUG, "Emitting file_changed");
emit_signal("file_changed");
_density = CLAMP(10.f / mesh->get_aabb().get_volume(), 0.01f, 10.0f);
} else {
LOG(ERROR, "No MeshInstance3D found in scene file");
}
notify_property_list_changed();
} else {
set_generated_type(TYPE_TEXTURE_CARD);
_density = 10.f;
}
LOG(DEBUG, "Emitting file_changed");
emit_signal("file_changed");
emit_signal("instancer_setting_changed");
}

void Terrain3DMeshAsset::set_material_override(const Ref<Material> &p_material) {
_set_material_override(p_material);
LOG(DEBUG, "Emitting setting_changed");
emit_signal("setting_changed");
emit_signal("instancer_setting_changed");
}

void Terrain3DMeshAsset::set_generated_type(const GenType p_type) {
_set_generated_type(p_type);
LOG(DEBUG, "Emitting file_changed");
notify_property_list_changed();
emit_signal("file_changed");
emit_signal("instancer_setting_changed");
}

void Terrain3DMeshAsset::set_generated_faces(const int p_count) {
Expand All @@ -295,6 +281,7 @@ void Terrain3DMeshAsset::set_generated_faces(const int p_count) {
_set_material_override(_get_material());
LOG(DEBUG, "Emitting setting_changed");
emit_signal("setting_changed");
emit_signal("instancer_setting_changed");
}
}
}
Expand All @@ -308,6 +295,7 @@ void Terrain3DMeshAsset::set_generated_size(const Vector2 &p_size) {
_set_material_override(_get_material());
LOG(DEBUG, "Emitting setting_changed");
emit_signal("setting_changed");
emit_signal("instancer_setting_changed");
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/terrain_3d_mesh_asset.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ class Terrain3DMeshAsset : public Terrain3DAssetResource {
Vector2 _generated_size = Vector2(1.f, 1.f);
Ref<PackedScene> _packed_scene;
Ref<Material> _material_override;
real_t _relative_density = -1.f;
real_t _calculated_density = -1.f;
real_t _density = 10.f;

// Working data
TypedArray<Mesh> _meshes;
Expand All @@ -65,7 +64,7 @@ class Terrain3DMeshAsset : public Terrain3DAssetResource {
void set_height_offset(const real_t p_offset);
real_t get_height_offset() const { return _height_offset; }
void set_density(const real_t p_density);
real_t get_density() const;
real_t get_density() const { return _density; }

void set_visibility_range(const real_t p_visibility_range);
real_t get_visibility_range() const { return _visibility_range; };
Expand Down

0 comments on commit 7ea2191

Please sign in to comment.