props_2d/material_cache/prop_material_cache_2d.cpp

364 lines
11 KiB
C++
Raw Normal View History

2021-11-23 16:53:47 +01:00
/*
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 "prop_material_cache_2d.h"
2021-11-23 16:53:47 +01:00
#include "../props/prop_data_2d.h"
#include "../props/prop_data_prop_2d.h"
#include "../props/prop_data_tiled_wall_2d.h"
#include "../singleton/prop_cache_2d.h"
#include "../tiled_wall/tiled_wall_data_2d.h"
2021-11-23 16:53:47 +01:00
#if MESH_DATA_RESOURCE_PRESENT
#define PROPS_PRESENT 1
#include "../../mesh_data_resource/props/prop_data_mesh_data.h"
#undef PROPS_PRESENT
#endif
#if VERSION_MAJOR > 3
#define VARIANT_ARRAY_GET(arr) \
Vector<Variant> r; \
for (int i = 0; i < arr.size(); i++) { \
r.push_back(arr[i]); \
} \
return r;
#else
#define VARIANT_ARRAY_GET(arr) \
Vector<Variant> r; \
for (int i = 0; i < arr.size(); i++) { \
r.push_back(arr[i].get_ref_ptr()); \
} \
return r;
#endif
2021-11-24 14:00:16 +01:00
bool PropMaterialCache2D::get_initialized() {
2021-11-23 16:53:47 +01:00
return _initialized;
}
2021-11-24 14:00:16 +01:00
void PropMaterialCache2D::set_initialized(const bool value) {
2021-11-23 16:53:47 +01:00
_initialized = value;
}
2021-11-24 14:00:16 +01:00
bool PropMaterialCache2D::mutex_locked() {
2021-11-23 16:53:47 +01:00
return _locked;
}
2021-11-24 14:00:16 +01:00
void PropMaterialCache2D::mutex_lock() {
2021-11-23 16:53:47 +01:00
_mutex.lock();
}
2021-11-24 14:00:16 +01:00
void PropMaterialCache2D::mutex_unlock() {
2021-11-23 16:53:47 +01:00
_mutex.unlock();
}
2021-11-24 14:00:16 +01:00
int PropMaterialCache2D::get_ref_count() {
2021-11-23 16:53:47 +01:00
return _ref_count;
}
2021-11-24 14:00:16 +01:00
void PropMaterialCache2D::set_ref_count(const int value) {
2021-11-23 16:53:47 +01:00
_ref_count = value;
}
2021-11-24 14:00:16 +01:00
void PropMaterialCache2D::inc_ref_count() {
2021-11-23 16:53:47 +01:00
_ref_count += 1;
}
2021-11-24 14:00:16 +01:00
void PropMaterialCache2D::dec_ref_count() {
2021-11-23 16:53:47 +01:00
_ref_count -= 1;
}
//Materials
2021-11-24 14:00:16 +01:00
Ref<Material> PropMaterialCache2D::material_get(const int index) {
2021-11-23 16:53:47 +01:00
ERR_FAIL_INDEX_V(index, _materials.size(), Ref<Material>(NULL));
return _materials[index];
}
2021-11-24 14:00:16 +01:00
Ref<Material> PropMaterialCache2D::material_lod_get(const int index) {
2021-11-23 16:53:47 +01:00
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];
}
2021-11-24 14:00:16 +01:00
void PropMaterialCache2D::material_add(const Ref<Material> &value) {
2021-11-23 16:53:47 +01:00
ERR_FAIL_COND(!value.is_valid());
_materials.push_back(value);
}
2021-11-24 14:00:16 +01:00
void PropMaterialCache2D::material_set(const int index, const Ref<Material> &value) {
2021-11-23 16:53:47 +01:00
ERR_FAIL_INDEX(index, _materials.size());
_materials.set(index, value);
}
2021-11-24 14:00:16 +01:00
void PropMaterialCache2D::material_remove(const int index) {
2021-11-23 16:53:47 +01:00
_materials.remove(index);
}
2021-11-24 14:00:16 +01:00
int PropMaterialCache2D::material_get_num() const {
2021-11-23 16:53:47 +01:00
return _materials.size();
}
2021-11-24 14:00:16 +01:00
void PropMaterialCache2D::materials_clear() {
2021-11-23 16:53:47 +01:00
_materials.clear();
}
2021-11-24 14:00:16 +01:00
Vector<Variant> PropMaterialCache2D::materials_get() {
2021-11-23 16:53:47 +01:00
VARIANT_ARRAY_GET(_materials);
}
2021-11-24 14:00:16 +01:00
void PropMaterialCache2D::materials_set(const Vector<Variant> &materials) {
2021-11-23 16:53:47 +01:00
_materials.clear();
for (int i = 0; i < materials.size(); i++) {
Ref<Material> material = Ref<Material>(materials[i]);
_materials.push_back(material);
}
}
2021-11-24 14:00:16 +01:00
void PropMaterialCache2D::texture_add(const Ref<Texture> &texture) {
2021-11-23 16:53:47 +01:00
_textures.push_back(texture);
}
2021-11-24 14:00:16 +01:00
void PropMaterialCache2D::texture_remove(const Ref<Texture> &texture) {
2021-11-23 16:53:47 +01:00
for (int i = 0; i < _textures.size(); ++i) {
if (_textures[i] == texture) {
_textures.remove(i);
return;
}
}
}
2021-11-24 14:00:16 +01:00
void PropMaterialCache2D::texture_remove_index(const int index) {
2021-11-23 16:53:47 +01:00
ERR_FAIL_INDEX(index, _textures.size());
_textures.remove(index);
}
2021-11-24 14:00:16 +01:00
void PropMaterialCache2D::textures_clear() {
2021-11-23 16:53:47 +01:00
_textures.clear();
}
2021-11-24 14:00:16 +01:00
int PropMaterialCache2D::texture_count() {
2021-11-23 16:53:47 +01:00
return _textures.size();
}
2021-11-24 14:00:16 +01:00
Ref<Texture> PropMaterialCache2D::texture_get(const int index) {
2021-11-23 16:53:47 +01:00
ERR_FAIL_INDEX_V(index, _textures.size(), Ref<Texture>());
return _textures[index];
}
2021-11-24 14:00:16 +01:00
Ref<AtlasTexture> PropMaterialCache2D::texture_get_atlas(const int index) {
2021-11-23 16:53:47 +01:00
ERR_FAIL_INDEX_V(index, _textures.size(), Ref<AtlasTexture>());
return texture_get_atlas_tex(_textures[index]);
}
2021-11-24 14:00:16 +01:00
Ref<AtlasTexture> PropMaterialCache2D::texture_get_atlas_tex(const Ref<Texture> &texture) {
2021-11-23 16:53:47 +01:00
return Ref<AtlasTexture>();
}
2021-11-24 14:00:16 +01:00
Rect2 PropMaterialCache2D::texture_get_uv_rect(const Ref<Texture> &texture) {
2021-11-23 16:53:47 +01:00
return Rect2(0, 0, 1, 1);
}
2021-11-24 14:00:16 +01:00
void PropMaterialCache2D::prop_add_textures(const Ref<PropData2D> &prop) {
2021-11-23 16:53:47 +01:00
if (!prop.is_valid()) {
return;
}
for (int i = 0; i < prop->get_prop_count(); ++i) {
#if MESH_DATA_RESOURCE_PRESENT
2021-11-24 14:00:16 +01:00
Ref<PropData2DMeshData> pdm = prop->get_prop(i);
2021-11-23 16:53:47 +01:00
if (pdm.is_valid()) {
Ref<Texture> tex = pdm->get_texture();
if (!tex.is_valid())
continue;
texture_add(tex);
continue;
}
#endif
2021-11-24 14:00:16 +01:00
Ref<PropDataTiledWall2D> pdtw = prop->get_prop(i);
2021-11-23 16:53:47 +01:00
if (pdtw.is_valid()) {
2021-11-24 14:00:16 +01:00
Ref<TiledWallData2D> twd = pdtw->get_data();
2021-11-23 16:53:47 +01:00
if (!twd.is_valid())
continue;
2021-11-24 14:00:16 +01:00
twd->setup_cache(Ref<PropMaterialCache2D>(this));
2021-11-23 16:53:47 +01:00
continue;
}
2021-11-24 14:00:16 +01:00
Ref<PropDataProp2D> pdp = prop->get_prop(i);
2021-11-23 16:53:47 +01:00
if (pdp.is_valid()) {
prop_add_textures(pdp->get_prop());
}
}
}
2021-11-24 14:00:16 +01:00
void PropMaterialCache2D::prop_remove_textures(const Ref<PropData2D> &prop) {
2021-11-23 16:53:47 +01:00
if (!prop.is_valid()) {
return;
}
for (int i = 0; i < prop->get_prop_count(); ++i) {
#if MESH_DATA_RESOURCE_PRESENT
2021-11-24 14:00:16 +01:00
Ref<PropData2DMeshData> pdm = prop->get_prop(i);
2021-11-23 16:53:47 +01:00
if (pdm.is_valid()) {
Ref<Texture> tex = pdm->get_texture();
if (!tex.is_valid())
continue;
texture_remove(tex);
}
#endif
2021-11-24 14:00:16 +01:00
Ref<PropDataTiledWall2D> pdtw = prop->get_prop(i);
2021-11-23 16:53:47 +01:00
if (pdtw.is_valid()) {
2021-11-24 14:00:16 +01:00
Ref<TiledWallData2D> twd = pdtw->get_data();
2021-11-23 16:53:47 +01:00
if (!twd.is_valid())
continue;
for (int j = 0; j < twd->get_texture_count(); ++j) {
const Ref<Texture> &tex = twd->get_texture(j);
if (tex.is_valid()) {
texture_remove(tex);
}
}
for (int j = 0; j < twd->get_flavour_texture_count(); ++j) {
const Ref<Texture> &tex = twd->get_flavour_texture(j);
if (tex.is_valid()) {
texture_remove(tex);
}
}
continue;
}
2021-11-24 14:00:16 +01:00
Ref<PropDataProp2D> pdp = prop->get_prop(i);
2021-11-23 16:53:47 +01:00
if (pdp.is_valid()) {
prop_remove_textures(pdp);
}
}
}
2021-11-24 14:00:16 +01:00
void PropMaterialCache2D::refresh_rects() {
2021-11-23 16:53:47 +01:00
_initialized = true;
}
2021-11-24 14:00:16 +01:00
void PropMaterialCache2D::initial_setup_default() {
2021-11-23 16:53:47 +01:00
//Note: call only on the main thread! Shader->duplicate() can crash if done from an another thread!
2021-11-24 14:00:16 +01:00
PropCache2D *pc = PropCache2D::get_singleton();
2021-11-23 16:53:47 +01:00
pc->ensure_materials_loaded();
int matc = pc->material_get_num();
for (int i = 0; i < matc; ++i) {
Ref<Material> m = pc->material_get(i);
ERR_CONTINUE(!m.is_valid());
Ref<Material> md = m->duplicate();
_materials.push_back(md);
}
}
2021-11-24 14:00:16 +01:00
void PropMaterialCache2D::setup_material_albedo(Ref<Texture> texture) {
2021-11-23 16:53:47 +01:00
if (has_method("_setup_material_albedo"))
call("_setup_material_albedo", texture);
}
2021-11-24 14:00:16 +01:00
PropMaterialCache2D::PropMaterialCache2D() {
2021-11-23 16:53:47 +01:00
_ref_count = 0;
_initialized = false;
_locked = false;
}
2021-11-24 14:00:16 +01:00
PropMaterialCache2D::~PropMaterialCache2D() {
2021-11-23 16:53:47 +01:00
_materials.clear();
}
2021-11-24 14:00:16 +01:00
void PropMaterialCache2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_initialized"), &PropMaterialCache2D::get_initialized);
ClassDB::bind_method(D_METHOD("set_initialized", "value"), &PropMaterialCache2D::set_initialized);
2021-11-23 16:53:47 +01:00
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "initialized"), "set_initialized", "get_initialized");
2021-11-24 14:00:16 +01:00
ClassDB::bind_method(D_METHOD("mutex_locked"), &PropMaterialCache2D::mutex_locked);
ClassDB::bind_method(D_METHOD("mutex_lock"), &PropMaterialCache2D::mutex_lock);
ClassDB::bind_method(D_METHOD("mutex_unlock"), &PropMaterialCache2D::mutex_unlock);
2021-11-23 16:53:47 +01:00
2021-11-24 14:00:16 +01:00
ClassDB::bind_method(D_METHOD("get_ref_count"), &PropMaterialCache2D::get_ref_count);
ClassDB::bind_method(D_METHOD("set_ref_count", "value"), &PropMaterialCache2D::set_ref_count);
2021-11-23 16:53:47 +01:00
ADD_PROPERTY(PropertyInfo(Variant::INT, "mat_ref_count"), "set_ref_count", "get_ref_count");
2021-11-24 14:00:16 +01:00
ClassDB::bind_method(D_METHOD("inc_ref_count"), &PropMaterialCache2D::inc_ref_count);
ClassDB::bind_method(D_METHOD("dec_ref_count"), &PropMaterialCache2D::dec_ref_count);
2021-11-23 16:53:47 +01:00
BIND_VMETHOD(MethodInfo("_setup_material_albedo", PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture")));
2021-11-24 14:00:16 +01:00
ClassDB::bind_method(D_METHOD("material_get", "index"), &PropMaterialCache2D::material_get);
ClassDB::bind_method(D_METHOD("material_lod_get", "index"), &PropMaterialCache2D::material_lod_get);
ClassDB::bind_method(D_METHOD("material_add", "value"), &PropMaterialCache2D::material_add);
ClassDB::bind_method(D_METHOD("material_set", "index", "value"), &PropMaterialCache2D::material_set);
ClassDB::bind_method(D_METHOD("material_remove", "index"), &PropMaterialCache2D::material_remove);
ClassDB::bind_method(D_METHOD("material_get_num"), &PropMaterialCache2D::material_get_num);
ClassDB::bind_method(D_METHOD("materials_clear"), &PropMaterialCache2D::materials_clear);
2021-11-23 16:53:47 +01:00
2021-11-24 14:00:16 +01:00
ClassDB::bind_method(D_METHOD("materials_get"), &PropMaterialCache2D::materials_get);
ClassDB::bind_method(D_METHOD("materials_set"), &PropMaterialCache2D::materials_set);
2021-11-23 16:53:47 +01:00
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "materials", PROPERTY_HINT_NONE, "17/17:Material", PROPERTY_USAGE_DEFAULT, "Material"), "materials_set", "materials_get");
2021-11-24 14:00:16 +01:00
ClassDB::bind_method(D_METHOD("texture_add", "texture"), &PropMaterialCache2D::texture_add);
ClassDB::bind_method(D_METHOD("texture_remove", "texture"), &PropMaterialCache2D::texture_remove);
ClassDB::bind_method(D_METHOD("texture_remove_index", "index"), &PropMaterialCache2D::texture_remove_index);
ClassDB::bind_method(D_METHOD("textures_clear"), &PropMaterialCache2D::textures_clear);
ClassDB::bind_method(D_METHOD("texture_count"), &PropMaterialCache2D::texture_count);
ClassDB::bind_method(D_METHOD("texture_get", "index"), &PropMaterialCache2D::texture_get);
ClassDB::bind_method(D_METHOD("texture_get_atlas", "index"), &PropMaterialCache2D::texture_get_atlas);
ClassDB::bind_method(D_METHOD("texture_get_atlas_tex", "index"), &PropMaterialCache2D::texture_get_atlas_tex);
ClassDB::bind_method(D_METHOD("texture_get_uv_rect", "texture"), &PropMaterialCache2D::texture_get_uv_rect);
2021-11-23 16:53:47 +01:00
2021-11-24 14:00:16 +01:00
ClassDB::bind_method(D_METHOD("prop_add_textures", "prop"), &PropMaterialCache2D::prop_add_textures);
ClassDB::bind_method(D_METHOD("prop_remove_textures", "prop"), &PropMaterialCache2D::prop_remove_textures);
2021-11-23 16:53:47 +01:00
2021-11-24 14:00:16 +01:00
ClassDB::bind_method(D_METHOD("refresh_rects"), &PropMaterialCache2D::refresh_rects);
2021-11-23 16:53:47 +01:00
2021-11-24 14:00:16 +01:00
ClassDB::bind_method(D_METHOD("setup_material_albedo", "texture"), &PropMaterialCache2D::setup_material_albedo);
2021-11-23 16:53:47 +01:00
}