terraman_2d/library/terrain_2d_library_merger_pcm.cpp

871 lines
22 KiB
C++
Raw Normal View History

2021-11-22 22:00:45 +01:00
/*
2022-02-22 10:33:06 +01:00
Copyright (c) 2019-2022 Péter Magyar
2021-11-22 22:00:45 +01:00
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.
*/
2022-02-22 11:09:36 +01:00
#include "terrain_2d_library_merger_pcm.h"
2021-11-22 22:00:45 +01:00
#include "../../texture_packer/texture_packer.h"
#include "scene/resources/packed_scene.h"
#include "scene/resources/texture.h"
#ifdef PROPS_PRESENT
#include "../../props/props/prop_data.h"
#include "../../props/props/prop_data_prop.h"
#if MESH_DATA_RESOURCE_PRESENT
#include "../../mesh_data_resource/props/prop_data_mesh_data.h"
#endif
#endif
#include "../defines.h"
2022-02-22 11:09:36 +01:00
#include "../world/default/terrain_2d_chunk_default.h"
2021-11-22 22:00:45 +01:00
#include "core/hashfuncs.h"
2022-02-22 10:33:06 +01:00
#include "core/message_queue.h"
2022-02-22 11:09:36 +01:00
#include "terrain_2d_material_cache_pcm.h"
2021-11-22 22:00:45 +01:00
2022-02-22 10:33:06 +01:00
bool TerrainLibraryMergerPCM::_supports_caching() {
2021-11-22 22:00:45 +01:00
return true;
}
2022-02-22 10:33:06 +01:00
void TerrainLibraryMergerPCM::_material_cache_get_key(Ref<TerrainChunk> chunk) {
uint8_t *ch = chunk->channel_get(TerrainChunkDefault::DEFAULT_CHANNEL_TYPE);
2021-11-22 22:00:45 +01:00
if (!ch) {
chunk->material_cache_key_set(0);
chunk->material_cache_key_has_set(false);
return;
}
Vector<uint8_t> surfaces;
uint32_t size = chunk->get_data_size();
for (uint32_t i = 0; i < size; ++i) {
uint8_t v = ch[i];
if (v == 0) {
continue;
}
int ssize = surfaces.size();
bool found = false;
for (uint8_t j = 0; j < ssize; ++j) {
if (surfaces[j] == v) {
found = true;
break;
}
}
if (!found) {
surfaces.push_back(v);
}
}
2022-02-22 10:33:06 +01:00
uint8_t *liquid_ch = chunk->channel_get(TerrainChunkDefault::DEFAULT_CHANNEL_LIQUID_TYPE);
if (liquid_ch) {
for (uint32_t i = 0; i < size; ++i) {
uint8_t v = liquid_ch[i];
if (v == 0) {
continue;
}
int ssize = surfaces.size();
bool found = false;
for (uint8_t j = 0; j < ssize; ++j) {
if (surfaces[j] == v) {
found = true;
break;
}
}
if (!found) {
surfaces.push_back(v);
}
}
}
2021-11-22 22:00:45 +01:00
if (surfaces.size() == 0) {
chunk->material_cache_key_set(0);
chunk->material_cache_key_has_set(false);
return;
}
surfaces.sort();
String hstr;
for (int i = 0; i < surfaces.size(); ++i) {
hstr += String::num(surfaces[i]) + "|";
}
int hash = static_cast<int>(hstr.hash());
chunk->material_cache_key_set(hash);
chunk->material_cache_key_has_set(true);
_material_cache_mutex.lock();
if (_material_cache.has(hash)) {
2022-02-22 10:33:06 +01:00
Ref<TerrainMaterialCachePCM> cc = _material_cache[hash];
2021-11-22 22:00:45 +01:00
if (cc.is_valid()) {
cc->inc_ref_count();
}
_material_cache_mutex.unlock();
return;
}
//print_error("New cache: " + hstr);
2022-02-22 10:33:06 +01:00
Ref<TerrainMaterialCachePCM> cache;
2021-11-22 22:00:45 +01:00
cache.instance();
cache->inc_ref_count();
cache->set_texture_flags(get_texture_flags());
cache->set_max_atlas_size(get_max_atlas_size());
cache->set_keep_original_atlases(get_keep_original_atlases());
cache->set_background_color(get_background_color());
cache->set_margin(get_margin());
for (int i = 0; i < surfaces.size(); ++i) {
int s = surfaces[i] - 1;
2022-02-22 10:33:06 +01:00
if (_terra_surfaces.size() <= s) {
2021-11-22 22:00:45 +01:00
continue;
}
2022-02-22 10:33:06 +01:00
Ref<TerrainSurfaceMerger> ms = _terra_surfaces[s];
2021-11-22 22:00:45 +01:00
if (!ms.is_valid()) {
continue;
}
2022-02-22 10:33:06 +01:00
Ref<TerrainSurfaceMerger> nms = ms->duplicate();
nms->set_library(Ref<TerrainLibraryMergerPCM>(this));
2021-11-22 22:00:45 +01:00
nms->set_id(s);
cache->surface_add(nms);
}
for (int i = 0; i < _materials.size(); ++i) {
Ref<Material> m = _materials[i];
if (!m.is_valid()) {
continue;
}
Ref<Material> nm = m->duplicate();
cache->material_add(nm);
}
_material_cache[hash] = cache;
//unlock here, so if a different thread need the cache it will be able to immediately access the materials and surfaces when it gets it.
_material_cache_mutex.unlock();
//this will generate the atlases
cache->refresh_rects();
}
2022-02-22 10:33:06 +01:00
Ref<TerrainMaterialCache> TerrainLibraryMergerPCM::_material_cache_get(const int key) {
2021-11-22 22:00:45 +01:00
_material_cache_mutex.lock();
2022-02-22 10:33:06 +01:00
ERR_FAIL_COND_V(!_material_cache.has(key), Ref<TerrainMaterialCache>());
2021-11-22 22:00:45 +01:00
2022-02-22 10:33:06 +01:00
Ref<TerrainMaterialCache> c = _material_cache[key];
2021-11-22 22:00:45 +01:00
_material_cache_mutex.unlock();
return c;
}
2022-02-22 10:33:06 +01:00
void TerrainLibraryMergerPCM::_material_cache_unref(const int key) {
if (_material_cache_mutex.try_lock() != OK) {
// If we don't get the lock try again later
// This is needed, because when duplicating materials the VisualServer apparently
// needs synchronization with the main thread. So if _material_cache_unref holds the mutex
// and is duplicating the materials, trying to get the lock from the main thread will deadlock
// the game. This can happen when chungs are spawned and despawned really fast.
// E.g. when flying around in the editor.
MessageQueue::get_singleton()->push_call(this, "_material_cache_unref", key, 1);
return;
}
//_material_cache_mutex.lock();
2021-11-22 22:00:45 +01:00
if (!_material_cache.has(key)) {
return;
}
2022-02-22 10:33:06 +01:00
Ref<TerrainMaterialCachePCM> cc = _material_cache[key];
2021-11-22 22:00:45 +01:00
if (!cc.is_valid()) {
return;
}
cc->dec_ref_count();
if (cc->get_ref_count() <= 0) {
_material_cache.erase(key);
}
_material_cache_mutex.unlock();
}
2022-02-22 10:33:06 +01:00
void TerrainLibraryMergerPCM::_prop_material_cache_get_key(Ref<TerrainChunk> chunk) {
2021-11-22 22:00:45 +01:00
Vector<uint64_t> props;
2022-02-22 10:33:06 +01:00
/*
#ifdef PROPS_PRESENT
for (int i = 0; i < chunk->prop_get_count(); ++i) {
Ref<PropData> prop = chunk->prop_get(i);
2021-11-22 22:00:45 +01:00
2022-02-22 10:33:06 +01:00
ERR_CONTINUE(!prop.is_valid());
2021-11-22 22:00:45 +01:00
2022-02-22 10:33:06 +01:00
//get pointer's value as uint64
uint64_t v = make_uint64_t<PropData *>(*prop);
2021-11-22 22:00:45 +01:00
2022-02-22 10:33:06 +01:00
int psize = props.size();
bool found = false;
for (int j = 0; j < psize; ++j) {
if (props[j] == v) {
found = true;
break;
}
2021-11-22 22:00:45 +01:00
}
2022-02-22 10:33:06 +01:00
if (!found) {
props.push_back(v);
}
2021-11-22 22:00:45 +01:00
}
2022-02-22 10:33:06 +01:00
#endif
*/
2021-11-22 22:00:45 +01:00
#if MESH_DATA_RESOURCE_PRESENT
for (int i = 0; i < chunk->mesh_data_resource_get_count(); ++i) {
Ref<Texture> tex = chunk->mesh_data_resource_get_texture(i);
if (!tex.is_valid())
continue;
//get pointer's value as uint64
uint64_t v = make_uint64_t<Texture *>(*tex);
int psize = props.size();
bool found = false;
for (int j = 0; j < psize; ++j) {
if (props[j] == v) {
found = true;
break;
}
}
if (!found) {
props.push_back(v);
}
}
#endif
if (props.size() == 0) {
chunk->prop_material_cache_key_set(0);
chunk->prop_material_cache_key_has_set(false);
return;
}
props.sort();
String hstr;
for (int i = 0; i < props.size(); ++i) {
hstr += String::num_uint64(props[i]) + "|";
}
int hash = static_cast<int>(hstr.hash());
chunk->prop_material_cache_key_set(hash);
chunk->prop_material_cache_key_has_set(true);
_prop_material_cache_mutex.lock();
if (_prop_material_cache.has(hash)) {
2022-02-22 10:33:06 +01:00
Ref<TerrainMaterialCachePCM> cc = _prop_material_cache[hash];
2021-11-22 22:00:45 +01:00
if (cc.is_valid()) {
cc->inc_ref_count();
}
_prop_material_cache_mutex.unlock();
return;
}
//print_error("New prop cache: " + hstr);
2022-02-22 10:33:06 +01:00
Ref<TerrainMaterialCachePCM> cache;
2021-11-22 22:00:45 +01:00
cache.instance();
cache->inc_ref_count();
cache->set_texture_flags(get_texture_flags());
cache->set_max_atlas_size(get_max_atlas_size());
cache->set_keep_original_atlases(get_keep_original_atlases());
cache->set_background_color(get_background_color());
cache->set_margin(get_margin());
for (int i = 0; i < _prop_materials.size(); ++i) {
Ref<Material> m = _prop_materials[i];
if (!m.is_valid()) {
continue;
}
Ref<Material> nm = m->duplicate();
cache->material_add(nm);
}
2022-02-22 10:33:06 +01:00
/*
#ifdef PROPS_PRESENT
for (int i = 0; i < chunk->prop_get_count(); ++i) {
Ref<PropData> prop = chunk->prop_get(i);
2021-11-22 22:00:45 +01:00
2022-02-22 10:33:06 +01:00
ERR_CONTINUE(!prop.is_valid());
2021-11-22 22:00:45 +01:00
2022-02-22 10:33:06 +01:00
cache->prop_add_textures(prop);
}
#endif
*/
2021-11-22 22:00:45 +01:00
#if MESH_DATA_RESOURCE_PRESENT
for (int i = 0; i < chunk->mesh_data_resource_get_count(); ++i) {
Ref<Texture> tex = chunk->mesh_data_resource_get_texture(i);
if (!tex.is_valid())
continue;
cache->additional_texture_add(tex);
}
#endif
_prop_material_cache[hash] = cache;
//unlock here, so if a different thread need the cache it will be able to immediately access the materials and surfaces when it gets it.
_prop_material_cache_mutex.unlock();
//this will generate the atlases
cache->refresh_rects();
}
2022-02-22 10:33:06 +01:00
Ref<TerrainMaterialCache> TerrainLibraryMergerPCM::_prop_material_cache_get(const int key) {
2021-11-22 22:00:45 +01:00
_prop_material_cache_mutex.lock();
2022-02-22 10:33:06 +01:00
ERR_FAIL_COND_V(!_prop_material_cache.has(key), Ref<TerrainMaterialCache>());
2021-11-22 22:00:45 +01:00
2022-02-22 10:33:06 +01:00
Ref<TerrainMaterialCache> c = _prop_material_cache[key];
2021-11-22 22:00:45 +01:00
_prop_material_cache_mutex.unlock();
return c;
}
2022-02-22 10:33:06 +01:00
void TerrainLibraryMergerPCM::_prop_material_cache_unref(const int key) {
if (_prop_material_cache_mutex.try_lock() != OK) {
// If we don't get the lock try again later
// This is needed, because when duplicating materials the VisualServer apparently
// needs synchronization with the main thread. So if _prop_material_cache_get_key holds the mutex
// and is duplicating the materials, trying to get the lock from the main thread will deadlock
// the game. This can happen when chungs are spawned and despawned really fast.
// E.g. when flying around in the editor.
MessageQueue::get_singleton()->push_call(this, "_prop_material_cache_unref", key, 1);
return;
}
//_prop_material_cache_mutex.lock();
2021-11-22 22:00:45 +01:00
if (!_prop_material_cache.has(key)) {
return;
}
2022-02-22 10:33:06 +01:00
Ref<TerrainMaterialCachePCM> cc = _prop_material_cache[key];
2021-11-22 22:00:45 +01:00
if (!cc.is_valid()) {
return;
}
cc->dec_ref_count();
if (cc->get_ref_count() <= 0) {
_prop_material_cache.erase(key);
}
_prop_material_cache_mutex.unlock();
}
2022-02-22 10:33:06 +01:00
int TerrainLibraryMergerPCM::get_texture_flags() const {
2021-11-22 22:00:45 +01:00
return _packer->get_texture_flags();
}
2022-02-22 10:33:06 +01:00
void TerrainLibraryMergerPCM::set_texture_flags(const int flags) {
2021-11-22 22:00:45 +01:00
_packer->set_texture_flags(flags);
_prop_packer->set_texture_flags(flags);
}
2022-02-22 10:33:06 +01:00
int TerrainLibraryMergerPCM::get_max_atlas_size() const {
2021-11-22 22:00:45 +01:00
return _packer->get_max_atlas_size();
}
2022-02-22 10:33:06 +01:00
void TerrainLibraryMergerPCM::set_max_atlas_size(const int size) {
2021-11-22 22:00:45 +01:00
_packer->set_max_atlas_size(size);
_prop_packer->set_max_atlas_size(size);
}
2022-02-22 10:33:06 +01:00
bool TerrainLibraryMergerPCM::get_keep_original_atlases() const {
2021-11-22 22:00:45 +01:00
return _packer->get_keep_original_atlases();
}
2022-02-22 10:33:06 +01:00
void TerrainLibraryMergerPCM::set_keep_original_atlases(const bool value) {
2021-11-22 22:00:45 +01:00
_packer->set_keep_original_atlases(value);
_prop_packer->set_keep_original_atlases(value);
}
2022-02-22 10:33:06 +01:00
Color TerrainLibraryMergerPCM::get_background_color() const {
2021-11-22 22:00:45 +01:00
return _packer->get_background_color();
}
2022-02-22 10:33:06 +01:00
void TerrainLibraryMergerPCM::set_background_color(const Color &color) {
2021-11-22 22:00:45 +01:00
_packer->set_background_color(color);
_prop_packer->set_background_color(color);
}
2022-02-22 10:33:06 +01:00
int TerrainLibraryMergerPCM::get_margin() const {
2021-11-22 22:00:45 +01:00
return _packer->get_margin();
}
2022-02-22 10:33:06 +01:00
void TerrainLibraryMergerPCM::set_margin(const int margin) {
2021-11-22 22:00:45 +01:00
_packer->set_margin(margin);
_prop_packer->set_margin(margin);
}
//Surfaces
2022-02-22 10:33:06 +01:00
Ref<TerrainSurface> TerrainLibraryMergerPCM::terra_surface_get(const int index) {
ERR_FAIL_INDEX_V(index, _terra_surfaces.size(), Ref<TerrainSurface>(NULL));
2021-11-22 22:00:45 +01:00
2022-02-22 10:33:06 +01:00
return _terra_surfaces[index];
2021-11-22 22:00:45 +01:00
}
2022-02-22 10:33:06 +01:00
void TerrainLibraryMergerPCM::terra_surface_add(Ref<TerrainSurface> value) {
2021-11-22 22:00:45 +01:00
ERR_FAIL_COND(!value.is_valid());
2022-02-22 10:33:06 +01:00
value->set_library(Ref<TerrainLibraryMergerPCM>(this));
value->set_id(_terra_surfaces.size());
2021-11-22 22:00:45 +01:00
2022-02-22 10:33:06 +01:00
_terra_surfaces.push_back(value);
2021-11-22 22:00:45 +01:00
}
2022-02-22 10:33:06 +01:00
void TerrainLibraryMergerPCM::terra_surface_set(const int index, Ref<TerrainSurface> value) {
2021-11-22 22:00:45 +01:00
ERR_FAIL_COND(index < 0);
2022-02-22 10:33:06 +01:00
if (_terra_surfaces.size() < index) {
_terra_surfaces.resize(index + 1);
2021-11-22 22:00:45 +01:00
}
2022-02-22 10:33:06 +01:00
if (_terra_surfaces[index].is_valid()) {
_terra_surfaces.get(index)->set_library(Ref<TerrainLibraryMergerPCM>(NULL));
2021-11-22 22:00:45 +01:00
}
if (value.is_valid()) {
2022-02-22 10:33:06 +01:00
value->set_library(Ref<TerrainLibraryMergerPCM>(this));
2021-11-22 22:00:45 +01:00
2022-02-22 10:33:06 +01:00
_terra_surfaces.set(index, value);
2021-11-22 22:00:45 +01:00
}
}
2022-02-22 10:33:06 +01:00
void TerrainLibraryMergerPCM::terra_surface_remove(const int index) {
_terra_surfaces.remove(index);
2021-11-22 22:00:45 +01:00
}
2022-02-22 10:33:06 +01:00
int TerrainLibraryMergerPCM::terra_surface_get_num() const {
return _terra_surfaces.size();
2021-11-22 22:00:45 +01:00
}
2022-02-22 10:33:06 +01:00
void TerrainLibraryMergerPCM::terra_surfaces_clear() {
2021-11-22 22:00:45 +01:00
_packer->clear();
2022-02-22 10:33:06 +01:00
for (int i = 0; i < _terra_surfaces.size(); i++) {
Ref<TerrainSurfaceMerger> surface = _terra_surfaces[i];
2021-11-22 22:00:45 +01:00
if (surface.is_valid()) {
surface->set_library(NULL);
}
}
2022-02-22 10:33:06 +01:00
_terra_surfaces.clear();
2021-11-22 22:00:45 +01:00
}
2022-02-22 10:33:06 +01:00
Vector<Variant> TerrainLibraryMergerPCM::get_terra_surfaces() {
VARIANT_ARRAY_GET(_terra_surfaces);
2021-11-22 22:00:45 +01:00
}
2022-02-22 10:33:06 +01:00
void TerrainLibraryMergerPCM::set_terra_surfaces(const Vector<Variant> &surfaces) {
_terra_surfaces.clear();
2021-11-22 22:00:45 +01:00
for (int i = 0; i < surfaces.size(); i++) {
2022-02-22 10:33:06 +01:00
Ref<TerrainSurfaceMerger> surface = Ref<TerrainSurfaceMerger>(surfaces[i]);
2021-11-22 22:00:45 +01:00
if (surface.is_valid()) {
2022-02-22 10:33:06 +01:00
surface->set_library(Ref<TerrainLibraryMergerPCM>(this));
2021-11-22 22:00:45 +01:00
}
2022-02-22 10:33:06 +01:00
_terra_surfaces.push_back(surface);
2021-11-22 22:00:45 +01:00
}
}
#ifdef PROPS_PRESENT
2022-02-22 10:33:06 +01:00
Ref<PropData> TerrainLibraryMergerPCM::get_prop(const int index) {
2021-11-22 22:00:45 +01:00
ERR_FAIL_INDEX_V(index, _props.size(), Ref<PropData>());
return _props[index];
}
2022-02-22 10:33:06 +01:00
void TerrainLibraryMergerPCM::add_prop(Ref<PropData> value) {
2021-11-22 22:00:45 +01:00
_props.push_back(value);
}
2022-02-22 10:33:06 +01:00
bool TerrainLibraryMergerPCM::has_prop(const Ref<PropData> &value) const {
2021-11-22 22:00:45 +01:00
return _props.find(value) != -1;
}
2022-02-22 10:33:06 +01:00
void TerrainLibraryMergerPCM::set_prop(const int index, const Ref<PropData> &value) {
2021-11-22 22:00:45 +01:00
ERR_FAIL_INDEX(index, _props.size());
_props.write[index] = value;
}
2022-02-22 10:33:06 +01:00
void TerrainLibraryMergerPCM::remove_prop(const int index) {
2021-11-22 22:00:45 +01:00
ERR_FAIL_INDEX(index, _props.size());
_props.remove(index);
}
2022-02-22 10:33:06 +01:00
int TerrainLibraryMergerPCM::get_num_props() const {
2021-11-22 22:00:45 +01:00
return _props.size();
}
2022-02-22 10:33:06 +01:00
void TerrainLibraryMergerPCM::clear_props() {
2021-11-22 22:00:45 +01:00
_props.clear();
}
2022-02-22 10:33:06 +01:00
Vector<Variant> TerrainLibraryMergerPCM::get_props() {
2021-11-22 22:00:45 +01:00
VARIANT_ARRAY_GET(_props);
}
2022-02-22 10:33:06 +01:00
void TerrainLibraryMergerPCM::set_props(const Vector<Variant> &props) {
2021-11-22 22:00:45 +01:00
VARIANT_ARRAY_SET(props, _props, PropData);
}
2022-02-22 10:33:06 +01:00
Rect2 TerrainLibraryMergerPCM::get_prop_uv_rect(const Ref<Texture> &texture) {
2021-11-22 22:00:45 +01:00
if (!texture.is_valid()) {
return Rect2(0, 0, 1, 1);
}
Ref<AtlasTexture> at = _prop_packer->get_texture(texture);
if (!at.is_valid()) {
return Rect2(0, 0, 1, 1);
}
Rect2 region = at->get_region();
Ref<Texture> tex = at->get_atlas();
if (!tex.is_valid()) {
return Rect2(0, 0, 1, 1);
}
Ref<Image> image = tex->get_data();
if (!image.is_valid()) {
return Rect2(0, 0, 1, 1);
}
2022-02-22 10:33:06 +01:00
float w = tex->get_width();
float h = tex->get_height();
2021-11-22 22:00:45 +01:00
region.position = Size2(region.position.x / w, region.position.y / h);
region.size = Size2(region.size.x / w, region.size.y / h);
return region;
}
2022-02-22 10:33:06 +01:00
Ref<TexturePacker> TerrainLibraryMergerPCM::get_prop_packer() {
2021-11-22 22:00:45 +01:00
return _prop_packer;
}
#endif
2022-02-22 10:33:06 +01:00
void TerrainLibraryMergerPCM::refresh_rects() {
2021-11-22 22:00:45 +01:00
bool texture_added = false;
2022-02-22 10:33:06 +01:00
for (int i = 0; i < _terra_surfaces.size(); i++) {
Ref<TerrainSurfaceMerger> surface = Ref<TerrainSurfaceMerger>(_terra_surfaces[i]);
2021-11-22 22:00:45 +01:00
if (surface.is_valid()) {
2022-02-22 10:33:06 +01:00
for (int j = 0; j < TerrainSurface::TERRAIN_SIDES_COUNT; ++j) {
Ref<Texture> tex = surface->get_texture(static_cast<TerrainSurface::TerrainSurfaceSides>(j));
2021-11-22 22:00:45 +01:00
if (!tex.is_valid())
continue;
if (!_packer->contains_texture(tex)) {
texture_added = true;
2022-02-22 10:33:06 +01:00
surface->set_region(static_cast<TerrainSurface::TerrainSurfaceSides>(j), _packer->add_texture(tex));
2021-11-22 22:00:45 +01:00
} else {
2022-02-22 10:33:06 +01:00
surface->set_region(static_cast<TerrainSurface::TerrainSurfaceSides>(j), _packer->get_texture(tex));
2021-11-22 22:00:45 +01:00
}
}
}
}
if (texture_added) {
_packer->merge();
ERR_FAIL_COND(_packer->get_texture_count() == 0);
Ref<Texture> tex = _packer->get_generated_texture(0);
2022-02-22 10:33:06 +01:00
setup_material_albedo(MATERIAL_INDEX_TERRAIN, tex);
2021-11-22 22:00:45 +01:00
setup_material_albedo(MATERIAL_INDEX_LIQUID, tex);
}
#ifdef PROPS_PRESENT
//todo add this back
//texture_added = false;
for (int i = 0; i < _props.size(); i++) {
Ref<PropData> prop = _props.get(i);
if (prop.is_valid()) {
if (process_prop_textures(prop))
texture_added = true;
}
}
//if (texture_added) {
if (_prop_packer->get_texture_count() > 0) {
_prop_packer->merge();
//ERR_FAIL_COND(_prop_packer->get_texture_count() == 0);
Ref<Texture> tex = _prop_packer->get_generated_texture(0);
setup_material_albedo(MATERIAL_INDEX_PROP, tex);
}
#endif
2022-02-22 10:33:06 +01:00
for (int i = 0; i < _terra_surfaces.size(); i++) {
Ref<TerrainSurfaceMerger> surface = _terra_surfaces[i];
2021-11-22 22:00:45 +01:00
if (surface.is_valid()) {
surface->refresh_rects();
}
}
set_initialized(true);
}
2022-02-22 10:33:06 +01:00
void TerrainLibraryMergerPCM::_setup_material_albedo(const int material_index, const Ref<Texture> &texture) {
2021-11-22 22:00:45 +01:00
Ref<SpatialMaterial> mat;
int count = 0;
switch (material_index) {
2022-02-22 10:33:06 +01:00
case MATERIAL_INDEX_TERRAIN:
2021-11-22 22:00:45 +01:00
count = material_get_num();
break;
case MATERIAL_INDEX_LIQUID:
count = liquid_material_get_num();
break;
case MATERIAL_INDEX_PROP:
count = prop_material_get_num();
break;
}
for (int i = 0; i < count; ++i) {
switch (material_index) {
2022-02-22 10:33:06 +01:00
case MATERIAL_INDEX_TERRAIN:
2021-11-22 22:00:45 +01:00
mat = material_get(i);
break;
case MATERIAL_INDEX_LIQUID:
mat = liquid_material_get(i);
break;
case MATERIAL_INDEX_PROP:
mat = prop_material_get(i);
break;
}
Ref<SpatialMaterial> spmat;
2022-02-22 10:33:06 +01:00
switch (material_index) {
case MATERIAL_INDEX_TERRAIN:
spmat = material_get(i);
break;
case MATERIAL_INDEX_LIQUID:
spmat = liquid_material_get(i);
break;
case MATERIAL_INDEX_PROP:
spmat = prop_material_get(i);
break;
}
2021-11-22 22:00:45 +01:00
if (spmat.is_valid()) {
spmat->set_texture(SpatialMaterial::TEXTURE_ALBEDO, texture);
return;
}
Ref<ShaderMaterial> shmat;
switch (material_index) {
2022-02-22 10:33:06 +01:00
case MATERIAL_INDEX_TERRAIN:
2021-11-22 22:00:45 +01:00
shmat = material_get(i);
break;
case MATERIAL_INDEX_LIQUID:
shmat = liquid_material_get(i);
break;
case MATERIAL_INDEX_PROP:
shmat = prop_material_get(i);
break;
}
if (shmat.is_valid()) {
shmat->set_shader_param("texture_albedo", texture);
}
}
}
2022-02-22 10:33:06 +01:00
TerrainLibraryMergerPCM::TerrainLibraryMergerPCM() {
2021-11-22 22:00:45 +01:00
_packer.instance();
#if GODOT4
#warning implement
#else
_packer->set_texture_flags(Texture::FLAG_MIPMAPS | Texture::FLAG_FILTER);
#endif
_packer->set_max_atlas_size(1024);
_packer->set_keep_original_atlases(false);
_packer->set_margin(0);
_prop_packer.instance();
#if GODOT4
#warning implement
#else
_prop_packer->set_texture_flags(Texture::FLAG_MIPMAPS | Texture::FLAG_FILTER);
#endif
_prop_packer->set_max_atlas_size(1024);
_prop_packer->set_keep_original_atlases(false);
_prop_packer->set_margin(0);
}
2022-02-22 10:33:06 +01:00
TerrainLibraryMergerPCM::~TerrainLibraryMergerPCM() {
for (int i = 0; i < _terra_surfaces.size(); ++i) {
Ref<TerrainSurface> surface = _terra_surfaces[i];
2021-11-22 22:00:45 +01:00
if (surface.is_valid()) {
2022-02-22 10:33:06 +01:00
surface->set_library(Ref<TerrainLibraryMergerPCM>());
2021-11-22 22:00:45 +01:00
}
}
2022-02-22 10:33:06 +01:00
_terra_surfaces.clear();
2021-11-22 22:00:45 +01:00
_packer->clear();
_packer.unref();
_prop_packer->clear();
_prop_packer.unref();
}
#ifdef PROPS_PRESENT
2022-02-22 10:33:06 +01:00
bool TerrainLibraryMergerPCM::process_prop_textures(Ref<PropData> prop) {
2021-11-22 22:00:45 +01:00
if (!prop.is_valid()) {
return false;
}
bool texture_added = false;
for (int i = 0; i < prop->get_prop_count(); ++i) {
Ref<PropDataMeshData> pdm = prop->get_prop(i);
if (pdm.is_valid()) {
Ref<Texture> tex = pdm->get_texture();
if (!tex.is_valid())
continue;
if (!_prop_packer->contains_texture(tex)) {
_prop_packer->add_texture(tex);
texture_added = true;
}
}
Ref<PropDataProp> pdp = prop->get_prop(i);
if (pdp.is_valid()) {
if (process_prop_textures(pdp))
texture_added = true;
}
}
return texture_added;
}
#endif
2022-02-22 10:33:06 +01:00
void TerrainLibraryMergerPCM::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_texture_flags"), &TerrainLibraryMergerPCM::get_texture_flags);
ClassDB::bind_method(D_METHOD("set_texture_flags", "flags"), &TerrainLibraryMergerPCM::set_texture_flags);
2021-11-22 22:00:45 +01:00
ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_flags", PROPERTY_HINT_FLAGS, "Mipmaps,Repeat,Filter,Anisotropic Linear,Convert to Linear,Mirrored Repeat,Video Surface"), "set_texture_flags", "get_texture_flags");
2022-02-22 10:33:06 +01:00
ClassDB::bind_method(D_METHOD("get_max_atlas_size"), &TerrainLibraryMergerPCM::get_max_atlas_size);
ClassDB::bind_method(D_METHOD("set_max_atlas_size", "size"), &TerrainLibraryMergerPCM::set_max_atlas_size);
2021-11-22 22:00:45 +01:00
ADD_PROPERTY(PropertyInfo(Variant::INT, "max_atlas_size"), "set_max_atlas_size", "get_max_atlas_size");
2022-02-22 10:33:06 +01:00
ClassDB::bind_method(D_METHOD("get_keep_original_atlases"), &TerrainLibraryMergerPCM::get_keep_original_atlases);
ClassDB::bind_method(D_METHOD("set_keep_original_atlases", "value"), &TerrainLibraryMergerPCM::set_keep_original_atlases);
2021-11-22 22:00:45 +01:00
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_original_atlases"), "set_keep_original_atlases", "get_keep_original_atlases");
2022-02-22 10:33:06 +01:00
ClassDB::bind_method(D_METHOD("get_background_color"), &TerrainLibraryMergerPCM::get_background_color);
ClassDB::bind_method(D_METHOD("set_background_color", "color"), &TerrainLibraryMergerPCM::set_background_color);
2021-11-22 22:00:45 +01:00
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "background_color"), "set_background_color", "get_background_color");
2022-02-22 10:33:06 +01:00
ClassDB::bind_method(D_METHOD("get_margin"), &TerrainLibraryMergerPCM::get_margin);
ClassDB::bind_method(D_METHOD("set_margin", "size"), &TerrainLibraryMergerPCM::set_margin);
2021-11-22 22:00:45 +01:00
ADD_PROPERTY(PropertyInfo(Variant::INT, "margin"), "set_margin", "get_margin");
2022-02-22 10:33:06 +01:00
ClassDB::bind_method(D_METHOD("get_terra_surfaces"), &TerrainLibraryMergerPCM::get_terra_surfaces);
ClassDB::bind_method(D_METHOD("set_terra_surfaces"), &TerrainLibraryMergerPCM::set_terra_surfaces);
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "terra_surfaces", PROPERTY_HINT_NONE, "17/17:TerrainSurfaceMerger", PROPERTY_USAGE_DEFAULT, "TerrainSurfaceMerger"), "set_terra_surfaces", "get_terra_surfaces");
2021-11-22 22:00:45 +01:00
#ifdef PROPS_PRESENT
2022-02-22 10:33:06 +01:00
ClassDB::bind_method(D_METHOD("get_props"), &TerrainLibraryMergerPCM::get_props);
ClassDB::bind_method(D_METHOD("set_props"), &TerrainLibraryMergerPCM::set_props);
2021-11-22 22:00:45 +01:00
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "props", PROPERTY_HINT_NONE, "17/17:PropData", PROPERTY_USAGE_DEFAULT, "PropData"), "set_props", "get_props");
2022-02-22 10:33:06 +01:00
ClassDB::bind_method(D_METHOD("get_prop_uv_rect", "texture"), &TerrainLibraryMergerPCM::get_prop_uv_rect);
2021-11-22 22:00:45 +01:00
2022-02-22 10:33:06 +01:00
ClassDB::bind_method(D_METHOD("get_prop_packer"), &TerrainLibraryMergerPCM::get_prop_packer);
2021-11-22 22:00:45 +01:00
#endif
2022-02-22 10:33:06 +01:00
ClassDB::bind_method(D_METHOD("_setup_material_albedo", "material_index", "texture"), &TerrainLibraryMergerPCM::_setup_material_albedo);
2021-11-22 22:00:45 +01:00
}