Files
WickedEngine/Editor/PaintToolWindow.cpp
T
2022-08-10 10:46:18 +02:00

1589 lines
51 KiB
C++

#include "stdafx.h"
#include "Editor.h"
#include "PaintToolWindow.h"
#include "shaders/ShaderInterop_Renderer.h"
#include <cmath>
using namespace wi::ecs;
using namespace wi::scene;
using namespace wi::graphics;
void PaintToolWindow::Create(EditorComponent* _editor)
{
editor = _editor;
wi::gui::Window::Create("Paint Tool", wi::gui::Window::WindowControls::COLLAPSE);
SetSize(XMFLOAT2(360, 600));
float x = 105;
float y = 0;
float hei = 20;
float step = hei + 4;
float wid = 160;
colorPicker.Create("Color", wi::gui::Window::WindowControls::NONE);
float mod_wid = colorPicker.GetScale().x;
modeComboBox.Create("Mode: ");
modeComboBox.SetTooltip("Choose paint tool mode");
modeComboBox.SetPos(XMFLOAT2(x, y));
modeComboBox.SetSize(XMFLOAT2(wid, hei));
modeComboBox.AddItem(ICON_DISABLED " Disabled");
modeComboBox.AddItem(ICON_MATERIAL " Texture");
modeComboBox.AddItem(ICON_MESH " Vertexcolor");
modeComboBox.AddItem(ICON_MESH " Sculpting - Add");
modeComboBox.AddItem(ICON_MESH " Sculpting - Subtract");
modeComboBox.AddItem(ICON_SOFTBODY " Softbody - Pinning");
modeComboBox.AddItem(ICON_SOFTBODY " Softbody - Physics");
modeComboBox.AddItem(ICON_HAIR " Hairparticle - Add Triangle");
modeComboBox.AddItem(ICON_HAIR " Hairparticle - Remove Triangle");
modeComboBox.AddItem(ICON_HAIR " Hairparticle - Length (Alpha)");
modeComboBox.AddItem(ICON_MESH " Wind weight (Alpha)");
modeComboBox.SetSelected(0);
modeComboBox.OnSelect([&](wi::gui::EventArgs args) {
switch (args.iValue)
{
case MODE_DISABLED:
infoLabel.SetText("Paint Tool is disabled.");
break;
case MODE_TEXTURE:
infoLabel.SetText("In texture paint mode, you can paint on textures. Brush will be applied in texture space.\nREMEMBER to save texture when finished to save texture file!\nREMEMBER to save scene to retain new texture bindings on materials!");
break;
case MODE_VERTEXCOLOR:
infoLabel.SetText("In vertex color mode, you can paint colors on selected geometry (per vertex). \"Use vertex colors\" will be automatically enabled for the selected material, or all materials if the whole object is selected. If there is no vertexcolors vertex buffer, one will be created with white as default for every vertex.");
break;
case MODE_SCULPTING_ADD:
infoLabel.SetText("In sculpt - ADD mode, you can modify vertex positions by ADD operation along normal vector (average normal of vertices touched by brush).");
break;
case MODE_SCULPTING_SUBTRACT:
infoLabel.SetText("In sculpt - SUBTRACT mode, you can modify vertex positions by SUBTRACT operation along normal vector (average normal of vertices touched by brush).");
break;
case MODE_SOFTBODY_PINNING:
infoLabel.SetText("In soft body pinning mode, the selected object's soft body vertices can be pinned down (so they will be fixed and drive physics)");
break;
case MODE_SOFTBODY_PHYSICS:
infoLabel.SetText("In soft body physics mode, the selected object's soft body vertices can be unpinned (so they will be simulated by physics)");
break;
case MODE_HAIRPARTICLE_ADD_TRIANGLE:
infoLabel.SetText("In hair particle add triangle mode, you can add triangles to the hair base mesh.\nThis will modify random distribution of hair!");
break;
case MODE_HAIRPARTICLE_REMOVE_TRIANGLE:
infoLabel.SetText("In hair particle remove triangle mode, you can remove triangles from the hair base mesh.\nThis will modify random distribution of hair!");
break;
case MODE_HAIRPARTICLE_LENGTH:
infoLabel.SetText("In hair particle length mode, you can adjust length of hair particles with the colorpicker Alpha channel (A). The Alpha channel is 0-255, but the length will be normalized to 0-1 range.\nThis will NOT modify random distribution of hair!");
break;
case MODE_WIND:
infoLabel.SetText("Paint the wind affection amount onto the vertices. Use the Alpha channel to control the amount.");
break;
}
});
AddWidget(&modeComboBox);
y += step + 5;
infoLabel.Create("Paint Tool is disabled.");
infoLabel.SetSize(XMFLOAT2(mod_wid - 10, 100));
infoLabel.SetPos(XMFLOAT2(5, y));
infoLabel.SetColor(wi::Color::Transparent());
AddWidget(&infoLabel);
y += infoLabel.GetScale().y - step + 5;
radiusSlider.Create(1.0f, 500.0f, 50, 10000, "Brush Radius: ");
radiusSlider.SetTooltip("Set the brush radius in pixel units");
radiusSlider.SetSize(XMFLOAT2(wid, hei));
radiusSlider.SetPos(XMFLOAT2(x, y += step));
AddWidget(&radiusSlider);
amountSlider.Create(0, 1, 1, 10000, "Brush Amount: ");
amountSlider.SetTooltip("Set the brush amount. 0 = minimum affection, 1 = maximum affection");
amountSlider.SetSize(XMFLOAT2(wid, hei));
amountSlider.SetPos(XMFLOAT2(x, y += step));
AddWidget(&amountSlider);
falloffSlider.Create(0, 16, 0, 10000, "Brush Falloff: ");
falloffSlider.SetTooltip("Set the brush power. 0 = no falloff, 1 = linear falloff, more = falloff power");
falloffSlider.SetSize(XMFLOAT2(wid, hei));
falloffSlider.SetPos(XMFLOAT2(x, y += step));
AddWidget(&falloffSlider);
spacingSlider.Create(0, 500, 1, 500, "Brush Spacing: ");
spacingSlider.SetTooltip("Brush spacing means how much brush movement (in pixels) starts a new stroke. 0 = new stroke every frame, 100 = every 100 pixel movement since last stroke will start a new stroke.");
spacingSlider.SetSize(XMFLOAT2(wid, hei));
spacingSlider.SetPos(XMFLOAT2(x, y += step));
AddWidget(&spacingSlider);
rotationSlider.Create(0, 1, 0, 10000, "Brush Rotation: ");
rotationSlider.SetTooltip("Brush rotation randomness. This will affect the splat mode brush texture.");
rotationSlider.SetSize(XMFLOAT2(wid, hei));
rotationSlider.SetPos(XMFLOAT2(x, y += step));
AddWidget(&rotationSlider);
backfaceCheckBox.Create("Backfaces: ");
backfaceCheckBox.SetTooltip("Set whether to paint on backfaces of geometry or not");
backfaceCheckBox.SetSize(XMFLOAT2(hei, hei));
backfaceCheckBox.SetPos(XMFLOAT2(x - 20, y += step));
AddWidget(&backfaceCheckBox);
wireCheckBox.Create("Wireframe: ");
wireCheckBox.SetTooltip("Set whether to draw wireframe on top of geometry or not");
wireCheckBox.SetSize(XMFLOAT2(hei, hei));
wireCheckBox.SetPos(XMFLOAT2(x - 20 + 100, y));
wireCheckBox.SetCheck(true);
AddWidget(&wireCheckBox);
pressureCheckBox.Create("Pressure: ");
pressureCheckBox.SetTooltip("Set whether to use pressure sensitivity (for example pen tablet)");
pressureCheckBox.SetSize(XMFLOAT2(hei, hei));
pressureCheckBox.SetPos(XMFLOAT2(x - 20 + 200, y));
pressureCheckBox.SetCheck(false);
AddWidget(&pressureCheckBox);
axisCombo.Create("Axis Lock: ");
axisCombo.SetTooltip("You can lock modification to an axis here.");
axisCombo.SetPos(XMFLOAT2(x, y));
axisCombo.SetSize(XMFLOAT2(wid, hei));
axisCombo.AddItem(ICON_DISABLED, (uint64_t)AxisLock::Disabled);
axisCombo.AddItem("X " ICON_LEFT_RIGHT, (uint64_t)AxisLock::X);
axisCombo.AddItem("Y " ICON_UP_DOWN, (uint64_t)AxisLock::Y);
axisCombo.AddItem("Z " ICON_UPRIGHT_DOWNLEFT, (uint64_t)AxisLock::Z);
AddWidget(&axisCombo);
colorPicker.SetPos(XMFLOAT2(5, y += step));
AddWidget(&colorPicker);
y += colorPicker.GetScale().y;
textureSlotComboBox.Create("Texture Slot: ");
textureSlotComboBox.SetTooltip("Choose texture slot of the selected material to paint (texture paint mode only)");
textureSlotComboBox.SetPos(XMFLOAT2(x, y += step));
textureSlotComboBox.SetSize(XMFLOAT2(wid, hei));
textureSlotComboBox.AddItem("BaseColor (RGBA)", MaterialComponent::BASECOLORMAP);
textureSlotComboBox.AddItem("Normal (RGB)", MaterialComponent::NORMALMAP);
textureSlotComboBox.AddItem("SurfaceMap (RGBA)", MaterialComponent::SURFACEMAP);
textureSlotComboBox.AddItem("DisplacementMap (R)", MaterialComponent::DISPLACEMENTMAP);
textureSlotComboBox.AddItem("EmissiveMap (RGBA)", MaterialComponent::EMISSIVEMAP);
textureSlotComboBox.AddItem("OcclusionMap (R)", MaterialComponent::OCCLUSIONMAP);
textureSlotComboBox.AddItem("TransmissionMap (R)", MaterialComponent::TRANSMISSIONMAP);
textureSlotComboBox.AddItem("SheenColorMap (R)", MaterialComponent::SHEENCOLORMAP);
textureSlotComboBox.AddItem("SheenRoughMap (R)", MaterialComponent::SHEENROUGHNESSMAP);
textureSlotComboBox.AddItem("ClearcoatMap (R)", MaterialComponent::CLEARCOATMAP);
textureSlotComboBox.AddItem("ClearcoatRoughMap (R)", MaterialComponent::CLEARCOATROUGHNESSMAP);
textureSlotComboBox.AddItem("ClearcoatNormMap (R)", MaterialComponent::CLEARCOATNORMALMAP);
textureSlotComboBox.SetSelected(0);
AddWidget(&textureSlotComboBox);
brushShapeComboBox.Create("Brush Shape: ");
brushShapeComboBox.SetTooltip("Choose shape for brush masking effect");
brushShapeComboBox.SetPos(XMFLOAT2(x, y += step));
brushShapeComboBox.SetSize(XMFLOAT2(wid, hei));
brushShapeComboBox.AddItem(ICON_CIRCLE);
brushShapeComboBox.AddItem(ICON_SQUARE);
brushShapeComboBox.SetSelected(0);
AddWidget(&brushShapeComboBox);
saveTextureButton.Create("Save Texture");
saveTextureButton.SetTooltip("Save edited texture.");
saveTextureButton.SetSize(XMFLOAT2(wid, hei));
saveTextureButton.SetPos(XMFLOAT2(x, y += step));
saveTextureButton.OnClick([this] (wi::gui::EventArgs args) {
Scene& scene = editor->GetCurrentScene();
ObjectComponent* object = scene.objects.GetComponent(entity);
if (object == nullptr || object->meshID == INVALID_ENTITY)
return;
MeshComponent* mesh = scene.meshes.GetComponent(object->meshID);
if (mesh == nullptr || (mesh->vertex_uvset_0.empty() && mesh->vertex_uvset_1.empty()))
return;
MaterialComponent* material = subset >= 0 && subset < (int)mesh->subsets.size() ? scene.materials.GetComponent(mesh->subsets[subset].materialID) : nullptr;
if (material == nullptr)
return;
Texture editTexture = GetEditTextureSlot(*material);
uint64_t sel = textureSlotComboBox.GetItemUserData(textureSlotComboBox.GetSelected());
wi::vector<uint8_t> texturefiledata;
if (wi::helper::saveTextureToMemoryFile(editTexture, "PNG", texturefiledata))
{
material->textures[sel].resource.SetFileData(texturefiledata);
}
else
{
wi::helper::messageBox("Saving texture failed! :(");
}
});
AddWidget(&saveTextureButton);
brushTextureButton.Create("");
brushTextureButton.SetDescription("Brush tex: ");
brushTextureButton.SetTooltip("Open an image to use as brush texture (splatting mode).\nSplat mode means that the texture will be relative to the brush position");
brushTextureButton.SetSize(XMFLOAT2(hei*2, hei*2));
brushTextureButton.SetPos(XMFLOAT2(x, y += step));
brushTextureButton.sprites[wi::gui::IDLE].params.color = wi::Color::White();
brushTextureButton.sprites[wi::gui::FOCUS].params.color = wi::Color::Gray();
brushTextureButton.sprites[wi::gui::ACTIVE].params.color = wi::Color::White();
brushTextureButton.sprites[wi::gui::DEACTIVATING].params.color = wi::Color::Gray();
brushTextureButton.OnClick([this](wi::gui::EventArgs args) {
if (brushTex.IsValid())
{
brushTex = {};
brushTextureButton.SetImage({});
}
else
{
wi::helper::FileDialogParams params;
params.type = wi::helper::FileDialogParams::OPEN;
params.description = "Texture";
params.extensions = wi::resourcemanager::GetSupportedImageExtensions();
wi::helper::FileDialog(params, [this](std::string fileName) {
wi::eventhandler::Subscribe_Once(wi::eventhandler::EVENT_THREAD_SAFE_POINT, [=](uint64_t userdata) {
brushTex = wi::resourcemanager::Load(fileName);
brushTextureButton.SetImage(brushTex);
});
});
}
});
AddWidget(&brushTextureButton);
revealTextureButton.Create("");
revealTextureButton.SetDescription("Reveal tex: ");
revealTextureButton.SetTooltip("Open an image to use as reveal mode texture.\nReveal mode means that the texture will use the UV of the mesh. It will be multiplied by brush tex.");
revealTextureButton.SetSize(XMFLOAT2(hei * 2, hei * 2));
revealTextureButton.SetPos(XMFLOAT2(x + 150, y));
revealTextureButton.sprites[wi::gui::IDLE].params.color = wi::Color::White();
revealTextureButton.sprites[wi::gui::FOCUS].params.color = wi::Color::Gray();
revealTextureButton.sprites[wi::gui::ACTIVE].params.color = wi::Color::White();
revealTextureButton.sprites[wi::gui::DEACTIVATING].params.color = wi::Color::Gray();
revealTextureButton.OnClick([this](wi::gui::EventArgs args) {
if (revealTex.IsValid())
{
revealTex = {};
revealTextureButton.SetImage({});
}
else
{
wi::helper::FileDialogParams params;
params.type = wi::helper::FileDialogParams::OPEN;
params.description = "Texture";
params.extensions = wi::resourcemanager::GetSupportedImageExtensions();
wi::helper::FileDialog(params, [this](std::string fileName) {
wi::eventhandler::Subscribe_Once(wi::eventhandler::EVENT_THREAD_SAFE_POINT, [=](uint64_t userdata) {
revealTex = wi::resourcemanager::Load(fileName);
revealTextureButton.SetImage(revealTex);
});
});
}
});
AddWidget(&revealTextureButton);
Translate(XMFLOAT3((float)editor->GetLogicalWidth() - 550, 50, 0));
SetMinimized(true);
}
void PaintToolWindow::Update(float dt)
{
RecordHistory(false);
if (GetMode() == MODE_TEXTURE)
{
rotationSlider.SetVisible(true);
textureSlotComboBox.SetVisible(true);
brushShapeComboBox.SetVisible(true);
saveTextureButton.SetVisible(true);
brushTextureButton.SetVisible(true);
revealTextureButton.SetVisible(true);
}
else
{
rotationSlider.SetVisible(false);
textureSlotComboBox.SetVisible(false);
brushShapeComboBox.SetVisible(false);
saveTextureButton.SetVisible(false);
brushTextureButton.SetVisible(false);
revealTextureButton.SetVisible(false);
}
if (GetMode() == MODE_SCULPTING_ADD || GetMode() == MODE_SCULPTING_SUBTRACT)
{
axisCombo.SetVisible(true);
}
else
{
axisCombo.SetVisible(false);
}
rot -= dt;
// by default, paint tool is on center of screen, this makes it easy to tweak radius with GUI:
XMFLOAT2 posNew;
posNew.x = editor->GetLogicalWidth() * 0.5f;
posNew.y = editor->GetLogicalHeight() * 0.5f;
if (editor->GetGUI().HasFocus() || wi::backlog::isActive() || entity == INVALID_ENTITY)
{
pos = posNew;
return;
}
const bool pointer_down = wi::input::Down(wi::input::MOUSE_BUTTON_LEFT);
if (!pointer_down)
{
stroke_dist = FLT_MAX;
sculpting_normal = XMFLOAT3(0, 0, 0);
}
auto pointer = wi::input::GetPointer();
posNew = XMFLOAT2(pointer.x, pointer.y);
stroke_dist += wi::math::Distance(pos, posNew);
const float pressure = pressureCheckBox.GetCheck() ? pointer.w : 1.0f;
const float spacing = spacingSlider.GetValue();
const bool pointer_moved = stroke_dist >= spacing;
const bool painting = pointer_moved && pointer_down;
if (painting)
{
stroke_dist = 0;
}
pos = posNew;
const MODE mode = GetMode();
const float radius = radiusSlider.GetValue();
const float pressure_radius = radius * pressure;
const float amount = amountSlider.GetValue();
const float falloff = falloffSlider.GetValue();
const wi::Color color = colorPicker.GetPickColor();
const XMFLOAT4 color_float = color.toFloat4();
const bool backfaces = backfaceCheckBox.GetCheck();
const bool wireframe = wireCheckBox.GetCheck();
Scene& scene = editor->GetCurrentScene();
const CameraComponent& camera = editor->GetCurrentEditorScene().camera;
const XMVECTOR C = XMLoadFloat2(&pos);
const XMMATRIX VP = camera.GetViewProjection();
const XMVECTOR MUL = XMVectorSet(0.5f, -0.5f, 1, 1);
const XMVECTOR ADD = XMVectorSet(0.5f, 0.5f, 0, 0);
const XMVECTOR SCREEN = XMVectorSet((float)editor->GetLogicalWidth(), (float)editor->GetLogicalHeight(), 1, 1);
const XMVECTOR F = camera.GetAt();
const float brush_rotation = wi::random::GetRandom(0.0f, rotationSlider.GetValue() * XM_2PI);
switch (mode)
{
case MODE_TEXTURE:
{
const wi::scene::PickResult& intersect = editor->hovered;
if (intersect.entity != entity)
break;
ObjectComponent* object = scene.objects.GetComponent(entity);
if (object == nullptr || object->meshID == INVALID_ENTITY)
break;
MeshComponent* mesh = scene.meshes.GetComponent(object->meshID);
if (mesh == nullptr || (mesh->vertex_uvset_0.empty() && mesh->vertex_uvset_1.empty()))
break;
MaterialComponent* material = subset >= 0 && subset < (int)mesh->subsets.size() ? scene.materials.GetComponent(mesh->subsets[subset].materialID) : nullptr;
if (material == nullptr)
break;
int uvset = 0;
Texture editTexture = GetEditTextureSlot(*material, &uvset);
if (!editTexture.IsValid())
break;
const TextureDesc& desc = editTexture.GetDesc();
auto& vertex_uvset = uvset == 0 ? mesh->vertex_uvset_0 : mesh->vertex_uvset_1;
const float u = intersect.bary.x;
const float v = intersect.bary.y;
const float w = 1 - u - v;
XMFLOAT2 uv;
uv.x = vertex_uvset[intersect.vertexID0].x * w +
vertex_uvset[intersect.vertexID1].x * u +
vertex_uvset[intersect.vertexID2].x * v;
uv.y = vertex_uvset[intersect.vertexID0].y * w +
vertex_uvset[intersect.vertexID1].y * u +
vertex_uvset[intersect.vertexID2].y * v;
uint2 center = XMUINT2(uint32_t(uv.x * desc.width), uint32_t(uv.y * desc.height));
if (painting)
{
GraphicsDevice* device = wi::graphics::GetDevice();
CommandList cmd = device->BeginCommandList();
RecordHistory(true, cmd);
// Need to requery this because RecordHistory might swap textures on material:
editTexture = GetEditTextureSlot(*material, &uvset);
device->BindComputeShader(wi::renderer::GetShader(wi::enums::CSTYPE_PAINT_TEXTURE), cmd);
if (brushTex.IsValid())
{
device->BindResource(&brushTex.GetTexture(), 0, cmd);
}
else
{
device->BindResource(wi::texturehelper::getWhite(), 0, cmd);
}
if (revealTex.IsValid())
{
device->BindResource(&revealTex.GetTexture(), 1, cmd);
}
else
{
device->BindResource(wi::texturehelper::getWhite(), 1, cmd);
}
device->BindUAV(&editTexture, 0, cmd);
PaintTextureCB cb;
cb.xPaintBrushCenter = center;
cb.xPaintBrushRadius = (uint32_t)pressure_radius;
if (brushShapeComboBox.GetSelected() == 1)
{
cb.xPaintBrushRadius = (uint)std::ceil((float(cb.xPaintBrushRadius) * 2 / std::sqrt(2.0f))); // square shape, diagonal dispatch size
}
cb.xPaintBrushAmount = amount;
cb.xPaintBrushFalloff = falloff;
cb.xPaintBrushColor = color.rgba;
cb.xPaintReveal = revealTex.IsValid() ? 1 : 0;
cb.xPaintBrushRotation = brush_rotation;
cb.xPaintBrushShape = (uint)brushShapeComboBox.GetSelected();
device->PushConstants(&cb, sizeof(cb), cmd);
const uint diameter = cb.xPaintBrushRadius * 2;
const uint dispatch_dim = (diameter + PAINT_TEXTURE_BLOCKSIZE - 1) / PAINT_TEXTURE_BLOCKSIZE;
device->Dispatch(dispatch_dim, dispatch_dim, 1, cmd);
GPUBarrier barriers[] = {
GPUBarrier::Memory(),
};
device->Barrier(barriers, arraysize(barriers), cmd);
if (editTexture.desc.mip_levels > 1)
{
wi::renderer::GenerateMipChain(editTexture, wi::renderer::MIPGENFILTER::MIPGENFILTER_LINEAR, cmd);
}
}
wi::renderer::PaintRadius paintrad;
paintrad.objectEntity = entity;
paintrad.subset = subset;
paintrad.radius = radius;
paintrad.center = center;
paintrad.uvset = uvset;
paintrad.dimensions.x = desc.width;
paintrad.dimensions.y = desc.height;
paintrad.rotation = brush_rotation;
paintrad.shape = (uint)brushShapeComboBox.GetSelected();
wi::renderer::DrawPaintRadius(paintrad);
}
break;
case MODE_VERTEXCOLOR:
case MODE_WIND:
{
ObjectComponent* object = scene.objects.GetComponent(entity);
if (object == nullptr || object->meshID == INVALID_ENTITY)
break;
MeshComponent* mesh = scene.meshes.GetComponent(object->meshID);
if (mesh == nullptr)
break;
MaterialComponent* material = subset >= 0 && subset < (int)mesh->subsets.size() ? scene.materials.GetComponent(mesh->subsets[subset].materialID) : nullptr;
if (material == nullptr)
{
for (auto& x : mesh->subsets)
{
material = scene.materials.GetComponent(x.materialID);
if (material != nullptr)
{
switch (mode)
{
case MODE_VERTEXCOLOR:
material->SetUseVertexColors(true);
break;
case MODE_WIND:
material->SetUseWind(true);
break;
}
}
}
material = nullptr;
}
else
{
switch (mode)
{
case MODE_VERTEXCOLOR:
material->SetUseVertexColors(true);
break;
case MODE_WIND:
material->SetUseWind(true);
break;
}
}
const ArmatureComponent* armature = mesh->IsSkinned() ? scene.armatures.GetComponent(mesh->armatureID) : nullptr;
const TransformComponent* transform = scene.transforms.GetComponent(entity);
if (transform == nullptr)
break;
const XMMATRIX W = XMLoadFloat4x4(&transform->world);
bool rebuild = false;
switch (mode)
{
case MODE_VERTEXCOLOR:
if (mesh->vertex_colors.empty())
{
mesh->vertex_colors.resize(mesh->vertex_positions.size());
std::fill(mesh->vertex_colors.begin(), mesh->vertex_colors.end(), wi::Color::White().rgba); // fill white
rebuild = true;
}
break;
case MODE_WIND:
if (mesh->vertex_windweights.empty())
{
mesh->vertex_windweights.resize(mesh->vertex_positions.size());
std::fill(mesh->vertex_windweights.begin(), mesh->vertex_windweights.end(), 0xFF); // fill max affection
rebuild = true;
}
break;
}
if (painting)
{
for (size_t j = 0; j < mesh->vertex_positions.size(); ++j)
{
XMVECTOR P, N;
if (armature == nullptr)
{
P = XMLoadFloat3(&mesh->vertex_positions[j]);
N = XMLoadFloat3(&mesh->vertex_normals[j]);
}
else
{
P = wi::scene::SkinVertex(*mesh, *armature, (uint32_t)j, &N);
}
P = XMVector3Transform(P, W);
N = XMVector3Normalize(XMVector3TransformNormal(N, W));
if (!backfaces && XMVectorGetX(XMVector3Dot(F, N)) > 0)
continue;
P = XMVector3TransformCoord(P, VP);
P = P * MUL + ADD;
P = P * SCREEN;
if (subset >= 0 && mesh->vertex_subsets[j] != subset)
continue;
const float z = XMVectorGetZ(P);
const float dist = XMVectorGetX(XMVector2Length(C - P));
if (z >= 0 && z <= 1 && dist <= pressure_radius)
{
RecordHistory(true);
rebuild = true;
const float affection = amount * std::pow(1.0f - (dist / pressure_radius), falloff);
switch (mode)
{
case MODE_VERTEXCOLOR:
{
wi::Color vcol = mesh->vertex_colors[j];
vcol = wi::Color::lerp(vcol, color, affection);
mesh->vertex_colors[j] = vcol.rgba;
}
break;
case MODE_WIND:
{
wi::Color vcol = wi::Color(0, 0, 0, mesh->vertex_windweights[j]);
vcol = wi::Color::lerp(vcol, color, affection);
mesh->vertex_windweights[j] = vcol.getA();
}
break;
}
}
}
}
if (wireframe)
{
uint32_t first_subset = 0;
uint32_t last_subset = 0;
mesh->GetLODSubsetRange(0, first_subset, last_subset);
for (uint32_t subsetIndex = first_subset; subsetIndex < last_subset; ++subsetIndex)
{
const MeshComponent::MeshSubset& subset = mesh->subsets[subsetIndex];
for (size_t j = 0; j < subset.indexCount; j += 3)
{
const uint32_t triangle[] = {
mesh->indices[j + 0],
mesh->indices[j + 1],
mesh->indices[j + 2],
};
if (this->subset >= 0 && (mesh->vertex_subsets[triangle[0]] != this->subset || mesh->vertex_subsets[triangle[1]] != this->subset || mesh->vertex_subsets[triangle[2]] != this->subset))
continue;
const XMVECTOR P[arraysize(triangle)] = {
XMVector3Transform(armature == nullptr ? XMLoadFloat3(&mesh->vertex_positions[triangle[0]]) : wi::scene::SkinVertex(*mesh, *armature, triangle[0]), W),
XMVector3Transform(armature == nullptr ? XMLoadFloat3(&mesh->vertex_positions[triangle[1]]) : wi::scene::SkinVertex(*mesh, *armature, triangle[1]), W),
XMVector3Transform(armature == nullptr ? XMLoadFloat3(&mesh->vertex_positions[triangle[2]]) : wi::scene::SkinVertex(*mesh, *armature, triangle[2]), W),
};
wi::renderer::RenderableTriangle tri;
XMStoreFloat3(&tri.positionA, P[0]);
XMStoreFloat3(&tri.positionB, P[1]);
XMStoreFloat3(&tri.positionC, P[2]);
if (mode == MODE_WIND)
{
tri.colorA = wi::Color(mesh->vertex_windweights[triangle[0]], 0, 0, 255);
tri.colorB = wi::Color(mesh->vertex_windweights[triangle[1]], 0, 0, 255);
tri.colorC = wi::Color(mesh->vertex_windweights[triangle[2]], 0, 0, 255);
}
else
{
tri.colorA.w = 0.8f;
tri.colorB.w = 0.8f;
tri.colorC.w = 0.8f;
}
wi::renderer::DrawTriangle(tri, true);
}
}
}
if (rebuild)
{
mesh->CreateRenderData();
}
}
break;
case MODE_SCULPTING_ADD:
case MODE_SCULPTING_SUBTRACT:
{
ObjectComponent* object = scene.objects.GetComponent(entity);
if (object == nullptr || object->meshID == INVALID_ENTITY)
break;
MeshComponent* mesh = scene.meshes.GetComponent(object->meshID);
if (mesh == nullptr)
break;
const ArmatureComponent* armature = mesh->IsSkinned() ? scene.armatures.GetComponent(mesh->armatureID) : nullptr;
const TransformComponent* transform = scene.transforms.GetComponent(entity);
if (transform == nullptr)
break;
const XMMATRIX W = XMLoadFloat4x4(&transform->world);
XMVECTOR sculptDir;
AxisLock axis_lock = (AxisLock)axisCombo.GetItemUserData(axisCombo.GetSelected());
switch (axis_lock)
{
default:
case PaintToolWindow::AxisLock::Disabled:
if (sculpting_normal.x < FLT_EPSILON && sculpting_normal.y < FLT_EPSILON && sculpting_normal.z < FLT_EPSILON)
{
sculpting_normal = editor->hovered.normal;
}
sculptDir = XMVector3TransformNormal(XMVector3Normalize(XMLoadFloat3(&sculpting_normal)), XMMatrixInverse(nullptr, W));
break;
case PaintToolWindow::AxisLock::X:
sculpting_normal = XMFLOAT3(1, 0, 0);
sculptDir = XMLoadFloat3(&sculpting_normal);
break;
case PaintToolWindow::AxisLock::Y:
sculpting_normal = XMFLOAT3(0, 1, 0);
sculptDir = XMLoadFloat3(&sculpting_normal);
break;
case PaintToolWindow::AxisLock::Z:
sculpting_normal = XMFLOAT3(0, 0, 1);
sculptDir = XMLoadFloat3(&sculpting_normal);
break;
}
bool rebuild = false;
if (painting)
{
sculpting_indices.clear();
sculpting_indices.reserve(mesh->vertex_positions.size());
for (size_t j = 0; j < mesh->vertex_positions.size(); ++j)
{
XMVECTOR P, N;
if (armature == nullptr)
{
P = XMLoadFloat3(&mesh->vertex_positions[j]);
N = XMLoadFloat3(&mesh->vertex_normals[j]);
}
else
{
P = wi::scene::SkinVertex(*mesh, *armature, (uint32_t)j, &N);
}
P = XMVector3Transform(P, W);
N = XMVector3Normalize(XMVector3TransformNormal(N, W));
const float dotN = XMVectorGetX(XMVector3Dot(F, N));
if (!backfaces && dotN > 0)
continue;
P = XMVector3TransformCoord(P, VP);
P = P * MUL + ADD;
P = P * SCREEN;
const float z = XMVectorGetZ(P);
const float dist = XMVectorGetX(XMVector2Length(C - P));
if (z >= 0 && z <= 1 && dist <= pressure_radius)
{
const float affection = amount * std::pow(1.0f - (dist / pressure_radius), falloff);
sculpting_indices.push_back({ j, affection });
}
}
if (!sculpting_indices.empty())
{
RecordHistory(true);
rebuild = true;
for (auto& x : sculpting_indices)
{
XMVECTOR PL = XMLoadFloat3(&mesh->vertex_positions[x.ind]);
switch (mode)
{
case MODE_SCULPTING_ADD:
PL += sculptDir * x.affection;
break;
case MODE_SCULPTING_SUBTRACT:
PL -= sculptDir * x.affection;
break;
}
XMStoreFloat3(&mesh->vertex_positions[x.ind], PL);
}
}
}
if (wireframe)
{
uint32_t first_subset = 0;
uint32_t last_subset = 0;
mesh->GetLODSubsetRange(0, first_subset, last_subset);
for (uint32_t subsetIndex = first_subset; subsetIndex < last_subset; ++subsetIndex)
{
const MeshComponent::MeshSubset& subset = mesh->subsets[subsetIndex];
for (size_t j = 0; j < subset.indexCount; j += 3)
{
const uint32_t triangle[] = {
mesh->indices[j + 0],
mesh->indices[j + 1],
mesh->indices[j + 2],
};
const XMVECTOR P[arraysize(triangle)] = {
XMVector3Transform(armature == nullptr ? XMLoadFloat3(&mesh->vertex_positions[triangle[0]]) : wi::scene::SkinVertex(*mesh, *armature, triangle[0]), W),
XMVector3Transform(armature == nullptr ? XMLoadFloat3(&mesh->vertex_positions[triangle[1]]) : wi::scene::SkinVertex(*mesh, *armature, triangle[1]), W),
XMVector3Transform(armature == nullptr ? XMLoadFloat3(&mesh->vertex_positions[triangle[2]]) : wi::scene::SkinVertex(*mesh, *armature, triangle[2]), W),
};
wi::renderer::RenderableTriangle tri;
XMStoreFloat3(&tri.positionA, P[0]);
XMStoreFloat3(&tri.positionB, P[1]);
XMStoreFloat3(&tri.positionC, P[2]);
tri.colorA.w = 0.8f;
tri.colorB.w = 0.8f;
tri.colorC.w = 0.8f;
wi::renderer::DrawTriangle(tri, true);
}
wi::renderer::RenderableLine sculpt_dir_line;
sculpt_dir_line.color_start = XMFLOAT4(0, 1, 0, 1);
sculpt_dir_line.color_end = XMFLOAT4(0, 1, 0, 1);
sculpt_dir_line.start = editor->hovered.position;
XMStoreFloat3(
&sculpt_dir_line.end,
XMLoadFloat3(&sculpt_dir_line.start) +
XMVector3Normalize(XMLoadFloat3(&sculpting_normal))
);
wi::renderer::DrawLine(sculpt_dir_line);
}
}
if (rebuild)
{
mesh->ComputeNormals(MeshComponent::COMPUTE_NORMALS_SMOOTH_FAST);
}
}
break;
case MODE_SOFTBODY_PINNING:
case MODE_SOFTBODY_PHYSICS:
{
ObjectComponent* object = scene.objects.GetComponent(entity);
if (object == nullptr || object->meshID == INVALID_ENTITY)
break;
const MeshComponent* mesh = scene.meshes.GetComponent(object->meshID);
if (mesh == nullptr)
break;
SoftBodyPhysicsComponent* softbody = scene.softbodies.GetComponent(object->meshID);
if (softbody == nullptr || softbody->vertex_positions_simulation.empty())
break;
// Painting:
if (pointer_moved && wi::input::Down(wi::input::MOUSE_BUTTON_LEFT))
{
size_t j = 0;
for (auto& ind : softbody->physicsToGraphicsVertexMapping)
{
XMVECTOR P = softbody->vertex_positions_simulation[ind].LoadPOS();
P = XMVector3TransformCoord(P, VP);
P = P * MUL + ADD;
P = P * SCREEN;
const float z = XMVectorGetZ(P);
if (z >= 0 && z <= 1 && XMVectorGetX(XMVector2Length(C - P)) <= pressure_radius)
{
RecordHistory(true);
softbody->weights[j] = (mode == MODE_SOFTBODY_PINNING ? 0.0f : 1.0f);
softbody->_flags |= SoftBodyPhysicsComponent::FORCE_RESET;
}
j++;
}
}
// Visualizing:
const XMMATRIX W = XMLoadFloat4x4(&softbody->worldMatrix);
uint32_t first_subset = 0;
uint32_t last_subset = 0;
mesh->GetLODSubsetRange(0, first_subset, last_subset);
for (uint32_t subsetIndex = first_subset; subsetIndex < last_subset; ++subsetIndex)
{
const MeshComponent::MeshSubset& subset = mesh->subsets[subsetIndex];
for (size_t j = 0; j < subset.indexCount; j += 3)
{
const uint32_t graphicsIndex0 = mesh->indices[j + 0];
const uint32_t graphicsIndex1 = mesh->indices[j + 1];
const uint32_t graphicsIndex2 = mesh->indices[j + 2];
const uint32_t physicsIndex0 = softbody->graphicsToPhysicsVertexMapping[graphicsIndex0];
const uint32_t physicsIndex1 = softbody->graphicsToPhysicsVertexMapping[graphicsIndex1];
const uint32_t physicsIndex2 = softbody->graphicsToPhysicsVertexMapping[graphicsIndex2];
const float weight0 = softbody->weights[physicsIndex0];
const float weight1 = softbody->weights[physicsIndex1];
const float weight2 = softbody->weights[physicsIndex2];
wi::renderer::RenderableTriangle tri;
if (softbody->vertex_positions_simulation.empty())
{
XMStoreFloat3(&tri.positionA, XMVector3Transform(XMLoadFloat3(&mesh->vertex_positions[graphicsIndex0]), W));
XMStoreFloat3(&tri.positionB, XMVector3Transform(XMLoadFloat3(&mesh->vertex_positions[graphicsIndex1]), W));
XMStoreFloat3(&tri.positionC, XMVector3Transform(XMLoadFloat3(&mesh->vertex_positions[graphicsIndex2]), W));
}
else
{
tri.positionA = softbody->vertex_positions_simulation[graphicsIndex0].pos;
tri.positionB = softbody->vertex_positions_simulation[graphicsIndex1].pos;
tri.positionC = softbody->vertex_positions_simulation[graphicsIndex2].pos;
}
if (weight0 == 0)
tri.colorA = XMFLOAT4(1, 1, 0, 1);
else
tri.colorA = XMFLOAT4(1, 1, 1, 1);
if (weight1 == 0)
tri.colorB = XMFLOAT4(1, 1, 0, 1);
else
tri.colorB = XMFLOAT4(1, 1, 1, 1);
if (weight2 == 0)
tri.colorC = XMFLOAT4(1, 1, 0, 1);
else
tri.colorC = XMFLOAT4(1, 1, 1, 1);
if (wireframe)
{
wi::renderer::DrawTriangle(tri, true);
}
if (weight0 == 0 && weight1 == 0 && weight2 == 0)
{
tri.colorA = tri.colorB = tri.colorC = XMFLOAT4(1, 0, 0, 0.8f);
wi::renderer::DrawTriangle(tri);
}
}
}
}
break;
case MODE_HAIRPARTICLE_ADD_TRIANGLE:
case MODE_HAIRPARTICLE_REMOVE_TRIANGLE:
case MODE_HAIRPARTICLE_LENGTH:
{
wi::HairParticleSystem* hair = scene.hairs.GetComponent(entity);
if (hair == nullptr || hair->meshID == INVALID_ENTITY)
break;
MeshComponent* mesh = scene.meshes.GetComponent(hair->meshID);
if (mesh == nullptr)
break;
const ArmatureComponent* armature = mesh->IsSkinned() ? scene.armatures.GetComponent(mesh->armatureID) : nullptr;
const TransformComponent* transform = scene.transforms.GetComponent(entity);
if (transform == nullptr)
break;
const XMMATRIX W = XMLoadFloat4x4(&transform->world);
if (painting)
{
for (size_t j = 0; j < mesh->vertex_positions.size(); ++j)
{
XMVECTOR P, N;
if (armature == nullptr)
{
P = XMLoadFloat3(&mesh->vertex_positions[j]);
N = XMLoadFloat3(&mesh->vertex_normals[j]);
}
else
{
P = wi::scene::SkinVertex(*mesh, *armature, (uint32_t)j, &N);
}
P = XMVector3Transform(P, W);
N = XMVector3Normalize(XMVector3TransformNormal(N, W));
if (!backfaces && XMVectorGetX(XMVector3Dot(F, N)) > 0)
continue;
P = XMVector3TransformCoord(P, VP);
P = P * MUL + ADD;
P = P * SCREEN;
const float z = XMVectorGetZ(P);
const float dist = XMVectorGetX(XMVector2Length(C - P));
if (z >= 0 && z <= 1 && dist <= pressure_radius)
{
RecordHistory(true);
switch (mode)
{
case MODE_HAIRPARTICLE_ADD_TRIANGLE:
hair->vertex_lengths[j] = 1.0f;
break;
case MODE_HAIRPARTICLE_REMOVE_TRIANGLE:
hair->vertex_lengths[j] = 0;
break;
case MODE_HAIRPARTICLE_LENGTH:
if (hair->vertex_lengths[j] > 0) // don't change distribution
{
const float affection = amount * std::pow(1.0f - (dist / pressure_radius), falloff);
hair->vertex_lengths[j] = wi::math::Lerp(hair->vertex_lengths[j], color_float.w, affection);
// don't let it "remove" the vertex by keeping its length above zero:
// (because if removed, distribution also changes which might be distracting)
hair->vertex_lengths[j] = wi::math::Clamp(hair->vertex_lengths[j], 1.0f / 255.0f, 1.0f);
}
break;
}
hair->_flags |= wi::HairParticleSystem::REBUILD_BUFFERS;
}
}
}
if (wireframe)
{
uint32_t first_subset = 0;
uint32_t last_subset = 0;
mesh->GetLODSubsetRange(0, first_subset, last_subset);
for (uint32_t subsetIndex = first_subset; subsetIndex < last_subset; ++subsetIndex)
{
const MeshComponent::MeshSubset& subset = mesh->subsets[subsetIndex];
for (size_t j = 0; j < subset.indexCount; j += 3)
{
const uint32_t triangle[] = {
mesh->indices[j + 0],
mesh->indices[j + 1],
mesh->indices[j + 2],
};
const XMVECTOR P[arraysize(triangle)] = {
XMVector3Transform(armature == nullptr ? XMLoadFloat3(&mesh->vertex_positions[triangle[0]]) : wi::scene::SkinVertex(*mesh, *armature, triangle[0]), W),
XMVector3Transform(armature == nullptr ? XMLoadFloat3(&mesh->vertex_positions[triangle[1]]) : wi::scene::SkinVertex(*mesh, *armature, triangle[1]), W),
XMVector3Transform(armature == nullptr ? XMLoadFloat3(&mesh->vertex_positions[triangle[2]]) : wi::scene::SkinVertex(*mesh, *armature, triangle[2]), W),
};
wi::renderer::RenderableTriangle tri;
XMStoreFloat3(&tri.positionA, P[0]);
XMStoreFloat3(&tri.positionB, P[1]);
XMStoreFloat3(&tri.positionC, P[2]);
wi::renderer::DrawTriangle(tri, true);
}
}
for (size_t j = 0; j < hair->indices.size() && wireframe; j += 3)
{
const uint32_t triangle[] = {
hair->indices[j + 0],
hair->indices[j + 1],
hair->indices[j + 2],
};
const XMVECTOR P[arraysize(triangle)] = {
XMVector3Transform(armature == nullptr ? XMLoadFloat3(&mesh->vertex_positions[triangle[0]]) : wi::scene::SkinVertex(*mesh, *armature, triangle[0]), W),
XMVector3Transform(armature == nullptr ? XMLoadFloat3(&mesh->vertex_positions[triangle[1]]) : wi::scene::SkinVertex(*mesh, *armature, triangle[1]), W),
XMVector3Transform(armature == nullptr ? XMLoadFloat3(&mesh->vertex_positions[triangle[2]]) : wi::scene::SkinVertex(*mesh, *armature, triangle[2]), W),
};
wi::renderer::RenderableTriangle tri;
XMStoreFloat3(&tri.positionA, P[0]);
XMStoreFloat3(&tri.positionB, P[1]);
XMStoreFloat3(&tri.positionC, P[2]);
tri.colorA = tri.colorB = tri.colorC = XMFLOAT4(1, 0, 1, 0.9f);
wi::renderer::DrawTriangle(tri, false);
}
}
}
break;
}
}
namespace PaintTool_Internal
{
PipelineState pso;
void LoadShaders()
{
GraphicsDevice* device = wi::graphics::GetDevice();
{
PipelineStateDesc desc;
desc.vs = wi::renderer::GetShader(wi::enums::VSTYPE_VERTEXCOLOR);
desc.ps = wi::renderer::GetShader(wi::enums::PSTYPE_VERTEXCOLOR);
desc.il = wi::renderer::GetInputLayout(wi::enums::ILTYPE_VERTEXCOLOR);
desc.dss = wi::renderer::GetDepthStencilState(wi::enums::DSSTYPE_DEPTHDISABLED);
desc.rs = wi::renderer::GetRasterizerState(wi::enums::RSTYPE_DOUBLESIDED);
desc.bs = wi::renderer::GetBlendState(wi::enums::BSTYPE_TRANSPARENT);
desc.pt = PrimitiveTopology::TRIANGLELIST;
device->CreatePipelineState(&desc, &pso);
}
}
struct Vertex
{
XMFLOAT4 position;
XMFLOAT4 color;
};
}
using namespace PaintTool_Internal;
void PaintToolWindow::DrawBrush(const wi::Canvas& canvas, CommandList cmd) const
{
const MODE mode = GetMode();
if (mode == MODE_DISABLED || mode == MODE_TEXTURE || entity == INVALID_ENTITY || wi::backlog::isActive())
return;
static bool shaders_loaded = false;
if (!shaders_loaded)
{
shaders_loaded = true;
static wi::eventhandler::Handle handle = wi::eventhandler::Subscribe(wi::eventhandler::EVENT_RELOAD_SHADERS, [](uint64_t userdata) { LoadShaders(); });
LoadShaders();
}
GraphicsDevice* device = wi::graphics::GetDevice();
device->EventBegin("Paint Tool", cmd);
device->BindPipelineState(&pso, cmd);
const uint32_t segmentCount = 36;
const uint32_t circle_triangleCount = segmentCount * 2;
uint32_t vertexCount = circle_triangleCount * 3;
GraphicsDevice::GPUAllocation mem = device->AllocateGPU(sizeof(Vertex) * vertexCount, cmd);
uint8_t* dst = (uint8_t*)mem.data;
for (uint32_t i = 0; i < segmentCount; ++i)
{
const float angle0 = (float)i / (float)segmentCount * XM_2PI;
const float angle1 = (float)(i + 1) / (float)segmentCount * XM_2PI;
// circle:
const float radius = radiusSlider.GetValue();
const float radius_outer = radius + 8;
float brightness = i % 2 == 0 ? 1.0f : 0.0f;
XMFLOAT4 color_inner = XMFLOAT4(brightness, brightness, brightness, 1);
XMFLOAT4 color_outer = XMFLOAT4(brightness, brightness, brightness, 0);
const Vertex verts[] = {
{XMFLOAT4(std::sin(angle0) * radius, std::cos(angle0) * radius, 0, 1), color_inner},
{XMFLOAT4(std::sin(angle1) * radius, std::cos(angle1) * radius, 0, 1), color_inner},
{XMFLOAT4(std::sin(angle0) * radius_outer, std::cos(angle0) * radius_outer, 0, 1), color_outer},
{XMFLOAT4(std::sin(angle0) * radius_outer, std::cos(angle0) * radius_outer, 0, 1), color_outer},
{XMFLOAT4(std::sin(angle1) * radius_outer, std::cos(angle1) * radius_outer, 0, 1), color_outer},
{XMFLOAT4(std::sin(angle1) * radius, std::cos(angle1) * radius, 0, 1), color_inner},
};
std::memcpy(dst, verts, sizeof(verts));
dst += sizeof(verts);
}
const GPUBuffer* vbs[] = {
&mem.buffer,
};
const uint32_t strides[] = {
sizeof(Vertex),
};
const uint64_t offsets[] = {
mem.offset,
};
device->BindVertexBuffers(vbs, 0, arraysize(vbs), strides, offsets, cmd);
MiscCB sb;
XMStoreFloat4x4(&sb.g_xTransform, XMMatrixRotationZ(rot) * XMMatrixTranslation(pos.x, pos.y, 0) * canvas.GetProjection());
sb.g_xColor = XMFLOAT4(1, 1, 1, 1);
device->BindDynamicConstantBuffer(sb, CBSLOT_RENDERER_MISC, cmd);
device->Draw(vertexCount, 0, cmd);
device->EventEnd(cmd);
}
PaintToolWindow::MODE PaintToolWindow::GetMode() const
{
return (MODE)modeComboBox.GetSelected();
}
void PaintToolWindow::SetEntity(wi::ecs::Entity value, int subsetindex)
{
entity = value;
subset = subsetindex;
if (entity == INVALID_ENTITY)
{
saveTextureButton.SetEnabled(false);
}
else if (GetMode() == MODE_TEXTURE)
{
saveTextureButton.SetEnabled(true);
}
}
void PaintToolWindow::RecordHistory(bool start, CommandList cmd)
{
if (start)
{
if (history_needs_recording_start)
return;
history_needs_recording_start = true;
history_needs_recording_end = true;
// Start history recording (undo)
currentHistory = &editor->AdvanceHistory();
wi::Archive& archive = *currentHistory;
archive << EditorComponent::HISTORYOP_PAINTTOOL;
archive << (uint32_t)GetMode();
archive << entity;
}
else
{
if (!history_needs_recording_end || wi::input::Down(wi::input::MOUSE_BUTTON_LEFT))
return;
history_needs_recording_end = false;
history_needs_recording_start = false;
// End history recording (redo)
}
wi::Archive& archive = *currentHistory;
Scene& scene = editor->GetCurrentScene();
switch (GetMode())
{
case PaintToolWindow::MODE_TEXTURE:
{
ObjectComponent* object = scene.objects.GetComponent(entity);
if (object == nullptr || object->meshID == INVALID_ENTITY)
break;
MeshComponent* mesh = scene.meshes.GetComponent(object->meshID);
if (mesh == nullptr || (mesh->vertex_uvset_0.empty() && mesh->vertex_uvset_1.empty()))
break;
MaterialComponent* material = subset >= 0 && subset < (int)mesh->subsets.size() ? scene.materials.GetComponent(mesh->subsets[subset].materialID) : nullptr;
if (material == nullptr)
break;
auto editTexture = GetEditTextureSlot(*material);
archive << textureSlotComboBox.GetSelected();
if (history_textureIndex >= history_textures.size())
{
history_textures.resize((history_textureIndex + 1) * 2);
}
history_textures[history_textureIndex] = editTexture;
archive << history_textureIndex;
history_textureIndex++;
if (start)
{
// Make a copy of texture to edit and replace material resource:
GraphicsDevice* device = wi::graphics::GetDevice();
Texture newTex;
TextureDesc desc = editTexture.GetDesc();
desc.format = Format::R8G8B8A8_UNORM; // force format to one that is writable by GPU
desc.bind_flags |= BindFlag::SHADER_RESOURCE | BindFlag::UNORDERED_ACCESS;
device->CreateTexture(&desc, nullptr, &newTex);
for (uint32_t i = 0; i < newTex.GetDesc().mip_levels; ++i)
{
int subresource_index;
subresource_index = device->CreateSubresource(&newTex, SubresourceType::SRV, 0, 1, i, 1);
assert(subresource_index == i);
subresource_index = device->CreateSubresource(&newTex, SubresourceType::UAV, 0, 1, i, 1);
assert(subresource_index == i);
}
assert(cmd.IsValid());
wi::renderer::CopyTexture2D(newTex, -1, 0, 0, editTexture, 0, cmd);
ReplaceEditTextureSlot(*material, newTex);
}
}
break;
case PaintToolWindow::MODE_VERTEXCOLOR:
{
ObjectComponent* object = scene.objects.GetComponent(entity);
if (object == nullptr || object->meshID == INVALID_ENTITY)
break;
MeshComponent* mesh = scene.meshes.GetComponent(object->meshID);
if (mesh == nullptr)
break;
archive << mesh->vertex_colors;
}
break;
case PaintToolWindow::MODE_WIND:
{
ObjectComponent* object = scene.objects.GetComponent(entity);
if (object == nullptr || object->meshID == INVALID_ENTITY)
break;
MeshComponent* mesh = scene.meshes.GetComponent(object->meshID);
if (mesh == nullptr)
break;
archive << mesh->vertex_windweights;
}
break;
case PaintToolWindow::MODE_SCULPTING_ADD:
case PaintToolWindow::MODE_SCULPTING_SUBTRACT:
{
ObjectComponent* object = scene.objects.GetComponent(entity);
if (object == nullptr || object->meshID == INVALID_ENTITY)
break;
MeshComponent* mesh = scene.meshes.GetComponent(object->meshID);
if (mesh == nullptr)
break;
archive << mesh->vertex_positions;
archive << mesh->vertex_normals;
}
break;
case PaintToolWindow::MODE_SOFTBODY_PINNING:
case PaintToolWindow::MODE_SOFTBODY_PHYSICS:
{
ObjectComponent* object = scene.objects.GetComponent(entity);
if (object == nullptr || object->meshID == INVALID_ENTITY)
break;
SoftBodyPhysicsComponent* softbody = scene.softbodies.GetComponent(object->meshID);
if (softbody == nullptr)
break;
archive << softbody->weights;
}
break;
case PaintToolWindow::MODE_HAIRPARTICLE_ADD_TRIANGLE:
case PaintToolWindow::MODE_HAIRPARTICLE_REMOVE_TRIANGLE:
case PaintToolWindow::MODE_HAIRPARTICLE_LENGTH:
{
wi::HairParticleSystem* hair = scene.hairs.GetComponent(entity);
if (hair == nullptr)
break;
archive << hair->vertex_lengths;
}
break;
default:
assert(0);
break;
}
}
void PaintToolWindow::ConsumeHistoryOperation(wi::Archive& archive, bool undo)
{
MODE historymode;
archive >> (uint32_t&)historymode;
archive >> entity;
modeComboBox.SetSelected(historymode);
Scene& scene = editor->GetCurrentScene();
switch (historymode)
{
case PaintToolWindow::MODE_TEXTURE:
{
ObjectComponent* object = scene.objects.GetComponent(entity);
if (object == nullptr || object->meshID == INVALID_ENTITY)
break;
MeshComponent* mesh = scene.meshes.GetComponent(object->meshID);
if (mesh == nullptr || (mesh->vertex_uvset_0.empty() && mesh->vertex_uvset_1.empty()))
break;
MaterialComponent* material = subset >= 0 && subset < (int)mesh->subsets.size() ? scene.materials.GetComponent(mesh->subsets[subset].materialID) : nullptr;
if (material == nullptr)
break;
int undo_slot;
archive >> undo_slot;
size_t undo_textureindex;
archive >> undo_textureindex;
int redo_slot;
archive >> redo_slot;
size_t redo_textureindex;
archive >> redo_textureindex;
if (undo)
{
textureSlotComboBox.SetSelected(undo_slot);
history_textureIndex = undo_textureindex;
}
else
{
textureSlotComboBox.SetSelected(redo_slot);
history_textureIndex = redo_textureindex;
}
ReplaceEditTextureSlot(*material, history_textures[history_textureIndex]);
}
break;
case PaintToolWindow::MODE_VERTEXCOLOR:
{
ObjectComponent* object = scene.objects.GetComponent(entity);
if (object == nullptr || object->meshID == INVALID_ENTITY)
break;
MeshComponent* mesh = scene.meshes.GetComponent(object->meshID);
if (mesh == nullptr)
break;
MeshComponent undo_mesh;
archive >> undo_mesh.vertex_colors;
MeshComponent redo_mesh;
archive >> redo_mesh.vertex_colors;
if (undo)
{
mesh->vertex_colors = undo_mesh.vertex_colors;
}
else
{
mesh->vertex_colors = redo_mesh.vertex_colors;
}
mesh->CreateRenderData();
}
break;
case PaintToolWindow::MODE_WIND:
{
ObjectComponent* object = scene.objects.GetComponent(entity);
if (object == nullptr || object->meshID == INVALID_ENTITY)
break;
MeshComponent* mesh = scene.meshes.GetComponent(object->meshID);
if (mesh == nullptr)
break;
MeshComponent undo_mesh;
archive >> undo_mesh.vertex_windweights;
MeshComponent redo_mesh;
archive >> redo_mesh.vertex_windweights;
if (undo)
{
mesh->vertex_windweights = undo_mesh.vertex_windweights;
}
else
{
mesh->vertex_windweights = redo_mesh.vertex_windweights;
}
mesh->CreateRenderData();
}
break;
case PaintToolWindow::MODE_SCULPTING_ADD:
case PaintToolWindow::MODE_SCULPTING_SUBTRACT:
{
ObjectComponent* object = scene.objects.GetComponent(entity);
if (object == nullptr || object->meshID == INVALID_ENTITY)
break;
MeshComponent* mesh = scene.meshes.GetComponent(object->meshID);
if (mesh == nullptr)
break;
MeshComponent undo_mesh;
archive >> undo_mesh.vertex_positions;
archive >> undo_mesh.vertex_normals;
MeshComponent redo_mesh;
archive >> redo_mesh.vertex_positions;
archive >> redo_mesh.vertex_normals;
if (undo)
{
mesh->vertex_positions = undo_mesh.vertex_positions;
mesh->vertex_normals = undo_mesh.vertex_normals;
}
else
{
mesh->vertex_positions = redo_mesh.vertex_positions;
mesh->vertex_normals = redo_mesh.vertex_normals;
}
mesh->CreateRenderData();
}
break;
case PaintToolWindow::MODE_SOFTBODY_PINNING:
case PaintToolWindow::MODE_SOFTBODY_PHYSICS:
{
ObjectComponent* object = scene.objects.GetComponent(entity);
if (object == nullptr || object->meshID == INVALID_ENTITY)
break;
SoftBodyPhysicsComponent* softbody = scene.softbodies.GetComponent(object->meshID);
if (softbody == nullptr)
break;
SoftBodyPhysicsComponent undo_softbody;
archive >> undo_softbody.weights;
SoftBodyPhysicsComponent redo_softbody;
archive >> redo_softbody.weights;
if (undo)
{
softbody->weights = undo_softbody.weights;
}
else
{
softbody->weights = redo_softbody.weights;
}
softbody->_flags |= SoftBodyPhysicsComponent::FORCE_RESET;
}
break;
case PaintToolWindow::MODE_HAIRPARTICLE_ADD_TRIANGLE:
case PaintToolWindow::MODE_HAIRPARTICLE_REMOVE_TRIANGLE:
case PaintToolWindow::MODE_HAIRPARTICLE_LENGTH:
{
wi::HairParticleSystem* hair = scene.hairs.GetComponent(entity);
if (hair == nullptr)
break;
wi::HairParticleSystem undo_hair;
archive >> undo_hair.vertex_lengths;
wi::HairParticleSystem redo_hair;
archive >> redo_hair.vertex_lengths;
if (undo)
{
hair->vertex_lengths = undo_hair.vertex_lengths;
}
else
{
hair->vertex_lengths = redo_hair.vertex_lengths;
}
hair->_flags |= wi::HairParticleSystem::REBUILD_BUFFERS;
}
break;
default:
assert(0);
break;
}
}
Texture PaintToolWindow::GetEditTextureSlot(const MaterialComponent& material, int* uvset)
{
uint64_t sel = textureSlotComboBox.GetItemUserData(textureSlotComboBox.GetSelected());
if (!material.textures[sel].resource.IsValid())
{
return Texture();
}
if (uvset)
*uvset = material.textures[sel].uvset;
return material.textures[sel].resource.GetTexture();
}
void PaintToolWindow::ReplaceEditTextureSlot(wi::scene::MaterialComponent& material, const Texture& texture)
{
uint64_t sel = textureSlotComboBox.GetItemUserData(textureSlotComboBox.GetSelected());
material.textures[sel].resource.SetTexture(texture);
material.SetDirty();
}
void PaintToolWindow::ResizeLayout()
{
wi::gui::Window::ResizeLayout();
const float padding = 4;
const float width = GetWidgetAreaSize().x;
float y = padding;
auto add = [&](wi::gui::Widget& widget) {
if (!widget.IsVisible())
return;
const float margin_left = 110;
const float margin_right = 30;
widget.SetPos(XMFLOAT2(margin_left, y));
widget.SetSize(XMFLOAT2(width - margin_left - margin_right, widget.GetScale().y));
y += widget.GetSize().y;
y += padding;
};
auto add_right = [&](wi::gui::Widget& widget) {
if (!widget.IsVisible())
return;
const float margin_right = 30;
widget.SetPos(XMFLOAT2(width - margin_right - widget.GetSize().x, y));
y += widget.GetSize().y;
y += padding;
};
auto add_fullwidth = [&](wi::gui::Widget& widget) {
if (!widget.IsVisible())
return;
const float margin_left = padding;
const float margin_right = padding;
widget.SetPos(XMFLOAT2(margin_left, y));
widget.SetSize(XMFLOAT2(width - margin_left - margin_right, widget.GetScale().y));
y += widget.GetSize().y;
y += padding;
};
add(modeComboBox);
add_fullwidth(infoLabel);
add(radiusSlider);
add(amountSlider);
add(falloffSlider);
add(spacingSlider);
add(rotationSlider);
add_right(backfaceCheckBox);
add_right(wireCheckBox);
add_right(pressureCheckBox);
add(textureSlotComboBox);
add(brushShapeComboBox);
add(axisCombo);
add(saveTextureButton);
add_right(brushTextureButton);
add_right(revealTextureButton);
colorPicker.SetPos(XMFLOAT2(padding, y));
}