Material: Appearance Updates 2

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
This commit is contained in:
David Carter
2024-06-01 18:26:38 -04:00
committed by Chris Hennes
parent c4d0f3ed97
commit 5feb963f9d
55 changed files with 1540 additions and 714 deletions

View File

@@ -23,7 +23,6 @@
#include "PreCompiled.h"
#ifndef _PreComp_
#include <random>
#include <Inventor/SoPickedPoint.h>
#include <Inventor/actions/SoRayPickAction.h>
#include <Inventor/actions/SoSearchAction.h>
@@ -35,12 +34,14 @@
#include <Inventor/nodes/SoMaterial.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoSwitch.h>
#include <Inventor/nodes/SoTexture2.h>
#endif
#include <Inventor/nodes/SoResetTransform.h>
#include <App/GeoFeature.h>
#include <App/PropertyGeo.h>
#include <Gui/BitmapFactory.h>
#include "Application.h"
#include "Document.h"
@@ -56,12 +57,12 @@ using namespace Gui;
namespace {
float fromPercent(long value)
{
return static_cast<float>(value) / 100.0F;
return std::roundf(value) / 100.0F;
}
long toPercent(float value)
{
return static_cast<long>(100.0 * value + 0.5);
return std::lround(100.0 * value);
}
}
@@ -71,7 +72,7 @@ const App::PropertyIntegerConstraint::Constraints intPercent = {0, 100, 5};
ViewProviderGeometryObject::ViewProviderGeometryObject()
{
App::Material mat = getUserDefinedMaterial();
App::Material mat = App::Material::getDefaultAppearance();
long initialTransparency = toPercent(mat.transparency);
static const char* dogroup = "Display Options";
@@ -95,10 +96,29 @@ ViewProviderGeometryObject::ViewProviderGeometryObject()
Selectable.setValue(isSelectionEnabled());
pcSwitchAppearance = new SoSwitch;
pcSwitchAppearance->ref();
pcSwitchTexture = new SoSwitch;
pcSwitchTexture->ref();
pcShapeMaterial = new SoMaterial;
setSoMaterial(mat);
setCoinAppearance(mat);
pcShapeMaterial->ref();
pcShapeTexture2D = new SoTexture2;
pcShapeTexture2D->ref();
pcTextureGroup3D = new SoGroup;
pcTextureGroup3D->ref();
// Materials go first, with textured faces drawing over them
pcSwitchAppearance->addChild(pcShapeMaterial);
pcSwitchAppearance->addChild(pcSwitchTexture);
pcSwitchTexture->addChild(pcShapeTexture2D);
pcSwitchTexture->addChild(pcTextureGroup3D);
pcSwitchAppearance->whichChild.setValue(0);
pcSwitchTexture->whichChild.setValue(SO_SWITCH_NONE);
pcBoundingBox = new Gui::SoFCBoundingBox;
pcBoundingBox->ref();
@@ -110,54 +130,15 @@ ViewProviderGeometryObject::ViewProviderGeometryObject()
ViewProviderGeometryObject::~ViewProviderGeometryObject()
{
pcSwitchAppearance->unref();
pcSwitchTexture->unref();
pcShapeMaterial->unref();
pcShapeTexture2D->unref();
pcTextureGroup3D->unref();
pcBoundingBox->unref();
pcBoundColor->unref();
}
App::Material ViewProviderGeometryObject::getUserDefinedMaterial()
{
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;
}
bool ViewProviderGeometryObject::isSelectionEnabled() const
{
ParameterGrp::handle hGrp =
@@ -187,12 +168,14 @@ void ViewProviderGeometryObject::onChanged(const App::Property* prop)
if (getObject() && getObject()->testStatus(App::ObjectStatus::TouchOnColorChange)) {
getObject()->touch(true);
}
const App::Material& Mat = ShapeAppearance[0];
long value = toPercent(ShapeAppearance.getTransparency());
if (value != Transparency.getValue()) {
Transparency.setValue(value);
}
setSoMaterial(Mat);
if (ShapeAppearance.getSize() == 1) {
const App::Material& Mat = ShapeAppearance[0];
setCoinAppearance(Mat);
}
}
else if (prop == &BoundingBox) {
showBoundingBox(BoundingBox.getValue());
@@ -288,14 +271,33 @@ unsigned long ViewProviderGeometryObject::getBoundColor() const
return bbcol;
}
void ViewProviderGeometryObject::setSoMaterial(const App::Material& source)
void ViewProviderGeometryObject::setCoinAppearance(const App::Material& source)
{
#if 0
if (!source.image.empty()) {
Base::Console().Log("setCoinAppearance(Texture)\n");
activateTexture2D();
QByteArray by = QByteArray::fromBase64(QString::fromStdString(source.image).toUtf8());
auto image = QImage::fromData(by, "PNG"); //.scaled(64, 64, Qt::KeepAspectRatio);
SoSFImage texture;
Gui::BitmapFactory().convert(image, texture);
pcShapeTexture2D->image = texture;
} else {
Base::Console().Log("setCoinAppearance(Material)\n");
activateMaterial();
}
#endif
activateMaterial();
// Always set the material for items such as lines that don't support textures
pcShapeMaterial->ambientColor.setValue(source.ambientColor.r,
source.ambientColor.g,
source.ambientColor.b);
source.ambientColor.g,
source.ambientColor.b);
pcShapeMaterial->diffuseColor.setValue(source.diffuseColor.r,
source.diffuseColor.g,
source.diffuseColor.b);
source.diffuseColor.g,
source.diffuseColor.b);
pcShapeMaterial->specularColor.setValue(source.specularColor.r,
source.specularColor.g,
source.specularColor.b);
@@ -306,6 +308,31 @@ void ViewProviderGeometryObject::setSoMaterial(const App::Material& source)
pcShapeMaterial->transparency.setValue(source.transparency);
}
void ViewProviderGeometryObject::activateMaterial()
{
pcSwitchAppearance->whichChild.setValue(0);
pcSwitchTexture->whichChild.setValue(SO_SWITCH_NONE);
}
void ViewProviderGeometryObject::activateTexture2D()
{
pcSwitchAppearance->whichChild.setValue(1);
pcSwitchTexture->whichChild.setValue(0);
}
void ViewProviderGeometryObject::activateTexture3D()
{
pcSwitchAppearance->whichChild.setValue(1);
pcSwitchTexture->whichChild.setValue(1);
}
void ViewProviderGeometryObject::activateMixed3D()
{
pcSwitchAppearance->whichChild.setValue(SO_SWITCH_ALL);
pcSwitchTexture->whichChild.setValue(1);
}
namespace
{
float getBoundBoxFontSize()