Skip to content

Commit

Permalink
Separate base color texture and imagery layers
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Oct 18, 2023
1 parent a848096 commit 23c2780
Show file tree
Hide file tree
Showing 12 changed files with 294 additions and 265 deletions.
135 changes: 83 additions & 52 deletions exts/cesium.omniverse/mdl/cesium.mdl
Original file line number Diff line number Diff line change
Expand Up @@ -11,64 +11,95 @@ module [[
anno::display_name("Cesium MDL functions")
]];

// For internal use only
float4 alpha_blend(float4 src, float4 dst) {
return src * float4(src.w, src.w, src.w, 1.0) + dst * (1.0 - src.w);
}

export gltf_texture_lookup_value cesium_texture_lookup(*) [[ anno::hidden() ]] = gltf_texture_lookup();
export material cesium_material(*) [[ anno::hidden() ]] = gltf_material();

export gltf_texture_lookup_value cesium_texture_array_lookup(
uniform int texture_count = 0,
gltf_texture_lookup_value texture_0 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_1 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_2 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_3 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_4 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_5 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_6 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_7 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_8 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_9 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_10 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_11 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_12 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_13 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_14 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_15 = gltf_texture_lookup()
) [[ anno::hidden() ]]
{
// The array length should match MAX_TEXTURE_LAYER_COUNT in Tokens.h
gltf_texture_lookup_value[16] texture_values(
texture_0,
texture_1,
texture_2,
texture_3,
texture_4,
texture_5,
texture_6,
texture_7,
texture_8,
texture_9,
texture_10,
texture_11,
texture_12,
texture_13,
texture_14,
texture_15,
export material cesium_material(
gltf_texture_lookup_value imagery_layers_texture = gltf_texture_lookup_value(true, float4(0.0)),
// gltf_material inputs below
gltf_texture_lookup_value base_color_texture = gltf_texture_lookup_value(),
uniform color base_color_factor = color(1.0),
uniform float metallic_factor = 1.0,
uniform float roughness_factor = 1.0,
uniform color emissive_factor = color(0.0),
uniform gltf_alpha_mode alpha_mode = opaque,
uniform float base_alpha = 1.0,
uniform float alpha_cutoff = 0.5
) [[ anno::hidden() ]] = let {
auto base_color_texture_value = base_color_texture.valid ? base_color_texture.value : float4(1.0);
auto final_base_color_texture_value = alpha_blend(imagery_layers_texture.value, base_color_texture_value);
auto final_base_color_texture = gltf_texture_lookup_value(true, final_base_color_texture_value);
auto final_base_color_factor = base_color_factor;

material base = gltf_material(
base_color_factor: final_base_color_factor,
base_color_texture: final_base_color_texture,
metallic_factor: metallic_factor,
roughness_factor: roughness_factor,
emissive_factor: emissive_factor,
alpha_mode: alpha_mode,
base_alpha: base_alpha,
alpha_cutoff: alpha_cutoff
);

float3 final = float3(1.0, 1.0, 1.0);
} in material(
thin_walled: base.thin_walled,
surface: base.surface,
volume: base.volume,
ior: base.ior,
geometry: base.geometry
);

for (int i = 0; i < texture_count; i++) {
gltf_texture_lookup_value value = texture_values[i];
if (value.valid) {
float3 rgb = float3(value.value[0], value.value[1], value.value[2]);
float alpha = value.value[3];
final = alpha * rgb + (1.0 - alpha) * final;
export gltf_texture_lookup_value cesium_imagery_layer_resolver(
uniform int imagery_layers_count = 0,
gltf_texture_lookup_value imagery_layer_0 = gltf_texture_lookup(),
gltf_texture_lookup_value imagery_layer_1 = gltf_texture_lookup(),
gltf_texture_lookup_value imagery_layer_2 = gltf_texture_lookup(),
gltf_texture_lookup_value imagery_layer_3 = gltf_texture_lookup(),
gltf_texture_lookup_value imagery_layer_4 = gltf_texture_lookup(),
gltf_texture_lookup_value imagery_layer_5 = gltf_texture_lookup(),
gltf_texture_lookup_value imagery_layer_6 = gltf_texture_lookup(),
gltf_texture_lookup_value imagery_layer_7 = gltf_texture_lookup(),
gltf_texture_lookup_value imagery_layer_8 = gltf_texture_lookup(),
gltf_texture_lookup_value imagery_layer_9 = gltf_texture_lookup(),
gltf_texture_lookup_value imagery_layer_10 = gltf_texture_lookup(),
gltf_texture_lookup_value imagery_layer_11 = gltf_texture_lookup(),
gltf_texture_lookup_value imagery_layer_12 = gltf_texture_lookup(),
gltf_texture_lookup_value imagery_layer_13 = gltf_texture_lookup(),
gltf_texture_lookup_value imagery_layer_14 = gltf_texture_lookup(),
gltf_texture_lookup_value imagery_layer_15 = gltf_texture_lookup()
) [[ anno::hidden() ]] {
// The array length should match MAX_IMAGERY_LAYERS_COUNT in Tokens.h
gltf_texture_lookup_value[16] imagery_layers(
imagery_layer_0,
imagery_layer_1,
imagery_layer_2,
imagery_layer_3,
imagery_layer_4,
imagery_layer_5,
imagery_layer_6,
imagery_layer_7,
imagery_layer_8,
imagery_layer_9,
imagery_layer_10,
imagery_layer_11,
imagery_layer_12,
imagery_layer_13,
imagery_layer_14,
imagery_layer_15,
);

auto resolved_value = float4(0.0);

for (int i = 0; i < imagery_layers_count; i++) {
auto imagery_layer = imagery_layers[i];
if (imagery_layer.valid) {
resolved_value = alpha_blend(imagery_layer.value, resolved_value);
}
}

gltf_texture_lookup_value tex_ret;
tex_ret.value = float4(final[0], final[1], final[2], 1.0);
tex_ret.valid = true;

return tex_ret;
return gltf_texture_lookup_value(true, resolved_value);
}
20 changes: 13 additions & 7 deletions src/core/include/cesium/omniverse/FabricMaterial.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,19 @@ class FabricMaterial {

void setMaterial(int64_t tilesetId, const MaterialInfo& materialInfo);
void setBaseColorTexture(
const pxr::TfToken& textureAssetPathToken,
const TextureInfo& textureInfo,
uint64_t texcoordIndex);
void setImageryLayer(
const pxr::TfToken& textureAssetPathToken,
const TextureInfo& textureInfo,
uint64_t texcoordIndex,
uint64_t textureIndex);
uint64_t imageryLayerIndex);

void clearMaterial();
void clearBaseColorTexture(uint64_t textureIndex);
void clearBaseColorTexture();
void clearImageryLayer(uint64_t imageryLayerIndex);
void clearImageryLayers();

void setActive(bool active);

Expand All @@ -48,8 +54,8 @@ class FabricMaterial {
const omni::fabric::Path& texturePath,
const omni::fabric::Path& shaderPath,
const omni::fabric::Token& shaderInput);
void createTextureArray(
const omni::fabric::Path& texturePath,
void createImageryLayerResolver(
const omni::fabric::Path& imageryLayerResolverPath,
const omni::fabric::Path& shaderPath,
const omni::fabric::Token& shaderInput,
uint64_t textureCount);
Expand All @@ -60,9 +66,7 @@ class FabricMaterial {
const pxr::TfToken& textureAssetPathToken,
const TextureInfo& textureInfo,
uint64_t texcoordIndex);
void setTilesetId(int64_t tilesetId);
bool stageDestroyed();
void clearBaseColorTextures();

omni::fabric::Path _materialPath;
const FabricMaterialDefinition _materialDefinition;
Expand All @@ -72,7 +76,9 @@ class FabricMaterial {

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

std::vector<omni::fabric::Path> _allPaths;
};

} // namespace cesium::omniverse
7 changes: 4 additions & 3 deletions src/core/include/cesium/omniverse/FabricMaterialDefinition.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ class FabricMaterialDefinition {
FabricMaterialDefinition(const MaterialInfo& materialInfo, uint64_t imageryLayerCount, bool disableTextures);

[[nodiscard]] bool hasVertexColors() const;
[[nodiscard]] uint64_t getBaseColorTextureCount() const;
[[nodiscard]] bool hasBaseColorTextures() const;
[[nodiscard]] bool hasBaseColorTexture() const;
[[nodiscard]] uint64_t getImageryLayerCount() const;

// Make sure to update this function when adding new fields to the class
bool operator==(const FabricMaterialDefinition& other) const;

private:
bool _hasVertexColors;
uint64_t _baseColorTextureCount;
bool _hasBaseColorTexture;
uint64_t _imageryLayerCount;
};

} // namespace cesium::omniverse
1 change: 1 addition & 0 deletions src/core/include/cesium/omniverse/FabricUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ 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);

} // namespace cesium::omniverse::FabricUtil
3 changes: 2 additions & 1 deletion src/core/include/cesium/omniverse/OmniTileset.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ class OmniTileset {

void reload();
void addImageryIon(const pxr::SdfPath& imageryPath);
[[nodiscard]] std::optional<uint64_t> findImageryIndex(const Cesium3DTilesSelection::RasterOverlay& overlay) const;
[[nodiscard]] std::optional<uint64_t>
findImageryLayerIndex(const Cesium3DTilesSelection::RasterOverlay& overlay) const;
[[nodiscard]] uint64_t getImageryLayerCount() const;
void onUpdateFrame(const std::vector<Viewport>& viewports);

Expand Down
Loading

0 comments on commit 23c2780

Please sign in to comment.