Skip to content

Commit

Permalink
Added base color texture and imagery MDL lookup shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Oct 19, 2023
1 parent 991ac3a commit ab9c82c
Show file tree
Hide file tree
Showing 12 changed files with 364 additions and 28 deletions.
2 changes: 1 addition & 1 deletion apps/cesium.omniverse.dev.kit
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ app = true

[settings]
app.window.title = "Cesium for Omniverse Testing App"
app.useFabricSceneDelegate = true
app.useFabricSceneDelegate = false
app.usdrt.scene_delegate.enableProxyCubes = false
app.usdrt.scene_delegate.geometryStreaming.enabled = false
omnihydra.parallelHydraSprimSync = false
Expand Down
42 changes: 42 additions & 0 deletions exts/cesium.omniverse/mdl/cesium.mdl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,48 @@ module [[
anno::display_name("Cesium MDL functions")
]];

annotation annotation_not_connectable();

export float4 cesium_base_color_texture(
gltf_texture_lookup_value base_color_texture = gltf_texture_lookup_value()
[[
anno::hidden(),
annotation_not_connectable()
]]
)
[[
anno::display_name("Base color texture lookup color"),
anno::description("Returns the base color texture of the tile. Returns [0, 0, 0, 0] if the base color texture does not exist."),
anno::author("Cesium GS Inc."),
anno::in_group("Cesium functions"),
anno::ui_order(300)
]]
{
return base_color_texture.valid ? base_color_texture.value : float4(0.0);
}

export float4 cesium_imagery_layer(
gltf_texture_lookup_value imagery_layer = gltf_texture_lookup_value()
[[
anno::hidden(),
annotation_not_connectable()
]],
int imagery_layer_index = 0
[[
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, 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 ? imagery_layer.value : float4(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);
}
Expand Down
7 changes: 4 additions & 3 deletions src/core/include/cesium/omniverse/FabricMaterial.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class FabricMaterial {

private:
void initialize();
void initializeFromExistingMaterial(const omni::fabric::Path& path);

void createMaterial(const omni::fabric::Path& materialPath);
void createShader(const omni::fabric::Path& shaderPath, const omni::fabric::Path& materialPath);
Expand Down Expand Up @@ -76,9 +77,9 @@ class FabricMaterial {
const bool _debugRandomColors;
const long _stageId;

omni::fabric::Path _shaderPath;
omni::fabric::Path _baseColorTexturePath;
std::vector<omni::fabric::Path> _imageryLayerPaths;
std::vector<omni::fabric::Path> _shaderPaths;
std::vector<omni::fabric::Path> _baseColorTexturePaths;
std::vector<std::vector<omni::fabric::Path>> _imageryLayerPaths;

std::vector<omni::fabric::Path> _allPaths;
};
Expand Down
10 changes: 9 additions & 1 deletion src/core/include/cesium/omniverse/FabricMaterialDefinition.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@
#include <glm/glm.hpp>
#include <pxr/base/gf/vec2f.h>
#include <pxr/base/gf/vec3f.h>
#include <pxr/usd/sdf/path.h>

namespace cesium::omniverse {

struct MaterialInfo;

class FabricMaterialDefinition {
public:
FabricMaterialDefinition(const MaterialInfo& materialInfo, uint64_t imageryLayerCount, bool disableTextures);
FabricMaterialDefinition(
const MaterialInfo& materialInfo,
uint64_t imageryLayerCount,
bool disableTextures,
const pxr::SdfPath& tilesetMaterialPath);

[[nodiscard]] bool hasVertexColors() const;
[[nodiscard]] bool hasBaseColorTexture() const;
[[nodiscard]] uint64_t getImageryLayerCount() const;
[[nodiscard]] bool hasTilesetMaterial() const;
[[nodiscard]] const pxr::SdfPath& getTilesetMaterialPath() const;

// Make sure to update this function when adding new fields to the class
bool operator==(const FabricMaterialDefinition& other) const;
Expand All @@ -23,6 +30,7 @@ class FabricMaterialDefinition {
bool _hasVertexColors;
bool _hasBaseColorTexture;
uint64_t _imageryLayerCount;
pxr::SdfPath _tilesetMaterialPath;
};

} // namespace cesium::omniverse
8 changes: 6 additions & 2 deletions src/core/include/cesium/omniverse/FabricResourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ class FabricResourceManager {
bool smoothNormals,
long stageId);

std::shared_ptr<FabricMaterial>
acquireMaterial(const MaterialInfo& materialInfo, uint64_t imageryLayerCount, long stageId, int64_t tilesetId);
std::shared_ptr<FabricMaterial> acquireMaterial(
const MaterialInfo& materialInfo,
uint64_t imageryLayerCount,
long stageId,
int64_t tilesetId,
const pxr::SdfPath& tilesetMaterialPath);

std::shared_ptr<FabricTexture> acquireTexture();

Expand Down
6 changes: 5 additions & 1 deletion src/core/include/cesium/omniverse/FabricUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ void setTilesetTransform(int64_t tilesetId, const glm::dmat4& ecefToUsdTransform
void setTilesetId(const omni::fabric::Path& path, int64_t tilesetId);
omni::fabric::Path toFabricPath(const pxr::SdfPath& path);
omni::fabric::Path joinPaths(const omni::fabric::Path& absolutePath, const omni::fabric::Token& relativePath);
bool isEmpty(const omni::fabric::Path& path);
std::vector<omni::fabric::Path>
copyMaterial(const omni::fabric::Path& srcMaterialPath, const omni::fabric::Path& dstMaterialPath);
bool materialHasCesiumNodes(const omni::fabric::Path& path);
bool isCesiumNode(const omni::fabric::Token& mdlIdentifier);
omni::fabric::Token getMdlIdentifier(const omni::fabric::Path& path);

} // namespace cesium::omniverse::FabricUtil
5 changes: 5 additions & 0 deletions src/core/include/cesium/omniverse/Tokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ __pragma(warning(push)) __pragma(warning(disable : 4003))
#define USD_TOKENS \
(base_color_texture) \
(cesium) \
(cesium_base_color_texture) \
(cesium_imagery_layer) \
(cesium_imagery_layer_resolver) \
(cesium_material) \
(cesium_texture_lookup) \
Expand All @@ -23,6 +25,7 @@ __pragma(warning(push)) __pragma(warning(disable : 4003))
(extent) \
(faceVertexCounts) \
(faceVertexIndices) \
(imagery_layer) \
(imagery_layer_0) \
(imagery_layer_1) \
(imagery_layer_2) \
Expand Down Expand Up @@ -80,6 +83,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")) \
Expand All @@ -97,6 +101,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")) \
Expand Down
76 changes: 64 additions & 12 deletions src/core/src/FabricMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ FabricMaterial::FabricMaterial(
return;
}

initialize();
if (materialDefinition.hasTilesetMaterial()) {
const auto tilesetMaterialPath = FabricUtil::toFabricPath(materialDefinition.getTilesetMaterialPath());
initializeFromExistingMaterial(tilesetMaterialPath);
} else {
initialize();
}

reset();
}

Expand Down Expand Up @@ -95,36 +101,36 @@ void FabricMaterial::initialize() {

const auto shaderPath = FabricUtil::joinPaths(materialPath, FabricTokens::Shader);
createShader(shaderPath, materialPath);
_shaderPath = shaderPath;
_shaderPaths.push_back(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);
_baseColorTexturePath = baseColorTexturePath;
_baseColorTexturePaths.push_back(baseColorTexturePath);
_allPaths.push_back(baseColorTexturePath);
}

const auto imageryLayerCount = getImageryLayerCount(_materialDefinition);
_imageryLayerPaths.resize(imageryLayerCount);

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]);
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);
createImageryLayerResolver(
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);
}
}
Expand All @@ -134,6 +140,50 @@ void FabricMaterial::initialize() {
}
}

void FabricMaterial::initializeFromExistingMaterial(const omni::fabric::Path& srcMaterialPath) {
auto srw = UsdUtil::getFabricStageReaderWriter();

const auto& dstMaterialPath = _materialPath;

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);

const auto mdlIdentifier = FabricUtil::getMdlIdentifier(dstPath);

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(dstPath, FabricTokens::base_color_texture);
createTexture(baseColorTexturePath, dstPath, FabricTokens::inputs_base_color_texture);
_baseColorTexturePaths.push_back(baseColorTexturePath);
_allPaths.push_back(baseColorTexturePath);
}
} else if (mdlIdentifier == FabricTokens::cesium_imagery_layer) {
const auto imageryLayerIndexFabric =
srw.getAttributeRd<int>(dstPath, FabricTokens::inputs_imagery_layer_index);
const auto imageryLayerIndex =
static_cast<uint64_t>(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);
}
}
}

for (const auto& path : _allPaths) {
FabricResourceManager::getInstance().retainPath(path);
}
}

void FabricMaterial::createMaterial(const omni::fabric::Path& materialPath) {
auto srw = UsdUtil::getFabricStageReaderWriter();
srw.createPrim(materialPath);
Expand Down Expand Up @@ -319,7 +369,9 @@ void FabricMaterial::setMaterial(int64_t tilesetId, const MaterialInfo& material

auto srw = UsdUtil::getFabricStageReaderWriter();

setShaderValues(_shaderPath, materialInfo);
for (auto& shaderPath : _shaderPaths) {
setShaderValues(shaderPath, materialInfo);
}

for (const auto& path : _allPaths) {
FabricUtil::setTilesetId(path, tilesetId);
Expand All @@ -334,11 +386,9 @@ void FabricMaterial::setBaseColorTexture(
return;
}

if (FabricUtil::isEmpty(_baseColorTexturePath)) {
return;
for (auto& baseColorTexturePath : _baseColorTexturePaths) {
setTextureValues(baseColorTexturePath, textureAssetPathToken, textureInfo, texcoordIndex);
}

setTextureValues(_baseColorTexturePath, textureAssetPathToken, textureInfo, texcoordIndex);
}

void FabricMaterial::setImageryLayer(
Expand All @@ -354,7 +404,9 @@ void FabricMaterial::setImageryLayer(
return;
}

setTextureValues(_imageryLayerPaths[imageryLayerIndex], textureAssetPathToken, textureInfo, texcoordIndex);
for (auto& imageryLayerPath : _imageryLayerPaths[imageryLayerIndex]) {
setTextureValues(imageryLayerPath, textureAssetPathToken, textureInfo, texcoordIndex);
}
}

void FabricMaterial::clearMaterial() {
Expand Down
16 changes: 15 additions & 1 deletion src/core/src/FabricMaterialDefinition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ namespace cesium::omniverse {
FabricMaterialDefinition::FabricMaterialDefinition(
const MaterialInfo& materialInfo,
uint64_t imageryLayerCount,
bool disableTextures) {
bool disableTextures,
const pxr::SdfPath& tilesetMaterialPath) {

auto hasBaseColorTexture = materialInfo.baseColorTexture.has_value();

Expand All @@ -26,6 +27,7 @@ FabricMaterialDefinition::FabricMaterialDefinition(
_hasVertexColors = materialInfo.hasVertexColors;
_hasBaseColorTexture = hasBaseColorTexture;
_imageryLayerCount = imageryLayerCount;
_tilesetMaterialPath = tilesetMaterialPath;
}

bool FabricMaterialDefinition::hasVertexColors() const {
Expand All @@ -40,6 +42,14 @@ uint64_t FabricMaterialDefinition::getImageryLayerCount() const {
return _imageryLayerCount;
}

bool FabricMaterialDefinition::hasTilesetMaterial() const {
return !_tilesetMaterialPath.IsEmpty();
}

const pxr::SdfPath& FabricMaterialDefinition::getTilesetMaterialPath() const {
return _tilesetMaterialPath;
}

// In C++ 20 we can use the default equality comparison (= default)
bool FabricMaterialDefinition::operator==(const FabricMaterialDefinition& other) const {
if (_hasVertexColors != other._hasVertexColors) {
Expand All @@ -54,6 +64,10 @@ bool FabricMaterialDefinition::operator==(const FabricMaterialDefinition& other)
return false;
}

if (_tilesetMaterialPath != other._tilesetMaterialPath) {
return false;
}

return true;
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/src/FabricPrepareRenderResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ std::vector<FabricMesh> acquireFabricMeshes(
if (shouldAcquireMaterial) {
const auto materialInfo = GltfUtil::getMaterialInfo(model, primitive);

const auto fabricMaterial =
fabricResourceManager.acquireMaterial(materialInfo, imageryLayerCount, stageId, tileset.getTilesetId());
const auto fabricMaterial = fabricResourceManager.acquireMaterial(
materialInfo, imageryLayerCount, stageId, tileset.getTilesetId(), tilesetMaterialPath);

fabricMesh.material = fabricMaterial;
fabricMesh.materialInfo = materialInfo;
Expand Down
7 changes: 4 additions & 3 deletions src/core/src/FabricResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ bool FabricResourceManager::shouldAcquireMaterial(
}

if (!tilesetMaterialPath.IsEmpty()) {
return false;
return FabricUtil::materialHasCesiumNodes(FabricUtil::toFabricPath(tilesetMaterialPath));
}

return hasImagery || GltfUtil::hasMaterial(primitive);
Expand Down Expand Up @@ -181,8 +181,9 @@ std::shared_ptr<FabricMaterial> FabricResourceManager::acquireMaterial(
const MaterialInfo& materialInfo,
uint64_t imageryLayerCount,
long stageId,
int64_t tilesetId) {
FabricMaterialDefinition materialDefinition(materialInfo, imageryLayerCount, _disableTextures);
int64_t tilesetId,
const pxr::SdfPath& tilesetMaterialPath) {
FabricMaterialDefinition materialDefinition(materialInfo, imageryLayerCount, _disableTextures, tilesetMaterialPath);

if (useSharedMaterial(materialDefinition)) {
return acquireSharedMaterial(materialInfo, materialDefinition, stageId, tilesetId);
Expand Down
Loading

0 comments on commit ab9c82c

Please sign in to comment.