Skip to content

Commit

Permalink
Working
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Oct 18, 2023
1 parent 594eeee commit 6afb3c0
Show file tree
Hide file tree
Showing 12 changed files with 386 additions and 24 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
80 changes: 80 additions & 0 deletions exts/cesium.omniverse/mdl/cesium.mdl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,86 @@ 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);
}

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 @@ -47,6 +47,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 @@ -74,9 +75,9 @@ class FabricMaterial {
const pxr::TfToken _defaultTransparentTextureAssetPathToken;
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
2 changes: 2 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 Down
61 changes: 50 additions & 11 deletions src/core/src/FabricMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ FabricMaterial::FabricMaterial(
return;
}

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

reset();
}

Expand Down Expand Up @@ -88,13 +94,13 @@ 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);
}

Expand All @@ -103,7 +109,7 @@ void FabricMaterial::initialize() {
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.push_back({imageryLayerPath});
_allPaths.push_back(imageryLayerPath);
} else if (imageryLayerCount > 1) {
const auto imageryLayerResolverPath = FabricUtil::joinPaths(materialPath, FabricTokens::imagery_layer_resolver);
Expand All @@ -115,7 +121,7 @@ void FabricMaterial::initialize() {
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.push_back({imageryLayerPath});
_allPaths.push_back(imageryLayerPath);
}
}
Expand All @@ -125,6 +131,37 @@ 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);

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(dstMaterialPath, FabricTokens::base_color_texture);
createTexture(baseColorTexturePath, dstPath, FabricTokens::inputs_base_color_texture);
_allPaths.push_back(baseColorTexturePath);
_baseColorTexturePaths.push_back(baseColorTexturePath);
FabricResourceManager::getInstance().retainPath(baseColorTexturePath);
}
}
}

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 @@ -324,7 +361,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 @@ -339,11 +378,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 @@ -359,7 +396,9 @@ void FabricMaterial::setImageryLayer(
return;
}

setTextureValues(_imageryLayerPaths[imageryIndex], textureAssetPathToken, textureInfo, texcoordIndex);
for (auto& imageryLayerPath : _imageryLayerPaths[imageryIndex]) {
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 @@ -176,8 +176,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 6afb3c0

Please sign in to comment.