Skip to content

Commit

Permalink
[mesh] removed out of place set texture function
Browse files Browse the repository at this point in the history
  • Loading branch information
PanosK92 committed Nov 11, 2024
1 parent 2ff00c4 commit 210a7ff
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 32 deletions.
2 changes: 1 addition & 1 deletion runtime/Rendering/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ namespace Spartan
return m_textures[(static_cast<uint32_t>(texture_type) * slots_per_texture_type) + slot];
}

void Material::Optimize()
void Material::PrepareForGPU()
{
if (HasTextureOfType(MaterialTextureType::Color) && HasTextureOfType(MaterialTextureType::AlphaMask))
{
Expand Down
2 changes: 1 addition & 1 deletion runtime/Rendering/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ namespace Spartan
}

// misc
void Optimize();
void PrepareForGPU();
uint32_t GetUsedSlotCount() const;
void SetIndex(const uint32_t index) { m_index = index; }
uint32_t GetIndex() const { return m_index; }
Expand Down
23 changes: 0 additions & 23 deletions runtime/Rendering/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,27 +373,4 @@ namespace Spartan
// create a Renderable and pass the material to it
entity->AddComponent<Renderable>()->SetMaterial(material);
}

void Mesh::AddTexture(shared_ptr<Material>& material, const MaterialTextureType texture_type, const string& file_path, bool is_gltf)
{
SP_ASSERT(material != nullptr);
SP_ASSERT(!file_path.empty());

// Try to get the texture
const auto tex_name = FileSystem::GetFileNameWithoutExtensionFromFilePath(file_path);
shared_ptr<RHI_Texture> texture = ResourceCache::GetByName<RHI_Texture>(tex_name);

if (texture)
{
material->SetTexture(texture_type, texture);
}
else // if we didn't get a texture, it's not cached, hence we have to load it and cache it now
{
// load texture
texture = ResourceCache::Load<RHI_Texture>(file_path, RHI_Texture_Srv | RHI_Texture_Compress);

// set the texture to the provided material
material->SetTexture(texture_type, texture);
}
}
}
2 changes: 0 additions & 2 deletions runtime/Rendering/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ namespace Spartan

// aabb
const Math::BoundingBox& GetAabb() const { return m_aabb; }
void ComputeAabb();

// gpu buffers
void CreateGpuBuffers();
Expand All @@ -109,7 +108,6 @@ namespace Spartan

void PostProcess();
void SetMaterial(std::shared_ptr<Material>& material, Entity* entity) const;
void AddTexture(std::shared_ptr<Material>& material, MaterialTextureType texture_type, const std::string& file_path, bool is_gltf);

private:
// geometry
Expand Down
28 changes: 23 additions & 5 deletions runtime/Resource/Import/ModelImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ 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.
*/

//= INCLUDES ==========================
//= INCLUDES ============================
#include "pch.h"
#include "ModelImporter.h"
#include "../../Core/ProgressTracker.h"
Expand All @@ -28,15 +28,16 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "../../Rendering/Mesh.h"
#include "../../World/World.h"
#include "../../World/Entity.h"
#include "../World/Components/Light.h"
#include "../../World/Components/Light.h"
#include "../../Resource/ResourceCache.h"
SP_WARNINGS_OFF
#include "assimp/scene.h"
#include "assimp/ProgressHandler.hpp"
#include "assimp/version.h"
#include "assimp/Importer.hpp"
#include "assimp/postprocess.h"
SP_WARNINGS_ON
//=====================================
//=======================================

//= NAMESPACES ===============
using namespace std;
Expand Down Expand Up @@ -244,7 +245,24 @@ namespace Spartan
return false;

// add the texture to the model
mesh->AddTexture(material, texture_type, texture_validate_path(texture_path.data, file_path), is_gltf);
{
// Try to get the texture
const string tex_name = FileSystem::GetFileNameWithoutExtensionFromFilePath(deduced_path);
shared_ptr<RHI_Texture> texture = ResourceCache::GetByName<RHI_Texture>(tex_name);

if (texture)
{
material->SetTexture(texture_type, texture);
}
else // if we didn't get a texture, it's not cached, hence we have to load it and cache it now
{
// load texture
texture = ResourceCache::Load<RHI_Texture>(deduced_path, RHI_Texture_Srv | RHI_Texture_Compress);

// set the texture to the provided material
material->SetTexture(texture_type, texture);
}
}

// FIX: materials that have a diffuse texture should not be tinted black/gray
if (type_assimp == aiTextureType_BASE_COLOR || type_assimp == aiTextureType_DIFFUSE)
Expand Down Expand Up @@ -385,7 +403,7 @@ namespace Spartan
}
}

material->Optimize();
material->PrepareForGPU();

return material;
}
Expand Down

0 comments on commit 210a7ff

Please sign in to comment.