From fbafc568d78858914d45077774ce161ec4f9ed68 Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 16 Aug 2021 17:44:28 +0200 Subject: [PATCH] Added TiledWall and TiledWallData classes. Just a skeleton for now. --- SCsub | 3 + config.py | 3 + register_types.cpp | 6 + tiled_wall/tiled_wall.cpp | 244 +++++++++++++++++++++++++++++++++ tiled_wall/tiled_wall.h | 94 +++++++++++++ tiled_wall/tiled_wall_data.cpp | 183 +++++++++++++++++++++++++ tiled_wall/tiled_wall_data.h | 101 ++++++++++++++ 7 files changed, 634 insertions(+) create mode 100644 tiled_wall/tiled_wall.cpp create mode 100644 tiled_wall/tiled_wall.h create mode 100644 tiled_wall/tiled_wall_data.cpp create mode 100644 tiled_wall/tiled_wall_data.h diff --git a/SCsub b/SCsub index ba452b8..1bb99a6 100644 --- a/SCsub +++ b/SCsub @@ -29,6 +29,9 @@ sources = [ "lights/prop_light.cpp", + "tiled_wall/tiled_wall.cpp", + "tiled_wall/tiled_wall_data.cpp", + "props/prop_data.cpp", "props/prop_data_entry.cpp", "props/prop_data_scene.cpp", diff --git a/config.py b/config.py index a5ea046..44d2e90 100644 --- a/config.py +++ b/config.py @@ -16,6 +16,9 @@ def get_doc_classes(): "PropDataScene", "PropDataPortal", "PropData", + + "TiledWall", + "TiledWallData", "PropDataProcessor", diff --git a/register_types.cpp b/register_types.cpp index b92c068..c898067 100644 --- a/register_types.cpp +++ b/register_types.cpp @@ -30,6 +30,9 @@ SOFTWARE. #include "core/engine.h" #endif +#include "tiled_wall/tiled_wall.h" +#include "tiled_wall/tiled_wall_data.h" + #include "props/prop_data.h" #include "props/prop_data_entry.h" #include "props/prop_data_light.h" @@ -71,6 +74,9 @@ static PropUtils *prop_utils = NULL; static PropCache *prop_texture_cache = NULL; void register_props_types() { + ClassDB::register_class(); + ClassDB::register_class(); + ClassDB::register_class(); ClassDB::register_class(); diff --git a/tiled_wall/tiled_wall.cpp b/tiled_wall/tiled_wall.cpp new file mode 100644 index 0000000..b2e0b3b --- /dev/null +++ b/tiled_wall/tiled_wall.cpp @@ -0,0 +1,244 @@ +#include "tiled_wall.h" + +#include "core/version.h" + +#include "core/version.h" +#include "scene/resources/texture.h" + +#if VERSION_MAJOR < 4 +#include "core/image.h" + +#define GET_WORLD get_world +#else +#include "core/io/image.h" + +#define GET_WORLD get_world_3d +#endif + +#if TEXTURE_PACKER_PRESENT +#include "../../texture_packer/texture_resource/packer_image_resource.h" +#endif + +#include "scene/3d/mesh_instance.h" + +int TiledWall::get_width() const { + return _width; +} +void TiledWall::set_width(const int value) { + _width = value; +} + +int TiledWall::get_heigt() const { + return _height; +} +void TiledWall::set_heigt(const int value) { + _height = value; +} + +Ref TiledWall::get_texture() { + return _texture; +} +void TiledWall::set_texture(const Ref &texture) { + _texture = texture; + + setup_material_texture(); + refresh(); +} + +Ref TiledWall::get_material() { + return _material; +} +void TiledWall::set_material(const Ref &mat) { + _material = mat; + + setup_material_texture(); + refresh(); +} + +AABB TiledWall::get_aabb() const { + return AABB(); +} + +PoolVector TiledWall::get_faces(uint32_t p_usage_flags) const { + PoolVector faces; +/* + if (_mesh.is_valid()) { + Array arrs = _mesh->get_array_const(); + + if (arrs.size() != Mesh::ARRAY_MAX) { + return faces; + } + + PoolVector vertices = arrs[Mesh::ARRAY_VERTEX]; + PoolVector indices = arrs[Mesh::ARRAY_INDEX]; + + int ts = indices.size() / 3; + faces.resize(ts); + + PoolVector::Write w = faces.write(); + PoolVector::Read rv = vertices.read(); + PoolVector::Read ri = indices.read(); + + for (int i = 0; i < ts; i++) { + int im3 = (i * 3); + + for (int j = 0; j < 3; j++) { + w[i].vertex[j] = rv[indices[im3 + j]]; + } + } + + w.release(); + } +*/ + return faces; +} + +void TiledWall::refresh() { + if (!is_inside_tree()) { + return; + } +/* + if (_mesh_rid == RID()) { + _mesh_rid = VisualServer::get_singleton()->mesh_create(); + + VS::get_singleton()->instance_set_base(get_instance(), _mesh_rid); + } + + VisualServer::get_singleton()->mesh_clear(_mesh_rid); + + if (!_mesh.is_valid()) { + return; + } + + Array arr = _mesh->get_array(); + + if (arr.size() != Mesh::ARRAY_MAX) { + return; + } + + PoolVector vertices = arr[Mesh::ARRAY_VERTEX]; + + if (vertices.size() == 0) { + return; + } + + VisualServer::get_singleton()->mesh_add_surface_from_arrays(_mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, arr); + + if (_material.is_valid()) { + VisualServer::get_singleton()->mesh_surface_set_material(_mesh_rid, 0, _material->get_rid()); + } + */ +} + +void TiledWall::setup_material_texture() { + if (!is_inside_tree()) { + return; + } + + if (!_texture.is_valid()) { + if (_material.is_valid()) { + Ref sm = _material; + + if (!sm.is_valid()) { + return; + } + + sm->set_texture(SpatialMaterial::TEXTURE_ALBEDO, _texture); + } + + return; + } else { + Ref sm = _material; + + if (!sm.is_valid()) { + return; + } + +#if TEXTURE_PACKER_PRESENT + Ref r = _texture; + + if (r.is_valid()) { + Ref i = r->get_data(); + + Ref tex; + tex.instance(); +#if VERSION_MAJOR < 4 + tex->create_from_image(i, 0); +#else + tex->create_from_image(i); +#endif + + if (sm.is_valid()) { + sm->set_texture(SpatialMaterial::TEXTURE_ALBEDO, tex); + } + + return; + } +#endif + + sm->set_texture(SpatialMaterial::TEXTURE_ALBEDO, _texture); + } +} + +void TiledWall::free_meshes() { + if (_mesh_rid != RID()) { + VS::get_singleton()->free(_mesh_rid); + _mesh_rid = RID(); + } +} + +TiledWall::TiledWall() { + _width = 1; + _height = 1; + + //temporary + set_portal_mode(PORTAL_MODE_GLOBAL); + + //set_notify_transform(true); +} +TiledWall::~TiledWall() { + _texture.unref(); +} + +void TiledWall::_notification(int p_what) { + switch (p_what) { + case NOTIFICATION_ENTER_TREE: { + setup_material_texture(); + refresh(); + + break; + } + case NOTIFICATION_EXIT_TREE: { + free_meshes(); + break; + } + /* + case NOTIFICATION_TRANSFORM_CHANGED: { + VisualServer *vs = VisualServer::get_singleton(); + + vs->instance_set_transform(get_instance(), get_global_transform()); + + break; + }*/ + } +} + +void TiledWall::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_width"), &TiledWall::get_width); + ClassDB::bind_method(D_METHOD("set_width", "value"), &TiledWall::set_width); + ADD_PROPERTY(PropertyInfo(Variant::INT, "width"), "set_width", "get_width"); + + ClassDB::bind_method(D_METHOD("get_heigt"), &TiledWall::get_heigt); + ClassDB::bind_method(D_METHOD("set_heigt", "value"), &TiledWall::set_heigt); + ADD_PROPERTY(PropertyInfo(Variant::INT, "heigt"), "set_heigt", "get_heigt"); + + ClassDB::bind_method(D_METHOD("get_texture"), &TiledWall::get_texture); + ClassDB::bind_method(D_METHOD("set_texture", "value"), &TiledWall::set_texture); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture"); + + ClassDB::bind_method(D_METHOD("get_material"), &TiledWall::get_material); + ClassDB::bind_method(D_METHOD("set_material", "value"), &TiledWall::set_material); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "Material"), "set_material", "get_material"); + + ClassDB::bind_method(D_METHOD("refresh"), &TiledWall::refresh); +} diff --git a/tiled_wall/tiled_wall.h b/tiled_wall/tiled_wall.h new file mode 100644 index 0000000..6d0aa79 --- /dev/null +++ b/tiled_wall/tiled_wall.h @@ -0,0 +1,94 @@ +/* +Copyright (c) 2020 Péter Magyar + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#ifndef TILED_WALL_H +#define TILED_WALL_H + +#include "core/version.h" +#include "scene/resources/texture.h" + +#if VERSION_MAJOR < 4 +#include "scene/3d/visual_instance.h" +#else +#include "scene/3d/node_3d.h" + +#define SpatialMaterial StandardMaterial3D +#define Spatial Node3D +#define Texture Texture2D +#endif + +#include "core/math/vector3.h" + +class PropInstance; + +class TiledWall : public GeometryInstance { + GDCLASS(TiledWall, GeometryInstance); + +public: + int get_width() const; + void set_width(const int value); + + int get_heigt() const; + void set_heigt(const int value); + + //array textures + //array flavour textures + //this class->add merger + //merge tex then: + //mesher->draw_tiled_wall(width, height, transform, array texture_rects, array flavour texture_rects, int tilemode) + //for propinstancemerger-> same, in diff thread + //job->add_wall()->add in the propentry + a transform -> can be easily meshed + + //get_faces -> keep mesh in mesher -> query + + + Ref get_texture(); + void set_texture(const Ref &texture); + + Ref get_material(); + void set_material(const Ref &mat); + + AABB get_aabb() const; + PoolVector get_faces(uint32_t p_usage_flags) const; + + void refresh(); + void setup_material_texture(); + void free_meshes(); + + TiledWall(); + ~TiledWall(); + +protected: + void _notification(int p_what); + static void _bind_methods(); + +private: + int _width; + int _height; + + Ref _texture; + Ref _material; + + RID _mesh_rid; +}; + +#endif diff --git a/tiled_wall/tiled_wall_data.cpp b/tiled_wall/tiled_wall_data.cpp new file mode 100644 index 0000000..0dff2fe --- /dev/null +++ b/tiled_wall/tiled_wall_data.cpp @@ -0,0 +1,183 @@ +/* +Copyright (c) 2019-2021 Péter Magyar + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "tiled_wall_data.h" + +#if VERSION_MAJOR < 4 +#include "servers/physics_server.h" +#else +#include "servers/physics_server_3d.h" + +#define Shape Shape3D +#endif + +int TiledWallData::get_id() const { + return _id; +} +void TiledWallData::set_id(const int value) { + _id = value; +} + +bool TiledWallData::get_snap_to_mesh() const { + return _snap_to_mesh; +} +void TiledWallData::set_snap_to_mesh(const bool value) { + _snap_to_mesh = value; +} + +Vector3 TiledWallData::get_snap_axis() const { + return _snap_axis; +} +void TiledWallData::set_snap_axis(const Vector3 &value) { + _snap_axis = value; +} + +Ref TiledWallData::get_prop(const int index) const { + ERR_FAIL_INDEX_V(index, _props.size(), Ref()); + + return _props.get(index); +} +void TiledWallData::set_prop(const int index, const Ref prop) { + ERR_FAIL_INDEX(index, _props.size()); + + _props.set(index, prop); +} +void TiledWallData::add_prop(const Ref prop) { + _props.push_back(prop); +} +void TiledWallData::remove_prop(const int index) { + ERR_FAIL_INDEX(index, _props.size()); + + _props.remove(index); +} + +int TiledWallData::get_prop_count() const { + return _props.size(); +} + +Vector TiledWallData::get_props() { + Vector r; + for (int i = 0; i < _props.size(); i++) { +#if VERSION_MAJOR < 4 + r.push_back(_props[i].get_ref_ptr()); +#else + r.push_back(_props[i]); +#endif + } + return r; +} +void TiledWallData::set_props(const Vector &props) { + _props.clear(); + for (int i = 0; i < props.size(); i++) { + Ref prop = Ref(props[i]); + + _props.push_back(prop); + } +} + +#if TEXTURE_PACKER_PRESENT +void TiledWallData::add_textures_into(Ref texture_packer) { + ERR_FAIL_COND(!texture_packer.is_valid()); + + for (int i = 0; i < _props.size(); ++i) { + + Ref entry = _props.get(i); + + if (entry.is_valid()) { + texture_packer->add_texture(entry); + } + } +} +#endif + +bool TiledWallData::get_is_room() const { + return _is_room; +} +void TiledWallData::set_is_room(const bool value) { + _is_room = value; +} + +PoolVector3Array TiledWallData::get_room_bounds() { + return _room_bounds; +} +void TiledWallData::set_room_bounds(const PoolVector3Array &bounds) { + _room_bounds = bounds; +} + +void TiledWallData::copy_from(const Ref &prop_data) { + _id = prop_data->_id; + _snap_to_mesh = prop_data->_snap_to_mesh; + _snap_axis = prop_data->_snap_axis; + + _props.clear(); + + for (int i = 0; i < prop_data->_props.size(); ++i) { + _props.push_back(prop_data->_props[i]); + } + + emit_changed(); +} + +TiledWallData::TiledWallData() { + _id = 0; + _snap_to_mesh = false; + _is_room = false; + _snap_axis = Vector3(0, -1, 0); +} +TiledWallData::~TiledWallData() { + _props.clear(); +} + +void TiledWallData::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_snap_to_mesh"), &TiledWallData::get_snap_to_mesh); + ClassDB::bind_method(D_METHOD("set_snap_to_mesh", "value"), &TiledWallData::set_snap_to_mesh); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "snap_to_mesh"), "set_snap_to_mesh", "get_snap_to_mesh"); + + ClassDB::bind_method(D_METHOD("get_snap_axis"), &TiledWallData::get_snap_axis); + ClassDB::bind_method(D_METHOD("set_snap_axis", "value"), &TiledWallData::set_snap_axis); + ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "snap_axis"), "set_snap_axis", "get_snap_axis"); + + ClassDB::bind_method(D_METHOD("get_prop", "index"), &TiledWallData::get_prop); + ClassDB::bind_method(D_METHOD("set_prop", "index", "spell"), &TiledWallData::set_prop); + ClassDB::bind_method(D_METHOD("add_prop", "prop"), &TiledWallData::add_prop); + ClassDB::bind_method(D_METHOD("remove_prop", "index"), &TiledWallData::remove_prop); + + ClassDB::bind_method(D_METHOD("get_prop_count"), &TiledWallData::get_prop_count); + + ClassDB::bind_method(D_METHOD("get_props"), &TiledWallData::get_props); + ClassDB::bind_method(D_METHOD("set_props", "props"), &TiledWallData::set_props); + ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "props", PROPERTY_HINT_NONE, "17/17:Texture", PROPERTY_USAGE_DEFAULT, "Texture"), "set_props", "get_props"); + +#if TEXTURE_PACKER_PRESENT + ClassDB::bind_method(D_METHOD("add_textures_into", "texture_packer"), &TiledWallData::add_textures_into); +#endif + + ClassDB::bind_method(D_METHOD("get_is_room"), &TiledWallData::get_is_room); + ClassDB::bind_method(D_METHOD("set_is_room", "value"), &TiledWallData::set_is_room); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "is_room"), "set_is_room", "get_is_room"); + + ClassDB::bind_method(D_METHOD("get_room_bounds"), &TiledWallData::get_room_bounds); + ClassDB::bind_method(D_METHOD("set_room_bounds", "value"), &TiledWallData::set_room_bounds); + ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR3_ARRAY, "room_bounds"), "set_room_bounds", "get_room_bounds"); + + ClassDB::bind_method(D_METHOD("copy_from", "prop_data"), &TiledWallData::copy_from); +} diff --git a/tiled_wall/tiled_wall_data.h b/tiled_wall/tiled_wall_data.h new file mode 100644 index 0000000..87b21ee --- /dev/null +++ b/tiled_wall/tiled_wall_data.h @@ -0,0 +1,101 @@ +/* +Copyright (c) 2019-2021 Péter Magyar + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#ifndef TILED_WALL_DATA_H +#define TILED_WALL_DATA_H + +#include "core/version.h" + +#if VERSION_MAJOR > 3 +#include "core/object/reference.h" +#include "core/templates/vector.h" +#else +#include "core/reference.h" +#include "core/vector.h" +#endif + +#include "core/math/rect2.h" +#include "core/math/transform.h" +#include "core/math/vector2.h" +#include "core/math/vector3.h" + +#include "core/version.h" + +#include "scene/resources/texture.h" + +#if TEXTURE_PACKER_PRESENT +#include "../../texture_packer/texture_packer.h" +#endif + +class TiledWallData : public Resource { + GDCLASS(TiledWallData, Resource); + +public: + int get_id() const; + void set_id(const int value); + + bool get_snap_to_mesh() const; + void set_snap_to_mesh(const bool value); + + Vector3 get_snap_axis() const; + void set_snap_axis(const Vector3 &value); + + Ref get_prop(const int index) const; + void set_prop(const int index, const Ref prop); + void add_prop(const Ref prop); + void remove_prop(const int index); + + int get_prop_count() const; + + Vector get_props(); + void set_props(const Vector &props); + +#if TEXTURE_PACKER_PRESENT + void add_textures_into(Ref texture_packer); +#endif + + bool get_is_room() const; + void set_is_room(const bool value); + + PoolVector3Array get_room_bounds(); + void set_room_bounds(const PoolVector3Array &bounds); + + void copy_from(const Ref &prop_data); + + TiledWallData(); + ~TiledWallData(); + +protected: + static void _bind_methods(); + +private: + int _id; + bool _snap_to_mesh; + Vector3 _snap_axis; + + Vector> _props; + + bool _is_room; + PoolVector3Array _room_bounds; +}; + +#endif