diff --git a/exts/cesium.omniverse/mdl/cesium.mdl b/exts/cesium.omniverse/mdl/cesium.mdl index edb7a407d..15857b2ce 100644 --- a/exts/cesium.omniverse/mdl/cesium.mdl +++ b/exts/cesium.omniverse/mdl/cesium.mdl @@ -203,7 +203,7 @@ float4 alpha_blend(float4 src, float4 dst) { } float4 compute_base_color( - gltf_texture_lookup_value imagery_layers_texture, + gltf_texture_lookup_value imagery_layer, float4 tile_color, gltf_texture_lookup_value base_color_texture, color base_color_factor, @@ -214,7 +214,7 @@ float4 compute_base_color( 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 *= ::scene::data_lookup_float4("COLOR_0", float4(1.0)); - base_color = alpha_blend(imagery_layers_texture.value, base_color); + base_color = alpha_blend(imagery_layer.value, base_color); base_color *= tile_color; return base_color; @@ -251,7 +251,7 @@ export gltf_texture_lookup_value cesium_internal_imagery_layer_lookup( } export material cesium_internal_material( - gltf_texture_lookup_value imagery_layers_texture = gltf_texture_lookup_value(true, float4(0.0)), + gltf_texture_lookup_value imagery_layer = gltf_texture_lookup_value(true, float4(0.0)), uniform float4 tile_color = float4(1.0), // gltf_material inputs below gltf_texture_lookup_value base_color_texture = gltf_texture_lookup_value(), @@ -263,7 +263,7 @@ export material cesium_internal_material( uniform float base_alpha = 1.0, uniform float alpha_cutoff = 0.5 ) [[ anno::hidden() ]] = let { - auto base_color = compute_base_color(imagery_layers_texture, tile_color, base_color_texture, base_color_factor, base_alpha); + auto base_color = compute_base_color(imagery_layer, tile_color, base_color_texture, base_color_factor, base_alpha); material base = gltf_material( base_color_texture: gltf_texture_lookup_value(true, base_color), metallic_factor: metallic_factor, diff --git a/src/core/include/cesium/omniverse/FabricMaterial.h b/src/core/include/cesium/omniverse/FabricMaterial.h index e18b35018..3ce308c4b 100644 --- a/src/core/include/cesium/omniverse/FabricMaterial.h +++ b/src/core/include/cesium/omniverse/FabricMaterial.h @@ -3,6 +3,11 @@ #include "cesium/omniverse/FabricMaterialDefinition.h" #include "cesium/omniverse/GltfUtil.h" +#ifdef CESIUM_OMNI_MSVC +#pragma push_macro("OPAQUE") +#undef OPAQUE +#endif + #include #include #include @@ -14,6 +19,8 @@ class DynamicTextureProvider; namespace cesium::omniverse { +class FabricTexture; + class FabricMaterial { public: FabricMaterial( @@ -28,80 +35,66 @@ class FabricMaterial { void setMaterial( int64_t tilesetId, const MaterialInfo& materialInfo, + const std::shared_ptr& baseColorTexture, const glm::dvec3& displayColor, - double displayOpacity); - void setBaseColorTexture( - const pxr::TfToken& textureAssetPathToken, - const TextureInfo& textureInfo, - uint64_t texcoordIndex); + double displayOpacity, + const std::unordered_map& texcoordIndexMapping); + void setImageryLayer( - const pxr::TfToken& textureAssetPathToken, + const std::shared_ptr& texture, const TextureInfo& textureInfo, - uint64_t texcoordIndex, uint64_t imageryLayerIndex, - double alpha); + double alpha, + const std::unordered_map& imageryTexcoordIndexMapping); + void setImageryLayerAlpha(uint64_t imageryLayerIndex, double alpha); void setDisplayColorAndOpacity(const glm::dvec3& displayColor, double displayOpacity); void updateShaderInput(const omni::fabric::Path& shaderPath, const omni::fabric::Token& attributeName); - - void clearMaterial(); - void clearBaseColorTexture(); void clearImageryLayer(uint64_t imageryLayerIndex); - void clearImageryLayers(); - void setActive(bool active); [[nodiscard]] const omni::fabric::Path& getPath() const; [[nodiscard]] const FabricMaterialDefinition& getMaterialDefinition() const; private: - void initialize(); - void initializeFromExistingMaterial(const omni::fabric::Path& path); + void initializeNodes(); + void initializeDefaultMaterial(); + void initializeExistingMaterial(const omni::fabric::Path& path); - void createMaterial(const omni::fabric::Path& materialPath); - void createShader(const omni::fabric::Path& shaderPath, const omni::fabric::Path& materialPath); + void createMaterial(const omni::fabric::Path& path); + void createShader(const omni::fabric::Path& path); void createTextureCommon( - const omni::fabric::Path& texturePath, - const omni::fabric::Path& shaderPath, - const omni::fabric::Token& shaderInput, + const omni::fabric::Path& path, const omni::fabric::Token& subIdentifier, const std::vector>& additionalAttributes = {}); - void createTexture( - const omni::fabric::Path& texturePath, - const omni::fabric::Path& shaderPath, - const omni::fabric::Token& shaderInput); - void createImageryLayer( - const omni::fabric::Path& imageryLayerPath, - const omni::fabric::Path& shaderPath, - const omni::fabric::Token& shaderInput); - void createImageryLayerResolver( - const omni::fabric::Path& imageryLayerResolverPath, - const omni::fabric::Path& shaderPath, - const omni::fabric::Token& shaderInput, - uint64_t textureCount); + void createTexture(const omni::fabric::Path& path); + void createImageryLayer(const omni::fabric::Path& path); + void createImageryLayerResolver(const omni::fabric::Path& path, uint64_t textureCount); + void reset(); + void setShaderValues( - const omni::fabric::Path& shaderPath, + const omni::fabric::Path& path, const MaterialInfo& materialInfo, const glm::dvec3& displayColor, double displayOpacity); void setTextureValuesCommon( - const omni::fabric::Path& texturePath, + const omni::fabric::Path& path, const pxr::TfToken& textureAssetPathToken, const TextureInfo& textureInfo, uint64_t texcoordIndex); void setTextureValues( - const omni::fabric::Path& texturePath, + const omni::fabric::Path& path, const pxr::TfToken& textureAssetPathToken, const TextureInfo& textureInfo, uint64_t texcoordIndex); void setImageryLayerValues( - const omni::fabric::Path& imageryLayerPath, + const omni::fabric::Path& path, const pxr::TfToken& textureAssetPathToken, const TextureInfo& textureInfo, uint64_t texcoordIndex, double alpha); - void setImageryLayerAlphaValue(const omni::fabric::Path& imageryLayerPath, double alpha); + void setImageryLayerAlphaValue(const omni::fabric::Path& path, double alpha); bool stageDestroyed(); @@ -112,12 +105,15 @@ class FabricMaterial { const bool _debugRandomColors; const long _stageId; - AlphaMode _alphaMode; - glm::dvec3 _debugColor; + bool _usesDefaultMaterial; + + AlphaMode _alphaMode{AlphaMode::OPAQUE}; + glm::dvec3 _debugColor{1.0, 1.0, 1.0}; - std::vector _shaderPaths; - std::vector _baseColorTexturePaths; - std::vector> _imageryLayerPaths; + omni::fabric::Path _shaderPath; + omni::fabric::Path _baseColorTexturePath; + std::vector _imageryLayerPaths; + omni::fabric::Path _imageryLayerResolverPath; std::vector _allPaths; }; diff --git a/src/core/include/cesium/omniverse/FabricTexture.h b/src/core/include/cesium/omniverse/FabricTexture.h index 48378b4b0..877bbb354 100644 --- a/src/core/include/cesium/omniverse/FabricTexture.h +++ b/src/core/include/cesium/omniverse/FabricTexture.h @@ -15,6 +15,7 @@ struct ImageCesium; } // namespace CesiumGltf namespace cesium::omniverse { + class FabricTexture { public: FabricTexture(const std::string& name); diff --git a/src/core/include/cesium/omniverse/Tokens.h b/src/core/include/cesium/omniverse/Tokens.h index dd243d7d9..fb95e6978 100644 --- a/src/core/include/cesium/omniverse/Tokens.h +++ b/src/core/include/cesium/omniverse/Tokens.h @@ -103,8 +103,6 @@ __pragma(warning(push)) __pragma(warning(disable : 4003)) ((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")) \ ((inputs_wrap_t, "inputs:wrap_t")) \ ((material_binding, "material:binding")) \ @@ -238,7 +236,6 @@ const omni::fabric::Type inputs_scale(omni::fabric::BaseDataType::eFloat, 2, 0, const omni::fabric::Type inputs_tex_coord_index(omni::fabric::BaseDataType::eInt, 1, 0, omni::fabric::AttributeRole::eNone); const omni::fabric::Type inputs_texture(omni::fabric::BaseDataType::eAsset, 1, 0, omni::fabric::AttributeRole::eNone); const omni::fabric::Type inputs_imagery_layers_count(omni::fabric::BaseDataType::eInt, 1, 0, omni::fabric::AttributeRole::eNone); -const omni::fabric::Type inputs_vertex_color_name(omni::fabric::BaseDataType::eUChar, 1, 1, omni::fabric::AttributeRole::eText); const omni::fabric::Type inputs_wrap_s(omni::fabric::BaseDataType::eInt, 1, 0, omni::fabric::AttributeRole::eNone); const omni::fabric::Type inputs_wrap_t(omni::fabric::BaseDataType::eInt, 1, 0, omni::fabric::AttributeRole::eNone); const omni::fabric::Type Material(omni::fabric::BaseDataType::eTag, 1, 0, omni::fabric::AttributeRole::ePrimTypeName); diff --git a/src/core/src/FabricMaterial.cpp b/src/core/src/FabricMaterial.cpp index 8c9d3a471..5c86c14a6 100644 --- a/src/core/src/FabricMaterial.cpp +++ b/src/core/src/FabricMaterial.cpp @@ -4,6 +4,7 @@ #include "cesium/omniverse/FabricAttributesBuilder.h" #include "cesium/omniverse/FabricMaterialDefinition.h" #include "cesium/omniverse/FabricResourceManager.h" +#include "cesium/omniverse/FabricTexture.h" #include "cesium/omniverse/FabricUtil.h" #include "cesium/omniverse/GltfUtil.h" #include "cesium/omniverse/LoggerSink.h" @@ -22,9 +23,12 @@ const auto DEFAULT_DEBUG_COLOR = glm::dvec3(1.0, 1.0, 1.0); const auto DEFAULT_ALPHA = 1.0f; const auto DEFAULT_DISPLAY_COLOR = glm::dvec3(1.0, 1.0, 1.0); const auto DEFAULT_DISPLAY_OPACITY = 1.0; +const auto DEFAULT_MATERIAL_INFO = GltfUtil::getDefaultMaterialInfo(); +const auto DEFAULT_TEXTURE_INFO = GltfUtil::getDefaultTextureInfo(); +const auto DEFAULT_TEXCOORD_INDEX = uint64_t(0); uint64_t getImageryLayerCount(const FabricMaterialDefinition& materialDefinition) { - uint64_t imageryLayerCount = materialDefinition.getImageryLayerCount(); + auto imageryLayerCount = materialDefinition.getImageryLayerCount(); if (imageryLayerCount > FabricTokens::MAX_IMAGERY_LAYERS_COUNT) { CESIUM_LOG_WARN( @@ -53,6 +57,18 @@ pxr::GfVec4f getTileColor(const glm::dvec3& debugColor, const glm::dvec3& displa }; } +void createConnection( + omni::fabric::StageReaderWriter& srw, + const omni::fabric::Path& outputPath, + const omni::fabric::Path& inputPath, + const omni::fabric::Token& inputName) { + srw.createConnection(inputPath, inputName, omni::fabric::Connection{outputPath, FabricTokens::outputs_out}); +} + +template const T& defaultValue(const T* value, const T& defaultValue) { + return value == nullptr ? defaultValue : *value; +} + } // namespace FabricMaterial::FabricMaterial( @@ -67,17 +83,24 @@ FabricMaterial::FabricMaterial( , _defaultTextureAssetPathToken(defaultTextureAssetPathToken) , _defaultTransparentTextureAssetPathToken(defaultTransparentTextureAssetPathToken) , _debugRandomColors(debugRandomColors) - , _stageId(stageId) { + , _stageId(stageId) + , _usesDefaultMaterial(!materialDefinition.hasTilesetMaterial()) { if (stageDestroyed()) { return; } - if (materialDefinition.hasTilesetMaterial()) { - const auto tilesetMaterialPath = FabricUtil::toFabricPath(materialDefinition.getTilesetMaterialPath()); - initializeFromExistingMaterial(tilesetMaterialPath); + initializeNodes(); + + if (_usesDefaultMaterial) { + initializeDefaultMaterial(); } else { - initialize(); + const auto existingMaterialPath = FabricUtil::toFabricPath(materialDefinition.getTilesetMaterialPath()); + initializeExistingMaterial(existingMaterialPath); + } + + for (const auto& nodePath : _allPaths) { + FabricResourceManager::getInstance().retainPath(nodePath); } reset(); @@ -111,112 +134,125 @@ const FabricMaterialDefinition& FabricMaterial::getMaterialDefinition() const { return _materialDefinition; } -void FabricMaterial::initialize() { +void FabricMaterial::initializeNodes() { + auto srw = UsdUtil::getFabricStageReaderWriter(); + + // Create base color texture + const auto hasBaseColorTexture = _materialDefinition.hasBaseColorTexture(); + if (hasBaseColorTexture) { + const auto baseColorTexturePath = FabricUtil::joinPaths(_materialPath, FabricTokens::base_color_texture); + createTexture(baseColorTexturePath); + _baseColorTexturePath = baseColorTexturePath; + _allPaths.push_back(baseColorTexturePath); + } + + // Create imagery layers + const auto imageryLayerCount = getImageryLayerCount(_materialDefinition); + _imageryLayerPaths.reserve(imageryLayerCount); + for (uint64_t i = 0; i < imageryLayerCount; i++) { + const auto imageryLayerPath = FabricUtil::joinPaths(_materialPath, FabricTokens::imagery_layer_n[i]); + createImageryLayer(imageryLayerPath); + _imageryLayerPaths.push_back(imageryLayerPath); + _allPaths.push_back(imageryLayerPath); + } +} + +void FabricMaterial::initializeDefaultMaterial() { + auto srw = UsdUtil::getFabricStageReaderWriter(); + + const auto imageryLayerCount = getImageryLayerCount(_materialDefinition); + const auto hasBaseColorTexture = _materialDefinition.hasBaseColorTexture(); + + // Create material const auto& materialPath = _materialPath; createMaterial(materialPath); _allPaths.push_back(materialPath); - const auto shaderPath = FabricUtil::joinPaths(materialPath, FabricTokens::Shader); - createShader(shaderPath, materialPath); - _shaderPaths.push_back(shaderPath); + // Create shader + const auto shaderPath = FabricUtil::joinPaths(materialPath, FabricTokens::cesium_internal_material); + createShader(shaderPath); + _shaderPath = shaderPath; _allPaths.push_back(shaderPath); - if (_materialDefinition.hasBaseColorTexture()) { - const auto baseColorTexturePath = FabricUtil::joinPaths(materialPath, FabricTokens::base_color_texture); - createTexture(baseColorTexturePath, shaderPath, FabricTokens::inputs_base_color_texture); - _baseColorTexturePaths.push_back(baseColorTexturePath); - _allPaths.push_back(baseColorTexturePath); + // Create imagery layer resolver if there are multiple imagery layers + if (imageryLayerCount > 1) { + const auto imageryLayerResolverPath = FabricUtil::joinPaths(materialPath, FabricTokens::imagery_layer_resolver); + createImageryLayerResolver(imageryLayerResolverPath, imageryLayerCount); + _imageryLayerResolverPath = imageryLayerResolverPath; + _allPaths.push_back(imageryLayerResolverPath); } - const auto imageryLayerCount = getImageryLayerCount(_materialDefinition); - _imageryLayerPaths.resize(imageryLayerCount); + // Create connection from shader to material + createConnection(srw, shaderPath, materialPath, FabricTokens::outputs_mdl_surface); + createConnection(srw, shaderPath, materialPath, FabricTokens::outputs_mdl_displacement); + createConnection(srw, shaderPath, materialPath, FabricTokens::outputs_mdl_volume); + + // Create connection from base color texture to shader + if (hasBaseColorTexture) { + createConnection(srw, _baseColorTexturePath, shaderPath, FabricTokens::inputs_base_color_texture); + } if (imageryLayerCount == 1) { - // If there's a single imagery layer plug into cesium_material directly - // instead of using a cesium_imagery_layer_resolver - const auto imageryLayerPath = FabricUtil::joinPaths(materialPath, FabricTokens::imagery_layer_n[0]); - createImageryLayer(imageryLayerPath, shaderPath, FabricTokens::inputs_imagery_layers_texture); - _imageryLayerPaths[0].push_back(imageryLayerPath); - _allPaths.push_back(imageryLayerPath); + // Create connection from imagery layer to shader + const auto& imageryLayerPath = _imageryLayerPaths.front(); + createConnection(srw, imageryLayerPath, shaderPath, FabricTokens::inputs_imagery_layer); } else if (imageryLayerCount > 1) { - const auto imageryLayerResolverPath = FabricUtil::joinPaths(materialPath, FabricTokens::imagery_layer_resolver); - createImageryLayerResolver( - imageryLayerResolverPath, shaderPath, FabricTokens::inputs_imagery_layers_texture, imageryLayerCount); - _allPaths.push_back(imageryLayerResolverPath); + // Create connection from imagery layer resolver to shader + createConnection(srw, _imageryLayerResolverPath, shaderPath, FabricTokens::inputs_imagery_layer); + // Create connections from imagery layers to imagery layer resolver for (uint64_t i = 0; i < imageryLayerCount; i++) { - const auto imageryLayerPath = FabricUtil::joinPaths(materialPath, FabricTokens::imagery_layer_n[i]); - createImageryLayer(imageryLayerPath, imageryLayerResolverPath, FabricTokens::inputs_imagery_layer_n[i]); - _imageryLayerPaths[i].push_back(imageryLayerPath); - _allPaths.push_back(imageryLayerPath); + const auto& imageryLayerPath = _imageryLayerPaths[i]; + createConnection(srw, imageryLayerPath, _imageryLayerResolverPath, FabricTokens::inputs_imagery_layer_n[i]); } } - - for (const auto& path : _allPaths) { - FabricResourceManager::getInstance().retainPath(path); - } } -void FabricMaterial::initializeFromExistingMaterial(const omni::fabric::Path& srcMaterialPath) { +void FabricMaterial::initializeExistingMaterial(const omni::fabric::Path& path) { auto srw = UsdUtil::getFabricStageReaderWriter(); - const auto& dstMaterialPath = _materialPath; - - const auto dstPaths = FabricUtil::copyMaterial(srcMaterialPath, dstMaterialPath); - const auto imageryLayerCount = getImageryLayerCount(_materialDefinition); - _imageryLayerPaths.resize(imageryLayerCount); + const auto hasBaseColorTexture = _materialDefinition.hasBaseColorTexture(); - for (const auto& dstPath : dstPaths) { - srw.createAttribute(dstPath, FabricTokens::_cesium_tilesetId, FabricTypes::_cesium_tilesetId); - _allPaths.push_back(dstPath); + const auto copiedPaths = FabricUtil::copyMaterial(path, _materialPath); - const auto mdlIdentifier = FabricUtil::getMdlIdentifier(dstPath); + for (const auto& copiedPath : copiedPaths) { + srw.createAttribute(copiedPath, FabricTokens::_cesium_tilesetId, FabricTypes::_cesium_tilesetId); + _allPaths.push_back(copiedPath); + + const auto mdlIdentifier = FabricUtil::getMdlIdentifier(copiedPath); if (mdlIdentifier == FabricTokens::cesium_base_color_texture_float4) { - if (_materialDefinition.hasBaseColorTexture()) { - // Create a base color texture node to fill the empty slot - const auto baseColorTexturePath = FabricUtil::joinPaths(dstPath, FabricTokens::base_color_texture); - createTexture(baseColorTexturePath, dstPath, FabricTokens::inputs_base_color_texture); - _baseColorTexturePaths.push_back(baseColorTexturePath); - _allPaths.push_back(baseColorTexturePath); + if (hasBaseColorTexture) { + createConnection(srw, _baseColorTexturePath, copiedPath, FabricTokens::inputs_base_color_texture); } } else if (mdlIdentifier == FabricTokens::cesium_imagery_layer_float4) { - 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); - createImageryLayer(imageryLayerPath, dstPath, FabricTokens::inputs_imagery_layer); - _imageryLayerPaths[imageryLayerIndex].push_back(imageryLayerPath); - _allPaths.push_back(imageryLayerPath); + const auto indexFabric = srw.getAttributeRd(copiedPath, FabricTokens::inputs_imagery_layer_index); + const auto index = static_cast(defaultValue(indexFabric, 0)); + + if (index < imageryLayerCount) { + createConnection(srw, _imageryLayerPaths[index], copiedPath, FabricTokens::inputs_imagery_layer); } } } - - for (const auto& path : _allPaths) { - FabricResourceManager::getInstance().retainPath(path); - } } -void FabricMaterial::createMaterial(const omni::fabric::Path& materialPath) { +void FabricMaterial::createMaterial(const omni::fabric::Path& path) { auto srw = UsdUtil::getFabricStageReaderWriter(); - srw.createPrim(materialPath); + srw.createPrim(path); FabricAttributesBuilder attributes; attributes.addAttribute(FabricTypes::Material, FabricTokens::Material); attributes.addAttribute(FabricTypes::_cesium_tilesetId, FabricTokens::_cesium_tilesetId); - attributes.createAttributes(materialPath); + attributes.createAttributes(path); } -void FabricMaterial::createShader(const omni::fabric::Path& shaderPath, const omni::fabric::Path& materialPath) { +void FabricMaterial::createShader(const omni::fabric::Path& path) { auto srw = UsdUtil::getFabricStageReaderWriter(); - srw.createPrim(shaderPath); + srw.createPrim(path); FabricAttributesBuilder attributes; @@ -240,16 +276,16 @@ void FabricMaterial::createShader(const omni::fabric::Path& shaderPath, const om attributes.addAttribute(FabricTypes::_cesium_tilesetId, FabricTokens::_cesium_tilesetId); // clang-format on - attributes.createAttributes(shaderPath); + attributes.createAttributes(path); - srw.setArrayAttributeSize(shaderPath, FabricTokens::_paramColorSpace, 0); - srw.setArrayAttributeSize(shaderPath, FabricTokens::_sdrMetadata, 0); + srw.setArrayAttributeSize(path, FabricTokens::_paramColorSpace, 0); + srw.setArrayAttributeSize(path, FabricTokens::_sdrMetadata, 0); // clang-format off - auto inputsExcludeFromWhiteModeFabric = srw.getAttributeWr(shaderPath, FabricTokens::inputs_excludeFromWhiteMode); - auto infoImplementationSourceFabric = srw.getAttributeWr(shaderPath, FabricTokens::info_implementationSource); - auto infoMdlSourceAssetFabric = srw.getAttributeWr(shaderPath, FabricTokens::info_mdl_sourceAsset); - auto infoMdlSourceAssetSubIdentifierFabric = srw.getAttributeWr(shaderPath, FabricTokens::info_mdl_sourceAsset_subIdentifier); + auto inputsExcludeFromWhiteModeFabric = srw.getAttributeWr(path, FabricTokens::inputs_excludeFromWhiteMode); + auto infoImplementationSourceFabric = srw.getAttributeWr(path, FabricTokens::info_implementationSource); + auto infoMdlSourceAssetFabric = srw.getAttributeWr(path, FabricTokens::info_mdl_sourceAsset); + auto infoMdlSourceAssetSubIdentifierFabric = srw.getAttributeWr(path, FabricTokens::info_mdl_sourceAsset_subIdentifier); // clang-format on *inputsExcludeFromWhiteModeFabric = false; @@ -257,31 +293,15 @@ void FabricMaterial::createShader(const omni::fabric::Path& shaderPath, const om infoMdlSourceAssetFabric->assetPath = Context::instance().getCesiumMdlPathToken(); infoMdlSourceAssetFabric->resolvedPath = pxr::TfToken(); *infoMdlSourceAssetSubIdentifierFabric = FabricTokens::cesium_internal_material; - - // Connect the material terminals to the shader. - srw.createConnection( - materialPath, - FabricTokens::outputs_mdl_surface, - omni::fabric::Connection{shaderPath, FabricTokens::outputs_out}); - srw.createConnection( - materialPath, - FabricTokens::outputs_mdl_displacement, - omni::fabric::Connection{shaderPath, FabricTokens::outputs_out}); - srw.createConnection( - materialPath, - FabricTokens::outputs_mdl_volume, - omni::fabric::Connection{shaderPath, FabricTokens::outputs_out}); } void FabricMaterial::createTextureCommon( - const omni::fabric::Path& texturePath, - const omni::fabric::Path& shaderPath, - const omni::fabric::Token& shaderInput, + const omni::fabric::Path& path, const omni::fabric::Token& subIdentifier, const std::vector>& additionalAttributes) { auto srw = UsdUtil::getFabricStageReaderWriter(); - srw.createPrim(texturePath); + srw.createPrim(path); FabricAttributesBuilder attributes; @@ -308,18 +328,18 @@ void FabricMaterial::createTextureCommon( attributes.addAttribute(additionalAttribute.first, additionalAttribute.second); } - attributes.createAttributes(texturePath); + attributes.createAttributes(path); // _paramColorSpace is an array of pairs: [texture_parameter_token, color_space_enum], [texture_parameter_token, color_space_enum], ... - srw.setArrayAttributeSize(texturePath, FabricTokens::_paramColorSpace, 2); - srw.setArrayAttributeSize(texturePath, FabricTokens::_sdrMetadata, 0); + srw.setArrayAttributeSize(path, FabricTokens::_paramColorSpace, 2); + srw.setArrayAttributeSize(path, FabricTokens::_sdrMetadata, 0); // clang-format off - auto inputsExcludeFromWhiteModeFabric = srw.getAttributeWr(texturePath, FabricTokens::inputs_excludeFromWhiteMode); - auto infoImplementationSourceFabric = srw.getAttributeWr(texturePath, FabricTokens::info_implementationSource); - auto infoMdlSourceAssetFabric = srw.getAttributeWr(texturePath, FabricTokens::info_mdl_sourceAsset); - auto infoMdlSourceAssetSubIdentifierFabric = srw.getAttributeWr(texturePath, FabricTokens::info_mdl_sourceAsset_subIdentifier); - auto paramColorSpaceFabric = srw.getArrayAttributeWr(texturePath, FabricTokens::_paramColorSpace); + auto inputsExcludeFromWhiteModeFabric = srw.getAttributeWr(path, FabricTokens::inputs_excludeFromWhiteMode); + auto infoImplementationSourceFabric = srw.getAttributeWr(path, FabricTokens::info_implementationSource); + auto infoMdlSourceAssetFabric = srw.getAttributeWr(path, FabricTokens::info_mdl_sourceAsset); + auto infoMdlSourceAssetSubIdentifierFabric = srw.getAttributeWr(path, FabricTokens::info_mdl_sourceAsset_subIdentifier); + auto paramColorSpaceFabric = srw.getArrayAttributeWr(path, FabricTokens::_paramColorSpace); // clang-format on *inputsExcludeFromWhiteModeFabric = false; @@ -329,41 +349,23 @@ void FabricMaterial::createTextureCommon( *infoMdlSourceAssetSubIdentifierFabric = subIdentifier; paramColorSpaceFabric[0] = FabricTokens::inputs_texture; paramColorSpaceFabric[1] = FabricTokens::_auto; - - // Create connection from shader to texture. - srw.createConnection(shaderPath, shaderInput, omni::fabric::Connection{texturePath, FabricTokens::outputs_out}); } -void FabricMaterial::createTexture( - const omni::fabric::Path& texturePath, - const omni::fabric::Path& shaderPath, - const omni::fabric::Token& shaderInput) { - return createTextureCommon(texturePath, shaderPath, shaderInput, FabricTokens::cesium_internal_texture_lookup); +void FabricMaterial::createTexture(const omni::fabric::Path& path) { + return createTextureCommon(path, FabricTokens::cesium_internal_texture_lookup); } -void FabricMaterial::createImageryLayer( - const omni::fabric::Path& imageryLayerPath, - const omni::fabric::Path& shaderPath, - const omni::fabric::Token& shaderInput) { - const auto additionalAttributes = std::vector>{{ +void FabricMaterial::createImageryLayer(const omni::fabric::Path& path) { + static const auto additionalAttributes = std::vector>{{ std::make_pair(FabricTypes::inputs_alpha, FabricTokens::inputs_alpha), }}; - return createTextureCommon( - imageryLayerPath, - shaderPath, - shaderInput, - FabricTokens::cesium_internal_imagery_layer_lookup, - additionalAttributes); + return createTextureCommon(path, FabricTokens::cesium_internal_imagery_layer_lookup, additionalAttributes); } -void FabricMaterial::createImageryLayerResolver( - const omni::fabric::Path& imageryLayerResolverPath, - const omni::fabric::Path& shaderPath, - const omni::fabric::Token& shaderInput, - uint64_t imageryLayerCount) { +void FabricMaterial::createImageryLayerResolver(const omni::fabric::Path& path, uint64_t imageryLayerCount) { auto srw = UsdUtil::getFabricStageReaderWriter(); - srw.createPrim(imageryLayerResolverPath); + srw.createPrim(path); FabricAttributesBuilder attributes; @@ -379,14 +381,14 @@ void FabricMaterial::createImageryLayerResolver( attributes.addAttribute(FabricTypes::_cesium_tilesetId, FabricTokens::_cesium_tilesetId); // clang-format on - attributes.createAttributes(imageryLayerResolverPath); + attributes.createAttributes(path); // clang-format off - auto imageryLayerCountFabric = srw.getAttributeWr(imageryLayerResolverPath, FabricTokens::inputs_imagery_layers_count); - auto inputsExcludeFromWhiteModeFabric = srw.getAttributeWr(imageryLayerResolverPath, FabricTokens::inputs_excludeFromWhiteMode); - auto infoImplementationSourceFabric = srw.getAttributeWr(imageryLayerResolverPath, FabricTokens::info_implementationSource); - auto infoMdlSourceAssetFabric = srw.getAttributeWr(imageryLayerResolverPath, FabricTokens::info_mdl_sourceAsset); - auto infoMdlSourceAssetSubIdentifierFabric = srw.getAttributeWr(imageryLayerResolverPath, FabricTokens::info_mdl_sourceAsset_subIdentifier); + auto imageryLayerCountFabric = srw.getAttributeWr(path, FabricTokens::inputs_imagery_layers_count); + auto inputsExcludeFromWhiteModeFabric = srw.getAttributeWr(path, FabricTokens::inputs_excludeFromWhiteMode); + auto infoImplementationSourceFabric = srw.getAttributeWr(path, FabricTokens::info_implementationSource); + auto infoMdlSourceAssetFabric = srw.getAttributeWr(path, FabricTokens::info_mdl_sourceAsset); + auto infoMdlSourceAssetSubIdentifierFabric = srw.getAttributeWr(path, FabricTokens::info_mdl_sourceAsset_subIdentifier); // clang-format on *imageryLayerCountFabric = static_cast(imageryLayerCount); @@ -395,42 +397,65 @@ void FabricMaterial::createImageryLayerResolver( infoMdlSourceAssetFabric->assetPath = Context::instance().getCesiumMdlPathToken(); infoMdlSourceAssetFabric->resolvedPath = pxr::TfToken(); *infoMdlSourceAssetSubIdentifierFabric = FabricTokens::cesium_internal_imagery_layer_resolver; - - // Create connection to shader - srw.createConnection( - shaderPath, shaderInput, omni::fabric::Connection{imageryLayerResolverPath, FabricTokens::outputs_out}); } void FabricMaterial::reset() { - clearMaterial(); - clearBaseColorTexture(); - clearImageryLayers(); + if (_usesDefaultMaterial) { + setShaderValues(_shaderPath, DEFAULT_MATERIAL_INFO, DEFAULT_DISPLAY_COLOR, DEFAULT_DISPLAY_OPACITY); + } + + if (_materialDefinition.hasBaseColorTexture()) { + setTextureValues( + _baseColorTexturePath, _defaultTextureAssetPathToken, DEFAULT_TEXTURE_INFO, DEFAULT_TEXCOORD_INDEX); + } + + for (const auto& imageryLayerPath : _imageryLayerPaths) { + setImageryLayerValues( + imageryLayerPath, + _defaultTransparentTextureAssetPathToken, + DEFAULT_TEXTURE_INFO, + DEFAULT_TEXCOORD_INDEX, + DEFAULT_ALPHA); + } + + for (const auto& path : _allPaths) { + FabricUtil::setTilesetId(path, NO_TILESET_ID); + } } void FabricMaterial::setMaterial( int64_t tilesetId, const MaterialInfo& materialInfo, + const std::shared_ptr& baseColorTexture, const glm::dvec3& displayColor, - double displayOpacity) { + double displayOpacity, + const std::unordered_map& texcoordIndexMapping) { + if (stageDestroyed()) { return; } - _alphaMode = materialInfo.alphaMode; + if (_usesDefaultMaterial) { + _alphaMode = materialInfo.alphaMode; - if (_debugRandomColors) { - const auto r = glm::linearRand(0.0, 1.0); - const auto g = glm::linearRand(0.0, 1.0); - const auto b = glm::linearRand(0.0, 1.0); - _debugColor = glm::dvec3(r, g, b); - } else { - _debugColor = DEFAULT_DEBUG_COLOR; + if (_debugRandomColors) { + const auto r = glm::linearRand(0.0, 1.0); + const auto g = glm::linearRand(0.0, 1.0); + const auto b = glm::linearRand(0.0, 1.0); + _debugColor = glm::dvec3(r, g, b); + } else { + _debugColor = DEFAULT_DEBUG_COLOR; + } + + setShaderValues(_shaderPath, materialInfo, displayColor, displayOpacity); } - auto srw = UsdUtil::getFabricStageReaderWriter(); + if (_materialDefinition.hasBaseColorTexture()) { + const auto& textureInfo = materialInfo.baseColorTexture.value(); + const auto& textureAssetPath = baseColorTexture->getAssetPathToken(); + const auto texcoordIndex = texcoordIndexMapping.at(textureInfo.setIndex); - for (auto& shaderPath : _shaderPaths) { - setShaderValues(shaderPath, materialInfo, displayColor, displayOpacity); + setTextureValues(_baseColorTexturePath, textureAssetPath, textureInfo, texcoordIndex); } for (const auto& path : _allPaths) { @@ -438,25 +463,12 @@ void FabricMaterial::setMaterial( } } -void FabricMaterial::setBaseColorTexture( - const pxr::TfToken& textureAssetPathToken, - const TextureInfo& textureInfo, - uint64_t texcoordIndex) { - if (stageDestroyed()) { - return; - } - - for (auto& baseColorTexturePath : _baseColorTexturePaths) { - setTextureValues(baseColorTexturePath, textureAssetPathToken, textureInfo, texcoordIndex); - } -} - void FabricMaterial::setImageryLayer( - const pxr::TfToken& textureAssetPathToken, + const std::shared_ptr& texture, const TextureInfo& textureInfo, - uint64_t texcoordIndex, uint64_t imageryLayerIndex, - double alpha) { + double alpha, + const std::unordered_map& imageryTexcoordIndexMapping) { if (stageDestroyed()) { return; } @@ -465,9 +477,10 @@ void FabricMaterial::setImageryLayer( return; } - for (auto& imageryLayerPath : _imageryLayerPaths[imageryLayerIndex]) { - setImageryLayerValues(imageryLayerPath, textureAssetPathToken, textureInfo, texcoordIndex, alpha); - } + const auto& textureAssetPath = texture->getAssetPathToken(); + const auto texcoordIndex = imageryTexcoordIndexMapping.at(textureInfo.setIndex); + const auto& imageryLayerPath = _imageryLayerPaths[imageryLayerIndex]; + setImageryLayerValues(imageryLayerPath, textureAssetPath, textureInfo, texcoordIndex, alpha); } void FabricMaterial::setImageryLayerAlpha(uint64_t imageryLayerIndex, double alpha) { @@ -479,9 +492,8 @@ void FabricMaterial::setImageryLayerAlpha(uint64_t imageryLayerIndex, double alp return; } - for (auto& imageryLayerPath : _imageryLayerPaths[imageryLayerIndex]) { - setImageryLayerAlphaValue(imageryLayerPath, alpha); - } + const auto& imageryLayerPath = _imageryLayerPaths[imageryLayerIndex]; + setImageryLayerAlphaValue(imageryLayerPath, alpha); } void FabricMaterial::setDisplayColorAndOpacity(const glm::dvec3& displayColor, double displayOpacity) { @@ -489,18 +501,20 @@ void FabricMaterial::setDisplayColorAndOpacity(const glm::dvec3& displayColor, d return; } + if (!_usesDefaultMaterial) { + return; + } + auto srw = UsdUtil::getFabricStageReaderWriter(); - for (auto& shaderPath : _shaderPaths) { - auto tileColorFabric = srw.getAttributeWr(shaderPath, FabricTokens::inputs_tile_color); - auto alphaModeFabric = srw.getAttributeWr(shaderPath, FabricTokens::inputs_alpha_mode); + auto tileColorFabric = srw.getAttributeWr(_shaderPath, FabricTokens::inputs_tile_color); + auto alphaModeFabric = srw.getAttributeWr(_shaderPath, FabricTokens::inputs_alpha_mode); - *tileColorFabric = getTileColor(_debugColor, displayColor, displayOpacity); - *alphaModeFabric = getAlphaMode(_alphaMode, displayOpacity); - } + *tileColorFabric = getTileColor(_debugColor, displayColor, displayOpacity); + *alphaModeFabric = getAlphaMode(_alphaMode, displayOpacity); } -void FabricMaterial::updateShaderInput(const omni::fabric::Path& shaderPath, const omni::fabric::Token& attributeName) { +void FabricMaterial::updateShaderInput(const omni::fabric::Path& path, const omni::fabric::Token& attributeName) { if (stageDestroyed()) { return; } @@ -508,59 +522,49 @@ void FabricMaterial::updateShaderInput(const omni::fabric::Path& shaderPath, con const auto srw = UsdUtil::getFabricStageReaderWriter(); const auto isrw = carb::getCachedInterface(); - const auto copiedShaderPath = FabricUtil::getCopiedShaderPath(_materialPath, shaderPath); + const auto copiedShaderPath = FabricUtil::getCopiedShaderPath(_materialPath, path); const auto attributesToCopy = std::vector{attributeName}; assert(isrw->primExists(srw.getId(), copiedShaderPath)); assert(isrw->attributeExists(srw.getId(), copiedShaderPath, attributeName)); isrw->copySpecifiedAttributes( - srw.getId(), - shaderPath, - attributesToCopy.data(), - copiedShaderPath, - attributesToCopy.data(), - attributesToCopy.size()); + srw.getId(), path, attributesToCopy.data(), copiedShaderPath, attributesToCopy.data(), attributesToCopy.size()); } -void FabricMaterial::clearMaterial() { - setMaterial(NO_TILESET_ID, GltfUtil::getDefaultMaterialInfo(), DEFAULT_DISPLAY_COLOR, DEFAULT_DISPLAY_OPACITY); -} +void FabricMaterial::clearImageryLayer(uint64_t imageryLayerIndex) { + if (stageDestroyed()) { + return; + } -void FabricMaterial::clearBaseColorTexture() { - setBaseColorTexture(_defaultTextureAssetPathToken, GltfUtil::getDefaultTextureInfo(), 0); -} + if (imageryLayerIndex >= _imageryLayerPaths.size()) { + return; + } -void FabricMaterial::clearImageryLayer(uint64_t imageryLayerIndex) { - setImageryLayer( + const auto& imageryLayerPath = _imageryLayerPaths[imageryLayerIndex]; + setImageryLayerValues( + imageryLayerPath, _defaultTransparentTextureAssetPathToken, - GltfUtil::getDefaultTextureInfo(), - 0, - imageryLayerIndex, + DEFAULT_TEXTURE_INFO, + DEFAULT_TEXCOORD_INDEX, DEFAULT_ALPHA); } -void FabricMaterial::clearImageryLayers() { - for (uint64_t i = 0; i < _imageryLayerPaths.size(); i++) { - clearImageryLayer(i); - } -} - void FabricMaterial::setShaderValues( - const omni::fabric::Path& shaderPath, + const omni::fabric::Path& path, const MaterialInfo& materialInfo, const glm::dvec3& displayColor, double displayOpacity) { auto srw = UsdUtil::getFabricStageReaderWriter(); - auto tileColorFabric = srw.getAttributeWr(shaderPath, FabricTokens::inputs_tile_color); - auto alphaCutoffFabric = srw.getAttributeWr(shaderPath, FabricTokens::inputs_alpha_cutoff); - auto alphaModeFabric = srw.getAttributeWr(shaderPath, FabricTokens::inputs_alpha_mode); - auto baseAlphaFabric = srw.getAttributeWr(shaderPath, FabricTokens::inputs_base_alpha); - auto baseColorFactorFabric = srw.getAttributeWr(shaderPath, FabricTokens::inputs_base_color_factor); - auto emissiveFactorFabric = srw.getAttributeWr(shaderPath, FabricTokens::inputs_emissive_factor); - auto metallicFactorFabric = srw.getAttributeWr(shaderPath, FabricTokens::inputs_metallic_factor); - auto roughnessFactorFabric = srw.getAttributeWr(shaderPath, FabricTokens::inputs_roughness_factor); + auto tileColorFabric = srw.getAttributeWr(path, FabricTokens::inputs_tile_color); + auto alphaCutoffFabric = srw.getAttributeWr(path, FabricTokens::inputs_alpha_cutoff); + auto alphaModeFabric = srw.getAttributeWr(path, FabricTokens::inputs_alpha_mode); + auto baseAlphaFabric = srw.getAttributeWr(path, FabricTokens::inputs_base_alpha); + auto baseColorFactorFabric = srw.getAttributeWr(path, FabricTokens::inputs_base_color_factor); + auto emissiveFactorFabric = srw.getAttributeWr(path, FabricTokens::inputs_emissive_factor); + auto metallicFactorFabric = srw.getAttributeWr(path, FabricTokens::inputs_metallic_factor); + auto roughnessFactorFabric = srw.getAttributeWr(path, FabricTokens::inputs_roughness_factor); *tileColorFabric = getTileColor(_debugColor, displayColor, displayOpacity); *alphaCutoffFabric = static_cast(materialInfo.alphaCutoff); @@ -573,7 +577,7 @@ void FabricMaterial::setShaderValues( } void FabricMaterial::setTextureValuesCommon( - const omni::fabric::Path& texturePath, + const omni::fabric::Path& path, const pxr::TfToken& textureAssetPathToken, const TextureInfo& textureInfo, uint64_t texcoordIndex) { @@ -596,13 +600,13 @@ void FabricMaterial::setTextureValuesCommon( scale = {scale.x, scale.y}; } - auto textureFabric = srw.getAttributeWr(texturePath, FabricTokens::inputs_texture); - auto texCoordIndexFabric = srw.getAttributeWr(texturePath, FabricTokens::inputs_tex_coord_index); - auto wrapSFabric = srw.getAttributeWr(texturePath, FabricTokens::inputs_wrap_s); - auto wrapTFabric = srw.getAttributeWr(texturePath, FabricTokens::inputs_wrap_t); - auto offsetFabric = srw.getAttributeWr(texturePath, FabricTokens::inputs_offset); - auto rotationFabric = srw.getAttributeWr(texturePath, FabricTokens::inputs_rotation); - auto scaleFabric = srw.getAttributeWr(texturePath, FabricTokens::inputs_scale); + auto textureFabric = srw.getAttributeWr(path, FabricTokens::inputs_texture); + auto texCoordIndexFabric = srw.getAttributeWr(path, FabricTokens::inputs_tex_coord_index); + auto wrapSFabric = srw.getAttributeWr(path, FabricTokens::inputs_wrap_s); + auto wrapTFabric = srw.getAttributeWr(path, FabricTokens::inputs_wrap_t); + auto offsetFabric = srw.getAttributeWr(path, FabricTokens::inputs_offset); + auto rotationFabric = srw.getAttributeWr(path, FabricTokens::inputs_rotation); + auto scaleFabric = srw.getAttributeWr(path, FabricTokens::inputs_scale); textureFabric->assetPath = textureAssetPathToken; textureFabric->resolvedPath = pxr::TfToken(); @@ -615,26 +619,26 @@ void FabricMaterial::setTextureValuesCommon( } void FabricMaterial::setTextureValues( - const omni::fabric::Path& texturePath, + const omni::fabric::Path& path, const pxr::TfToken& textureAssetPathToken, const TextureInfo& textureInfo, uint64_t texcoordIndex) { - setTextureValuesCommon(texturePath, textureAssetPathToken, textureInfo, texcoordIndex); + setTextureValuesCommon(path, textureAssetPathToken, textureInfo, texcoordIndex); } void FabricMaterial::setImageryLayerValues( - const omni::fabric::Path& imageryLayerPath, + const omni::fabric::Path& path, const pxr::TfToken& textureAssetPathToken, const TextureInfo& textureInfo, uint64_t texcoordIndex, double alpha) { - setTextureValuesCommon(imageryLayerPath, textureAssetPathToken, textureInfo, texcoordIndex); - setImageryLayerAlphaValue(imageryLayerPath, alpha); + setTextureValuesCommon(path, textureAssetPathToken, textureInfo, texcoordIndex); + setImageryLayerAlphaValue(path, alpha); } -void FabricMaterial::setImageryLayerAlphaValue(const omni::fabric::Path& imageryLayerPath, double alpha) { +void FabricMaterial::setImageryLayerAlphaValue(const omni::fabric::Path& path, double alpha) { auto srw = UsdUtil::getFabricStageReaderWriter(); - auto alphaFabric = srw.getAttributeWr(imageryLayerPath, FabricTokens::inputs_alpha); + auto alphaFabric = srw.getAttributeWr(path, FabricTokens::inputs_alpha); *alphaFabric = static_cast(alpha); } diff --git a/src/core/src/FabricPrepareRenderResources.cpp b/src/core/src/FabricPrepareRenderResources.cpp index 6f3126738..a85cb0771 100644 --- a/src/core/src/FabricPrepareRenderResources.cpp +++ b/src/core/src/FabricPrepareRenderResources.cpp @@ -55,9 +55,8 @@ struct TileLoadThreadResult { glm::dmat4 tileTransform; }; -bool hasBaseColorTextureGltf(const FabricMesh& fabricMesh) { - return fabricMesh.material != nullptr && fabricMesh.material->getMaterialDefinition().hasBaseColorTexture() && - fabricMesh.materialInfo.baseColorTexture.has_value(); +bool hasBaseColorTexture(const FabricMesh& fabricMesh) { + return fabricMesh.material != nullptr && fabricMesh.material->getMaterialDefinition().hasBaseColorTexture(); } std::vector @@ -117,6 +116,7 @@ std::vector acquireFabricMeshes( auto& fabricMesh = fabricMeshes.emplace_back(); const auto& primitive = model.meshes[mesh.meshId].primitives[mesh.primitiveId]; + const auto fabricGeometry = fabricResourceManager.acquireGeometry(model, primitive, mesh.smoothNormals, stageId); fabricMesh.geometry = fabricGeometry; @@ -133,7 +133,7 @@ std::vector acquireFabricMeshes( fabricMesh.material = fabricMaterial; fabricMesh.materialInfo = materialInfo; - if (hasBaseColorTextureGltf(fabricMesh)) { + if (hasBaseColorTexture(fabricMesh)) { fabricMesh.baseColorTexture = fabricResourceManager.acquireTexture(); } } @@ -165,8 +165,9 @@ void setFabricTextures( auto& mesh = fabricMeshes[i]; auto& baseColorTexture = mesh.baseColorTexture; - if (hasBaseColorTextureGltf(mesh)) { + if (hasBaseColorTexture(mesh)) { const auto baseColorTextureImage = GltfUtil::getBaseColorTextureImage(model, primitive); + assert(baseColorTextureImage); baseColorTexture->setImage(*baseColorTextureImage); } } @@ -187,11 +188,9 @@ void setFabricMeshes( const auto& meshInfo = meshes[i]; const auto& primitive = model.meshes[meshInfo.meshId].primitives[meshInfo.primitiveId]; - auto& mesh = fabricMeshes[i]; - auto& geometry = mesh.geometry; - auto& material = mesh.material; - auto& baseColorTexture = mesh.baseColorTexture; - auto& materialInfo = mesh.materialInfo; + const auto& mesh = fabricMeshes[i]; + const auto& geometry = mesh.geometry; + const auto& material = mesh.material; geometry->setGeometry( meshInfo.tilesetId, @@ -200,21 +199,21 @@ void setFabricMeshes( meshInfo.nodeTransform, model, primitive, - materialInfo, + mesh.materialInfo, meshInfo.smoothNormals, mesh.texcoordIndexMapping, mesh.imageryTexcoordIndexMapping); if (material != nullptr) { - material->setMaterial(meshInfo.tilesetId, materialInfo, displayColor, displayOpacity); - geometry->setMaterial(material->getPath()); + material->setMaterial( + meshInfo.tilesetId, + mesh.materialInfo, + mesh.baseColorTexture, + displayColor, + displayOpacity, + mesh.texcoordIndexMapping); - if (hasBaseColorTextureGltf(mesh)) { - const auto& textureInfo = materialInfo.baseColorTexture.value(); - const auto texcoordIndex = mesh.texcoordIndexMapping[textureInfo.setIndex]; - const auto& textureAssetPath = baseColorTexture->getAssetPathToken(); - material->setBaseColorTexture(textureAssetPath, textureInfo, texcoordIndex); - } + geometry->setMaterial(material->getPath()); } else if (!tilesetMaterialPath.IsEmpty()) { geometry->setMaterial(FabricUtil::toFabricPath(tilesetMaterialPath)); } @@ -471,10 +470,8 @@ void FabricPrepareRenderResources::attachRasterInMainThread( CesiumGltf::Sampler::WrapT::CLAMP_TO_EDGE, false, }; - - const auto texcoordIndex = mesh.imageryTexcoordIndexMapping.at(gltfSetIndex); - const auto& textureAssetPath = texture->getAssetPathToken(); - material->setImageryLayer(textureAssetPath, textureInfo, texcoordIndex, imageryLayerIndex.value(), alpha); + material->setImageryLayer( + texture, textureInfo, imageryLayerIndex.value(), alpha, mesh.imageryTexcoordIndexMapping); } } } diff --git a/src/core/src/GltfUtil.cpp b/src/core/src/GltfUtil.cpp index ea11fa609..0ef308e81 100644 --- a/src/core/src/GltfUtil.cpp +++ b/src/core/src/GltfUtil.cpp @@ -287,7 +287,7 @@ int32_t getDefaultWrapT() { TextureInfo getTextureInfo(const CesiumGltf::Model& model, const CesiumGltf::TextureInfo& textureInfoGltf) { TextureInfo textureInfo = getDefaultTextureInfo(); - textureInfo.setIndex = textureInfoGltf.texCoord; + textureInfo.setIndex = static_cast(textureInfoGltf.texCoord); if (textureInfoGltf.hasExtension()) { const auto& textureTransform = *textureInfoGltf.getExtension(); @@ -301,7 +301,7 @@ TextureInfo getTextureInfo(const CesiumGltf::Model& model, const CesiumGltf::Tex const auto& texture = model.textures[static_cast(index)]; const auto samplerIndex = texture.sampler; if (samplerIndex != -1) { - const auto& sampler = model.samplers[samplerIndex]; + const auto& sampler = model.samplers[static_cast(samplerIndex)]; textureInfo.wrapS = getWrapS(sampler); textureInfo.wrapT = getWrapT(sampler); } @@ -320,8 +320,8 @@ std::pair parseAttributeName(const std::string& attribute int searchPosition = static_cast(attributeName.size()) - 1; int lastUnderscorePosition{-1}; while (searchPosition > 0) { - if (!isdigit(attributeName[searchPosition])) { - if (attributeName[searchPosition] == '_') { + if (!isdigit(attributeName[static_cast(searchPosition)])) { + if (attributeName[static_cast(searchPosition)] == '_') { lastUnderscorePosition = searchPosition; } @@ -335,7 +335,7 @@ std::pair parseAttributeName(const std::string& attribute if (lastUnderscorePosition == -1) { semantic = attributeName; } else { - semantic = attributeName.substr(0, lastUnderscorePosition); + semantic = attributeName.substr(0, static_cast(lastUnderscorePosition)); std::from_chars( attributeName.data() + lastUnderscorePosition + 1, attributeName.data() + attributeName.size(),