mirror of
https://github.com/Relintai/props.git
synced 2025-02-04 16:05:54 +01:00
Ported the MaterialCache class from Terraman.
This commit is contained in:
parent
33095a83a1
commit
6c8deb8956
8
SCsub
8
SCsub
@ -7,7 +7,10 @@ module_env = env.Clone()
|
||||
if os.path.isdir('../mesh_data_resource'):
|
||||
module_env.Append(CPPDEFINES=['MESH_DATA_RESOURCE_PRESENT'])
|
||||
|
||||
has_texture_packer = False
|
||||
|
||||
if os.path.isdir('../texture_packer'):
|
||||
has_texture_packer = True
|
||||
module_env.Append(CPPDEFINES=['TEXTURE_PACKER_PRESENT'])
|
||||
|
||||
if os.path.isdir('../terraman'):
|
||||
@ -51,8 +54,13 @@ sources = [
|
||||
"jobs/prop_texture_job.cpp",
|
||||
|
||||
"jobs/prop_mesher_job_step.cpp",
|
||||
|
||||
"material_cache/prop_material_cache.cpp"
|
||||
]
|
||||
|
||||
if has_texture_packer:
|
||||
sources.append("material_cache/prop_material_cache_pcm.cpp")
|
||||
|
||||
if ARGUMENTS.get('custom_modules_shared', 'no') == 'yes':
|
||||
# Shared lib compilation
|
||||
module_env.Append(CCFLAGS=['-fPIC'])
|
||||
|
290
material_cache/prop_material_cache.cpp
Normal file
290
material_cache/prop_material_cache.cpp
Normal file
@ -0,0 +1,290 @@
|
||||
/*
|
||||
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.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
|
||||
|
||||
#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
|
||||
|
||||
|
||||
bool PropMaterialCache::get_initialized() {
|
||||
return _initialized;
|
||||
}
|
||||
void PropMaterialCache::set_initialized(const bool value) {
|
||||
_initialized = value;
|
||||
}
|
||||
|
||||
int PropMaterialCache::get_ref_count() {
|
||||
return _ref_count;
|
||||
}
|
||||
void PropMaterialCache::set_ref_count(const int value) {
|
||||
_ref_count = value;
|
||||
}
|
||||
void PropMaterialCache::inc_ref_count() {
|
||||
_ref_count += 1;
|
||||
}
|
||||
void PropMaterialCache::dec_ref_count() {
|
||||
_ref_count -= 1;
|
||||
}
|
||||
|
||||
//Materials
|
||||
Ref<Material> PropMaterialCache::material_get(const int index) {
|
||||
ERR_FAIL_INDEX_V(index, _materials.size(), Ref<Material>(NULL));
|
||||
|
||||
return _materials[index];
|
||||
}
|
||||
|
||||
Ref<Material> PropMaterialCache::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 PropMaterialCache::material_add(const Ref<Material> &value) {
|
||||
ERR_FAIL_COND(!value.is_valid());
|
||||
|
||||
_materials.push_back(value);
|
||||
}
|
||||
|
||||
void PropMaterialCache::material_set(const int index, const Ref<Material> &value) {
|
||||
ERR_FAIL_INDEX(index, _materials.size());
|
||||
|
||||
_materials.set(index, value);
|
||||
}
|
||||
|
||||
void PropMaterialCache::material_remove(const int index) {
|
||||
_materials.remove(index);
|
||||
}
|
||||
|
||||
int PropMaterialCache::material_get_num() const {
|
||||
return _materials.size();
|
||||
}
|
||||
|
||||
void PropMaterialCache::materials_clear() {
|
||||
_materials.clear();
|
||||
}
|
||||
|
||||
Vector<Variant> PropMaterialCache::materials_get() {
|
||||
VARIANT_ARRAY_GET(_materials);
|
||||
}
|
||||
|
||||
void PropMaterialCache::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);
|
||||
}
|
||||
}
|
||||
|
||||
void PropMaterialCache::texture_add(const Ref<Texture> &texture) {
|
||||
_textures.push_back(texture);
|
||||
}
|
||||
void PropMaterialCache::texture_remove(const Ref<Texture> &texture) {
|
||||
for (int i = 0; i < _textures.size(); ++i) {
|
||||
if (_textures[i] == texture) {
|
||||
_textures.remove(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
void PropMaterialCache::texture_remove_index(const int index) {
|
||||
ERR_FAIL_INDEX(index, _textures.size());
|
||||
|
||||
_textures.remove(index);
|
||||
}
|
||||
void PropMaterialCache::textures_clear() {
|
||||
_textures.clear();
|
||||
}
|
||||
int PropMaterialCache::texture_count() {
|
||||
return _textures.size();
|
||||
}
|
||||
Ref<Texture> PropMaterialCache::texture_get(const int index) {
|
||||
ERR_FAIL_INDEX_V(index, _textures.size(), Ref<Texture>());
|
||||
|
||||
return _textures[index];
|
||||
}
|
||||
Ref<AtlasTexture> PropMaterialCache::texture_get_atlas(const int index) {
|
||||
ERR_FAIL_INDEX_V(index, _textures.size(), Ref<AtlasTexture>());
|
||||
|
||||
return texture_get_atlas_tex(_textures[index]);
|
||||
}
|
||||
Ref<AtlasTexture> PropMaterialCache::texture_get_atlas_tex(const Ref<Texture> &texture) {
|
||||
return Ref<AtlasTexture>();
|
||||
}
|
||||
Rect2 PropMaterialCache::texture_get_uv_rect(const Ref<Texture> &texture) {
|
||||
return Rect2(0, 0, 1, 1);
|
||||
}
|
||||
|
||||
|
||||
#ifdef PROPS_PRESENT
|
||||
void PropMaterialCache::prop_add_textures(const Ref<PropData> &prop) {
|
||||
if (!prop.is_valid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < prop->get_prop_count(); ++i) {
|
||||
#if MESH_DATA_RESOURCE_PRESENT
|
||||
Ref<PropDataMeshData> pdm = prop->get_prop(i);
|
||||
|
||||
if (pdm.is_valid()) {
|
||||
Ref<Texture> tex = pdm->get_texture();
|
||||
|
||||
if (!tex.is_valid())
|
||||
continue;
|
||||
|
||||
texture_add(tex);
|
||||
}
|
||||
#endif
|
||||
|
||||
Ref<PropDataProp> pdp = prop->get_prop(i);
|
||||
|
||||
if (pdp.is_valid()) {
|
||||
prop_add_textures(pdp);
|
||||
}
|
||||
}
|
||||
}
|
||||
void PropMaterialCache::prop_remove_textures(const Ref<PropData> &prop) {
|
||||
if (!prop.is_valid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < prop->get_prop_count(); ++i) {
|
||||
#if MESH_DATA_RESOURCE_PRESENT
|
||||
Ref<PropDataMeshData> pdm = prop->get_prop(i);
|
||||
|
||||
if (pdm.is_valid()) {
|
||||
Ref<Texture> tex = pdm->get_texture();
|
||||
|
||||
if (!tex.is_valid())
|
||||
continue;
|
||||
|
||||
texture_remove(tex);
|
||||
}
|
||||
#endif
|
||||
|
||||
Ref<PropDataProp> pdp = prop->get_prop(i);
|
||||
|
||||
if (pdp.is_valid()) {
|
||||
prop_remove_textures(pdp);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void PropMaterialCache::refresh_rects() {
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
void PropMaterialCache::setup_material_albedo(Ref<Texture> texture) {
|
||||
if (has_method("_setup_material_albedo"))
|
||||
call("_setup_material_albedo", texture);
|
||||
}
|
||||
|
||||
PropMaterialCache::PropMaterialCache() {
|
||||
_ref_count = 0;
|
||||
_initialized = false;
|
||||
}
|
||||
|
||||
PropMaterialCache::~PropMaterialCache() {
|
||||
_materials.clear();
|
||||
}
|
||||
|
||||
void PropMaterialCache::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("get_initialized"), &PropMaterialCache::get_initialized);
|
||||
ClassDB::bind_method(D_METHOD("set_initialized", "value"), &PropMaterialCache::set_initialized);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "initialized"), "set_initialized", "get_initialized");
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_ref_count"), &PropMaterialCache::get_ref_count);
|
||||
ClassDB::bind_method(D_METHOD("set_ref_count", "value"), &PropMaterialCache::set_ref_count);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::INT, "mat_ref_count"), "set_ref_count", "get_ref_count");
|
||||
ClassDB::bind_method(D_METHOD("inc_ref_count"), &PropMaterialCache::inc_ref_count);
|
||||
ClassDB::bind_method(D_METHOD("dec_ref_count"), &PropMaterialCache::dec_ref_count);
|
||||
|
||||
BIND_VMETHOD(MethodInfo("_setup_material_albedo", PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture")));
|
||||
|
||||
ClassDB::bind_method(D_METHOD("material_get", "index"), &PropMaterialCache::material_get);
|
||||
ClassDB::bind_method(D_METHOD("material_lod_get", "index"), &PropMaterialCache::material_lod_get);
|
||||
ClassDB::bind_method(D_METHOD("material_add", "value"), &PropMaterialCache::material_add);
|
||||
ClassDB::bind_method(D_METHOD("material_set", "index", "value"), &PropMaterialCache::material_set);
|
||||
ClassDB::bind_method(D_METHOD("material_remove", "index"), &PropMaterialCache::material_remove);
|
||||
ClassDB::bind_method(D_METHOD("material_get_num"), &PropMaterialCache::material_get_num);
|
||||
ClassDB::bind_method(D_METHOD("materials_clear"), &PropMaterialCache::materials_clear);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("materials_get"), &PropMaterialCache::materials_get);
|
||||
ClassDB::bind_method(D_METHOD("materials_set"), &PropMaterialCache::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("texture_add", "texture"), &PropMaterialCache::texture_add);
|
||||
ClassDB::bind_method(D_METHOD("texture_remove", "texture"), &PropMaterialCache::texture_remove);
|
||||
ClassDB::bind_method(D_METHOD("texture_remove_index", "index"), &PropMaterialCache::texture_remove_index);
|
||||
ClassDB::bind_method(D_METHOD("textures_clear"), &PropMaterialCache::textures_clear);
|
||||
ClassDB::bind_method(D_METHOD("texture_count"), &PropMaterialCache::texture_count);
|
||||
ClassDB::bind_method(D_METHOD("texture_get", "index"), &PropMaterialCache::texture_get);
|
||||
ClassDB::bind_method(D_METHOD("texture_get_atlas", "index"), &PropMaterialCache::texture_get_atlas);
|
||||
ClassDB::bind_method(D_METHOD("texture_get_atlas_tex", "index"), &PropMaterialCache::texture_get_atlas_tex);
|
||||
ClassDB::bind_method(D_METHOD("texture_get_uv_rect", "texture"), &PropMaterialCache::texture_get_uv_rect);
|
||||
|
||||
#ifdef PROPS_PRESENT
|
||||
ClassDB::bind_method(D_METHOD("prop_add_textures", "prop"), &PropMaterialCache::prop_add_textures);
|
||||
ClassDB::bind_method(D_METHOD("prop_remove_textures", "prop"), &PropMaterialCache::prop_remove_textures);
|
||||
#endif
|
||||
|
||||
ClassDB::bind_method(D_METHOD("refresh_rects"), &PropMaterialCache::refresh_rects);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("setup_material_albedo", "texture"), &PropMaterialCache::setup_material_albedo);
|
||||
}
|
97
material_cache/prop_material_cache.h
Normal file
97
material_cache/prop_material_cache.h
Normal file
@ -0,0 +1,97 @@
|
||||
/*
|
||||
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 PROP_MATERIAL_CACHE_H
|
||||
#define PROP_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"
|
||||
|
||||
class PropMaterialCache : public Resource {
|
||||
GDCLASS(PropMaterialCache, Resource)
|
||||
|
||||
public:
|
||||
bool get_initialized();
|
||||
void set_initialized(const bool value);
|
||||
|
||||
int get_ref_count();
|
||||
void set_ref_count(const int value);
|
||||
void inc_ref_count();
|
||||
void dec_ref_count();
|
||||
|
||||
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 void texture_add(const Ref<Texture> &texture);
|
||||
virtual void texture_remove(const Ref<Texture> &texture);
|
||||
virtual void texture_remove_index(const int index);
|
||||
virtual void textures_clear();
|
||||
virtual int texture_count();
|
||||
virtual Ref<Texture> texture_get(const int index);
|
||||
virtual Ref<AtlasTexture> texture_get_atlas(const int index);
|
||||
virtual Ref<AtlasTexture> texture_get_atlas_tex(const Ref<Texture> &texture);
|
||||
virtual Rect2 texture_get_uv_rect(const Ref<Texture> &texture);
|
||||
|
||||
#ifdef PROPS_PRESENT
|
||||
void prop_add_textures(const Ref<PropData> &prop);
|
||||
void prop_remove_textures(const Ref<PropData> &prop);
|
||||
#endif
|
||||
|
||||
virtual void refresh_rects();
|
||||
|
||||
void setup_material_albedo(Ref<Texture> texture);
|
||||
|
||||
PropMaterialCache();
|
||||
~PropMaterialCache();
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
bool _initialized;
|
||||
|
||||
Vector<Ref<Material>> _materials;
|
||||
Vector<Ref<Texture>> _textures;
|
||||
|
||||
int _ref_count;
|
||||
};
|
||||
|
||||
#endif
|
193
material_cache/prop_material_cache_pcm.cpp
Normal file
193
material_cache/prop_material_cache_pcm.cpp
Normal file
@ -0,0 +1,193 @@
|
||||
/*
|
||||
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_pcm.h"
|
||||
|
||||
#include "../../texture_packer/texture_packer.h"
|
||||
#include "scene/resources/texture.h"
|
||||
|
||||
int PropMaterialCachePCM::get_texture_flags() const {
|
||||
return _packer->get_texture_flags();
|
||||
}
|
||||
void PropMaterialCachePCM::set_texture_flags(const int flags) {
|
||||
_packer->set_texture_flags(flags);
|
||||
}
|
||||
|
||||
int PropMaterialCachePCM::get_max_atlas_size() const {
|
||||
return _packer->get_max_atlas_size();
|
||||
}
|
||||
void PropMaterialCachePCM::set_max_atlas_size(const int size) {
|
||||
_packer->set_max_atlas_size(size);
|
||||
}
|
||||
|
||||
bool PropMaterialCachePCM::get_keep_original_atlases() const {
|
||||
return _packer->get_keep_original_atlases();
|
||||
}
|
||||
void PropMaterialCachePCM::set_keep_original_atlases(const bool value) {
|
||||
_packer->set_keep_original_atlases(value);
|
||||
}
|
||||
|
||||
Color PropMaterialCachePCM::get_background_color() const {
|
||||
return _packer->get_background_color();
|
||||
}
|
||||
void PropMaterialCachePCM::set_background_color(const Color &color) {
|
||||
_packer->set_background_color(color);
|
||||
}
|
||||
|
||||
int PropMaterialCachePCM::get_margin() const {
|
||||
return _packer->get_margin();
|
||||
}
|
||||
void PropMaterialCachePCM::set_margin(const int margin) {
|
||||
_packer->set_margin(margin);
|
||||
}
|
||||
|
||||
Ref<AtlasTexture> PropMaterialCachePCM::texture_get_atlas_tex(const Ref<Texture> &texture) {
|
||||
if (!_packer->contains_texture(texture)) {
|
||||
return Ref<AtlasTexture>();
|
||||
}
|
||||
|
||||
return _packer->get_texture(texture);
|
||||
}
|
||||
Rect2 PropMaterialCachePCM::texture_get_uv_rect(const Ref<Texture> &texture) {
|
||||
if (!texture.is_valid()) {
|
||||
return Rect2(0, 0, 1, 1);
|
||||
}
|
||||
|
||||
Ref<AtlasTexture> at = _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);
|
||||
}
|
||||
|
||||
float w = image->get_width();
|
||||
float h = image->get_height();
|
||||
|
||||
region.position = Size2(region.position.x / w, region.position.y / h);
|
||||
region.size = Size2(region.size.x / w, region.size.y / h);
|
||||
|
||||
return region;
|
||||
}
|
||||
|
||||
void PropMaterialCachePCM::refresh_rects() {
|
||||
bool texture_added = false;
|
||||
|
||||
for (int i = 0; i < _textures.size(); i++) {
|
||||
Ref<Texture> tex = _textures.get(i);
|
||||
|
||||
ERR_CONTINUE(!tex.is_valid());
|
||||
|
||||
if (!_packer->contains_texture(tex)) {
|
||||
_packer->add_texture(tex);
|
||||
texture_added = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (texture_added) {
|
||||
_packer->merge();
|
||||
|
||||
ERR_FAIL_COND(_packer->get_texture_count() == 0);
|
||||
|
||||
Ref<Texture> tex = _packer->get_generated_texture(0);
|
||||
|
||||
setup_material_albedo(tex);
|
||||
}
|
||||
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
void PropMaterialCachePCM::_setup_material_albedo(Ref<Texture> texture) {
|
||||
int count = material_get_num();
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
Ref<Material> m = material_get(i);
|
||||
|
||||
Ref<SpatialMaterial> spmat = m;
|
||||
|
||||
if (spmat.is_valid()) {
|
||||
spmat->set_texture(SpatialMaterial::TEXTURE_ALBEDO, texture);
|
||||
return;
|
||||
}
|
||||
|
||||
Ref<ShaderMaterial> shmat = m;
|
||||
|
||||
if (shmat.is_valid()) {
|
||||
shmat->set_shader_param("texture_albedo", texture);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PropMaterialCachePCM::PropMaterialCachePCM() {
|
||||
_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);
|
||||
}
|
||||
|
||||
PropMaterialCachePCM::~PropMaterialCachePCM() {
|
||||
_packer->clear();
|
||||
_packer.unref();
|
||||
}
|
||||
|
||||
void PropMaterialCachePCM::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("get_texture_flags"), &PropMaterialCachePCM::get_texture_flags);
|
||||
ClassDB::bind_method(D_METHOD("set_texture_flags", "flags"), &PropMaterialCachePCM::set_texture_flags);
|
||||
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");
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_max_atlas_size"), &PropMaterialCachePCM::get_max_atlas_size);
|
||||
ClassDB::bind_method(D_METHOD("set_max_atlas_size", "size"), &PropMaterialCachePCM::set_max_atlas_size);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::INT, "max_atlas_size"), "set_max_atlas_size", "get_max_atlas_size");
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_keep_original_atlases"), &PropMaterialCachePCM::get_keep_original_atlases);
|
||||
ClassDB::bind_method(D_METHOD("set_keep_original_atlases", "value"), &PropMaterialCachePCM::set_keep_original_atlases);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_original_atlases"), "set_keep_original_atlases", "get_keep_original_atlases");
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_background_color"), &PropMaterialCachePCM::get_background_color);
|
||||
ClassDB::bind_method(D_METHOD("set_background_color", "color"), &PropMaterialCachePCM::set_background_color);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "background_color"), "set_background_color", "get_background_color");
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_margin"), &PropMaterialCachePCM::get_margin);
|
||||
ClassDB::bind_method(D_METHOD("set_margin", "size"), &PropMaterialCachePCM::set_margin);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::INT, "margin"), "set_margin", "get_margin");
|
||||
|
||||
ClassDB::bind_method(D_METHOD("_setup_material_albedo", "texture"), &PropMaterialCachePCM::_setup_material_albedo);
|
||||
}
|
81
material_cache/prop_material_cache_pcm.h
Normal file
81
material_cache/prop_material_cache_pcm.h
Normal file
@ -0,0 +1,81 @@
|
||||
/*
|
||||
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 PROP_MATERIAL_CACHE_PCM_H
|
||||
#define PROP_MATERIAL_CACHE_PCM_H
|
||||
|
||||
#include "prop_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"
|
||||
|
||||
class TexturePacker;
|
||||
class PropData;
|
||||
|
||||
class PropMaterialCachePCM : public PropMaterialCache {
|
||||
GDCLASS(PropMaterialCachePCM, PropMaterialCache);
|
||||
|
||||
public:
|
||||
int get_texture_flags() const;
|
||||
void set_texture_flags(const int flags);
|
||||
|
||||
int get_max_atlas_size() const;
|
||||
void set_max_atlas_size(const int size);
|
||||
|
||||
bool get_keep_original_atlases() const;
|
||||
void set_keep_original_atlases(const bool value);
|
||||
|
||||
Color get_background_color() const;
|
||||
void set_background_color(const Color &color);
|
||||
|
||||
int get_margin() const;
|
||||
void set_margin(const int margin);
|
||||
|
||||
Ref<AtlasTexture> texture_get_atlas_tex(const Ref<Texture> &texture);
|
||||
Rect2 texture_get_uv_rect(const Ref<Texture> &texture);
|
||||
|
||||
void refresh_rects();
|
||||
|
||||
void _setup_material_albedo(Ref<Texture> texture);
|
||||
|
||||
PropMaterialCachePCM();
|
||||
~PropMaterialCachePCM();
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
Ref<TexturePacker> _packer;
|
||||
};
|
||||
|
||||
#endif
|
@ -46,8 +46,8 @@ SOFTWARE.
|
||||
#include "prop_instance_job.h"
|
||||
#include "prop_instance_prop_job.h"
|
||||
|
||||
#include "jobs/prop_texture_job.h"
|
||||
#include "jobs/prop_mesher_job_step.h"
|
||||
#include "jobs/prop_texture_job.h"
|
||||
|
||||
#include "prop_scene_instance.h"
|
||||
|
||||
@ -58,6 +58,12 @@ SOFTWARE.
|
||||
|
||||
#include "prop_mesher.h"
|
||||
|
||||
#include "material_cache/prop_material_cache.h"
|
||||
|
||||
#ifdef TEXTURE_PACKER_PRESENT
|
||||
#include "material_cache/prop_material_cache_pcm.h"
|
||||
#endif
|
||||
|
||||
static PropUtils *prop_utils = NULL;
|
||||
static PropTextureCache *prop_texture_cache = NULL;
|
||||
|
||||
@ -86,6 +92,12 @@ void register_props_types() {
|
||||
|
||||
ClassDB::register_class<PropSceneInstance>();
|
||||
|
||||
ClassDB::register_class<PropMaterialCache>();
|
||||
|
||||
#ifdef TEXTURE_PACKER_PRESENT
|
||||
ClassDB::register_class<PropMaterialCachePCM>();
|
||||
#endif
|
||||
|
||||
prop_utils = memnew(PropUtils);
|
||||
ClassDB::register_class<PropUtils>();
|
||||
Engine::get_singleton()->add_singleton(Engine::Singleton("PropUtils", PropUtils::get_singleton()));
|
||||
|
Loading…
Reference in New Issue
Block a user