Skip to content

Commit

Permalink
WIP stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Warreign committed Aug 29, 2024
1 parent 545cebc commit 4c64964
Show file tree
Hide file tree
Showing 15 changed files with 566 additions and 15 deletions.
22 changes: 16 additions & 6 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@ file(GLOB_RECURSE gdcesium_src CONFIGURE_DEPENDS

target_sources( ${PROJECT_NAME}
PRIVATE
# Example.h
# Example.cpp
# GDExtensionTemplate.h
# GDExtensionTemplate.cpp
# RegisterExtension.cpp
${gdcesium_src}
# ${gdcesium_src}
Example.h
Example.cpp
GDCesium.h
GDCesium.cpp
RegisterExtension.cpp
GodotAssetAccessor.h
GodotAssetAccessor.cpp
GodotTaskProcessor.h
GodotTaskProcessor.cpp
CesiumTileset.h
CesiumTileset.cpp
CesiumTilesetPlugin.h
CesiumTilesetPlugin.cpp
CesiumTilesetInspector.h
CesiumTilesetInspector.cpp
)

target_include_directories( ${PROJECT_NAME}
Expand Down
115 changes: 115 additions & 0 deletions src/CesiumTileset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include "CesiumTileset.h"

#include "godot_cpp/classes/gltf_document.hpp"
#include "godot_cpp/classes/gltf_state.hpp"
#include "godot_cpp/classes/file_access.hpp"
#include "godot_cpp/classes/engine.hpp"
#include "godot_cpp/classes/node.hpp"
#include "godot_cpp/classes/gltf_document_extension_convert_importer_mesh.hpp"

#include "godot_cpp/classes/scene_tree.hpp"
#include "godot_cpp/classes/dir_access.hpp"

#include "godot_cpp/variant/utility_functions.hpp"

namespace CesiumGodot
{
void _recursive_change_owner(godot::Node* node, godot::Node* owner)
{
godot::Callable chown_callable = callable_mp_static(_recursive_change_owner);
node->get_children().map(chown_callable.bind(owner));
}

CesiumTileset::CesiumTileset()
{
m_assetId = -1;
m_gltfNode = nullptr;
}

CesiumTileset::~CesiumTileset()
{
}

godot::String CesiumTileset::getAccessToken() const
{
return m_accessToken;
}

void CesiumTileset::setAccessToken( const godot::String &inToken )
{

m_accessToken = inToken;
}


int32_t CesiumTileset::getAssetId() const
{
return m_assetId;
}

void CesiumTileset::setAssetId( int32_t id )
{
m_assetId = id;
}

void CesiumTileset::loadGltfPath(const godot::String& gltfPath)
{
godot::UtilityFunctions::print("Loading GLTF model: ", gltfPath);

if (!godot::FileAccess::file_exists(gltfPath))
{
godot::UtilityFunctions::print("File ", gltfPath, " does not exist");
return;
}

if (m_gltfNode != nullptr)
{
if (get_children().has(m_gltfNode)) remove_child(m_gltfNode);
memdelete(m_gltfNode);
}

godot::GLTFDocument* gltfDoc = memnew(godot::GLTFDocument);

// Register convert extension. Apparently needed for correct editor runtime mesh import
godot::Ref<godot::GLTFDocumentExtensionConvertImporterMesh> gltfConvertExt;
gltfConvertExt.instantiate();
gltfDoc->register_gltf_document_extension(gltfConvertExt);

godot::Ref<godot::GLTFState> gltfState;
gltfState.instantiate();
gltfState->set_handle_binary_image(godot::GLTFState::HANDLE_BINARY_EMBED_AS_UNCOMPRESSED);

godot::Error err = gltfDoc->append_from_file(gltfPath, gltfState, 0, "base_path?");
godot::UtilityFunctions::print("Load result: ", err);

m_gltfNode = gltfDoc->generate_scene(gltfState);
add_child(m_gltfNode);
_recursive_change_owner(m_gltfNode, get_owner());

memdelete(gltfDoc);
}

void CesiumTileset::loadGltf()
{
const godot::String& path = m_accessToken;
loadGltfPath(path);
}

void CesiumTileset::_bind_methods()
{
godot::ClassDB::bind_method( godot::D_METHOD("set_access_token"), &CesiumTileset::setAccessToken );
godot::ClassDB::bind_method( godot::D_METHOD("get_access_token"), &CesiumTileset::getAccessToken );
godot::ClassDB::bind_method( godot::D_METHOD("set_asset_id"), &CesiumTileset::setAssetId );
godot::ClassDB::bind_method( godot::D_METHOD("get_asset_id"), &CesiumTileset::getAssetId );
godot::ClassDB::bind_method( godot::D_METHOD("load_gltf"), &CesiumTileset::loadGltf );

ADD_GROUP("Cesium", "cesium_");
ADD_SUBGROUP("Asset Data", "cesium_assetData_");

ADD_PROPERTY( godot::PropertyInfo( godot::Variant::STRING, "cesium_assetData_access_token" ),
"set_access_token", "get_access_token");
ADD_PROPERTY( godot::PropertyInfo( godot::Variant::INT, "cesium_assetData_asset_id" ),
"set_asset_id", "get_asset_id");

}
}
48 changes: 48 additions & 0 deletions src/CesiumTileset.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

#include "godot_cpp/classes/node3d.hpp"
#include "godot_cpp/classes/node.hpp"
#include "godot_cpp/classes/gltf_state.hpp"
#include "godot_cpp/classes/gltf_document.hpp"

#include "Cesium3DTilesSelection/Tileset.h"

#include <memory>


#include "godot_cpp/classes/sphere_mesh.hpp"
#include "godot_cpp/classes/mesh_instance3d.hpp"

namespace CesiumGodot
{
/// @brief Godot object representing cesium tileset
class CesiumTileset : public godot::Node3D
{
GDCLASS( CesiumTileset, godot::Node3D )

public:
CesiumTileset();
~CesiumTileset();

godot::String getAccessToken() const;
void setAccessToken(const godot::String& inToken);
int32_t getAssetId() const;
void setAssetId(int32_t id);

/// @brief Load glTF model at gltfPath and add it to this node's children
/// @param gltfPath model path
void loadGltfPath(const godot::String& gltfPath);
void loadGltf();

protected:
static void _bind_methods();

private:
godot::Node* m_gltfNode;

std::shared_ptr<Cesium3DTilesSelection::Tileset> m_tileset;

godot::String m_accessToken;
int32_t m_assetId;
};
}
74 changes: 74 additions & 0 deletions src/CesiumTilesetInspector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "CesiumTilesetInspector.h"

#include "godot_cpp/variant/utility_functions.hpp"
#include "godot_cpp/core/memory.hpp"
#include "godot_cpp/classes/thread.hpp"
#include "godot_cpp/classes/label.hpp"
#include "godot_cpp/classes/button.hpp"
#include "godot_cpp/variant/callable.hpp"

#include "CesiumTileset.h"

namespace CesiumGodot
{


CesiumTilesetInspector::CesiumTilesetInspector()
{
godot::UtilityFunctions::print("Initializing tileset inspector plugin");
}

CesiumTilesetInspector::~CesiumTilesetInspector()
{
godot::UtilityFunctions::print("Deinitializing tileset inspector plugin");
}

bool CesiumTilesetInspector::_can_handle(godot::Object * object) const
{
godot::UtilityFunctions::print(object->get_class());
return object->is_class("CesiumTileset");
}

void memfree_helper(void* ptr)
{
godot::Memory::free_static(ptr);
}

void CesiumTilesetInspector::_parse_begin( godot::Object *object )
{
godot::Label* pLabel = memnew(godot::Label);
add_custom_control(pLabel);
}

void CesiumTilesetInspector::_parse_category( godot::Object *object,
const godot::String &category )
{
godot::UtilityFunctions::print("Parsing category: ", category);

if (category == "CesiumTileset")
{
godot::Button* pAddButton = memnew(godot::Button);
pAddButton->set_text("Add GLTF");
pAddButton->set_action_mode(godot::BaseButton::ACTION_MODE_BUTTON_PRESS);
pAddButton->connect("pressed", callable_mp(godot::Object::cast_to<CesiumTileset>(object), &CesiumTileset::loadGltf), godot::Object::ConnectFlags::CONNECT_DEFERRED);
add_custom_control(pAddButton);
}
}

void CesiumTilesetInspector::_parse_group( godot::Object *object, const godot::String &group )
{
godot::UtilityFunctions::print("Parsing group: ", group);

if (group == "Cesium/Asset Data")
{
}
}

void CesiumTilesetInspector::_bind_methods()
{
}

void CesiumTilesetInspector::add_gltf_helper()
{
}
}
39 changes: 39 additions & 0 deletions src/CesiumTilesetInspector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include "godot_cpp/classes/editor_inspector_plugin.hpp"

#include <memory>

namespace godot
{
class Label;
class Button;
} // namespace godot


namespace CesiumGodot
{
/// @brief Godot editor inspector plugin for cesium tileset node
class CesiumTilesetInspector : public godot::EditorInspectorPlugin
{
GDCLASS( CesiumTilesetInspector, godot::EditorInspectorPlugin )

public:
CesiumTilesetInspector();
~CesiumTilesetInspector();

bool _can_handle(godot::Object* object) const override;
void _parse_begin(godot::Object* object) override;
void _parse_category(godot::Object* object, const godot::String& category) override;
void _parse_group(godot::Object* object, const godot::String& group) override;

static void _bind_methods();

private:
void add_gltf_helper();

// std::shared_ptr<godot::Label> m_pLabel;
godot::Label* m_pLabel;
godot::Button* m_pAddGlftButton;
};
}
61 changes: 61 additions & 0 deletions src/CesiumTilesetPlugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "CesiumTilesetPlugin.h"

#include "godot_cpp/variant/utility_functions.hpp"
#include "godot_cpp/classes/button.hpp"
#include "godot_cpp/core/memory.hpp"
#include "godot_cpp/classes/editor_inspector_plugin.hpp"
#include "godot_cpp/variant/callable.hpp"

#include "CesiumTilesetPlugin.h"

namespace CesiumGodot
{
CesiumTilesetPlugin::CesiumTilesetPlugin()
{
godot::UtilityFunctions::print("CesiumTilesetPlugin initialized.");
}

CesiumTilesetPlugin::~CesiumTilesetPlugin()
{
godot::UtilityFunctions::print("CesiumTilesetPlugin deinitialized.");
}

bool CesiumTilesetPlugin::_has_main_screen() const
{
return true;
}

godot::String CesiumTilesetPlugin::_get_plugin_name() const
{
return m_pluginName;
}

void CesiumTilesetPlugin::_enter_tree()
{
godot::UtilityFunctions::print("CesiumTilesetPlugin entered");

// godot::Ref<godot::Button> button;
// button.instantiate();

m_rTilesetInspectorPlugin.instantiate();
add_inspector_plugin(m_rTilesetInspectorPlugin);
godot::UtilityFunctions::print("Inspector plugin is valid: ", m_rTilesetInspectorPlugin.is_valid());
}

void CesiumTilesetPlugin::_exit_tree()
{
godot::UtilityFunctions::print("CesiumTilesetPlugin exited");

remove_inspector_plugin(m_rTilesetInspectorPlugin);
m_rTilesetInspectorPlugin.unref();
}

void CesiumTilesetPlugin::_bind_methods()
{
}

void CesiumTilesetPlugin::print_helper()
{
godot::UtilityFunctions::print("Test button pressed");
}
}
Loading

0 comments on commit 4c64964

Please sign in to comment.