์์ :)
https://github.com/eazuooz/YamYamEngine/commit/4eeca35f9022cf076e299466032e9e4db0679707
Animation 2D · eazuooz/YamYamEngine@4eeca35
Show file tree Showing 19 changed files with 236 additions and 60 deletions.
github.com
2D ๊ฒ์์์ ์ ๋๋ฉ์ด์ ์ ์ฌ์์ํค๋ ๋ฐฉ๋ฒ์ ์ํ์์ ์ฐ์ด๋ ๋ฐฉ์๊ณผ ํฌ๊ฒ ๋ค๋ฅด์ง ์๋ค.
์ด๋ฌํ ์ ๋๋ฉ์ด์ ๊ธฐ๋ฒ์ Sprite ์ ๋๋ฉ์ด์ ์ด๋ผ๊ณ ํ๋ค.
์ด๋ฌํ ๊ทธ๋ฆผ์ ํ์ฅ์ฅํ์ ์์ง์ด๋ ์์๋๋ก ๋น ๋ฅธ์๊ฐ๋์ ์ฌ์์์ผ์ฃผ๋ฉด ๋์ ์ฐฉ์๋ก ์์ง์ด๋ ๊ฒ ์ฒ๋ผ ๋ณด์ด๊ฒ ๋๋ค.
์ฐ์ ์ Animation์ด ์ฌ์๋๊ฒ ํ๋ Animator ์ปดํฌ๋ํธ์
Animator ์ปดํฌ๋ํธ๋ Animation ์ ๊ฐ์ง๊ณ ์๋ค.
#pragma once
#include "yaComponent.h"
#include "yaTexture.h"
#include "yaAnimation.h"
namespace ya
{
using namespace ya::graphics;
class Animator : public Component
{
public:
struct Event
{
void operator=(std::function<void()> func)
{
mEvent = std::move(func);
}
void operator()()
{
if (mEvent)
mEvent();
}
std::function<void()> mEvent;
};
struct Events
{
Event mStartEvent;
Event mCompleteEvent;
Event mEndEvent;
};
Animator();
~Animator();
virtual void Initialize();
virtual void Update();
virtual void FixedUpdate();
virtual void Render();
bool CreateAnimation(const std::wstring& name, std::shared_ptr<Texture> atlas, Vector2 leftTop, Vector2 size, Vector2 offset, float columnLegth, UINT spriteLength, float duration);
Animation* Find(const std::wstring& name);
void Play(const std::wstring& name, bool loop);
void Binds();
private:
std::map<std::wstring, Animation*> mAnimations;
std::map<std::wstring, Events*> mEvents;
Animation* mActiveAnimation;
bool mbLoop;
};
}
Animator ์ปดํฌ๋ํธ๋ ์ฌ๋ฌ๊ฐ์ Animation ๋ค (์ : ๋ฌ๋ฆฌ๊ธฐ, ๊ฑท๊ธฐ, ๋๊ธฐ, ์ฃฝ๋ ๋ชจ์ ๋ฑ) ์ ๊ฐ์ง๊ณ ์๊ฒ ๋๋ค.
๊ทธ๋ฆฌ๊ณ ํ์ฌ ์คํ๋๋ ์ ๋๋ฉ์ด์ ์ PlayAnimation์ด๋ค.
Loop๊ฐ ํ์ฑํ ๋์ด์๋ค๋ฉด ํด๋น ์ ๋๋ฉ์ด์ ์ ์ข ๋ฃ๋์ง ์๊ณ
๋ค์ Reset() ์์ผ์ ์ฒ์๋ถํฐ ๋ค์ ์ฌ์๋๊ฒ ๋๋ค.
void Animator::FixedUpdate()
{
if (mActiveAnimation == nullptr)
return;
if (mActiveAnimation->IsComplete() && mbLoop)
{
mActiveAnimation->Reset();
}
mActiveAnimation->FixedUpdate();
}
์ ๋๋ฉ์ด์ ํด๋์ค๋ ๋ค์๊ณผ ๊ฐ์ด ๊ตฌ์ฑ๋์ด ์๋ค.
#pragma once
#include "yaEntity.h"
#include "yaTexture.h"
namespace ya
{
using namespace ya::math;
using namespace ya::graphics;
class Animator;
class Animation : public Entity
{
public:
struct Sprite
{
Vector2 leftTop; // ์ข์ธก ์๋จ ํฝ์
์ขํ
Vector2 size; // ์ข์ธก ์๋จ์ผ๋ก ๋ถํฐ ์๋ผ๋ผ ๊ฐ๋ก ์ธ๋ก ํฝ์
๊ธธ์ด
Vector2 offset;
Vector2 atlasSize;
float duration; // ํด๋น ํ๋ ์ ์ ์ง์๊ฐ
Sprite()
: leftTop(0.0f, 0.0f)
, size(0.0f, 0.0f)
, offset(0.0f, 0.0f)
, atlasSize(0.0f, 0.0f)
, duration(0.0f)
{
}
};
Animation();
~Animation();
void Update();
void FixedUpdate();
void Render();
void Create(const std::wstring& name, std::shared_ptr<Texture> atlas, Vector2 leftTop, Vector2 size, Vector2 offset
, float columnLegth, UINT spriteLength, float duration);
void Binds();
void Reset();
bool IsComplete() { return mbComplete; }
private:
std::shared_ptr<Texture> mAtlas;
Animator* mAnimator;
std::vector<Sprite> mSpriteSheet;
int mIndex;
float mTime;
bool mbComplete;
};
}
Animation ํด๋์ค๋ Sprite Image๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
์ฌ๋ฌ๊ฐ์ Sprite ๊ฐ ๋ชจ์ฌ์ ํ๋์ Sprite sheet๊ฐ ๋๊ณ ๊ทธ๊ฒ์ด ํ๋์ ์ ๋๋ฉ์ด์ ์ ๋ณด๋ผ๊ณ ๋ณด๋ฉด ๋๋ค.
์ ๋๋ฉ์ด์ ์ ์์ฑํ ๋ ํด๋น ์ด๋ฏธ์ง์์ ์์์ ์ ๊ธฐ์ค์ผ๋ก ํ์นธ์ฉ ์ด๋ํ๋ฉด์ ๊ทธ๋ฆผ๋ณ๋ก Sprite์ ๋ณด๋ฅผ Spritesheet์๋ค๊ฐ ์ ์ฅํ๋ค.
์ ๋๋ฉ์ด์ ์ ๋ง๋ค๋๋ ํด๋น ํ ์ค์ฒ์ UV ์ขํ๋ฅผ ํตํด ์ ๋๋ฉ์ด์ ์์์์น์ ๋์์น๋ฅผ ๊ณ์ฐํด์ ์ ์ฅํด๋๋ค.
void Animation::Create(const std::wstring& name, std::shared_ptr<Texture> atlas, Vector2 leftTop, Vector2 size, Vector2 offset, float spriteLength, UINT column, float duration)
{
// Animation Name
SetName(name);
@@ -47,13 +48,15 @@ namespace ya
float height = (float)atlas->GetHeight();
// Frame Info
for (int i = 0; i < column; ++i)
{
Sprite frm = {};
frm.leftTop = Vector2( (leftTop.x + spriteLength * (float)i) / width, leftTop.y / height );
frm.size = Vector2(size.x / width, size.y / height) ;
frm.offset = offset;
frm.duration = duration;
//frm.atlasSize = Vector2(width, height);
frm.atlasSize = Vector2(200.0f / width, 200.0f / height);
mSpriteSheet.push_back(frm);
}
@@ -63,6 +66,18 @@ namespace ya
{
mAtlas->BindShader(eShaderStage::PS, 12);
ConstantBuffer* cb = renderer::constantBuffers[(UINT)eCBType::Animator];
renderer::AnimatorCB info = {};
info.type = (UINT)eAnimatorType::SecondDimension;
info.leftTop = mSpriteSheet[mIndex].leftTop;
info.offset = mSpriteSheet[mIndex].offset;
info.size = mSpriteSheet[mIndex].size;
info.atlasSize = mSpriteSheet[mIndex].atlasSize;
cb->Bind(&info);
cb->SetPipline(eShaderStage::PS);
//์ ๋๋ฉ์ด์
์์๋ฒํผ ์ ์
}
๊ทธ๋ฆฌ๊ณ ๋์ ํฝ์ ์ ฐ์ด๋์์ ํด๋น ์ขํ๋ก ๊ทธ๋ ค์ฃผ๋ฉด ๋๋ค.
float4 main(VSOut Out) : SV_Target
{
float4 color = float4(1.0f, 0.0f, 1.0f, 1.0f);
if (animationType == 1) // 2D
{
float2 diff = (atlasSize - spriteSize) / 2.0f;
float2 UV = (leftTop - diff - offset) + (atlasSize * Out.UV);
// ์ฌ๋ฐฑ ์ง์ ์ฃผ๋ ์ฝ๋
if (UV.x < leftTop.x || leftTop.x + spriteSize.x < UV.x
|| UV.y < leftTop.y || leftTop.y + spriteSize.y < UV.y)
{
discard;
}
color = atlasTexture.Sample(anisotropicSampler, UV);
}
else if (animationType == 2) // 3D
{
}
else
{
color = triangleTexture.Sample(anisotropicSampler, Out.UV);
}
return color;
}
'๐ Development Study > ๐ผ GameProgramming 2D' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
2D Light (0) | 2023.04.08 |
---|---|
Animation Event (0) | 2023.03.02 |
Collision check / OBB (0) | 2023.03.02 |
Grid ์ค๋ธ์ ํธ (0) | 2023.03.02 |
๊ณ์ธต ๊ตฌ์กฐ (0) | 2023.02.07 |
๋๊ธ