Material: Material appearance

Uses new material system for appearance

Each feature object now has a property called ShapeMaterial that
describes its physical properties. If it has a shape, it has a
material.

The ShapeColor attribute is replaced by a ShapeAppearance attribute.
This is a material list that describes all appearance properties, not
just diffuse color. As a list in can be used for all elements of a
shape, such as edges and faces.

A new widget is provided to allow the user to select materials in a
consistent fashion. It can also launch the material editor with its
more advanced capabilities.
This commit is contained in:
David Carter
2024-03-17 18:37:56 -04:00
committed by Chris Hennes
parent 37c38acd19
commit ba20441935
121 changed files with 4682 additions and 1685 deletions

View File

@@ -23,17 +23,17 @@
#include "PreCompiled.h"
#ifndef _PreComp_
# include <Inventor/SoPickedPoint.h>
# include <Inventor/actions/SoRayPickAction.h>
# include <Inventor/actions/SoSearchAction.h>
# include <Inventor/nodes/SoBaseColor.h>
# include <Inventor/nodes/SoCamera.h>
# include <Inventor/nodes/SoDrawStyle.h>
# include <Inventor/nodes/SoFont.h>
# include <Inventor/nodes/SoMaterial.h>
# include <Inventor/nodes/SoSeparator.h>
# include <Inventor/nodes/SoSwitch.h>
# include <Inventor/nodes/SoDirectionalLight.h>
#include <Inventor/SoPickedPoint.h>
#include <Inventor/actions/SoRayPickAction.h>
#include <Inventor/actions/SoSearchAction.h>
#include <Inventor/nodes/SoBaseColor.h>
#include <Inventor/nodes/SoCamera.h>
#include <Inventor/nodes/SoDirectionalLight.h>
#include <Inventor/nodes/SoDrawStyle.h>
#include <Inventor/nodes/SoFont.h>
#include <Inventor/nodes/SoMaterial.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoSwitch.h>
#endif
#include <Inventor/nodes/SoResetTransform.h>
@@ -41,13 +41,13 @@
#include <App/GeoFeature.h>
#include <App/PropertyGeo.h>
#include "ViewProviderGeometryObject.h"
#include "Application.h"
#include "Document.h"
#include "SoFCBoundingBox.h"
#include "SoFCSelection.h"
#include "View3DInventorViewer.h"
#include "ViewProviderGeometryObject.h"
#include "ViewProviderGeometryObjectPy.h"
using namespace Gui;
@@ -57,44 +57,59 @@ const App::PropertyIntegerConstraint::Constraints intPercent = {0, 100, 5};
ViewProviderGeometryObject::ViewProviderGeometryObject()
{
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
bool randomColor = hGrp->GetBool("RandomColor", false);
float r, g, b;
if (randomColor){
auto fMax = (float)RAND_MAX;
r = (float)rand() / fMax;
g = (float)rand() / fMax;
b = (float)rand() / fMax;
}
else {
unsigned long shcol = hGrp->GetUnsigned("DefaultShapeColor", 3435980543UL);
r = ((shcol >> 24) & 0xff) / 255.0;
g = ((shcol >> 16) & 0xff) / 255.0;
b = ((shcol >> 8) & 0xff) / 255.0;
}
ParameterGrp::handle hGrp =
App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
int initialTransparency = hGrp->GetInt("DefaultShapeTransparency", 0);
static const char *dogroup = "Display Options";
static const char *sgroup = "Selection";
static const char *osgroup = "Object Style";
static const char* dogroup = "Display Options";
static const char* sgroup = "Selection";
static const char* osgroup = "Object Style";
ADD_PROPERTY_TYPE(ShapeColor, (r, g, b), osgroup, App::Prop_None, "Set shape color");
ADD_PROPERTY_TYPE(Transparency, (initialTransparency), osgroup, App::Prop_None, "Set object transparency");
ADD_PROPERTY_TYPE(Transparency,
(initialTransparency),
osgroup,
App::Prop_None,
"Set object transparency");
Transparency.setConstraints(&intPercent);
App::Material mat(App::Material::DEFAULT);
mat.transparency = (float)initialTransparency / 100.0f;
ADD_PROPERTY_TYPE(ShapeMaterial,(mat), osgroup, App::Prop_None, "Shape material");
auto geometry = dynamic_cast<App::GeoFeature*>(getObject());
if (geometry) {
mat = geometry->getMaterialAppearance();
} else {
// This is handled in the material code when using the object appearance
bool randomColor = hGrp->GetBool("RandomColor", false);
float r, g, b;
if (randomColor) {
auto fMax = (float)RAND_MAX;
r = (float)rand() / fMax;
g = (float)rand() / fMax;
b = (float)rand() / fMax;
}
else {
unsigned long shcol = hGrp->GetUnsigned("DefaultShapeColor", 3435980543UL);
r = ((shcol >> 24) & 0xff) / 255.0;
g = ((shcol >> 16) & 0xff) / 255.0;
b = ((shcol >> 8) & 0xff) / 255.0;
}
mat.diffuseColor = App::Color(r,g,b);
}
ADD_PROPERTY_TYPE(ShapeAppearance, (mat), osgroup, App::Prop_None, "Shape appearrance");
ADD_PROPERTY_TYPE(BoundingBox, (false), dogroup, App::Prop_None, "Display object bounding box");
ADD_PROPERTY_TYPE(Selectable, (true), sgroup, App::Prop_None, "Set if the object is selectable in the 3d view");
ADD_PROPERTY_TYPE(Selectable,
(true),
sgroup,
App::Prop_None,
"Set if the object is selectable in the 3d view");
bool enableSel = hGrp->GetBool("EnableSelection", true);
Selectable.setValue(enableSel);
pcShapeMaterial = new SoMaterial;
pcShapeMaterial->diffuseColor.setValue(r, g, b);
pcShapeMaterial->transparency = float(initialTransparency);
setSoMaterial(mat);
pcShapeMaterial->ref();
pcBoundingBox = new Gui::SoFCBoundingBox;
@@ -115,44 +130,32 @@ ViewProviderGeometryObject::~ViewProviderGeometryObject()
void ViewProviderGeometryObject::onChanged(const App::Property* prop)
{
// Actually, the properties 'ShapeColor' and 'Transparency' are part of the property 'ShapeMaterial'.
// Both redundant properties are kept due to more convenience for the user. But we must keep the values
// consistent of all these properties.
// Actually, the properties 'ShapeColor' and 'Transparency' are part of the property
// 'ShapeMaterial'. Both redundant properties are kept due to more convenience for the user. But
// we must keep the values consistent of all these properties.
std::string propName = prop->getName();
if (prop == &Selectable) {
bool Sel = Selectable.getValue();
setSelectable(Sel);
}
else if (prop == &ShapeColor) {
const App::Color &c = ShapeColor.getValue();
pcShapeMaterial->diffuseColor.setValue(c.r, c.g, c.b);
if (c != ShapeMaterial.getValue().diffuseColor)
ShapeMaterial.setDiffuseColor(c);
}
else if (prop == &Transparency) {
const App::Material &Mat = ShapeMaterial.getValue();
long value = (long)(100 * Mat.transparency);
long value = (long)(100 * ShapeAppearance.getTransparency());
if (value != Transparency.getValue()) {
float trans = Transparency.getValue() / 100.0f;
float trans = (float)Transparency.getValue() / 100.0f;
pcShapeMaterial->transparency = trans;
ShapeMaterial.setTransparency(trans);
ShapeAppearance.setTransparency(trans);
}
}
else if (prop == &ShapeMaterial) {
if (getObject() && getObject()->testStatus(App::ObjectStatus::TouchOnColorChange))
else if (prop == &ShapeAppearance) {
if (getObject() && getObject()->testStatus(App::ObjectStatus::TouchOnColorChange)) {
getObject()->touch(true);
const App::Material &Mat = ShapeMaterial.getValue();
long value = (long)(100 * Mat.transparency);
if (value != Transparency.getValue())
}
const App::Material& Mat = ShapeAppearance[0];
long value = (long)(100.0 * ShapeAppearance.getTransparency() + 0.5);
if (value != Transparency.getValue()) {
Transparency.setValue(value);
const App::Color &color = Mat.diffuseColor;
if (color != ShapeColor.getValue())
ShapeColor.setValue(Mat.diffuseColor);
pcShapeMaterial->ambientColor.setValue(Mat.ambientColor.r, Mat.ambientColor.g, Mat.ambientColor.b);
pcShapeMaterial->diffuseColor.setValue(Mat.diffuseColor.r, Mat.diffuseColor.g, Mat.diffuseColor.b);
pcShapeMaterial->specularColor.setValue(Mat.specularColor.r, Mat.specularColor.g, Mat.specularColor.b);
pcShapeMaterial->emissiveColor.setValue(Mat.emissiveColor.r, Mat.emissiveColor.g, Mat.emissiveColor.b);
pcShapeMaterial->shininess.setValue(Mat.shininess);
pcShapeMaterial->transparency.setValue(Mat.transparency);
}
setSoMaterial(Mat);
}
else if (prop == &BoundingBox) {
showBoundingBox(BoundingBox.getValue());
@@ -161,15 +164,22 @@ void ViewProviderGeometryObject::onChanged(const App::Property* prop)
ViewProviderDragger::onChanged(prop);
}
void ViewProviderGeometryObject::attach(App::DocumentObject *pcObj)
void ViewProviderGeometryObject::attach(App::DocumentObject* pcObj)
{
ViewProviderDragger::attach(pcObj);
}
void ViewProviderGeometryObject::updateData(const App::Property* prop)
{
std::string propName = prop->getName();
if (propName == "Shape") {
// Reapply the appearance
const App::Material& Mat = ShapeAppearance[0];
setSoMaterial(Mat);
}
if (prop->isDerivedFrom(App::PropertyComplexGeoData::getClassTypeId())) {
Base::BoundBox3d box = static_cast<const App::PropertyComplexGeoData*>(prop)->getBoundingBox();
Base::BoundBox3d box =
static_cast<const App::PropertyComplexGeoData*>(prop)->getBoundingBox();
pcBoundingBox->minBounds.setValue(box.MinX, box.MinY, box.MinZ);
pcBoundingBox->maxBounds.setValue(box.MaxX, box.MaxY, box.MaxZ);
}
@@ -184,11 +194,21 @@ void ViewProviderGeometryObject::updateData(const App::Property* prop)
}
}
}
else if (std::string(prop->getName()) == "ShapeMaterial") {
// Set the appearance from the material
auto geometry = dynamic_cast<App::GeoFeature*>(getObject());
if (geometry) {
auto material = geometry->getMaterialAppearance();
ShapeAppearance.setValue(material);
}
}
ViewProviderDragger::updateData(prop);
}
SoPickedPointList ViewProviderGeometryObject::getPickedPoints(const SbVec2s& pos, const View3DInventorViewer& viewer,bool pickAll) const
SoPickedPointList ViewProviderGeometryObject::getPickedPoints(const SbVec2s& pos,
const View3DInventorViewer& viewer,
bool pickAll) const
{
auto root = new SoSeparator;
root->ref();
@@ -207,7 +227,8 @@ SoPickedPointList ViewProviderGeometryObject::getPickedPoints(const SbVec2s& pos
return rp.getPickedPointList();
}
SoPickedPoint* ViewProviderGeometryObject::getPickedPoint(const SbVec2s& pos, const View3DInventorViewer& viewer) const
SoPickedPoint* ViewProviderGeometryObject::getPickedPoint(const SbVec2s& pos,
const View3DInventorViewer& viewer) const
{
auto root = new SoSeparator;
root->ref();
@@ -223,31 +244,55 @@ SoPickedPoint* ViewProviderGeometryObject::getPickedPoint(const SbVec2s& pos, co
// returns a copy of the point
SoPickedPoint* pick = rp.getPickedPoint();
//return (pick ? pick->copy() : 0); // needs the same instance of CRT under MS Windows
// return (pick ? pick->copy() : 0); // needs the same instance of CRT under MS Windows
return (pick ? new SoPickedPoint(*pick) : nullptr);
}
unsigned long ViewProviderGeometryObject::getBoundColor() const
{
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
unsigned long bbcol = hGrp->GetUnsigned("BoundingBoxColor",4294967295UL); // white (255,255,255)
ParameterGrp::handle hGrp =
App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
unsigned long bbcol =
hGrp->GetUnsigned("BoundingBoxColor", 4294967295UL); // white (255,255,255)
return bbcol;
}
namespace {
void ViewProviderGeometryObject::setSoMaterial(const App::Material& source)
{
pcShapeMaterial->ambientColor.setValue(source.ambientColor.r,
source.ambientColor.g,
source.ambientColor.b);
pcShapeMaterial->diffuseColor.setValue(source.diffuseColor.r,
source.diffuseColor.g,
source.diffuseColor.b);
pcShapeMaterial->specularColor.setValue(source.specularColor.r,
source.specularColor.g,
source.specularColor.b);
pcShapeMaterial->emissiveColor.setValue(source.emissiveColor.r,
source.emissiveColor.g,
source.emissiveColor.b);
pcShapeMaterial->shininess.setValue(source.shininess);
pcShapeMaterial->transparency.setValue(source.transparency);
}
namespace
{
float getBoundBoxFontSize()
{
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
ParameterGrp::handle hGrp =
App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
return hGrp->GetFloat("BoundingBoxFontSize", 10.0);
}
}
} // namespace
void ViewProviderGeometryObject::showBoundingBox(bool show)
{
if (!pcBoundSwitch && show) {
unsigned long bbcol = getBoundColor();
float r,g,b;
r = ((bbcol >> 24) & 0xff) / 255.0; g = ((bbcol >> 16) & 0xff) / 255.0; b = ((bbcol >> 8) & 0xff) / 255.0;
float r, g, b;
r = ((bbcol >> 24) & 0xff) / 255.0;
g = ((bbcol >> 16) & 0xff) / 255.0;
b = ((bbcol >> 8) & 0xff) / 255.0;
pcBoundSwitch = new SoSwitch();
auto pBoundingSep = new SoSeparator();
@@ -284,9 +329,9 @@ void ViewProviderGeometryObject::setSelectable(bool selectable)
sa.setType(Gui::SoFCSelection::getClassTypeId());
sa.apply(pcRoot);
SoPathList & pathList = sa.getPaths();
SoPathList& pathList = sa.getPaths();
for (int i=0;i<pathList.getLength();i++) {
for (int i = 0; i < pathList.getLength(); i++) {
auto selNode = dynamic_cast<SoFCSelection*>(pathList[i]->getTail());
if (selectable) {
if (selNode) {
@@ -303,3 +348,33 @@ void ViewProviderGeometryObject::setSelectable(bool selectable)
}
}
}
PyObject* ViewProviderGeometryObject::getPyObject()
{
if (!pyViewObject) {
pyViewObject = new ViewProviderGeometryObjectPy(this);
}
pyViewObject->IncRef();
return pyViewObject;
}
void ViewProviderGeometryObject::handleChangedPropertyName(Base::XMLReader& reader,
const char* TypeName,
const char* PropName)
{
if (strcmp(PropName, "ShapeColor") == 0
&& strcmp(TypeName, App::PropertyColor::getClassTypeId().getName()) == 0) {
App::PropertyColor prop;
prop.Restore(reader);
ShapeAppearance.setDiffuseColor(prop.getValue());
}
else if (strcmp(PropName, "ShapeMaterial") == 0
&& strcmp(TypeName, App::PropertyMaterial::getClassTypeId().getName()) == 0) {
App::PropertyMaterial prop;
prop.Restore(reader);
ShapeAppearance.setValue(prop.getValue());
}
else {
App::PropertyContainer::handleChangedPropertyName(reader, TypeName, PropName);
}
}