diff --git a/Editor/PaintToolWindow.cpp b/Editor/PaintToolWindow.cpp index 58ab78f12..766d2136f 100644 --- a/Editor/PaintToolWindow.cpp +++ b/Editor/PaintToolWindow.cpp @@ -219,6 +219,7 @@ void PaintToolWindow::Update(float dt) if (!pointer_down) { stroke_dist = FLT_MAX; + sculpting_average_normal = XMFLOAT3(0, 0, 0); } auto pointer = wi::input::GetPointer(); @@ -541,15 +542,11 @@ void PaintToolWindow::Update(float dt) bool rebuild = false; if (painting) { - XMVECTOR averageNormal = XMVectorZero(); - struct PaintVert - { - size_t ind; - float affection; - }; - static wi::vector paintindices; - paintindices.clear(); - paintindices.reserve(mesh->vertex_positions.size()); + XMVECTOR averageNormal = XMLoadFloat3(&sculpting_average_normal); + const bool compute_average_normal = XMVectorGetX(XMVector3Length(averageNormal)) < FLT_EPSILON; + + sculpting_indices.clear(); + sculpting_indices.reserve(mesh->vertex_positions.size()); for (size_t j = 0; j < mesh->vertex_positions.size(); ++j) { @@ -577,19 +574,25 @@ void PaintToolWindow::Update(float dt) const float dist = XMVectorGetX(XMVector2Length(C - P)); if (z >= 0 && z <= 1 && dist <= pressure_radius) { - averageNormal += N; + if (compute_average_normal) + { + averageNormal += N; + } const float affection = amount * std::pow(1.0f - (dist / pressure_radius), falloff); - paintindices.push_back({ j, affection }); + sculpting_indices.push_back({ j, affection }); } } - if (!paintindices.empty()) + if (!sculpting_indices.empty()) { RecordHistory(true); rebuild = true; - averageNormal = XMVector3Normalize(averageNormal); + if (compute_average_normal) + { + averageNormal = XMVector3TransformNormal(XMVector3Normalize(averageNormal), XMMatrixInverse(nullptr, W)); + } - for (auto& x : paintindices) + for (auto& x : sculpting_indices) { XMVECTOR PL = XMLoadFloat3(&mesh->vertex_positions[x.ind]); switch (mode) @@ -604,6 +607,11 @@ void PaintToolWindow::Update(float dt) XMStoreFloat3(&mesh->vertex_positions[x.ind], PL); } } + + if (compute_average_normal) + { + XMStoreFloat3(&sculpting_average_normal, averageNormal); + } } if (wireframe) diff --git a/Editor/PaintToolWindow.h b/Editor/PaintToolWindow.h index 789663b74..a7b7177a0 100644 --- a/Editor/PaintToolWindow.h +++ b/Editor/PaintToolWindow.h @@ -13,6 +13,15 @@ class PaintToolWindow : public wi::gui::Window wi::vector history_textures; // we'd like to keep history textures in GPU memory to avoid GPU readback wi::graphics::Texture GetEditTextureSlot(const wi::scene::MaterialComponent& material, int* uvset = nullptr); void ReplaceEditTextureSlot(wi::scene::MaterialComponent& material, const wi::graphics::Texture& texture); + + struct SculptingIndex + { + size_t ind; + float affection; + }; + wi::vector sculpting_indices; + XMFLOAT3 sculpting_average_normal = XMFLOAT3(0, 0, 0); + public: void Create(EditorComponent* editor);