Skip to content

Commit

Permalink
Fix debug color with vertex colors
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Oct 18, 2023
1 parent 23c2780 commit 26954e2
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 71 deletions.
32 changes: 23 additions & 9 deletions exts/cesium.omniverse/mdl/cesium.mdl
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,29 @@ float4 alpha_blend(float4 src, float4 dst) {
return src * float4(src.w, src.w, src.w, 1.0) + dst * (1.0 - src.w);
}

float4 compute_base_color(
gltf_texture_lookup_value imagery_layers_texture,
color debug_color,
gltf_texture_lookup_value base_color_texture,
color base_color_factor,
float base_alpha) {

auto debug_color_float3 = float3(debug_color);
auto base_color_factor_float3 = float3(base_color_factor);

auto base_color = base_color_texture.valid ? base_color_texture.value : float4(1.0);
base_color *= float4(base_color_factor_float3.x, base_color_factor_float3.y, base_color_factor_float3.z, base_alpha);
base_color = alpha_blend(imagery_layers_texture.value, base_color);
base_color *= float4(debug_color_float3.x, debug_color_float3.y, debug_color_float3.z, 1.0);

return base_color;
}

export gltf_texture_lookup_value cesium_texture_lookup(*) [[ anno::hidden() ]] = gltf_texture_lookup();

export material cesium_material(
gltf_texture_lookup_value imagery_layers_texture = gltf_texture_lookup_value(true, float4(0.0)),
uniform color debug_color = color(1.0),
// gltf_material inputs below
gltf_texture_lookup_value base_color_texture = gltf_texture_lookup_value(),
uniform color base_color_factor = color(1.0),
Expand All @@ -29,20 +48,15 @@ export material cesium_material(
uniform float base_alpha = 1.0,
uniform float alpha_cutoff = 0.5
) [[ anno::hidden() ]] = let {
auto base_color_texture_value = base_color_texture.valid ? base_color_texture.value : float4(1.0);
auto final_base_color_texture_value = alpha_blend(imagery_layers_texture.value, base_color_texture_value);
auto final_base_color_texture = gltf_texture_lookup_value(true, final_base_color_texture_value);
auto final_base_color_factor = base_color_factor;

auto base_color = compute_base_color(imagery_layers_texture, debug_color, base_color_texture, base_color_factor, base_alpha);
material base = gltf_material(
base_color_factor: final_base_color_factor,
base_color_texture: final_base_color_texture,
base_color_texture: gltf_texture_lookup_value(true, base_color),
metallic_factor: metallic_factor,
roughness_factor: roughness_factor,
emissive_factor: emissive_factor,
alpha_mode: alpha_mode,
base_alpha: base_alpha,
alpha_cutoff: alpha_cutoff
alpha_cutoff: alpha_cutoff,
vertex_color_name: string("vertexColor")
);

} in material(
Expand Down
2 changes: 0 additions & 2 deletions src/core/include/cesium/omniverse/FabricGeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class FabricGeometry {
FabricGeometry(
const omni::fabric::Path& path,
const FabricGeometryDefinition& geometryDefinition,
bool debugRandomColors,
long stageId);
~FabricGeometry();

Expand Down Expand Up @@ -48,7 +47,6 @@ class FabricGeometry {

const omni::fabric::Path _path;
const FabricGeometryDefinition _geometryDefinition;
const bool _debugRandomColors;
const long _stageId;
};

Expand Down
2 changes: 0 additions & 2 deletions src/core/include/cesium/omniverse/FabricGeometryPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class FabricGeometryPool final : public ObjectPool<FabricGeometry> {
int64_t poolId,
const FabricGeometryDefinition& geometryDefinition,
uint64_t initialCapacity,
bool debugRandomColors,
long stageId);

[[nodiscard]] const FabricGeometryDefinition& getGeometryDefinition() const;
Expand All @@ -24,7 +23,6 @@ class FabricGeometryPool final : public ObjectPool<FabricGeometry> {
private:
const int64_t _poolId;
const FabricGeometryDefinition _geometryDefinition;
const bool _debugRandomColors;
const long _stageId;
};

Expand Down
2 changes: 2 additions & 0 deletions src/core/include/cesium/omniverse/FabricMaterial.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class FabricMaterial {
const FabricMaterialDefinition& materialDefinition,
const pxr::TfToken& defaultTextureAssetPathToken,
const pxr::TfToken& defaultTransparentTextureAssetPathToken,
bool debugRandomColors,
long stageId);
~FabricMaterial();

Expand Down Expand Up @@ -72,6 +73,7 @@ class FabricMaterial {
const FabricMaterialDefinition _materialDefinition;
const pxr::TfToken _defaultTextureAssetPathToken;
const pxr::TfToken _defaultTransparentTextureAssetPathToken;
const bool _debugRandomColors;
const long _stageId;

omni::fabric::Path _shaderPath;
Expand Down
2 changes: 2 additions & 0 deletions src/core/include/cesium/omniverse/FabricMaterialPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class FabricMaterialPool final : public ObjectPool<FabricMaterial> {
uint64_t initialCapacity,
const pxr::TfToken& defaultTextureAssetPathToken,
const pxr::TfToken& defaultTransparentTextureAssetPathToken,
bool debugRandomColors,
long stageId);

[[nodiscard]] const FabricMaterialDefinition& getMaterialDefinition() const;
Expand All @@ -29,6 +30,7 @@ class FabricMaterialPool final : public ObjectPool<FabricMaterial> {
const FabricMaterialDefinition _materialDefinition;
const pxr::TfToken _defaultTextureAssetPathToken;
const pxr::TfToken _defaultTransparentTextureAssetPathToken;
const bool _debugRandomColors;
const long _stageId;
};

Expand Down
6 changes: 2 additions & 4 deletions src/core/include/cesium/omniverse/Tokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ __pragma(warning(push)) __pragma(warning(disable : 4003))
((inputs_base_alpha, "inputs:base_alpha")) \
((inputs_base_color_factor, "inputs:base_color_factor")) \
((inputs_base_color_texture, "inputs:base_color_texture")) \
((inputs_debug_color, "inputs:debug_color")) \
((inputs_emissive_factor, "inputs:emissive_factor")) \
((inputs_excludeFromWhiteMode, "inputs:excludeFromWhiteMode")) \
((inputs_metallic_factor, "inputs:metallic_factor")) \
Expand Down Expand Up @@ -105,8 +106,6 @@ __pragma(warning(push)) __pragma(warning(disable : 4003))
((outputs_mdl_surface, "outputs:mdl:surface")) \
((outputs_mdl_volume, "outputs:mdl:volume")) \
((outputs_out, "outputs:out")) \
((primvars_displayColor, "primvars:displayColor")) \
((primvars_displayOpacity, "primvars:displayOpacity")) \
((primvars_normals, "primvars:normals")) \
((primvars_st_0, "primvars:st_0")) \
((primvars_st_1, "primvars:st_1")) \
Expand Down Expand Up @@ -219,6 +218,7 @@ const omni::fabric::Type inputs_alpha_cutoff(omni::fabric::BaseDataType::eFloat,
const omni::fabric::Type inputs_alpha_mode(omni::fabric::BaseDataType::eInt, 1, 0, omni::fabric::AttributeRole::eNone);
const omni::fabric::Type inputs_base_alpha(omni::fabric::BaseDataType::eFloat, 1, 0, omni::fabric::AttributeRole::eNone);
const omni::fabric::Type inputs_base_color_factor(omni::fabric::BaseDataType::eFloat, 3, 0, omni::fabric::AttributeRole::eColor);
const omni::fabric::Type inputs_debug_color(omni::fabric::BaseDataType::eFloat, 3, 0, omni::fabric::AttributeRole::eNone);
const omni::fabric::Type inputs_emissive_factor(omni::fabric::BaseDataType::eFloat, 3, 0, omni::fabric::AttributeRole::eColor);
const omni::fabric::Type inputs_excludeFromWhiteMode(omni::fabric::BaseDataType::eBool, 1, 0, omni::fabric::AttributeRole::eNone);
const omni::fabric::Type inputs_metallic_factor(omni::fabric::BaseDataType::eFloat, 1, 0, omni::fabric::AttributeRole::eNone);
Expand All @@ -239,8 +239,6 @@ const omni::fabric::Type outputs_out(omni::fabric::BaseDataType::eToken, 1, 0, o
const omni::fabric::Type points(omni::fabric::BaseDataType::eFloat, 3, 1, omni::fabric::AttributeRole::ePosition);
const omni::fabric::Type primvarInterpolations(omni::fabric::BaseDataType::eToken, 1, 1, omni::fabric::AttributeRole::eNone);
const omni::fabric::Type primvars(omni::fabric::BaseDataType::eToken, 1, 1, omni::fabric::AttributeRole::eNone);
const omni::fabric::Type primvars_displayColor(omni::fabric::BaseDataType::eFloat, 3, 1, omni::fabric::AttributeRole::eColor);
const omni::fabric::Type primvars_displayOpacity(omni::fabric::BaseDataType::eFloat, 1, 1, omni::fabric::AttributeRole::eNone);
const omni::fabric::Type primvars_normals(omni::fabric::BaseDataType::eFloat, 3, 1, omni::fabric::AttributeRole::eNormal);
const omni::fabric::Type primvars_st(omni::fabric::BaseDataType::eFloat, 2, 1, omni::fabric::AttributeRole::eTexCoord);
const omni::fabric::Type primvars_vertexColor(omni::fabric::BaseDataType::eFloat, 3, 1, omni::fabric::AttributeRole::eColor);
Expand Down
33 changes: 0 additions & 33 deletions src/core/src/FabricGeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#endif

#include <CesiumGltf/Model.h>
#include <glm/gtc/random.hpp>
#include <omni/fabric/FabricUSD.h>

namespace cesium::omniverse {
Expand Down Expand Up @@ -54,11 +53,9 @@ uint64_t getTexcoordSetCount(const FabricGeometryDefinition& geometryDefinition)
FabricGeometry::FabricGeometry(
const omni::fabric::Path& path,
const FabricGeometryDefinition& geometryDefinition,
bool debugRandomColors,
long stageId)
: _path(path)
, _geometryDefinition(geometryDefinition)
, _debugRandomColors(debugRandomColors)
, _stageId(stageId) {
if (stageDestroyed()) {
return;
Expand Down Expand Up @@ -137,8 +134,6 @@ void FabricGeometry::initialize() {
attributes.addAttribute(FabricTypes::_worldVisibility, FabricTokens::_worldVisibility);
attributes.addAttribute(FabricTypes::primvars, FabricTokens::primvars);
attributes.addAttribute(FabricTypes::primvarInterpolations, FabricTokens::primvarInterpolations);
attributes.addAttribute(FabricTypes::primvars_displayColor, FabricTokens::primvars_displayColor);
attributes.addAttribute(FabricTypes::primvars_displayOpacity, FabricTokens::primvars_displayOpacity);
attributes.addAttribute(FabricTypes::Mesh, FabricTokens::Mesh);
attributes.addAttribute(FabricTypes::_cesium_tilesetId, FabricTokens::_cesium_tilesetId);
attributes.addAttribute(FabricTypes::_cesium_localToEcefTransform, FabricTokens::_cesium_localToEcefTransform);
Expand Down Expand Up @@ -176,9 +171,6 @@ void FabricGeometry::initialize() {
size_t primvarIndexNormal = 0;
size_t primvarIndexVertexColor = 0;

const size_t primvarIndexDisplayColor = primvarsCount++;
const size_t primvarIndexDisplayOpacity = primvarsCount++;

std::vector<uint64_t> primvarIndexStArray;
primvarIndexStArray.reserve(texcoordSetCount);

Expand All @@ -196,20 +188,12 @@ void FabricGeometry::initialize() {

srw.setArrayAttributeSize(_path, FabricTokens::primvars, primvarsCount);
srw.setArrayAttributeSize(_path, FabricTokens::primvarInterpolations, primvarsCount);
srw.setArrayAttributeSize(_path, FabricTokens::primvars_displayColor, 1);
srw.setArrayAttributeSize(_path, FabricTokens::primvars_displayOpacity, 1);

// clang-format off
auto primvarsFabric = srw.getArrayAttributeWr<omni::fabric::TokenC>(_path, FabricTokens::primvars);
auto primvarInterpolationsFabric = srw.getArrayAttributeWr<omni::fabric::TokenC>(_path, FabricTokens::primvarInterpolations);
// clang-format on

primvarsFabric[primvarIndexDisplayColor] = FabricTokens::primvars_displayColor;
primvarsFabric[primvarIndexDisplayOpacity] = FabricTokens::primvars_displayOpacity;

primvarInterpolationsFabric[primvarIndexDisplayColor] = FabricTokens::constant;
primvarInterpolationsFabric[primvarIndexDisplayOpacity] = FabricTokens::constant;

for (uint64_t i = 0; i < texcoordSetCount; i++) {
primvarsFabric[primvarIndexStArray[i]] = FabricTokens::primvars_st_n[i];
primvarInterpolationsFabric[primvarIndexStArray[i]] = FabricTokens::vertex;
Expand Down Expand Up @@ -241,8 +225,6 @@ void FabricGeometry::reset() {
auto worldPositionFabric = srw.getAttributeWr<pxr::GfVec3d>(_path, FabricTokens::_worldPosition);
auto worldOrientationFabric = srw.getAttributeWr<pxr::GfQuatf>(_path, FabricTokens::_worldOrientation);
auto worldScaleFabric = srw.getAttributeWr<pxr::GfVec3f>(_path, FabricTokens::_worldScale);
auto displayColorFabric = srw.getArrayAttributeWr<pxr::GfVec3f>(_path, FabricTokens::primvars_displayColor);
auto displayOpacityFabric = srw.getArrayAttributeWr<float>(_path, FabricTokens::primvars_displayOpacity);
// clang-format on

*extentFabric = DEFAULT_EXTENT;
Expand All @@ -252,8 +234,6 @@ void FabricGeometry::reset() {
*worldPositionFabric = DEFAULT_POSITION;
*worldOrientationFabric = DEFAULT_ORIENTATION;
*worldScaleFabric = DEFAULT_SCALE;
displayColorFabric[0] = DEFAULT_VERTEX_COLOR;
displayOpacityFabric[0] = DEFAULT_VERTEX_OPACITY;

FabricUtil::setTilesetId(_path, NO_TILESET_ID);

Expand Down Expand Up @@ -462,8 +442,6 @@ void FabricGeometry::setGeometry(
auto worldPositionFabric = srw.getAttributeWr<pxr::GfVec3d>(_path, FabricTokens::_worldPosition);
auto worldOrientationFabric = srw.getAttributeWr<pxr::GfQuatf>(_path, FabricTokens::_worldOrientation);
auto worldScaleFabric = srw.getAttributeWr<pxr::GfVec3f>(_path, FabricTokens::_worldScale);
auto displayColorFabric = srw.getArrayAttributeWr<pxr::GfVec3f>(_path, FabricTokens::primvars_displayColor);
auto displayOpacityFabric = srw.getArrayAttributeWr<float>(_path, FabricTokens::primvars_displayOpacity);
// clang-format on

*extentFabric = localExtent;
Expand All @@ -473,17 +451,6 @@ void FabricGeometry::setGeometry(
*worldOrientationFabric = worldOrientation;
*worldScaleFabric = worldScale;

if (_debugRandomColors) {
const auto r = glm::linearRand(0.0f, 1.0f);
const auto g = glm::linearRand(0.0f, 1.0f);
const auto b = glm::linearRand(0.0f, 1.0f);
displayColorFabric[0] = pxr::GfVec3f(r, g, b);
} else {
displayColorFabric[0] = DEFAULT_VERTEX_COLOR;
}

displayOpacityFabric[0] = DEFAULT_VERTEX_OPACITY;

FabricUtil::setTilesetId(_path, tilesetId);
}

Expand Down
4 changes: 1 addition & 3 deletions src/core/src/FabricGeometryPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ FabricGeometryPool::FabricGeometryPool(
int64_t poolId,
const FabricGeometryDefinition& geometryDefinition,
uint64_t initialCapacity,
bool debugRandomColors,
long stageId)
: ObjectPool<FabricGeometry>()
, _poolId(poolId)
, _geometryDefinition(geometryDefinition)
, _debugRandomColors(debugRandomColors)
, _stageId(stageId) {
setCapacity(initialCapacity);
}
Expand All @@ -25,7 +23,7 @@ const FabricGeometryDefinition& FabricGeometryPool::getGeometryDefinition() cons
std::shared_ptr<FabricGeometry> FabricGeometryPool::createObject(uint64_t objectId) {
const auto pathStr = fmt::format("/fabric_geometry_pool_{}_object_{}", _poolId, objectId);
const auto path = omni::fabric::Path(pathStr.c_str());
return std::make_shared<FabricGeometry>(path, _geometryDefinition, _debugRandomColors, _stageId);
return std::make_shared<FabricGeometry>(path, _geometryDefinition, _stageId);
}

void FabricGeometryPool::setActive(std::shared_ptr<FabricGeometry> geometry, bool active) {
Expand Down
32 changes: 18 additions & 14 deletions src/core/src/FabricMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
#include "cesium/omniverse/Tokens.h"
#include "cesium/omniverse/UsdUtil.h"

#include <glm/gtc/random.hpp>
#include <omni/fabric/FabricUSD.h>
#include <spdlog/fmt/fmt.h>

namespace cesium::omniverse {

namespace {

const auto DEFAULT_DEBUG_COLOR = pxr::GfVec3f(1.0f, 1.0f, 1.0f);

uint64_t getImageryLayerCount(const FabricMaterialDefinition& materialDefinition) {
uint64_t imageryLayerCount = materialDefinition.getImageryLayerCount();

Expand All @@ -31,18 +35,21 @@ uint64_t getImageryLayerCount(const FabricMaterialDefinition& materialDefinition

return imageryLayerCount;
}

} // namespace

FabricMaterial::FabricMaterial(
const omni::fabric::Path& path,
const FabricMaterialDefinition& materialDefinition,
const pxr::TfToken& defaultTextureAssetPathToken,
const pxr::TfToken& defaultTransparentTextureAssetPathToken,
bool debugRandomColors,
long stageId)
: _materialPath(path)
, _materialDefinition(materialDefinition)
, _defaultTextureAssetPathToken(defaultTextureAssetPathToken)
, _defaultTransparentTextureAssetPathToken(defaultTransparentTextureAssetPathToken)
, _debugRandomColors(debugRandomColors)
, _stageId(stageId) {

if (stageDestroyed()) {
Expand Down Expand Up @@ -140,15 +147,14 @@ void FabricMaterial::createMaterial(const omni::fabric::Path& materialPath) {
}

void FabricMaterial::createShader(const omni::fabric::Path& shaderPath, const omni::fabric::Path& materialPath) {
const auto hasVertexColors = _materialDefinition.hasVertexColors();

auto srw = UsdUtil::getFabricStageReaderWriter();

srw.createPrim(shaderPath);

FabricAttributesBuilder attributes;

// clang-format off
attributes.addAttribute(FabricTypes::inputs_debug_color, FabricTokens::inputs_debug_color);
attributes.addAttribute(FabricTypes::inputs_alpha_cutoff, FabricTokens::inputs_alpha_cutoff);
attributes.addAttribute(FabricTypes::inputs_alpha_mode, FabricTokens::inputs_alpha_mode);
attributes.addAttribute(FabricTypes::inputs_base_alpha, FabricTokens::inputs_base_alpha);
Expand All @@ -167,10 +173,6 @@ void FabricMaterial::createShader(const omni::fabric::Path& shaderPath, const om
attributes.addAttribute(FabricTypes::_cesium_tilesetId, FabricTokens::_cesium_tilesetId);
// clang-format on

if (hasVertexColors) {
attributes.addAttribute(FabricTypes::inputs_vertex_color_name, FabricTokens::inputs_vertex_color_name);
}

attributes.createAttributes(shaderPath);

srw.setArrayAttributeSize(shaderPath, FabricTokens::_paramColorSpace, 0);
Expand All @@ -189,14 +191,6 @@ void FabricMaterial::createShader(const omni::fabric::Path& shaderPath, const om
infoMdlSourceAssetFabric->resolvedPath = pxr::TfToken();
*infoMdlSourceAssetSubIdentifierFabric = FabricTokens::cesium_material;

if (hasVertexColors) {
const auto vertexColorPrimvarNameSize = pxr::UsdTokens->vertexColor.GetString().size();
srw.setArrayAttributeSize(shaderPath, FabricTokens::inputs_vertex_color_name, vertexColorPrimvarNameSize);
auto vertexColorNameFabric =
srw.getArrayAttributeWr<uint8_t>(shaderPath, FabricTokens::inputs_vertex_color_name);
memcpy(vertexColorNameFabric.data(), pxr::UsdTokens->vertexColor.GetText(), vertexColorPrimvarNameSize);
}

// Connect the material terminals to the shader.
srw.createConnection(
materialPath,
Expand Down Expand Up @@ -384,6 +378,7 @@ void FabricMaterial::clearImageryLayers() {
void FabricMaterial::setShaderValues(const omni::fabric::Path& shaderPath, const MaterialInfo& materialInfo) {
auto srw = UsdUtil::getFabricStageReaderWriter();

auto debugColorFabric = srw.getAttributeWr<pxr::GfVec3f>(shaderPath, FabricTokens::inputs_debug_color);
auto alphaCutoffFabric = srw.getAttributeWr<float>(shaderPath, FabricTokens::inputs_alpha_cutoff);
auto alphaModeFabric = srw.getAttributeWr<int>(shaderPath, FabricTokens::inputs_alpha_mode);
auto baseAlphaFabric = srw.getAttributeWr<float>(shaderPath, FabricTokens::inputs_base_alpha);
Expand All @@ -399,6 +394,15 @@ void FabricMaterial::setShaderValues(const omni::fabric::Path& shaderPath, const
*emissiveFactorFabric = UsdUtil::glmToUsdVector(glm::fvec3(materialInfo.emissiveFactor));
*metallicFactorFabric = static_cast<float>(materialInfo.metallicFactor);
*roughnessFactorFabric = static_cast<float>(materialInfo.roughnessFactor);

if (_debugRandomColors) {
const auto r = glm::linearRand(0.0f, 1.0f);
const auto g = glm::linearRand(0.0f, 1.0f);
const auto b = glm::linearRand(0.0f, 1.0f);
*debugColorFabric = pxr::GfVec3f(r, g, b);
} else {
*debugColorFabric = DEFAULT_DEBUG_COLOR;
}
}

void FabricMaterial::setTextureValues(
Expand Down
Loading

0 comments on commit 26954e2

Please sign in to comment.