Skip to content

Commit d12e21d

Browse files
committed
Integrate AO Baker into Editor
1 parent 5ac746a commit d12e21d

File tree

508 files changed

+20570
-3731
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

508 files changed

+20570
-3731
lines changed

blender/bake_ao.py

+19-4
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def export_gltf(object, file_path):
4747
bpy.ops.export_scene.gltf(
4848
filepath=file_path,
4949
export_format="GLTF_SEPARATE",
50-
export_keep_originals=False,
50+
export_keep_originals=True,
5151
export_extras=True,
5252
use_mesh_edges=True,
5353
use_selection=True,
@@ -288,9 +288,11 @@ def create_ground_ao_catcher(object: bpy.types.Object) -> bpy.types.Object:
288288
for file in config.file:
289289
clear_scene()
290290

291-
print("baking Ambient Occlusion texture for " + file + "...")
291+
print("File|" + file, flush=True)
292+
print("baking Ambient Occlusion texture for " + file + "...", flush=True)
292293

293294
# Import the glTF model
295+
print("Progress|Importing model...", flush=True)
294296
object = import_gltf(file)
295297

296298
# Specify the output path for the AO map
@@ -303,22 +305,35 @@ def create_ground_ao_catcher(object: bpy.types.Object) -> bpy.types.Object:
303305
ao_ground_output_path = str(ao_output_path.with_stem(ao_output_path.stem + '_ground'))
304306
ao_output_path = str(ao_output_path)
305307

308+
print("Progress|Creating UV map...", flush=True)
306309
create_uv_map(object)
310+
print("Progress|Baking Ambient Occlusion...", flush=True)
307311
bake_ambient_occlusion(object, ao_output_path)
312+
print("Progress|Setting up Ground Ambient Occlusion...", flush=True)
313+
308314
ground_ao_catcher = create_ground_ao_catcher(object)
315+
316+
print("Progress|Baking Ground Ambient Occlusion...", flush=True)
309317
bake_ambient_occlusion(ground_ao_catcher, ao_ground_output_path, is_orm=True)
310318

311-
print("Ambient Occlusion map baked and saved successfully.")
319+
print("Ambient Occlusion map baked and saved successfully.", flush=True)
312320

321+
print("Progress|Clearing Scene...", flush=True)
313322
clear_scene()
314323

315-
object= import_gltf(file)
324+
print("Progress|Re-Importing model...", flush=True)
325+
object = import_gltf(file)
326+
327+
print("Progress|Re-Creating UV Map...", flush=True)
316328
create_uv_map(object)
329+
print("Progress|Setting up Ambient Occlusion map...", flush=True)
317330
setup_ao_map(object, ao_output_path)
318331

319332
ao_ground_object = create_ground_ao_catcher(object)
333+
print("Progress|Setting up Ground Ambient Occlusion map...", flush=True)
320334
setup_ao_decal(ao_ground_object, ao_ground_output_path)
321335

322336
file = str(Path(file).with_suffix(".gltf"))
323337

338+
print("Progress|Exporting modified model...", flush=True)
324339
export_gltf(object, file)

native/Cargo.lock

+14-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

native/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ edition = "2021"
77
crate-type = ["cdylib"]
88

99
[dependencies]
10-
godot = { version = "0.2.2", features = ["experimental-threads", "api-4-3"] }
10+
godot = { version = "0.2.3", features = ["experimental-threads", "api-4-3"] }
1111
godot-rust-script = { git = "https://github.com/titannano/godot-rust-script", rev = "46b151f558370c260fa1e79502637f128ea7bb7f" }
1212
lerp = "0.4.0"
1313
backtrace = "0.3.64"

native/src/editor.rs

+74-2
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,114 @@
1+
mod ao_baker;
12
mod building_imports;
23
mod gltf;
4+
pub mod ui;
35

6+
use std::num::NonZero;
7+
8+
use ao_baker::AoBaker;
49
use gltf::GltfImporter;
10+
use godot::builtin::{Dictionary, VariantType};
511
use godot::classes::notify::NodeNotification;
6-
use godot::classes::{EditorPlugin, GltfDocument, IEditorPlugin};
7-
use godot::obj::{Base, Gd, NewGd, WithBaseField};
12+
use godot::classes::{EditorPlugin, GltfDocument, IEditorPlugin, ProjectSettings};
13+
use godot::global::PropertyHint;
14+
use godot::obj::{Base, Gd, NewGd, OnReady, WithBaseField};
815
use godot::register::{godot_api, GodotClass};
916

1017
use building_imports::SetupBuildingImports;
1118

1219
use crate::engine_callable;
20+
use crate::util::variant_type_default_value;
1321

1422
#[derive(GodotClass)]
1523
#[class(tool, base=EditorPlugin)]
1624
struct EditorExtension {
1725
setup_building_imports: Gd<SetupBuildingImports>,
1826
gltf_importer: Gd<GltfImporter>,
27+
ao_baker: OnReady<Gd<AoBaker>>,
28+
1929
base: Base<EditorPlugin>,
2030
}
2131

32+
const fn new_non_zero(value: u32) -> NonZero<u32> {
33+
NonZero::new(value).unwrap()
34+
}
35+
36+
#[godot_api]
37+
impl EditorExtension {
38+
fn define_project_settings(
39+
settings: &[(&'static str, VariantType, PropertyHint, &'static str)],
40+
project_settings: &mut Gd<ProjectSettings>,
41+
) {
42+
for (name, ty, hint, hint_str) in settings {
43+
Self::define_project_settings_property(name, *ty, *hint, hint_str, project_settings);
44+
}
45+
}
46+
47+
fn define_project_settings_property(
48+
name: &'static str,
49+
ty: VariantType,
50+
hint: PropertyHint,
51+
hint_str: &'static str,
52+
project_settings: &mut Gd<ProjectSettings>,
53+
) {
54+
let default_value = variant_type_default_value(ty);
55+
56+
if !project_settings.has_setting(name) {
57+
project_settings.set_setting(name, &default_value);
58+
}
59+
60+
project_settings.set_initial_value(name, &default_value);
61+
62+
let mut property = Dictionary::new();
63+
64+
property.set("name", name);
65+
property.set("type", ty);
66+
property.set("hint", hint);
67+
property.set("hint_string", hint_str);
68+
69+
project_settings.add_property_info(&property);
70+
}
71+
}
72+
2273
#[godot_api]
2374
impl IEditorPlugin for EditorExtension {
2475
fn init(base: Base<EditorPlugin>) -> Self {
2576
Self {
2677
setup_building_imports: SetupBuildingImports::new(base.to_gd().get_editor_interface()),
2778
gltf_importer: GltfImporter::new_gd(),
79+
ao_baker: OnReady::manual(),
2880
base,
2981
}
3082
}
3183

3284
fn enter_tree(&mut self) {
3385
let building_imports = self.setup_building_imports.clone();
3486

87+
Self::define_project_settings(&AoBaker::SETTINGS, &mut ProjectSettings::singleton());
88+
3589
self.base_mut().add_tool_menu_item(
3690
"Setup Building Imports...",
3791
&engine_callable!(&building_imports, SetupBuildingImports::start),
3892
);
3993

94+
let ao_baker = {
95+
let mut base = self.base_mut();
96+
97+
AoBaker::new(
98+
base.get_editor_interface()
99+
.expect("editor interface should be available after entering the tree"),
100+
base.get_tree()
101+
.expect("SceneTree should be available after entering the tree"),
102+
)
103+
};
104+
105+
self.ao_baker.init(ao_baker);
106+
107+
let callable = &engine_callable!(&self.ao_baker, AoBaker::bake);
108+
109+
self.base_mut()
110+
.add_tool_menu_item("Bake Ambient Occlusion...", callable);
111+
40112
GltfDocument::register_gltf_document_extension(&self.gltf_importer);
41113
}
42114

0 commit comments

Comments
 (0)