This commit is contained in:
Isuru Samarathunga
2025-09-19 00:40:06 +05:30
parent 05aa5b08f8
commit 1c6d38781f
7 changed files with 61 additions and 26 deletions

View File

@ -23,6 +23,12 @@
namespace ia::iae
{
SpriteRendererComponent::AnimationKeyFrame SpriteRendererComponent::GetKeyFrame(IN INT32 index)
{
index %= m_activeAnimation.Keys.size();
return m_reverseAnimation ? m_activeAnimation.Keys[m_activeAnimation.Keys.size() - 1 - index] : m_activeAnimation.Keys[index];
}
SpriteRendererComponent::SpriteRendererComponent(IN Node *node) : IComponent(node)
{
}
@ -48,8 +54,10 @@ namespace ia::iae
IA_RELEASE_ASSERT((animation != INVALID_HANDLE) && (animation < m_animations.size()));
m_prevAnimationKeyFrameIndex = 0;
m_activeAnimation = m_animations[animation];
m_prevAnimationKeyFrame = m_activeAnimation.Keys[m_prevAnimationKeyFrameIndex + 0];
m_nextAnimationKeyFrame = m_activeAnimation.Keys[(m_prevAnimationKeyFrameIndex + 1) % m_activeAnimation.Keys.size()];
m_prevAnimationKeyFrame = GetKeyFrame(m_prevAnimationKeyFrameIndex + 0);
m_nextAnimationKeyFrame = GetKeyFrame(m_prevAnimationKeyFrameIndex + 1);
m_currentAnimationState = m_prevAnimationKeyFrame;
m_activeAnimationHandle = animation;
}
@ -97,8 +105,8 @@ namespace ia::iae
m_activeAnimation = {};
return;
}
m_prevAnimationKeyFrame = m_activeAnimation.Keys[m_prevAnimationKeyFrameIndex + 0];
m_nextAnimationKeyFrame = m_activeAnimation.Keys[(m_prevAnimationKeyFrameIndex + 1) % keyCount];
m_prevAnimationKeyFrame = GetKeyFrame(m_prevAnimationKeyFrameIndex + 0);
m_nextAnimationKeyFrame = GetKeyFrame(m_prevAnimationKeyFrameIndex + 1);
m_currentAnimationState = m_prevAnimationKeyFrame;
m_timelinePosition = 0;
}

View File

@ -55,6 +55,8 @@ namespace ia::iae
glm::mat4 matView{1.0f};
glm::mat4 matModel{1.0f};
BOOL g_flipH{false};
BOOL g_flipV{false};
FLOAT32 g_parallaxFactor{0.0f};
BOOL Renderer::Initialize()
@ -219,20 +221,22 @@ namespace ia::iae
SDL_SubmitGPUCommandBuffer(g_cmdBuffer);
}
VOID Renderer::BindTexture(IN Handle handle, IN BOOL flipV, IN BOOL flipH, IN CONST glm::vec4 &colorOverlay)
VOID Renderer::SetFlipH(IN BOOL value)
{
STATIC struct
{
glm::vec4 colorOverlay;
UINT32 flipV;
UINT32 flipH;
} textureState;
g_flipH = value;
}
textureState = {colorOverlay, flipV, flipH};
VOID Renderer::SetFlipV(IN BOOL value)
{
g_flipV = value;
}
VOID Renderer::BindTexture(IN Handle handle, IN CONST glm::vec4 &colorOverlay)
{
SDL_GPUTextureSamplerBinding binding{.texture = (SDL_GPUTexture *) handle,
.sampler = (SDL_GPUSampler *) GPUTexture::GetDefaultSampler()};
SDL_BindGPUFragmentSamplers(g_renderPass, 0, &binding, 1);
SDL_PushGPUFragmentUniformData(g_cmdBuffer, 0, &textureState, sizeof(textureState));
SDL_PushGPUFragmentUniformData(g_cmdBuffer, 0, &colorOverlay, sizeof(colorOverlay));
}
VOID SetModelTransformMatrix(IN FLOAT32 sortOffset, IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale,
@ -243,13 +247,28 @@ namespace ia::iae
else
matView = glm::mat4(1.0f);
SDL_PushGPUVertexUniformData(g_cmdBuffer, 1, &matView, sizeof(matView));
g_parallaxFactor *= g_camera.Position().x/640.0f;
SDL_PushGPUFragmentUniformData(g_cmdBuffer, 1, &g_parallaxFactor, sizeof(g_parallaxFactor));
FLOAT32 shaderParallaxFactor = g_parallaxFactor * g_camera.Position().x / 640.0f;
SDL_PushGPUFragmentUniformData(g_cmdBuffer, 1, &shaderParallaxFactor, sizeof(shaderParallaxFactor));
matModel = glm::mat4(1.0f);
glm::vec3 poff = {0.0f, 0.0f, 0.0f};
const auto f = 1.0f;
if(g_flipH)
{
poff.x = scale.x * f;
}
// [IATODO]: IMPL Flip V
const auto depthTestOffset = sortOffset + (Engine::GetActiveScene()->YSortingEnabled() ? position.y : 0);
matModel = glm::translate(glm::mat4(1.0f), {position.x, position.y, position.z + depthTestOffset + (g_parallaxFactor < 0.0f ? 0.0f : -100.0f)});
matModel =
glm::translate(matModel, {position.x + poff.x, position.y,
position.z + depthTestOffset + (g_parallaxFactor < 0.0f ? 0.0f : -100.0f)});
matModel = glm::rotate(matModel, rotation, glm::vec3(0.0f, 0.0f, 1.0f));
matModel = glm::scale(matModel, scale);
matModel = glm::scale(matModel, scale * (g_flipH ? glm::vec3{-1.0f, 1.0f, 1.0f} : glm::vec3{1.0f, 1.0f, 1.0f}));
SDL_PushGPUVertexUniformData(g_cmdBuffer, 2, &matModel, sizeof(matModel));
}

View File

@ -38,7 +38,9 @@ namespace ia::iae
VOID Texture::Draw(IN FLOAT32 sortOffset, IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation, IN BOOL flipH,
IN BOOL flipV, IN CONST glm::vec4 &colorOverlay) CONST
{
Renderer::BindTexture(m_handle, flipV, flipH, colorOverlay);
Renderer::SetFlipH(flipH);
Renderer::SetFlipV(flipV);
Renderer::BindTexture(m_handle, colorOverlay);
QuadMesh::Draw(sortOffset, position, m_size * scale, rotation);
}
} // namespace ia::iae

View File

@ -8,8 +8,6 @@ layout(location = 0) out vec4 outColor;
layout(set = 2, binding = 0) uniform sampler2D texSampler;
layout(set = 3, binding = 0) uniform UniformBufferObject {
vec4 colorOverlay;
bool flipV;
bool flipH;
} ubo;
layout(set = 3, binding = 1) uniform UniformBufferObject2 {
float parallaxFactor;
@ -19,8 +17,6 @@ void main()
{
vec2 uv = inTexCoord;
uv.x += ubo2.parallaxFactor;
if(ubo.flipH) uv.x = 1 - uv.x;
if(ubo.flipV) uv.y = 1 - uv.y;
outColor = texture(texSampler, uv) * ubo.colorOverlay;
if(outColor.w < 0.1)
discard;

File diff suppressed because one or more lines are too long

View File

@ -76,11 +76,16 @@ namespace ia::iae
return m_sortOffset;
}
FLOAT32& ParallaxFactor()
FLOAT32 &ParallaxFactor()
{
return m_parallaxFactor;
}
BOOL &ReverseAnimation()
{
return m_reverseAnimation;
}
public:
VOID Draw();
VOID Update();
@ -88,7 +93,10 @@ namespace ia::iae
private:
VOID UpdateAnimation();
AnimationKeyFrame GetKeyFrame(IN INT32 index);
private:
BOOL m_reverseAnimation{};
FLOAT32 m_parallaxFactor{0.0f};
FLOAT32 m_sortOffset{};
BOOL m_isFlippedV{false};

View File

@ -44,13 +44,15 @@ namespace ia::iae
STATIC VOID BeginFrame();
STATIC VOID EndFrame();
STATIC VOID BindTexture(IN Handle handle, IN BOOL flipV, IN BOOL flipH, IN CONST glm::vec4& colorOverlay);
STATIC VOID BindTexture(IN Handle handle, IN CONST glm::vec4& colorOverlay);
STATIC VOID Draw(IN FLOAT32 sortOffset, IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation, IN Handle vertexBufferHandle, IN INT32 vertexCount);
STATIC VOID Draw(IN FLOAT32 sortOffset, IN CONST glm::vec3 &position, IN CONST glm::vec3 &scale, IN FLOAT32 rotation, IN Handle vertexBufferHandle, IN Handle indexBufferHandle, IN INT32 indexCount);
STATIC Camera2D* GetCamera();
STATIC VOID SetFlipH(IN BOOL value);
STATIC VOID SetFlipV(IN BOOL value);
STATIC VOID SetParallaxFactor(IN FLOAT32 value);
public: