Skip to content

Commit

Permalink
Material: Appearance Updates 2
Browse files Browse the repository at this point in the history
Improves the use of the ShapeAppearance property for the Part workbench.

    removes DiffuseColor property
        adds Python compatibility using custom attributes
        transitions DiffuseColor to ShapeAppearance on open
    Improved UI elements for setting object appearance, and appearance per face
    Lays the foundation for future texture support
  • Loading branch information
davesrocketshop committed May 14, 2024
1 parent cf57d0a commit 542cd63
Show file tree
Hide file tree
Showing 55 changed files with 1,531 additions and 700 deletions.
59 changes: 59 additions & 0 deletions src/App/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,26 @@

#ifndef _PreComp_
#include <cstring>
#include <random>
#endif

#include "Application.h"
#include "Material.h"

// Helper functions to consistently convert between float and long
namespace
{
float fromPercent(long value)
{
return static_cast<float>(value) / 100.0F;
}

long toPercent(float value)
{
return static_cast<long>(100.0 * value + 0.5);

Check warning on line 44 in src/App/Material.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

casting (double + 0.5) to integer leads to incorrect rounding; consider using lround (#include <cmath>) instead [bugprone-incorrect-roundings]

Check warning on line 44 in src/App/Material.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

0.5 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]
}
} // namespace

using namespace App;


Expand Down Expand Up @@ -317,4 +333,47 @@ void Material::setType(MaterialType MatType)
break;
}
}

App::Material Material::getDefaultAppearance()
{
ParameterGrp::handle hGrp =
App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");

auto getColor = [hGrp](const char* parameter, App::Color& color) {
uint32_t packed = color.getPackedRGB();
packed = hGrp->GetUnsigned(parameter, packed);
color.setPackedRGB(packed);
};
auto intRandom = [](int min, int max) -> int {
static std::mt19937 generator;
std::uniform_int_distribution<int> distribution(min, max);
return distribution(generator);
};

App::Material mat(App::Material::DEFAULT);
mat.transparency = fromPercent(hGrp->GetInt("DefaultShapeTransparency", 0));
long shininess = toPercent(mat.shininess);
mat.shininess = fromPercent(hGrp->GetInt("DefaultShapeShininess", shininess));

// This is handled in the material code when using the object appearance
bool randomColor = hGrp->GetBool("RandomColor", false);

// diffuse color
if (randomColor) {
float red = static_cast<float>(intRandom(0, 255)) / 255.0F;
float green = static_cast<float>(intRandom(0, 255)) / 255.0F;
float blue = static_cast<float>(intRandom(0, 255)) / 255.0F;
mat.diffuseColor = App::Color(red, green, blue);
}
else {
// Color = (204, 204, 230) = 3435980543UL
getColor("DefaultShapeColor", mat.diffuseColor);
}

getColor("DefaultAmbientColor", mat.ambientColor);
getColor("DefaultEmissiveColor", mat.emissiveColor);
getColor("DefaultSpecularColor", mat.specularColor);

return mat;
}
// NOLINTEND(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
6 changes: 6 additions & 0 deletions src/App/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ class AppExport Material
Color emissiveColor; /**< Defines the emissive color. */
float shininess;
float transparency;
std::string image;
std::string imagePath;
std::string uuid;
// NOLINTEND
//@}
Expand All @@ -145,6 +147,8 @@ class AppExport Material
&& diffuseColor == m.diffuseColor
&& specularColor == m.specularColor
&& emissiveColor == m.emissiveColor
&& image == m.image
&& image == m.imagePath
&& uuid == m.uuid;
// clang-format on
}
Expand All @@ -155,6 +159,8 @@ class AppExport Material
Material& operator=(const Material& other) = default;
Material& operator=(Material&& other) = default;

static Material getDefaultAppearance();

private:
MaterialType _matType;
};
Expand Down

0 comments on commit 542cd63

Please sign in to comment.