From f53e6572b1ca70cb918c9d7fec150107193681e7 Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 2 Aug 2021 13:13:37 +0200 Subject: [PATCH] Added a new TerraMaterialCache class. --- SCsub | 1 + config.py | 2 + library/terra_material_cache.cpp | 142 ++++++++++++++++++++++++++ library/terra_material_cache.h | 85 +++++++++++++++ library/terraman_library_merger_pcm.h | 6 -- register_types.cpp | 3 + 6 files changed, 233 insertions(+), 6 deletions(-) create mode 100644 library/terra_material_cache.cpp create mode 100644 library/terra_material_cache.h diff --git a/SCsub b/SCsub index 2aeb4fc..7eb2c13 100644 --- a/SCsub +++ b/SCsub @@ -27,6 +27,7 @@ sources = [ "library/terraman_library.cpp", "library/terraman_library_simple.cpp", + "library/terra_material_cache.cpp", "nodes/terraman_light.cpp", diff --git a/config.py b/config.py index 41be662..58f8405 100644 --- a/config.py +++ b/config.py @@ -26,6 +26,8 @@ def get_doc_classes(): "TerramanLibrary", "TerramanLibraryMergerPCM", + "TerraMaterialCache", + "TerraCubePoints", "TerraMesherCubic", "TerraMeshData", diff --git a/library/terra_material_cache.cpp b/library/terra_material_cache.cpp new file mode 100644 index 0000000..535b9fa --- /dev/null +++ b/library/terra_material_cache.cpp @@ -0,0 +1,142 @@ +/* +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 "terra_material_cache.h" + +//Materials +Ref TerraMaterialCache::material_get(const int index) { + ERR_FAIL_INDEX_V(index, _materials.size(), Ref(NULL)); + + return _materials[index]; +} + +Ref TerraMaterialCache::material_lod_get(const int index) { + ERR_FAIL_COND_V(_materials.size() == 0, Ref(NULL)); + + if (index < 0) { + return _materials[0]; + } + + if (index >= _materials.size()) { + return _materials[_materials.size() - 1]; + } + + return _materials[index]; +} + +void TerraMaterialCache::material_add(const Ref &value) { + ERR_FAIL_COND(!value.is_valid()); + + _materials.push_back(value); +} + +void TerraMaterialCache::material_set(const int index, const Ref &value) { + ERR_FAIL_INDEX(index, _materials.size()); + + _materials.set(index, value); +} + +void TerraMaterialCache::material_remove(const int index) { + _materials.remove(index); +} + +int TerraMaterialCache::material_get_num() const { + return _materials.size(); +} + +void TerraMaterialCache::materials_clear() { + _materials.clear(); +} + +Vector TerraMaterialCache::materials_get() { + VARIANT_ARRAY_GET(_materials); +} + +void TerraMaterialCache::materials_set(const Vector &materials) { + _materials.clear(); + + for (int i = 0; i < materials.size(); i++) { + Ref material = Ref(materials[i]); + + _materials.push_back(material); + } +} + +//Surfaces +Ref TerraMaterialCache::voxel_surface_get(const int index) { + return Ref(); +} +void TerraMaterialCache::voxel_surface_add(Ref value) { +} +void TerraMaterialCache::voxel_surface_set(int index, Ref value) { +} +void TerraMaterialCache::voxel_surface_remove(const int index) { +} +int TerraMaterialCache::voxel_surface_get_num() const { + return 0; +} +void TerraMaterialCache::voxel_surfaces_clear() { +} + +void TerraMaterialCache::refresh_rects() { +} + +void TerraMaterialCache::setup_material_albedo(Ref texture) { + if (has_method("_setup_material_albedo")) + call("_setup_material_albedo", texture); +} + +TerraMaterialCache::TerraMaterialCache() { + material_users = 0; +} + +TerraMaterialCache::~TerraMaterialCache() { + _materials.clear(); + _surfaces.clear(); +} + +void TerraMaterialCache::_bind_methods() { + BIND_VMETHOD(MethodInfo("_setup_material_albedo", PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"))); + + ClassDB::bind_method(D_METHOD("material_get", "index"), &TerraMaterialCache::material_get); + ClassDB::bind_method(D_METHOD("material_lod_get", "index"), &TerraMaterialCache::material_lod_get); + ClassDB::bind_method(D_METHOD("material_add", "value"), &TerraMaterialCache::material_add); + ClassDB::bind_method(D_METHOD("material_set", "index", "value"), &TerraMaterialCache::material_set); + ClassDB::bind_method(D_METHOD("material_remove", "index"), &TerraMaterialCache::material_remove); + ClassDB::bind_method(D_METHOD("material_get_num"), &TerraMaterialCache::material_get_num); + ClassDB::bind_method(D_METHOD("materials_clear"), &TerraMaterialCache::materials_clear); + + ClassDB::bind_method(D_METHOD("materials_get"), &TerraMaterialCache::materials_get); + ClassDB::bind_method(D_METHOD("materials_set"), &TerraMaterialCache::materials_set); + ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "materials", PROPERTY_HINT_NONE, "17/17:Material", PROPERTY_USAGE_DEFAULT, "Material"), "materials_set", "materials_get"); + + ClassDB::bind_method(D_METHOD("voxel_surface_get", "index"), &TerraMaterialCache::voxel_surface_get); + ClassDB::bind_method(D_METHOD("voxel_surface_add", "value"), &TerraMaterialCache::voxel_surface_add); + ClassDB::bind_method(D_METHOD("voxel_surface_set", "index", "surface"), &TerraMaterialCache::voxel_surface_set); + ClassDB::bind_method(D_METHOD("voxel_surface_remove", "index"), &TerraMaterialCache::voxel_surface_remove); + ClassDB::bind_method(D_METHOD("voxel_surface_get_num"), &TerraMaterialCache::voxel_surface_get_num); + ClassDB::bind_method(D_METHOD("voxel_surfaces_clear"), &TerraMaterialCache::voxel_surfaces_clear); + + ClassDB::bind_method(D_METHOD("refresh_rects"), &TerraMaterialCache::refresh_rects); + + ClassDB::bind_method(D_METHOD("setup_material_albedo", "texture"), &TerraMaterialCache::setup_material_albedo); +} diff --git a/library/terra_material_cache.h b/library/terra_material_cache.h new file mode 100644 index 0000000..3c77eee --- /dev/null +++ b/library/terra_material_cache.h @@ -0,0 +1,85 @@ +/* +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 TERRA_MATERIAL_CACHE_H +#define TERRA_MATERIAL_CACHE_H + +#include "core/version.h" + +#if VERSION_MAJOR > 3 +#include "core/io/resource.h" +#include "core/math/color.h" +#include "core/templates/vector.h" +#else +#include "core/color.h" +#include "core/resource.h" +#include "core/vector.h" +#endif + +#include "core/math/rect2.h" +#include "scene/resources/material.h" + +#include "terraman_library.h" + +#include "../defines.h" + +class TerramanLibrary; + +class TerraMaterialCache : public Resource { + GDCLASS(TerraMaterialCache, Resource) + +public: + Ref material_get(const int index); + Ref material_lod_get(const int index); + void material_add(const Ref &value); + void material_set(const int index, const Ref &value); + void material_remove(const int index); + int material_get_num() const; + void materials_clear(); + + Vector materials_get(); + void materials_set(const Vector &materials); + + virtual Ref voxel_surface_get(const int index); + virtual void voxel_surface_add(Ref value); + virtual void voxel_surface_set(const int index, Ref value); + virtual void voxel_surface_remove(const int index); + virtual int voxel_surface_get_num() const; + virtual void voxel_surfaces_clear(); + + virtual void refresh_rects(); + + void setup_material_albedo(Ref texture); + + TerraMaterialCache(); + ~TerraMaterialCache(); + +protected: + static void _bind_methods(); + + Vector> _surfaces; + Vector> _materials; + //Ref merger; inherited + int material_users; +}; + +#endif diff --git a/library/terraman_library_merger_pcm.h b/library/terraman_library_merger_pcm.h index 964d1c9..cde23a4 100644 --- a/library/terraman_library_merger_pcm.h +++ b/library/terraman_library_merger_pcm.h @@ -35,12 +35,6 @@ SOFTWARE. #include "terraman_library_merger.h" -class MaterialEntry { //registered + reference - //Vector > surfaces; - //Vector > materials; - //Ref merger; - //int material_users; -}; //pcm = per chunk material class TerramanLibraryMergerPCM : public TerramanLibraryMerger { //inherit it from the normal library? diff --git a/register_types.cpp b/register_types.cpp index 31bfcb2..6f70816 100644 --- a/register_types.cpp +++ b/register_types.cpp @@ -28,6 +28,7 @@ SOFTWARE. #include "library/terraman_library.h" #include "library/terraman_library_simple.h" +#include "library/terra_material_cache.h" #ifdef TEXTURE_PACKER_PRESENT #include "library/terra_surface_merger.h" @@ -78,6 +79,8 @@ void register_terraman_types() { ClassDB::register_class(); ClassDB::register_class(); + ClassDB::register_class(); + #ifdef TEXTURE_PACKER_PRESENT ClassDB::register_class(); ClassDB::register_class();