Skip to content

Commit

Permalink
material transparency map, paint tool improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
turanszkij committed Feb 14, 2024
1 parent a73f92b commit fa70f6d
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 4 deletions.
6 changes: 6 additions & 0 deletions Editor/MaterialWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,9 @@ void MaterialWindow::Create(EditorComponent* _editor)
case MaterialComponent::ANISOTROPYMAP:
textureSlotComboBox.AddItem("Anisotropy map");
break;
case MaterialComponent::TRANSPARENCYMAP:
textureSlotComboBox.AddItem("Transparency map");
break;
default:
break;
}
Expand Down Expand Up @@ -648,6 +651,9 @@ void MaterialWindow::Create(EditorComponent* _editor)
case MaterialComponent::ANISOTROPYMAP:
tooltiptext = "RG: The anisotropy texture. Red and green channels represent the anisotropy direction in [-1, 1] tangent, bitangent space.\nThe vector is rotated by anisotropyRotation, and multiplied by anisotropyStrength, to obtain the final anisotropy direction and strength.";
break;
case MaterialComponent::TRANSPARENCYMAP:
tooltiptext = "R: transparency.";
break;
default:
break;
}
Expand Down
61 changes: 60 additions & 1 deletion Editor/PaintToolWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ void PaintToolWindow::Create(EditorComponent* _editor)
textureSlotComboBox.AddItem("ClearcoatMap (R)", MaterialComponent::CLEARCOATMAP);
textureSlotComboBox.AddItem("ClearcoatRoughMap (R)", MaterialComponent::CLEARCOATROUGHNESSMAP);
textureSlotComboBox.AddItem("ClearcoatNormMap (R)", MaterialComponent::CLEARCOATNORMALMAP);
textureSlotComboBox.AddItem("SpecularMap (RGBA)", MaterialComponent::SPECULARMAP);
textureSlotComboBox.AddItem("AnisotropyMap (RG)", MaterialComponent::ANISOTROPYMAP);
textureSlotComboBox.AddItem("TransparencyMap (R)", MaterialComponent::TRANSPARENCYMAP);
textureSlotComboBox.SetSelected(0);
AddWidget(&textureSlotComboBox);

Expand Down Expand Up @@ -248,6 +251,13 @@ void PaintToolWindow::Create(EditorComponent* _editor)
if (wi::helper::saveTextureToMemoryFile(editTexture.texture, "PNG", texturefiledata))
{
material->textures[sel].resource.SetFileData(texturefiledata);
// Register into resource manager:
material->textures[sel].resource = wi::resourcemanager::Load(
material->textures[sel].name,
wi::resourcemanager::Flags::IMPORT_RETAIN_FILEDATA,
texturefiledata.data(),
texturefiledata.size()
);
}
else
{
Expand Down Expand Up @@ -476,12 +486,61 @@ void PaintToolWindow::Update(float dt)
if (mesh == nullptr || (mesh->vertex_uvset_0.empty() && mesh->vertex_uvset_1.empty()))
break;

MaterialComponent* material = selected.subsetIndex >= 0 && selected.subsetIndex < (int)mesh->subsets.size() ? scene.materials.GetComponent(mesh->subsets[selected.subsetIndex].materialID) : nullptr;
Entity materialID = mesh->subsets[selected.subsetIndex].materialID;
MaterialComponent* material = selected.subsetIndex >= 0 && selected.subsetIndex < (int)mesh->subsets.size() ? scene.materials.GetComponent(materialID) : nullptr;
if (material == nullptr)
break;

int uvset = 0;
TextureSlot editTexture = GetEditTextureSlot(*material, &uvset);

// Missing texture will be created a blank one:
if (!editTexture.texture.IsValid())
{
std::string texturename = "painttool/";
const NameComponent* materialname = scene.names.GetComponent(materialID);
if (materialname != nullptr)
{
texturename += materialname->name;
texturename += "_";
}
texturename += std::to_string(wi::random::GetRandom(std::numeric_limits<int>::max()));
texturename += ".PNG";
uint64_t sel = textureSlotComboBox.GetItemUserData(textureSlotComboBox.GetSelected());
material->textures[sel].name = texturename;

TextureDesc desc;
desc.width = 1024;
desc.height = 1024;
desc.format = Format::R8G8B8A8_UNORM;
desc.bind_flags = BindFlag::SHADER_RESOURCE | BindFlag::UNORDERED_ACCESS;
wi::vector<wi::Color> whitedata(desc.width * desc.height);
std::fill(whitedata.begin(), whitedata.end(), wi::Color::White());
SubresourceData initdata;
initdata.data_ptr = whitedata.data();
initdata.row_pitch = desc.width * GetFormatStride(desc.format);
Texture texture;
GraphicsDevice* device = GetDevice();
device->CreateTexture(&desc, &initdata, &texture);
device->SetName(&texture, texturename.c_str());
// This part must be AFTER mip level subresource creation:
int srgb_subresource = -1;
{
Format srgb_format = GetFormatSRGB(desc.format);
srgb_subresource = device->CreateSubresource(
&texture,
SubresourceType::SRV,
0, -1,
0, -1,
&srgb_format
);
}
material->textures[sel].resource.SetTexture(texture, srgb_subresource);
editTexture = GetEditTextureSlot(*material, &uvset);

wi::backlog::post("Paint Tool created default texture: " + texturename);
}

if (!editTexture.texture.IsValid())
break;
const TextureDesc& desc = editTexture.texture.GetDesc();
Expand Down
1 change: 1 addition & 0 deletions WickedEngine/shaders/ShaderInterop_Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ enum TEXTURESLOT
CLEARCOATNORMALMAP,
SPECULARMAP,
ANISOTROPYMAP,
TRANSPARENCYMAP,

TEXTURESLOT_COUNT
};
Expand Down
6 changes: 6 additions & 0 deletions WickedEngine/shaders/objectHF.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,12 @@ float4 main(PixelInput input, in bool is_frontface : SV_IsFrontFace) : SV_Target
{
surface.baseColor *= GetMaterial().textures[BASECOLORMAP].Sample(sampler_objectshader, uvsets);
}

[branch]
if (GetMaterial().textures[TRANSPARENCYMAP].IsValid())
{
surface.baseColor.a *= GetMaterial().textures[TRANSPARENCYMAP].Sample(sampler_objectshader, uvsets).r;
}
#endif // OBJECTSHADER_USE_UVSETS


Expand Down
12 changes: 11 additions & 1 deletion WickedEngine/shaders/shadowPS_alphatest.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@

void main(PixelInput input)
{
float alpha = 1;

[branch]
if (GetMaterial().textures[BASECOLORMAP].IsValid())
{
clip(GetMaterial().textures[BASECOLORMAP].Sample(sampler_point_wrap, input.GetUVSets()).a - GetMaterial().alphaTest);
alpha = GetMaterial().textures[BASECOLORMAP].Sample(sampler_point_wrap, input.GetUVSets()).a;
}

[branch]
if (GetMaterial().textures[TRANSPARENCYMAP].IsValid())
{
alpha *= GetMaterial().textures[TRANSPARENCYMAP].Sample(sampler_point_wrap, input.GetUVSets()).r;
}

clip(alpha - GetMaterial().alphaTest);
}
7 changes: 7 additions & 0 deletions WickedEngine/shaders/shadowPS_transparent.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ float4 main(PixelInput input) : SV_TARGET
{
color = 1;
}

[branch]
if (GetMaterial().textures[TRANSPARENCYMAP].IsValid())
{
color.a *= GetMaterial().textures[TRANSPARENCYMAP].Sample(sampler_objectshader, uvsets).r;
}

color *= input.color;

clip(color.a - GetMaterial().alphaTest);
Expand Down
14 changes: 14 additions & 0 deletions WickedEngine/shaders/surfaceHF.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,20 @@ struct Surface
baseColor.a *= baseColorMap.a;
}
}

[branch]
if (material.textures[TRANSPARENCYMAP].IsValid())
{
#ifdef SURFACE_LOAD_QUAD_DERIVATIVES
baseColor.a *= material.textures[TRANSPARENCYMAP].SampleGrad(sam, uvsets, uvsets_dx, uvsets_dy).r;
#else
float lod = 0;
#ifdef SURFACE_LOAD_MIPCONE
lod = compute_texture_lod(material.textures[TRANSPARENCYMAP].GetTexture(), material.textures[TRANSPARENCYMAP].GetUVSet() == 0 ? lod_constant0 : lod_constant1, ray_direction, surf_normal, cone_width);
#endif // SURFACE_LOAD_MIPCONE
baseColor.a *= material.textures[TRANSPARENCYMAP].SampleLevel(sam, uvsets, lod).r;
#endif // SURFACE_LOAD_QUAD_DERIVATIVES
}

[branch]
if (geometry.vb_col >= 0 && material.IsUsingVertexColors())
Expand Down
2 changes: 1 addition & 1 deletion WickedEngine/wiScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace wi::scene
wi::ecs::ComponentManager<LayerComponent>& layers = componentLibrary.Register<LayerComponent>("wi::scene::Scene::layers");
wi::ecs::ComponentManager<TransformComponent>& transforms = componentLibrary.Register<TransformComponent>("wi::scene::Scene::transforms");
wi::ecs::ComponentManager<HierarchyComponent>& hierarchy = componentLibrary.Register<HierarchyComponent>("wi::scene::Scene::hierarchy");
wi::ecs::ComponentManager<MaterialComponent>& materials = componentLibrary.Register<MaterialComponent>("wi::scene::Scene::materials", 2); // version = 2
wi::ecs::ComponentManager<MaterialComponent>& materials = componentLibrary.Register<MaterialComponent>("wi::scene::Scene::materials", 3); // version = 3
wi::ecs::ComponentManager<MeshComponent>& meshes = componentLibrary.Register<MeshComponent>("wi::scene::Scene::meshes", 2); // version = 2
wi::ecs::ComponentManager<ImpostorComponent>& impostors = componentLibrary.Register<ImpostorComponent>("wi::scene::Scene::impostors");
wi::ecs::ComponentManager<ObjectComponent>& objects = componentLibrary.Register<ObjectComponent>("wi::scene::Scene::objects", 3); // version = 3
Expand Down
1 change: 1 addition & 0 deletions WickedEngine/wiScene_BindLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ TextureSlot = {
CLEARCOATNORMALMAP = 11,
SPECULARMAP = 12,
ANISOTROPYMAP = 13,
TRANSPARENCYMAP = 14,
}
SHADERTYPE_PBR = 0
Expand Down
1 change: 1 addition & 0 deletions WickedEngine/wiScene_Components.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ namespace wi::scene
CLEARCOATNORMALMAP,
SPECULARMAP,
ANISOTROPYMAP,
TRANSPARENCYMAP,

TEXTURESLOT_COUNT
};
Expand Down
12 changes: 12 additions & 0 deletions WickedEngine/wiScene_Serializers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ namespace wi::scene
anisotropy_strength = parallaxOcclusionMapping; // old version fix
}

if (seri.GetVersion() >= 3)
{
archive >> textures[TRANSPARENCYMAP].name;
archive >> textures[TRANSPARENCYMAP].uvset;
}

for (auto& x : textures)
{
if (!x.name.empty())
Expand Down Expand Up @@ -369,6 +375,12 @@ namespace wi::scene
archive << wi::helper::GetPathRelative(dir, textures[ANISOTROPYMAP].name);
archive << textures[ANISOTROPYMAP].uvset;
}

if (seri.GetVersion() >= 2)
{
archive << wi::helper::GetPathRelative(dir, textures[TRANSPARENCYMAP].name);
archive << textures[TRANSPARENCYMAP].uvset;
}
}
}
void MeshComponent::Serialize(wi::Archive& archive, EntitySerializer& seri)
Expand Down
2 changes: 1 addition & 1 deletion WickedEngine/wiVersion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace wi::version
// minor features, major updates, breaking compatibility changes
const int minor = 71;
// minor bug fixes, alterations, refactors, updates
const int revision = 376;
const int revision = 377;

const std::string version_string = std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(revision);

Expand Down

0 comments on commit fa70f6d

Please sign in to comment.