Added a new TerraMaterialCache class.

This commit is contained in:
Relintai 2021-08-02 13:13:37 +02:00
parent e9c314eedc
commit f53e6572b1
6 changed files with 233 additions and 6 deletions

1
SCsub
View File

@ -27,6 +27,7 @@ sources = [
"library/terraman_library.cpp", "library/terraman_library.cpp",
"library/terraman_library_simple.cpp", "library/terraman_library_simple.cpp",
"library/terra_material_cache.cpp",
"nodes/terraman_light.cpp", "nodes/terraman_light.cpp",

View File

@ -26,6 +26,8 @@ def get_doc_classes():
"TerramanLibrary", "TerramanLibrary",
"TerramanLibraryMergerPCM", "TerramanLibraryMergerPCM",
"TerraMaterialCache",
"TerraCubePoints", "TerraCubePoints",
"TerraMesherCubic", "TerraMesherCubic",
"TerraMeshData", "TerraMeshData",

View File

@ -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<Material> TerraMaterialCache::material_get(const int index) {
ERR_FAIL_INDEX_V(index, _materials.size(), Ref<Material>(NULL));
return _materials[index];
}
Ref<Material> TerraMaterialCache::material_lod_get(const int index) {
ERR_FAIL_COND_V(_materials.size() == 0, Ref<Material>(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<Material> &value) {
ERR_FAIL_COND(!value.is_valid());
_materials.push_back(value);
}
void TerraMaterialCache::material_set(const int index, const Ref<Material> &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<Variant> TerraMaterialCache::materials_get() {
VARIANT_ARRAY_GET(_materials);
}
void TerraMaterialCache::materials_set(const Vector<Variant> &materials) {
_materials.clear();
for (int i = 0; i < materials.size(); i++) {
Ref<Material> material = Ref<Material>(materials[i]);
_materials.push_back(material);
}
}
//Surfaces
Ref<TerraSurface> TerraMaterialCache::voxel_surface_get(const int index) {
return Ref<TerraSurface>();
}
void TerraMaterialCache::voxel_surface_add(Ref<TerraSurface> value) {
}
void TerraMaterialCache::voxel_surface_set(int index, Ref<TerraSurface> 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> 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);
}

View File

@ -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> material_get(const int index);
Ref<Material> material_lod_get(const int index);
void material_add(const Ref<Material> &value);
void material_set(const int index, const Ref<Material> &value);
void material_remove(const int index);
int material_get_num() const;
void materials_clear();
Vector<Variant> materials_get();
void materials_set(const Vector<Variant> &materials);
virtual Ref<TerraSurface> voxel_surface_get(const int index);
virtual void voxel_surface_add(Ref<TerraSurface> value);
virtual void voxel_surface_set(const int index, Ref<TerraSurface> 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> texture);
TerraMaterialCache();
~TerraMaterialCache();
protected:
static void _bind_methods();
Vector<Ref<TerraSurface>> _surfaces;
Vector<Ref<Material>> _materials;
//Ref<TextureMerger> merger; inherited
int material_users;
};
#endif

View File

@ -35,12 +35,6 @@ SOFTWARE.
#include "terraman_library_merger.h" #include "terraman_library_merger.h"
class MaterialEntry { //registered + reference
//Vector<Ref<TerraSurface> > surfaces;
//Vector<Ref<Material> > materials;
//Ref<TextureMerger> merger;
//int material_users;
};
//pcm = per chunk material //pcm = per chunk material
class TerramanLibraryMergerPCM : public TerramanLibraryMerger { //inherit it from the normal library? class TerramanLibraryMergerPCM : public TerramanLibraryMerger { //inherit it from the normal library?

View File

@ -28,6 +28,7 @@ SOFTWARE.
#include "library/terraman_library.h" #include "library/terraman_library.h"
#include "library/terraman_library_simple.h" #include "library/terraman_library_simple.h"
#include "library/terra_material_cache.h"
#ifdef TEXTURE_PACKER_PRESENT #ifdef TEXTURE_PACKER_PRESENT
#include "library/terra_surface_merger.h" #include "library/terra_surface_merger.h"
@ -78,6 +79,8 @@ void register_terraman_types() {
ClassDB::register_class<TerramanLibrary>(); ClassDB::register_class<TerramanLibrary>();
ClassDB::register_class<TerramanLibrarySimple>(); ClassDB::register_class<TerramanLibrarySimple>();
ClassDB::register_class<TerraMaterialCache>();
#ifdef TEXTURE_PACKER_PRESENT #ifdef TEXTURE_PACKER_PRESENT
ClassDB::register_class<TerraSurfaceMerger>(); ClassDB::register_class<TerraSurfaceMerger>();
ClassDB::register_class<TerramanLibraryMerger>(); ClassDB::register_class<TerramanLibraryMerger>();