๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“ Development Study/๐Ÿ–ผ GameProgramming 2D

Animator, Animation2D

by eazuooz 2023. 3. 2.

์˜ˆ์ œ :)

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

๋Œ“๊ธ€