Material
더보기
#include "Framework.h"
Material::Material()
{
diffuseMap = Texture::Add(L"Textures/Color/White.png", L"DM");
specularMap = Texture::Add(L"Textures/Color/White.png", L"SM");
normalMap = Texture::Add(L"Textures/Color/White.png", L"NM");
secondDiffuseMap = Texture::Add(L"Textures/Color/White.png", L"2nd DM");
secondSpecularMap = Texture::Add(L"Textures/Color/White.png", L"2nd SM");
thirdDiffuseMap = Texture::Add(L"Textures/Color/White.png", L"3rd DM");
thirdSpecularMap = Texture::Add(L"Textures/Color/White.png", L"3rd SM");
buffer = new MaterialBuffer();
}
Material::Material(wstring shaderFile)
{
SetShader(shaderFile);
diffuseMap = Texture::Add(L"Textures/Color/White.png", L"DM");
specularMap = Texture::Add(L"Textures/Color/White.png", L"SM");
normalMap = Texture::Add(L"Textures/Color/White.png", L"NM");
secondDiffuseMap = Texture::Add(L"Textures/Color/White.png", L"2nd DM");
secondSpecularMap = Texture::Add(L"Textures/Color/White.png", L"2nd SM");
thirdDiffuseMap = Texture::Add(L"Textures/Color/White.png", L"3rd DM");
thirdSpecularMap = Texture::Add(L"Textures/Color/White.png", L"3rd SM");
buffer = new MaterialBuffer();
}
Material::~Material()
{
delete buffer;
}
void Material::Set()
{
diffuseMap->PSSet(0);
specularMap->PSSet(1);
normalMap->PSSet(2);
secondDiffuseMap->PSSet(11);
secondSpecularMap->PSSet(12);
thirdDiffuseMap->PSSet(21);
thirdSpecularMap->PSSet(22);
buffer->SetPS(1);
vertexShader->Set();
pixelShader->Set();
}
void Material::RenderUI()
{
string title = name + "_Material";
if (ImGui::TreeNode(title.c_str()))
{
ImGui::ColorEdit3("Diffuse", (float*)&buffer->Get().diffuse);
ImGui::ColorEdit3("Specular", (float*)&buffer->Get().specular);
ImGui::ColorEdit3("Ambient", (float*)&buffer->Get().ambient);
ImGui::ColorEdit4("Emissive", (float*)&buffer->Get().emissive);
ImGui::SliderFloat("Shininess", &buffer->Get().shininess, 1, 50);
SelectMap(1, "DM", DIFFUSE);
ImGui::SameLine();
UnselectMap(1, DIFFUSE);
SelectMap(1, "SM", SPECULAR);
ImGui::SameLine();
UnselectMap(1, SPECULAR);
SelectMap(1, "NM", NORMAL);
ImGui::SameLine();
UnselectMap(1, NORMAL);
SelectMap(2, "2nd DM", DIFFUSE);
ImGui::SameLine();
UnselectMap(2, DIFFUSE);
SelectMap(2, "2nd SM", SPECULAR);
ImGui::SameLine();
UnselectMap(2, SPECULAR);
SelectMap(3, "3rd DM", DIFFUSE);
ImGui::SameLine();
UnselectMap(3, DIFFUSE);
SelectMap(3, "3rd SM", SPECULAR);
ImGui::SameLine();
UnselectMap(3, SPECULAR);
ImGui::TreePop();
}
}
void Material::SetShader(wstring shaderFile)
{
vertexShader = Shader::AddVS(shaderFile);
pixelShader = Shader::AddPS(shaderFile);
}
void Material::SetDiffuseMap(int num, wstring textureFile)
{
if (num == 1)
{
if (textureFile.length() > 0)
diffuseMap = Texture::Add(textureFile);
else
diffuseMap = Texture::Add(L"Textures/Color/White.png", L"DM");
}
else if (num == 2)
{
if (textureFile.length() > 0)
secondDiffuseMap = Texture::Add(textureFile);
else
secondDiffuseMap = Texture::Add(L"Textures/Color/White.png", L"2nd DM");
}
else if(num == 3)
{
if (textureFile.length() > 0)
thirdDiffuseMap = Texture::Add(textureFile);
else
thirdDiffuseMap = Texture::Add(L"Textures/Color/White.png", L"3rd DM");
}
}
void Material::SetSpecularMap(int num, wstring textureFile)
{
if (num == 1)
{
if (textureFile.length() > 0)
specularMap = Texture::Add(textureFile);
else
specularMap = Texture::Add(L"Textures/Color/White.png", L"SM");
}
else if (num == 2)
{
if (textureFile.length() > 0)
secondSpecularMap = Texture::Add(textureFile);
else
secondSpecularMap = Texture::Add(L"Textures/Color/White.png", L"2nd SM");
}
else if (num == 3)
{
if (textureFile.length() > 0)
thirdSpecularMap = Texture::Add(textureFile);
else
thirdSpecularMap = Texture::Add(L"Textures/Color/White.png", L"3rd SM");
}
}
void Material::SetNormalMap(wstring textureFile)
{
if (textureFile.length() > 0)
{
normalMap = Texture::Add(textureFile);
buffer->Get().hasNormalMap = 1;
}
else
{
normalMap = Texture::Add(L"Textures/Color/White.png", L"NM");
buffer->Get().hasNormalMap = 0;
}
}
Texture* Material::GetDiffuseMap(int num)
{
if (num == 1)
return diffuseMap;
else if (num == 2)
return secondDiffuseMap;
else
return thirdDiffuseMap;
}
Texture* Material::GetSpecularMap(int num)
{
if (num == 1)
return specularMap;
else if (num == 2)
return secondSpecularMap;
else
return thirdSpecularMap;
}
void Material::SelectMap(int num, string name, MapType type)
{
ImGui::SetWindowFontScale(2.0f);
ImGui::TextColored(ImVec4(1, 0.5f, 0.8f, 1), name.c_str());
ImGui::SetWindowFontScale(1.0f);
ImGui::SameLine();
ImTextureID textureID = nullptr;
switch (type)
{
case Material::DIFFUSE:
if( num == 1)
textureID = diffuseMap->GetSRV();
else if (num == 2)
textureID = secondDiffuseMap->GetSRV();
else if (num == 3)
textureID = thirdDiffuseMap->GetSRV();
break;
case Material::SPECULAR:
if(num == 1)
textureID = specularMap->GetSRV();
else if (num == 2)
textureID = secondSpecularMap->GetSRV();
else if (num == 3)
textureID = thirdSpecularMap->GetSRV();
break;
case Material::NORMAL:
if (num == 1)
textureID = normalMap->GetSRV();
break;
default:
break;
}
if (ImGui::ImageButton(textureID, ImVec2(50, 50)))
{
DIALOG->OpenDialog(this->name + name, name, ".png,.jpg,.tga", ".");
}
if (DIALOG->Display(this->name + name))
{
if (DIALOG->IsOk())
{
string file = DIALOG->GetFilePathName();
char path[128];
GetCurrentDirectoryA(128, path);
file = file.substr(strlen(path) + 1, file.length());
switch (type)
{
case Material::DIFFUSE:
SetDiffuseMap(num, ToWString(file));
break;
case Material::SPECULAR:
SetSpecularMap(num, ToWString(file));
break;
case Material::NORMAL:
if(num == 1)
SetNormalMap(ToWString(file));
break;
default:
break;
}
}
DIALOG->Close();
}
}
void Material::UnselectMap(int num, MapType type)
{
ImTextureID textureID = nullptr;
switch (type)
{
case Material::DIFFUSE:
textureID = Texture::Add(L"Textures/UI/Cancel.png", L"DMCancel" + to_wstring(num))->GetSRV();
break;
case Material::SPECULAR:
textureID = Texture::Add(L"Textures/UI/Cancel.png", L"SMCancel" + to_wstring(num))->GetSRV();
break;
case Material::NORMAL:
textureID = Texture::Add(L"Textures/UI/Cancel.png", L"NMCancel" + to_wstring(num))->GetSRV();
break;
default:
break;
}
if (ImGui::ImageButton(textureID, ImVec2(20, 20)))
{
switch (type)
{
case Material::DIFFUSE:
if(textureID == Texture::Add(L"Textures/UI/Cancel.png", L"DMCancel1")->GetSRV())
SetDiffuseMap(1, L"");
else if (textureID == Texture::Add(L"Textures/UI/Cancel.png", L"DMCancel2")->GetSRV())
SetDiffuseMap(2, L"");
else if (textureID == Texture::Add(L"Textures/UI/Cancel.png", L"DMCancel3")->GetSRV())
SetDiffuseMap(3, L"");
break;
case Material::SPECULAR:
if (textureID == Texture::Add(L"Textures/UI/Cancel.png", L"SMCancel1")->GetSRV())
SetSpecularMap(1, L"");
else if (textureID == Texture::Add(L"Textures/UI/Cancel.png", L"SMCancel2")->GetSRV())
SetSpecularMap(2, L"");
else if (textureID == Texture::Add(L"Textures/UI/Cancel.png", L"SMCancel3")->GetSRV())
SetSpecularMap(3, L"");
break;
case Material::NORMAL:
SetNormalMap(L"");
break;
default:
break;
}
}
}
맵 Save
더보기
void TerrainEditor::SaveMap()
{
if (ImGui::Button("SaveMap"))
DIALOG->OpenDialog("SaveMap", "SaveMap", ".map", ".");
if (DIALOG->Display("SaveMap"))
{
if (DIALOG->IsOk())
{
string file = DIALOG->GetFilePathName();
BinaryWriter* writer = new BinaryWriter(file);
writer->WString(GetMaterial()->GetDiffuseMap(1)->GetFile());
writer->WString(GetMaterial()->GetDiffuseMap(2)->GetFile());
writer->WString(GetMaterial()->GetDiffuseMap(3)->GetFile());
writer->WString(GetMaterial()->GetSpecularMap(1)->GetFile());
writer->WString(GetMaterial()->GetSpecularMap(2)->GetFile());
writer->WString(GetMaterial()->GetSpecularMap(3)->GetFile());
writer->WString(GetMaterial()->GetNormalMap()->GetFile());
//Height
UINT size = width * height * 4;
uint8_t* pixels = new uint8_t[size];
vector<VertexType> vertices = mesh->GetVertices();
for (UINT i = 0; i < size / 4; i++)
{
float y = vertices[i].pos.y;
uint8_t height = (y - MIN_HEIGHT) / (MAX_HEIGHT - MIN_HEIGHT) * 255;
pixels[i * 4 + 0] = height;
pixels[i * 4 + 1] = height;
pixels[i * 4 + 2] = height;
pixels[i * 4 + 3] = 255;
}
Image image;
image.width = width;
image.height = height;
image.format = DXGI_FORMAT_R8G8B8A8_UNORM;
image.rowPitch = width * 4;
image.slicePitch = size;
image.pixels = pixels;
int textPos = file.find("_");
file = file.substr(0, textPos) + "_height.png";
SaveToWICFile(image, WIC_FLAGS_FORCE_RGB,
GetWICCodec(WIC_CODEC_PNG), ToWString(file).c_str());
writer->WString(ToWString(file));
delete[] pixels;
//Alpha
file = file.substr(projectPath.size() + 1, file.size());
size = width * height * 4;
pixels = new uint8_t[size];
vertices = mesh->GetVertices();
for (UINT i = 0; i < size / 4; i++)
{
pixels[i * 4 + 0] = vertices[i].alpha[0] * 255;
pixels[i * 4 + 1] = vertices[i].alpha[1] * 255;
pixels[i * 4 + 2] = vertices[i].alpha[2] * 255;
pixels[i * 4 + 3] = 255;
}
image.width = width;
image.height = height;
image.format = DXGI_FORMAT_R8G8B8A8_UNORM;
image.rowPitch = width * 4;
image.slicePitch = size;
image.pixels = pixels;
textPos = file.find(".");
file = file.substr(0, textPos) + "_alpha.png";
SaveToWICFile(image, WIC_FLAGS_FORCE_RGB,
GetWICCodec(WIC_CODEC_PNG), ToWString(file).c_str());
writer->WString(ToWString(file));
delete[] pixels;
delete writer;
}
DIALOG->Close();
}
}
맵 Load
더보기
void TerrainEditor::LoadMap()
{
if (ImGui::Button("LoadMap"))
DIALOG->OpenDialog("LoadMap", "LoadMap", ".map", ".");
if (DIALOG->Display("LoadMap"))
{
if (DIALOG->IsOk())
{
string file = DIALOG->GetFilePathName();
BinaryReader* reader = new BinaryReader(file);
GetMaterial()->SetDiffuseMap(1, reader->WString());
GetMaterial()->SetDiffuseMap(2, reader->WString());
GetMaterial()->SetDiffuseMap(3, reader->WString());
GetMaterial()->SetSpecularMap(1, reader->WString());
GetMaterial()->SetSpecularMap(2, reader->WString());
GetMaterial()->SetSpecularMap(3, reader->WString());
GetMaterial()->SetNormalMap(reader->WString());
//Height
heightMap = Texture::Add(reader->WString());
Resize();
//Alpha
Texture* alphaMap = Texture::Add(reader->WString());
vector<Float4> pixels;
alphaMap->ReadPixels(pixels);
vector<VertexType>& vertices = mesh->GetVertices();
for (UINT i = 0; i < vertices.size(); i++)
{
vertices[i].alpha[0] = pixels[i].z;
vertices[i].alpha[1] = pixels[i].y;
vertices[i].alpha[2] = pixels[i].x;
vertices[i].alpha[3] = pixels[i].w;
}
mesh->UpdateVertex();
delete reader;
}
DIALOG->Close();
}
}
'DirectX 3D > DirectX 3D 정리 및 영상' 카테고리의 다른 글
230227 Terrain Pick좌표 y값 조절하기 (0) | 2023.02.27 |
---|---|
230226 자동차 만들기, Height에 따른 자동차 y값 보간하기, 실린더 출력하기 (0) | 2023.02.26 |
230222 Grid, Terrain 에디터 만들기 (0) | 2023.02.22 |
230220 DX11 3D 큐브 출력하기 (0) | 2023.02.20 |