FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat(graphic): support local and file-based textures in material setup by Miou-zora · Pull Request #435 · EngineSquared/EngineSquared · GitHub

23 changes: 18 additions & 5 deletions examples/graphic_material_usage/src/main.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,33 @@ void Setup(Engine::Core &core)
cube.AddComponent<Object::Component::Transform>(glm::vec3(-2.0f, 0.0f, 0.0f));
cube.AddComponent<Object::Component::Mesh>(Object::Utils::GenerateCubeMesh());

// Custom Material with Texture
// Custom Material from file
Object::Component::Material materialWithTexture;
materialWithTexture.ambientTexName = "./asset/texture.png";
auto cube1 = core.CreateEntity();
cube1.AddComponent<Object::Component::Transform>();
cube1.AddComponent<Object::Component::Mesh>(Object::Utils::GenerateCubeMesh());
cube1.AddComponent<Object::Component::Material>(std::move(materialWithTexture));

// Custom Material without Texture
Object::Component::Material materialWithoutTexture;
// Custom Material from local texture
Object::Component::Material materialFromLocalTexture;
materialFromLocalTexture.ambientTexName = "LocalTextureName";
auto &textureManager = core.GetResource<Graphic::Resource::TextureContainer>();
auto texture =
Graphic::Resource::Texture(core.GetResource<Graphic::Resource::Context>(), "LocalTexture", glm::uvec2(255, 255),
[](glm::uvec2 pos) { return glm::u8vec4(pos.x, pos.y, 0, 255); });
textureManager.Add("LocalTextureName", std::move(texture));
auto cube2 = core.CreateEntity();
cube2.AddComponent<Object::Component::Transform>(glm::vec3(2.0f, 0.0f, 0.0f));
cube2.AddComponent<Object::Component::Transform>(glm::vec3(0.0f, 2.0f, 0.0f));
cube2.AddComponent<Object::Component::Mesh>(Object::Utils::GenerateCubeMesh());
cube2.AddComponent<Object::Component::Material>(std::move(materialWithoutTexture));
cube2.AddComponent<Object::Component::Material>(std::move(materialFromLocalTexture));

// Custom Material without Texture
Object::Component::Material materialWithoutTexture;
auto cube3 = core.CreateEntity();
cube3.AddComponent<Object::Component::Transform>(glm::vec3(2.0f, 0.0f, 0.0f));
cube3.AddComponent<Object::Component::Mesh>(Object::Utils::GenerateCubeMesh());
cube3.AddComponent<Object::Component::Material>(std::move(materialWithoutTexture));

// Camera
auto camera = core.CreateEntity();
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct Object {
}

struct Material {
emission: vec3f,
ambient: vec3f,
padding: f32,
};

Expand Down Expand Up @@ -89,7 +89,7 @@ fn fs_main(
var output : GBufferOutput;
var uv = vec2f(1.0 - fragUV.x, 1.0 - fragUV.y);
output.normal = vec4(normalize(fragNormal), 1.0);
output.albedo = vec4(textureSample(texture, textureSampler, uv).rgb, 1.0);
output.albedo = vec4(textureSample(texture, textureSampler, uv).rgb * material.ambient, 1.0);

return output;
}
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,21 @@ void DefaultPipeline::System::OnMaterialCreation(Engine::Core &core, Engine::Ent
entt::hashed_string textureId{material.ambientTexName.data(), material.ambientTexName.size()};
entt::hashed_string samplerId{material.ambientTexName.data(), material.ambientTexName.size()};

if (std::filesystem::exists(material.ambientTexName) == false)
{
Log::Warn(fmt::format("Material texture file not found: {}", material.ambientTexName));
GPUMaterial.texture = entt::hashed_string{};
}
else
if (std::filesystem::exists(material.ambientTexName))
{
Graphic::Resource::Texture texture{context, material.ambientTexName,
Graphic::Resource::Image(material.ambientTexName)};
textureContainer.Add(textureId, std::move(texture));
GPUMaterial.texture = textureId;
}
else if (textureContainer.Contains(textureId))
{
GPUMaterial.texture = textureId;
}
Comment thread
Miou-zora marked this conversation as resolved.
else if (!material.ambientTexName.empty())
{
Log::Warn(fmt::format("Texture '{}' not found as file or in texture container", material.ambientTexName));
}

Graphic::Resource::Sampler sampler{context.deviceContext.GetDevice().value()};
samplerContainer.Add(samplerId, std::move(sampler));
Expand Down
Loading

Back | FazBrowse Home | New Git URL