generated from nathanfranke/gdextension
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add terrain occlusion baking to the editor
- Loading branch information
1 parent
ac9fc8a
commit 5dfaa13
Showing
9 changed files
with
278 additions
and
1 deletion.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
project/addons/terrain_3d/editor/components/bake_dialog.gd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
@tool | ||
extends ConfirmationDialog | ||
|
||
var lod: int = 0 | ||
var description: String = "" | ||
|
||
|
||
func _ready() -> void: | ||
set_unparent_when_invisible(true) | ||
about_to_popup.connect(_on_about_to_popup) | ||
visibility_changed.connect(_on_visibility_changed) | ||
%LodBox.value_changed.connect(_on_lod_box_value_changed) | ||
|
||
|
||
func _on_about_to_popup() -> void: | ||
lod = %LodBox.value | ||
|
||
|
||
func _on_visibility_changed() -> void: | ||
# Change text on the autowrap label only when the popup is visible. | ||
# Works around Godot issue #47005: | ||
# https://github.com/godotengine/godot/issues/47005 | ||
if visible: | ||
%DescriptionLabel.text = description | ||
|
||
|
||
func _on_lod_box_value_changed(value) -> void: | ||
lod = %LodBox.value |
41 changes: 41 additions & 0 deletions
41
project/addons/terrain_3d/editor/components/bake_dialog.tscn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
[gd_scene load_steps=2 format=3 uid="uid://bhvrrmb8bk1bt"] | ||
|
||
[ext_resource type="Script" path="res://addons/terrain_3d/editor/components/bake_dialog.gd" id="1_57670"] | ||
|
||
[node name="bake_dialog" type="ConfirmationDialog"] | ||
title = "Bake Terrain3D Mesh" | ||
position = Vector2i(0, 36) | ||
size = Vector2i(400, 115) | ||
visible = true | ||
script = ExtResource("1_57670") | ||
|
||
[node name="VBoxContainer" type="VBoxContainer" parent="."] | ||
anchors_preset = 15 | ||
anchor_right = 1.0 | ||
anchor_bottom = 1.0 | ||
offset_left = 8.0 | ||
offset_top = 8.0 | ||
offset_right = -8.0 | ||
offset_bottom = -49.0 | ||
grow_horizontal = 2 | ||
grow_vertical = 2 | ||
|
||
[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer"] | ||
layout_mode = 2 | ||
theme_override_constants/separation = 20 | ||
|
||
[node name="Label" type="Label" parent="VBoxContainer/HBoxContainer"] | ||
layout_mode = 2 | ||
text = "LOD:" | ||
|
||
[node name="LodBox" type="SpinBox" parent="VBoxContainer/HBoxContainer"] | ||
unique_name_in_owner = true | ||
layout_mode = 2 | ||
size_flags_horizontal = 3 | ||
max_value = 8.0 | ||
value = 4.0 | ||
|
||
[node name="DescriptionLabel" type="Label" parent="VBoxContainer"] | ||
unique_name_in_owner = true | ||
layout_mode = 2 | ||
autowrap_mode = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
extends HBoxContainer | ||
|
||
const BakeDialog: PackedScene = preload("res://addons/terrain_3d/editor/components/bake_dialog.tscn") | ||
const BAKE_MESH_DESCRIPTION: String = "This will create a child MeshInstance3D. LOD4+ is recommended. LOD0 is slow and dense with vertices every 1 unit. It is not an optimal mesh." | ||
const BAKE_OCCLUDER_DESCRIPTION: String = "This will create a child OccluderInstance3D. LOD4+ is recommended and will take 5+ seconds per region to generate. LOD0 is unnecessarily dense and slow." | ||
|
||
var plugin: EditorPlugin | ||
var bake_method: Callable | ||
var bake_dialog: ConfirmationDialog | ||
var bake_mesh_btn: Button | ||
var bake_occluder_btn: Button | ||
|
||
|
||
func _enter_tree() -> void: | ||
bake_dialog = BakeDialog.instantiate() | ||
bake_dialog.hide() | ||
bake_dialog.confirmed.connect(func(): bake_method.call()) | ||
|
||
bake_mesh_btn = Button.new() | ||
bake_mesh_btn.text = "Bake ArrayMesh" | ||
bake_mesh_btn.pressed.connect(_on_bake_mesh_btn_pressed) | ||
add_child(bake_mesh_btn) | ||
|
||
bake_occluder_btn = Button.new() | ||
bake_occluder_btn.text = "Bake Occluder3D" | ||
bake_occluder_btn.pressed.connect(_on_bake_occluder_btn_pressed) | ||
add_child(bake_occluder_btn) | ||
|
||
|
||
func _on_bake_mesh_btn_pressed() -> void: | ||
if plugin.terrain: | ||
bake_method = _bake_mesh | ||
bake_dialog.description = BAKE_MESH_DESCRIPTION | ||
plugin.get_editor_interface().popup_dialog_centered(bake_dialog) | ||
|
||
|
||
func _bake_mesh() -> void: | ||
var mesh: Mesh = plugin.terrain.bake_mesh(bake_dialog.lod, Terrain3DStorage.HEIGHT_FILTER_NEAREST) | ||
if !mesh: | ||
push_error("Failed to bake mesh from Terrain3D") | ||
return | ||
var undo := plugin.get_undo_redo() | ||
|
||
var mesh_instance := MeshInstance3D.new() | ||
mesh_instance.name = &"MeshInstance3D" | ||
mesh_instance.mesh = mesh | ||
mesh_instance.set_skeleton_path(NodePath()) | ||
|
||
undo.create_action("Terrain3D Bake ArrayMesh") | ||
undo.add_do_method(plugin.terrain, &"add_child", mesh_instance, true) | ||
undo.add_undo_method(plugin.terrain, &"remove_child", mesh_instance) | ||
undo.add_do_property(mesh_instance, &"owner", plugin.terrain.owner) | ||
undo.add_do_reference(mesh_instance) | ||
undo.commit_action() | ||
|
||
|
||
func _on_bake_occluder_btn_pressed() -> void: | ||
if plugin.terrain: | ||
bake_method = _bake_occluder | ||
bake_dialog.description = BAKE_OCCLUDER_DESCRIPTION | ||
plugin.get_editor_interface().popup_dialog_centered(bake_dialog) | ||
|
||
|
||
func _bake_occluder() -> void: | ||
var mesh: Mesh = plugin.terrain.bake_mesh(bake_dialog.lod, Terrain3DStorage.HEIGHT_FILTER_MINIMUM) | ||
if !mesh: | ||
push_error("Failed to bake mesh from Terrain3D") | ||
return | ||
assert(mesh.get_surface_count() == 1) | ||
|
||
var undo := plugin.get_undo_redo() | ||
|
||
var occluder := ArrayOccluder3D.new() | ||
var arrays := mesh.surface_get_arrays(0) | ||
assert(arrays.size() > Mesh.ARRAY_INDEX) | ||
assert(arrays[Mesh.ARRAY_INDEX] != null) | ||
occluder.set_arrays(arrays[Mesh.ARRAY_VERTEX], arrays[Mesh.ARRAY_INDEX]) | ||
|
||
var occluder_instance := OccluderInstance3D.new() | ||
occluder_instance.name = &"OccluderInstance3D" | ||
occluder_instance.occluder = occluder | ||
|
||
undo.create_action("Terrain3D Bake Occluder3D") | ||
undo.add_do_method(plugin.terrain, &"add_child", occluder_instance, true) | ||
undo.add_undo_method(plugin.terrain, &"remove_child", occluder_instance) | ||
undo.add_do_property(occluder_instance, &"owner", plugin.terrain.owner) | ||
undo.add_do_reference(occluder_instance) | ||
undo.commit_action() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.