From cb2d9ad00805bb49125c59596aba8de8bdb3a44e Mon Sep 17 00:00:00 2001 From: Sean Lilley Date: Wed, 18 Oct 2023 15:37:39 -0400 Subject: [PATCH] Add imagery layer support --- exts/cesium.omniverse/mdl/cesium.mdl | 21 +++++++++++++++++ src/core/include/cesium/omniverse/Tokens.h | 3 +++ src/core/src/FabricMaterial.cpp | 27 ++++++++++++++++------ src/core/src/FabricUtil.cpp | 3 ++- 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/exts/cesium.omniverse/mdl/cesium.mdl b/exts/cesium.omniverse/mdl/cesium.mdl index ec65fb650..2357263f3 100644 --- a/exts/cesium.omniverse/mdl/cesium.mdl +++ b/exts/cesium.omniverse/mdl/cesium.mdl @@ -91,6 +91,27 @@ export color cesium_base_color_texture( return base_color_texture.valid ? color(base_color_texture.value.x, base_color_texture.value.y, base_color_texture.value.z) : color(0.0); } +export color cesium_imagery_layer( + gltf_texture_lookup_value imagery_layer + [[ + anno::hidden() + ]], + int imagery_layer_index + [[ + anno::unused() + ]] +) +[[ + anno::display_name("Imagery layer lookup color"), + anno::description("Returns the color of the imagery layer at the given index. Returns [0, 0, 0] if the imagery layer does not exist."), + anno::author("Cesium GS Inc."), + anno::in_group("Cesium functions"), + anno::ui_order(300) +]] +{ + return imagery_layer.valid ? color(imagery_layer.value.x, imagery_layer.value.y, imagery_layer.value.z) : color(0.0); +} + float4 alpha_blend(float4 src, float4 dst) { return src * float4(src.w, src.w, src.w, 1.0) + dst * (1.0 - src.w); } diff --git a/src/core/include/cesium/omniverse/Tokens.h b/src/core/include/cesium/omniverse/Tokens.h index eaeed4965..059921983 100644 --- a/src/core/include/cesium/omniverse/Tokens.h +++ b/src/core/include/cesium/omniverse/Tokens.h @@ -25,6 +25,7 @@ __pragma(warning(push)) __pragma(warning(disable : 4003)) (extent) \ (faceVertexCounts) \ (faceVertexIndices) \ + (imagery_layer) \ (imagery_layer_0) \ (imagery_layer_1) \ (imagery_layer_2) \ @@ -81,6 +82,7 @@ __pragma(warning(push)) __pragma(warning(disable : 4003)) ((inputs_scale, "inputs:scale")) \ ((inputs_tex_coord_index, "inputs:tex_coord_index")) \ ((inputs_texture, "inputs:texture")) \ + ((inputs_imagery_layer, "inputs:imagery_layer")) \ ((inputs_imagery_layer_0, "inputs:imagery_layer_0")) \ ((inputs_imagery_layer_1, "inputs:imagery_layer_1")) \ ((inputs_imagery_layer_2, "inputs:imagery_layer_2")) \ @@ -98,6 +100,7 @@ __pragma(warning(push)) __pragma(warning(disable : 4003)) ((inputs_imagery_layer_14, "inputs:imagery_layer_14")) \ ((inputs_imagery_layer_15, "inputs:imagery_layer_15")) \ ((inputs_imagery_layers_count, "inputs:imagery_layers_count")) \ + ((inputs_imagery_layer_index, "inputs:imagery_layer_index")) \ ((inputs_imagery_layers_texture, "inputs:imagery_layers_texture")) \ ((inputs_vertex_color_name, "inputs:vertex_color_name")) \ ((inputs_wrap_s, "inputs:wrap_s")) \ diff --git a/src/core/src/FabricMaterial.cpp b/src/core/src/FabricMaterial.cpp index bc686f4e6..dfd77f51d 100644 --- a/src/core/src/FabricMaterial.cpp +++ b/src/core/src/FabricMaterial.cpp @@ -105,11 +105,12 @@ void FabricMaterial::initialize() { } const auto imageryLayerCount = getImageryLayerCount(_materialDefinition); + _imageryLayerPaths.resize(imageryLayerCount); if (imageryLayerCount == 1) { const auto imageryLayerPath = FabricUtil::joinPaths(materialPath, FabricTokens::imagery_layer_n[0]); createTexture(imageryLayerPath, shaderPath, FabricTokens::inputs_imagery_layers_texture); - _imageryLayerPaths.push_back({imageryLayerPath}); + _imageryLayerPaths[0].push_back(imageryLayerPath); _allPaths.push_back(imageryLayerPath); } else if (imageryLayerCount > 1) { const auto imageryLayerResolverPath = FabricUtil::joinPaths(materialPath, FabricTokens::imagery_layer_resolver); @@ -117,11 +118,10 @@ void FabricMaterial::initialize() { imageryLayerResolverPath, shaderPath, FabricTokens::inputs_imagery_layers_texture, imageryLayerCount); _allPaths.push_back(imageryLayerResolverPath); - _imageryLayerPaths.reserve(imageryLayerCount); for (uint64_t i = 0; i < imageryLayerCount; i++) { const auto imageryLayerPath = FabricUtil::joinPaths(materialPath, FabricTokens::imagery_layer_n[i]); createTexture(imageryLayerPath, imageryLayerResolverPath, FabricTokens::inputs_imagery_layer_n[i]); - _imageryLayerPaths.push_back({imageryLayerPath}); + _imageryLayerPaths[i].push_back(imageryLayerPath); _allPaths.push_back(imageryLayerPath); } } @@ -138,6 +138,9 @@ void FabricMaterial::initializeFromExistingMaterial(const omni::fabric::Path& sr const auto dstPaths = FabricUtil::copyMaterial(srcMaterialPath, dstMaterialPath); + const auto imageryLayerCount = getImageryLayerCount(_materialDefinition); + _imageryLayerPaths.resize(imageryLayerCount); + for (const auto& dstPath : dstPaths) { srw.createAttribute(dstPath, FabricTokens::_cesium_tilesetId, FabricTypes::_cesium_tilesetId); _allPaths.push_back(dstPath); @@ -147,12 +150,22 @@ void FabricMaterial::initializeFromExistingMaterial(const omni::fabric::Path& sr if (mdlIdentifier == FabricTokens::cesium_base_color_texture) { if (_materialDefinition.hasBaseColorTexture()) { // Create a base color texture node to fill the empty slot - const auto baseColorTexturePath = - FabricUtil::joinPaths(dstMaterialPath, FabricTokens::base_color_texture); + const auto baseColorTexturePath = FabricUtil::joinPaths(dstPath, FabricTokens::base_color_texture); createTexture(baseColorTexturePath, dstPath, FabricTokens::inputs_base_color_texture); - _allPaths.push_back(baseColorTexturePath); _baseColorTexturePaths.push_back(baseColorTexturePath); - FabricResourceManager::getInstance().retainPath(baseColorTexturePath); + _allPaths.push_back(baseColorTexturePath); + } + } else if (mdlIdentifier == FabricTokens::cesium_imagery_layer) { + const auto imageryLayerIndexFabric = + srw.getAttributeRd(dstPath, FabricTokens::inputs_imagery_layer_index); + const auto imageryLayerIndex = + static_cast(imageryLayerIndexFabric == nullptr ? 0 : *imageryLayerIndexFabric); + + if (imageryLayerIndex < imageryLayerCount) { + const auto imageryLayerPath = FabricUtil::joinPaths(dstPath, FabricTokens::imagery_layer); + createTexture(imageryLayerPath, dstPath, FabricTokens::inputs_imagery_layer); + _imageryLayerPaths[imageryLayerIndex].push_back(imageryLayerPath); + _allPaths.push_back(imageryLayerPath); } } } diff --git a/src/core/src/FabricUtil.cpp b/src/core/src/FabricUtil.cpp index 8f3f12c63..3a92fca7a 100644 --- a/src/core/src/FabricUtil.cpp +++ b/src/core/src/FabricUtil.cpp @@ -877,7 +877,8 @@ bool materialHasCesiumNodes(const omni::fabric::Path& path) { } bool isCesiumNode(const omni::fabric::Token& mdlIdentifier) { - return mdlIdentifier == FabricTokens::cesium_base_color_texture; + return mdlIdentifier == FabricTokens::cesium_base_color_texture || + mdlIdentifier == FabricTokens::cesium_imagery_layer; } omni::fabric::Token getMdlIdentifier(const omni::fabric::Path& path) {