Skip to content

Commit

Permalink
Merge pull request #298 from favreau/master
Browse files Browse the repository at this point in the history
Fixed chameleon mode and node Id attributes in Material
  • Loading branch information
favreau authored Sep 6, 2023
2 parents 72f8343 + 812c808 commit 1dd4c23
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -590,14 +590,6 @@ void AbstractCircuitLoader::_importMeshes(const PropertyMap &properties, Model &
++meshIndex;
callback.updateProgress("Loading cells as meshes...", (float)meshIndex / (float)gids.size());
}
// Add custom properties to materials
for (auto &material : model.getMaterials())
{
PropertyMap props;
props.setProperty({MATERIAL_PROPERTY_CAST_USER_DATA, true});
props.setProperty({MATERIAL_PROPERTY_CLIPPING_MODE, static_cast<int>(MaterialClippingMode::no_clipping)});
material.second->setProperties(props);
}
}
#else
void AbstractCircuitLoader::_importMeshes(const PropertyMap & /*props*/, Model & /*model*/,
Expand Down
7 changes: 1 addition & 6 deletions bioexplorer/backend/science/common/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,7 @@ void Node::_setMaterialExtraAttributes()
{
auto materials = _modelDescriptor->getModel().getMaterials();
for (auto& material : materials)
{
PropertyMap props;
props.setProperty({MATERIAL_PROPERTY_CHAMELEON_MODE, static_cast<int>(MaterialChameleonMode::receiver)});
props.setProperty({MATERIAL_PROPERTY_NODE_ID, static_cast<int>(_uuid)});
material.second->updateProperties(props);
}
material.second->setNodeId(_uuid);
}

} // namespace common
Expand Down
11 changes: 1 addition & 10 deletions bioexplorer/backend/science/common/ThreadSafeContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,7 @@ void ThreadSafeContainer::_finalizeSDFGeometries()
void ThreadSafeContainer::_commitMaterials()
{
for (const auto materialId : _materialIds)
{
Vector3f color{1.f, 1.f, 1.f};
auto material = _model.createMaterial(materialId, std::to_string(materialId));

material->setDiffuseColor(color);
material->setSpecularColor(color);
material->setSpecularExponent(100.f);
material->setShadingMode(MaterialShadingMode::undefined_shading_mode);
material->setUserParameter(1.f);
}
_model.createMaterial(materialId, std::to_string(materialId));
}

void ThreadSafeContainer::_commitSpheresToModel()
Expand Down
9 changes: 4 additions & 5 deletions bioexplorer/backend/science/io/CacheLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,11 @@ ModelDescriptorPtr CacheLoader::_importModel(std::stringstream& buffer, const in

int32_t chameleonMode;
buffer.read((char*)&chameleonMode, sizeof(int32_t));
props.setProperty({MATERIAL_PROPERTY_CHAMELEON_MODE, chameleonMode});
material->setChameleonMode(static_cast<MaterialChameleonMode>(chameleonMode));

int32_t nodeId;
buffer.read((char*)&nodeId, sizeof(int32_t));
props.setProperty({MATERIAL_PROPERTY_NODE_ID, nodeId});
material->updateProperties(props);
material->setNodeId(nodeId);
}

uint64_t bufferSize{0};
Expand Down Expand Up @@ -547,7 +546,7 @@ bool CacheLoader::_exportModel(const ModelDescriptorPtr modelDescriptor, std::st
int32_t chameleonMode = MaterialChameleonMode::undefined_chameleon_mode;
try
{
shadingMode = material.second->getProperty<int32_t>(MATERIAL_PROPERTY_CHAMELEON_MODE);
shadingMode = material.second->getChameleonMode();
}
catch (const std::runtime_error&)
{
Expand All @@ -557,7 +556,7 @@ bool CacheLoader::_exportModel(const ModelDescriptorPtr modelDescriptor, std::st
int32_t nodeId = 0;
try
{
shadingMode = material.second->getProperty<int32_t>(MATERIAL_PROPERTY_NODE_ID);
shadingMode = material.second->getNodeId();
}
catch (const std::runtime_error&)
{
Expand Down
11 changes: 3 additions & 8 deletions bioexplorer/backend/science/molecularsystems/Molecule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,8 @@ void Molecule::_buildModel(const std::string& assemblyName, const std::string& n
if (it != atomColorMap.end())
rgb = (*it).second;

core::PropertyMap props;
props.setProperty(
{MATERIAL_PROPERTY_CHAMELEON_MODE, static_cast<int>(MaterialChameleonMode::undefined_chameleon_mode)});
props.setProperty({MATERIAL_PROPERTY_NODE_ID, static_cast<int>(_uuid)});
material->setNodeId(_uuid);
material->setDiffuseColor({rgb.r / 255.0, rgb.g / 255.0, rgb.b / 255.0});
material->updateProperties(props);
}

_modelDescriptor = std::make_shared<ModelDescriptor>(std::move(model), name, header, metadata);
Expand Down Expand Up @@ -355,10 +351,9 @@ void Molecule::_buildModel(const std::string& assemblyName, const std::string& n
rgb = (*it).second;

core::PropertyMap props;
props.setProperty({MATERIAL_PROPERTY_CHAMELEON_MODE, static_cast<int>(MaterialChameleonMode::emitter)});
props.setProperty({MATERIAL_PROPERTY_NODE_ID, static_cast<int>(_uuid)});
material->setChameleonMode(MaterialChameleonMode::emitter);
material->setNodeId(_uuid);
material->setDiffuseColor({rgb.r / 255.0, rgb.g / 255.0, rgb.b / 255.0});
material->updateProperties(props);
}
break;
}
Expand Down
4 changes: 0 additions & 4 deletions bioexplorer/backend/science/molecularsystems/RNASequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,7 @@ void RNASequence::_buildRNAAsCurve(const Quaterniond& rotation)
for (const auto& nucleotid : nucleotidMap)
{
auto material = model->createMaterial(materialId, nucleotid.second.name);
core::PropertyMap props;
props.setProperty(
{MATERIAL_PROPERTY_CHAMELEON_MODE, static_cast<int>(MaterialChameleonMode::undefined_chameleon_mode)});
material->setDiffuseColor(nucleotid.second.color);
material->updateProperties(props);
++materialId;
}
PLUGIN_INFO(3, "Created " << materialId << " materials");
Expand Down
2 changes: 0 additions & 2 deletions platform/core/common/CommonTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@
#define MATERIAL_PROPERTY_CLIPPING_MODE "clipping_mode"
#define MATERIAL_PROPERTY_CHAMELEON_MODE "chameleon_mode"
#define MATERIAL_PROPERTY_NODE_ID "node_id"
#define MATERIAL_PROPERTY_SKYBOX "skybox"
#define MATERIAL_PROPERTY_APPLY_SIMULATION "apply_simulation"

enum CameraStereoMode
{
Expand Down
11 changes: 11 additions & 0 deletions platform/core/engineapi/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ class Material : public PropertyObject
*/
PLATFORM_API MaterialChameleonMode getChameleonMode() const { return _chameleonMode; }

/**
* @brief Sets the cast user data of the material
* @param The value to set for the cast user data
*/
PLATFORM_API void setNodeId(const int32_t value) { _updateValue(_nodeId, value); }
/**
* @brief Returns the cast user data of the material
*/
PLATFORM_API int32_t getNodeId() const { return _nodeId; }

/**
* @brief Returns the texture descriptors of the material.
*/
Expand Down Expand Up @@ -259,6 +269,7 @@ class Material : public PropertyObject
MaterialShadingMode _shadingMode{MaterialShadingMode::undefined_shading_mode}; // The shading mode of the material
bool _castUserData{false}; // The cast user data of the material
MaterialClippingMode _clippingMode{MaterialClippingMode::no_clipping}; // The clipping mode of the material
int32_t _nodeId; // ID attached to the material
MaterialChameleonMode _chameleonMode{MaterialChameleonMode::undefined_chameleon_mode}; // The chameleon mode of the
// material

Expand Down
10 changes: 3 additions & 7 deletions platform/engines/ospray/OSPRayMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ void OSPRayMaterial::commit()
if (!_ospMaterial || !isModified())
return;

if (getCurrentType() == "simulation")
osphelper::set(_ospMaterial, MATERIAL_PROPERTY_APPLY_SIMULATION, true);
else
ospRemoveParam(_ospMaterial, MATERIAL_PROPERTY_APPLY_SIMULATION);

osphelper::set(_ospMaterial, MATERIAL_PROPERTY_DIFFUSE_COLOR, Vector3f(_diffuseColor));
osphelper::set(_ospMaterial, MATERIAL_PROPERTY_SPECULAR_COLOR, Vector3f(_specularColor));
osphelper::set(_ospMaterial, MATERIAL_PROPERTY_SPECULAR_INDEX, static_cast<float>(_specularExponent));
Expand All @@ -74,9 +69,10 @@ void OSPRayMaterial::commit()
osphelper::set(_ospMaterial, MATERIAL_PROPERTY_GLOSSINESS, static_cast<float>(_glossiness));
osphelper::set(_ospMaterial, MATERIAL_PROPERTY_SHADING_MODE, static_cast<MaterialShadingMode>(_shadingMode));
osphelper::set(_ospMaterial, MATERIAL_PROPERTY_USER_PARAMETER, static_cast<float>(_userParameter));
osphelper::set(_ospMaterial, MATERIAL_PROPERTY_CAST_USER_DATA, static_cast<bool>(_castUserData));
osphelper::set(_ospMaterial, MATERIAL_PROPERTY_CAST_USER_DATA, static_cast<int32_t>(_castUserData));
osphelper::set(_ospMaterial, MATERIAL_PROPERTY_CLIPPING_MODE, static_cast<MaterialClippingMode>(_clippingMode));
osphelper::set(_ospMaterial, MATERIAL_PROPERTY_SKYBOX, _isBackGroundMaterial);
osphelper::set(_ospMaterial, MATERIAL_PROPERTY_CHAMELEON_MODE, static_cast<MaterialChameleonMode>(_chameleonMode));
osphelper::set(_ospMaterial, MATERIAL_PROPERTY_NODE_ID, static_cast<int32_t>(_nodeId));

// Properties
toOSPRayProperties(*this, _ospMaterial);
Expand Down
24 changes: 11 additions & 13 deletions platform/engines/ospray/ispc/render/AdvancedRenderer.ispc
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,7 @@ inline bool launchRandomRay(const uniform AdvancedRenderer* uniform self, varyin
backgroundColor = make_vec3f(0.f);

if (dot(randomDirection, normal) < 0.f)
// Invert direction of random ray direction is opposite to surface
// normal
// Invert direction of random ray direction is opposite to surface normal
randomDirection = neg(randomDirection);

Ray randomRay;
Expand Down Expand Up @@ -465,7 +464,7 @@ inline vec4f getVolumeContribution(Volume* uniform volume, const uniform Advance
float shadowIntensity = 0.f;

// Ray marching
uint32 shadingOccurence = 0;
uint32 shadingOccurrence = 0;
for (float t = t0 + epsilon /** (sample.sampleID.z % 100)*/; t < t1 && pathColor.w < 1.f; t += epsilon)
{
const vec3f point = ray.org + t * ray.dir;
Expand All @@ -480,7 +479,7 @@ inline vec4f getVolumeContribution(Volume* uniform volume, const uniform Advance
// sampling threshold
continue;

if (shadingOccurence == 0)
if (shadingOccurrence == 0)
{
firstIntersection = t;
ray.t = t;
Expand All @@ -489,23 +488,23 @@ inline vec4f getVolumeContribution(Volume* uniform volume, const uniform Advance
((t1 - t0) * 0.01f);
}

// Adapt sampling rate to shading occurence
epsilon = (volume->samplingStep + shadingOccurence) / volume->samplingRate;
// Adapt sampling rate to shading occurrence
epsilon = (volume->samplingStep + shadingOccurrence) / volume->samplingRate;

// Look up the color associated with the volume sample
vec3f volumeSampleColor = volume->transferFunction->getColorForValue(volume->transferFunction, volumeSample);

// Voxel shading
const bool firstShadingOccurence = shadingOccurence == 0;
const bool shade = volume->singleShade ? firstShadingOccurence : true;
const bool firstShadingOccurrence = shadingOccurrence == 0;
const bool shade = volume->singleShade ? firstShadingOccurrence : true;

vec3f gradient;
DifferentialGeometry dg;
const bool shadingEnabled = (shade && volume->gradientShadingEnabled);

const bool aoEnabled = (firstShadingOccurence && self->giStrength > 0.f);
const bool aoEnabled = (firstShadingOccurrence && self->giStrength > 0.f);

const bool shadowsEnabled = (firstShadingOccurence && self->shadows > 0.f);
const bool shadowsEnabled = (firstShadingOccurrence && self->shadows > 0.f);

if (shadingEnabled || aoEnabled)
{
Expand Down Expand Up @@ -558,7 +557,7 @@ inline vec4f getVolumeContribution(Volume* uniform volume, const uniform Advance
// Compose color with according alpha correction
composite(make_vec4f(volumeSampleColor, sampleOpacity), pathColor, self->volumeAlphaCorrection * epsilon);

++shadingOccurence;
++shadingOccurrence;
}

// Apply shadow to RGB values only
Expand Down Expand Up @@ -990,8 +989,7 @@ inline vec3f AdvancedRenderer_shadeRay(const uniform AdvancedRenderer* uniform s
{
float totalOpacity = 0.f;

// Shading attributes store all color contributions for the
// current ray
// Shading attributes store all color contributions for the current ray
ShadingAttributes attributes;
initializeShadingAttributes(self, attributes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,4 @@ void AdvancedMaterial::commit()

OSP_REGISTER_MATERIAL(basic, AdvancedMaterial, default);
OSP_REGISTER_MATERIAL(advanced, AdvancedMaterial, default);

} // namespace core
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ struct AdvancedMaterial : public ospray::Material
/*! Clipping mode applied to geometry */
MaterialChameleonMode chameleonMode{MaterialChameleonMode::undefined_chameleon_mode};

std::string toString() const override { return "core::AdvancedMaterial"; }
std::string toString() const override { return "default"; }

void commit() override;
};
Expand Down

0 comments on commit 1dd4c23

Please sign in to comment.