응애맘마조

230831 강의 본문

공부/3D강의

230831 강의

TH.Wert 2023. 8. 31. 18:09

이펙트 중 하나인 트레일에 대해서 강의했습니다. 검을 휘둘렀을 때 그 궤도를 생각하면 쉬울 것 같습니다.

#pragma once
class SlashTrail
{
    shared_ptr<Shader> shader;

    vector<VertexPT> vertices;
    ID3D11Buffer*           vertexBuffer;
    D3D_PRIMITIVE_TOPOLOGY  primitiveTopology;
    VertexType              vertexType;
    UINT                    byteWidth;

    Vector3 lastTop;
    Vector3 lastBottom;
public:
    GameObject* Top = nullptr;
    GameObject* Bottom = nullptr;
    SlashTrail();
    ~SlashTrail();
    void Update();
    void Render();
};
#include "framework.h"

SlashTrail::SlashTrail()
{
    vertexType = VertexType::PT;
    primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
    byteWidth = sizeof(VertexPT);

    shader = RESOURCE->shaders.Load("6.Trail.hlsl");
}

SlashTrail::~SlashTrail()
{
}

void SlashTrail::Update()
{
    //틱당 사각형 하나 추가

    if (lastTop != Vector3())
    {
        vertices.push_back(VertexPT(lastTop, Vector2(0, 0)));
        vertices.push_back(VertexPT(lastBottom, Vector2(0, 0)));
        vertices.push_back(VertexPT(Top->GetWorldPos(), Vector2(0, 0)));

        vertices.push_back(VertexPT(lastBottom, Vector2(0, 0)));
        vertices.push_back(VertexPT(Bottom->GetWorldPos(), Vector2(0, 0)));
        vertices.push_back(VertexPT(Top->GetWorldPos(), Vector2(0, 0)));

        //CreateVertexBuffer
        {
            D3D11_BUFFER_DESC desc;
            desc = { 0 };
            desc.Usage = D3D11_USAGE_DEFAULT;
            desc.ByteWidth = byteWidth * vertices.size();
            desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;

            D3D11_SUBRESOURCE_DATA data = { 0 };
            data.pSysMem = &vertices[0];
            SafeRelease(vertexBuffer);
            HRESULT hr = D3D->GetDevice()->CreateBuffer(&desc, &data, &vertexBuffer);
            assert(SUCCEEDED(hr));
        }
    }
    lastTop = Top->GetWorldPos();
    lastBottom = Bottom->GetWorldPos();
}

void SlashTrail::Render()
{
    if (vertices.size() < 1) return;

    UINT offset = 0;

    D3D->GetDC()->IASetVertexBuffers(0,
        1,
        &vertexBuffer,
        &byteWidth,
        &offset);
    D3D->GetDC()->IASetPrimitiveTopology
    (primitiveTopology);

    shader->Set();

    BLEND->Set(true);
    RASTER->Set(D3D11_CULL_NONE);
    D3D->GetDC()->Draw(vertices.size(), 0);
    RASTER->Set(D3D11_CULL_BACK);
    BLEND->Set(false);
}

삼각형을 이어 붙이는 식으로 검의 시작점과 끝 점을 연결하고 선입선출 방식으로 이펙트를 나타냈다가 지우는 방식을 사용하게 됩니다. 아직 미완성이라 완벽하게 구현은 되지 않았지만 내일 완성될 예정입니다.

움직이는 궤도를 나타낸 모습

아직 선입선출과 텍스쳐까지는 구현되지 않았습니다.

 

읽어주셔서 감사합니다.

'공부 > 3D강의' 카테고리의 다른 글

230904 강의  (0) 2023.09.04
230901 강의  (0) 2023.09.01
230830 강의  (0) 2023.08.30
230829 강의  (0) 2023.08.29
230828 강의  (0) 2023.08.29
Comments