Removed multiple material support from the MaterialCache. Multiple material support was built for lods, and it's not needed here.

This commit is contained in:
Relintai 2022-02-23 02:34:15 +01:00
parent 3cde50a5c5
commit cca967333d
6 changed files with 21 additions and 98 deletions

View File

@ -86,62 +86,12 @@ void Prop2DMaterialCache::dec_ref_count() {
}
//Materials
Ref<Material> Prop2DMaterialCache::material_get(const int index) {
ERR_FAIL_INDEX_V(index, _materials.size(), Ref<Material>(NULL));
return _materials[index];
Ref<Material> Prop2DMaterialCache::material_get() {
return _material;
}
Ref<Material> Prop2DMaterialCache::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 Prop2DMaterialCache::material_add(const Ref<Material> &value) {
ERR_FAIL_COND(!value.is_valid());
_materials.push_back(value);
}
void Prop2DMaterialCache::material_set(const int index, const Ref<Material> &value) {
ERR_FAIL_INDEX(index, _materials.size());
_materials.set(index, value);
}
void Prop2DMaterialCache::material_remove(const int index) {
_materials.remove(index);
}
int Prop2DMaterialCache::material_get_num() const {
return _materials.size();
}
void Prop2DMaterialCache::materials_clear() {
_materials.clear();
}
Vector<Variant> Prop2DMaterialCache::materials_get() {
VARIANT_ARRAY_GET(_materials);
}
void Prop2DMaterialCache::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 Prop2DMaterialCache::material_set(const Ref<Material> &value) {
_material = value;
}
void Prop2DMaterialCache::texture_add(const Ref<Texture> &texture) {
@ -301,7 +251,7 @@ void Prop2DMaterialCache::initial_setup_default() {
Ref<Material> md = m->duplicate();
_materials.push_back(md);
_material = md;
}
}
@ -317,7 +267,6 @@ Prop2DMaterialCache::Prop2DMaterialCache() {
}
Prop2DMaterialCache::~Prop2DMaterialCache() {
_materials.clear();
}
void Prop2DMaterialCache::_bind_methods() {
@ -337,17 +286,9 @@ void Prop2DMaterialCache::_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"), &Prop2DMaterialCache::material_get);
ClassDB::bind_method(D_METHOD("material_lod_get", "index"), &Prop2DMaterialCache::material_lod_get);
ClassDB::bind_method(D_METHOD("material_add", "value"), &Prop2DMaterialCache::material_add);
ClassDB::bind_method(D_METHOD("material_set", "index", "value"), &Prop2DMaterialCache::material_set);
ClassDB::bind_method(D_METHOD("material_remove", "index"), &Prop2DMaterialCache::material_remove);
ClassDB::bind_method(D_METHOD("material_get_num"), &Prop2DMaterialCache::material_get_num);
ClassDB::bind_method(D_METHOD("materials_clear"), &Prop2DMaterialCache::materials_clear);
ClassDB::bind_method(D_METHOD("materials_get"), &Prop2DMaterialCache::materials_get);
ClassDB::bind_method(D_METHOD("materials_set"), &Prop2DMaterialCache::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("material_get"), &Prop2DMaterialCache::material_get);
ClassDB::bind_method(D_METHOD("material_set", "value"), &Prop2DMaterialCache::material_set);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "Material"), "material_set", "material_get");
ClassDB::bind_method(D_METHOD("texture_add", "texture"), &Prop2DMaterialCache::texture_add);
ClassDB::bind_method(D_METHOD("texture_remove", "texture"), &Prop2DMaterialCache::texture_remove);

View File

@ -57,16 +57,8 @@ public:
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);
Ref<Material> material_get();
void material_set(const Ref<Material> &value);
virtual void texture_add(const Ref<Texture> &texture);
virtual void texture_remove(const Ref<Texture> &texture);
@ -98,7 +90,7 @@ protected:
bool _locked;
bool _initialized;
Vector<Ref<Material>> _materials;
Ref<Material> _material;
Vector<Ref<Texture>> _textures;
int _ref_count;

View File

@ -146,24 +146,13 @@ void Prop2DMaterialCachePCM::initial_setup_default() {
}
void Prop2DMaterialCachePCM::_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);
continue;
}
Ref<Material> m = material_get();
Ref<ShaderMaterial> shmat = m;
if (shmat.is_valid()) {
shmat->set_shader_param("texture_albedo", texture);
}
}
}
Prop2DMaterialCachePCM::Prop2DMaterialCachePCM() {

View File

@ -547,13 +547,14 @@ void Prop2DInstanceMerger::_build() {
Ref<Prop2DMaterialCache> cache = Prop2DCache::get_singleton()->material_cache_get(_prop_data);
if (cache->material_get_num() == 0) {
//TODO this needs to be changed
if (!cache->material_get().is_valid()) {
//lock it!
cache->mutex_lock();
//check again, this thread might have gotten here after an another one already did the initialization!
//this check might not be needed here
if (cache->material_get_num() == 0) {
if (!cache->material_get().is_valid()) {
//this will set up materials, and settings
//needs to be called from the main thread!
cache->initial_setup_default();

View File

@ -384,7 +384,7 @@ void Prop2DInstanceProp2DJob::step_type_normal() {
VS::get_singleton()->mesh_add_surface_from_arrays(mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr);
Ref<Material> lmat = _material_cache->material_lod_get(0);
Ref<Material> lmat = _material_cache->material_get();
if (lmat.is_valid()) {
VisualServer::get_singleton()->mesh_surface_set_material(mesh_rid, 0, lmat->get_rid());

View File

@ -247,14 +247,14 @@ void TiledWall2DData::setup_cache(Ref<Prop2DMaterialCache> cache) {
call("_setup_cache", cache);
}
void TiledWall2DData::_setup_cache(Ref<Prop2DMaterialCache> cache) {
if (cache->material_get_num() == 0) {
if (!cache->material_get().is_valid()) {
for (int i = 0; i < _materials.size(); ++i) {
const Ref<Material> &m = _materials[i];
if (m.is_valid()) {
Ref<Material> nm = m->duplicate();
cache->material_add(nm);
cache->material_set(nm);
}
}
}