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 d7fea97
Show file tree
Hide file tree
Showing 12 changed files with 425 additions and 25 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
101 changes: 101 additions & 0 deletions exts/cesium.omniverse/mdl/cesium.mdl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,107 @@ module [[
anno::display_name("Cesium MDL functions")
]];

export enum up_axis_mode {
Y,
Z
};

float2 world_coordinate_2d(float2 min_world, float2 max_world, up_axis_mode up_axis)
{
// Get the world pos of the pixel
float3 world_pos = state::transform_vector(state::coordinate_internal, state::coordinate_world, state::position());
float2 world_pos_horizontal = float2(world_pos.x, up_axis == Y ? world_pos.z : world_pos.y);

// Return 0-1 UVs based on the min/max world coordinates provided
return (world_pos_horizontal - min_world) / (max_world - min_world);
}

export float4 cesium_lookup_world_texture_float4(uniform texture_2d texture = texture_2d(), float2 min_world = float2(-5000.0, -5000.0), float2 max_world = float2(5000.0, 5000.0), up_axis_mode up_axis = Y)
[[
anno::display_name("World-mapped texture lookup float4"),
anno::description("Returns float4 from a texture mapped to world UV coordinates"),
anno::author("Cesium GS Inc."),
anno::in_group("Cesium functions"),
anno::ui_order(300)
]]
{
return tex::lookup_float4(
tex: texture,
coord: world_coordinate_2d(min_world, max_world, up_axis),
wrap_u: ::tex::wrap_clamp,
wrap_v: ::tex::wrap_clamp);
}

export float3 cesium_lookup_world_texture_float3(uniform texture_2d texture = texture_2d(), float2 min_world = float2(-5000.0, -5000.0), float2 max_world = float2(5000.0, 5000.0), up_axis_mode up_axis = Y)
[[
anno::display_name("World-mapped texture lookup float3"),
anno::description("Returns float3 from a texture mapped to world UV coordinates"),
anno::author("Cesium GS Inc."),
anno::in_group("Cesium functions"),
anno::ui_order(300)
]]
{
return tex::lookup_float3(
tex: texture,
coord: world_coordinate_2d(min_world, max_world, up_axis),
wrap_u: ::tex::wrap_clamp,
wrap_v: ::tex::wrap_clamp);
}

export color cesium_lookup_world_texture_color(uniform texture_2d texture = texture_2d(), float2 min_world = float2(-5000.0, -5000.0), float2 max_world = float2(5000.0, 5000.0), up_axis_mode up_axis = Y)
[[
anno::display_name("World-mapped texture lookup color"),
anno::description("Returns color from a texture mapped to world UV coordinates"),
anno::author("Cesium GS Inc."),
anno::in_group("Cesium functions"),
anno::ui_order(300)
]]
{
return tex::lookup_color(
tex: texture,
coord: world_coordinate_2d(min_world, max_world, up_axis),
wrap_u: ::tex::wrap_clamp,
wrap_v: ::tex::wrap_clamp);
}

export color cesium_base_color_texture(
gltf_texture_lookup_value base_color_texture
[[
anno::hidden()
]]
)
[[
anno::display_name("Base color texture lookup color"),
anno::description("Returns the base color texture of the tile. Returns [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 ? 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);
}
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
5 changes: 5 additions & 0 deletions src/core/include/cesium/omniverse/FabricUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,10 @@ 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
Loading

0 comments on commit d7fea97

Please sign in to comment.