Skip to content

Commit

Permalink
POC texture array not working
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Aug 4, 2023
1 parent c2a1c2c commit fe22312
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 74 deletions.
40 changes: 40 additions & 0 deletions exts/cesium.omniverse/mdl/cesium.mdl
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,43 @@ export color cesium_base_color_texture(
{
return color(base_color_texture.value.x, base_color_texture.value.y, base_color_texture.value.z);
}

export gltf_texture_lookup_value cesium_read_from_texture_array(
uniform texture_2d[2] textures,
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("Read from texture array"),
anno::description("Read from texture array"),
anno::author("Cesium GS Inc."),
anno::in_group("Cesium functions"),
anno::ui_order(300)
]]
{
gltf_texture_lookup_value tex_ret;
tex_ret.valid = true;

int texture_index = 0;//::scene::data_lookup_int("texture_index");

for (int i = 0; i < 2; i++) {
if (i == texture_index) {
tex_ret.value = tex::lookup_float4(
tex: textures[i],
coord: world_coordinate_2d(min_world, max_world, up_axis),
wrap_u: ::tex::wrap_clamp,
wrap_v: ::tex::wrap_clamp);

return tex_ret;
}
}

tex_ret.value = tex::lookup_float4(
tex: textures[1],
coord: world_coordinate_2d(min_world, max_world, up_axis),
wrap_u: ::tex::wrap_clamp,
wrap_v: ::tex::wrap_clamp);

return tex_ret;
}
1 change: 0 additions & 1 deletion src/core/include/cesium/omniverse/FabricMaterial.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ 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
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 @@ -8,6 +8,7 @@
namespace cesium::omniverse::FabricTokens {
extern const omni::fabric::TokenC baseColorTex;
extern const omni::fabric::TokenC cesium_base_color_texture;
extern const omni::fabric::TokenC cesium_read_from_texture_array;
extern const omni::fabric::TokenC cesium_texture_lookup;
extern const omni::fabric::TokenC constant;
extern const omni::fabric::TokenC doubleSided;
Expand All @@ -34,6 +35,7 @@ extern const omni::fabric::TokenC inputs_roughness_factor;
extern const omni::fabric::TokenC inputs_scale;
extern const omni::fabric::TokenC inputs_tex_coord_index;
extern const omni::fabric::TokenC inputs_texture;
extern const omni::fabric::TokenC inputs_textures;
extern const omni::fabric::TokenC inputs_vertex_color_name;
extern const omni::fabric::TokenC inputs_wrap_s;
extern const omni::fabric::TokenC inputs_wrap_t;
Expand Down Expand Up @@ -75,6 +77,7 @@ extern const omni::fabric::TokenC _worldVisibility;
namespace cesium::omniverse::UsdTokens {
extern const pxr::TfToken& baseColorTex;
extern const pxr::TfToken& cesium_base_color_texture;
extern const pxr::TfToken& cesium_read_from_texture_array;
extern const pxr::TfToken& cesium_texture_lookup;
extern const pxr::TfToken& constant;
extern const pxr::TfToken& doubleSided;
Expand All @@ -101,6 +104,7 @@ extern const pxr::TfToken& inputs_roughness_factor;
extern const pxr::TfToken& inputs_scale;
extern const pxr::TfToken& inputs_tex_coord_index;
extern const pxr::TfToken& inputs_texture;
extern const pxr::TfToken& inputs_textures;
extern const pxr::TfToken& inputs_vertex_color_name;
extern const pxr::TfToken& inputs_wrap_s;
extern const pxr::TfToken& inputs_wrap_t;
Expand Down Expand Up @@ -161,6 +165,7 @@ const omni::fabric::Type inputs_roughness_factor(omni::fabric::BaseDataType::eFl
const omni::fabric::Type inputs_scale(omni::fabric::BaseDataType::eFloat, 2, 0, omni::fabric::AttributeRole::eNone);
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_textures(omni::fabric::BaseDataType::eAsset, 1, 1, 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);
Expand Down
86 changes: 13 additions & 73 deletions src/core/src/FabricMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,33 +98,6 @@ void FabricMaterial::initialize() {
}
}

void FabricMaterial::initializeFromExistingMaterial(const omni::fabric::Path& srcMaterialPath) {
const auto hasBaseColorTexture = _materialDefinition.hasBaseColorTexture();

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 (hasBaseColorTexture) {
// Create a base color texture node to fill the empty slot
const auto baseColorTexturePath = FabricUtil::joinPaths(dstMaterialPath, FabricTokens::baseColorTex);
createTexture(baseColorTexturePath, dstPath, FabricTokens::inputs_base_color_texture);
_allPaths.push_back(baseColorTexturePath);
_baseColorTexturePaths.push_back(baseColorTexturePath);
}
}
}
}

void FabricMaterial::createMaterial(const omni::fabric::Path& materialPath) {
auto srw = UsdUtil::getFabricStageReaderWriter();
srw.createPrim(materialPath);
Expand Down Expand Up @@ -221,51 +194,36 @@ void FabricMaterial::createTexture(
FabricAttributesBuilder attributes;

// clang-format off
attributes.addAttribute(FabricTypes::inputs_offset, FabricTokens::inputs_offset);
attributes.addAttribute(FabricTypes::inputs_rotation, FabricTokens::inputs_rotation);
attributes.addAttribute(FabricTypes::inputs_scale, FabricTokens::inputs_scale);
attributes.addAttribute(FabricTypes::inputs_tex_coord_index, FabricTokens::inputs_tex_coord_index);
attributes.addAttribute(FabricTypes::inputs_texture, FabricTokens::inputs_texture);
attributes.addAttribute(FabricTypes::inputs_wrap_s, FabricTokens::inputs_wrap_s);
attributes.addAttribute(FabricTypes::inputs_wrap_t, FabricTokens::inputs_wrap_t);
attributes.addAttribute(FabricTypes::inputs_excludeFromWhiteMode, FabricTokens::inputs_excludeFromWhiteMode);
attributes.addAttribute(FabricTypes::inputs_textures, FabricTokens::inputs_textures);
attributes.addAttribute(FabricTypes::outputs_out, FabricTokens::outputs_out);
attributes.addAttribute(FabricTypes::info_implementationSource, FabricTokens::info_implementationSource);
attributes.addAttribute(FabricTypes::info_mdl_sourceAsset, FabricTokens::info_mdl_sourceAsset);
attributes.addAttribute(FabricTypes::info_mdl_sourceAsset_subIdentifier, FabricTokens::info_mdl_sourceAsset_subIdentifier);
attributes.addAttribute(FabricTypes::_paramColorSpace, FabricTokens::_paramColorSpace);
attributes.addAttribute(FabricTypes::_sdrMetadata, FabricTokens::_sdrMetadata);
attributes.addAttribute(FabricTypes::Shader, FabricTokens::Shader);
attributes.addAttribute(FabricTypes::_cesium_tilesetId, FabricTokens::_cesium_tilesetId);
// clang-format on

attributes.createAttributes(texturePath);

// _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::inputs_textures, 2);
srw.setArrayAttributeSize(texturePath, FabricTokens::_sdrMetadata, 0);

// clang-format off
auto inputsExcludeFromWhiteModeFabric = srw.getAttributeWr<bool>(texturePath, FabricTokens::inputs_excludeFromWhiteMode);
auto infoImplementationSourceFabric = srw.getAttributeWr<omni::fabric::Token>(texturePath, FabricTokens::info_implementationSource);
auto infoMdlSourceAssetFabric = srw.getAttributeWr<omni::fabric::AssetPath>(texturePath, FabricTokens::info_mdl_sourceAsset);
auto infoMdlSourceAssetSubIdentifierFabric = srw.getAttributeWr<omni::fabric::Token>(texturePath, FabricTokens::info_mdl_sourceAsset_subIdentifier);
auto paramColorSpaceFabric = srw.getArrayAttributeWr<omni::fabric::Token>(texturePath, FabricTokens::_paramColorSpace);
// clang-format on

// For some reason, when a material network has both cesium nodes and glTF nodes it causes an infinite loop of
// material loading. If this material is initialized from a tileset material use the cesium wrapper nodes instead.
const auto& custom = _materialDefinition.hasTilesetMaterial();
const auto& assetPath = custom ? Context::instance().getCesiumMdlPathToken() : UsdTokens::gltf_pbr_mdl;
const auto& subIdentifier = custom ? FabricTokens::cesium_texture_lookup : FabricTokens::gltf_texture_lookup;
const auto& assetPath = Context::instance().getCesiumMdlPathToken();
const auto& subIdentifier = FabricTokens::cesium_read_from_texture_array;

*inputsExcludeFromWhiteModeFabric = false;
*infoImplementationSourceFabric = FabricTokens::sourceAsset;
infoMdlSourceAssetFabric->assetPath = assetPath;
infoMdlSourceAssetFabric->resolvedPath = pxr::TfToken();
*infoMdlSourceAssetSubIdentifierFabric = subIdentifier;
paramColorSpaceFabric[0] = FabricTokens::inputs_texture;
paramColorSpaceFabric[1] = FabricTokens::_auto;

// Create connection from shader to texture.
srw.createConnection(
Expand Down Expand Up @@ -329,36 +287,18 @@ void FabricMaterial::setTextureValues(
const omni::fabric::Path& texturePath,
const pxr::TfToken& textureAssetPathToken,
const TextureInfo& textureInfo) {
auto srw = UsdUtil::getFabricStageReaderWriter();

auto offset = textureInfo.offset;
auto rotation = textureInfo.rotation;
auto scale = textureInfo.scale;
(void)textureAssetPathToken;
(void)textureInfo;

if (!textureInfo.flipVertical) {
// gltf/pbr.mdl does texture transform math in glTF coordinates (top-left origin), so we needed to convert
// the translation and scale parameters to work in that space. This doesn't handle rotation yet because we
// haven't needed it for imagery layers.
offset = {offset.x, 1.0 - offset.y - scale.y};
scale = {scale.x, scale.y};
}
auto srw = UsdUtil::getFabricStageReaderWriter();

auto texturesFabric = srw.getArrayAttributeWr<omni::fabric::AssetPath>(texturePath, FabricTokens::inputs_textures);

auto textureFabric = srw.getAttributeWr<omni::fabric::AssetPath>(texturePath, FabricTokens::inputs_texture);
auto texCoordIndexFabric = srw.getAttributeWr<int>(texturePath, FabricTokens::inputs_tex_coord_index);
auto wrapSFabric = srw.getAttributeWr<int>(texturePath, FabricTokens::inputs_wrap_s);
auto wrapTFabric = srw.getAttributeWr<int>(texturePath, FabricTokens::inputs_wrap_t);
auto offsetFabric = srw.getAttributeWr<pxr::GfVec2f>(texturePath, FabricTokens::inputs_offset);
auto rotationFabric = srw.getAttributeWr<float>(texturePath, FabricTokens::inputs_rotation);
auto scaleFabric = srw.getAttributeWr<pxr::GfVec2f>(texturePath, FabricTokens::inputs_scale);

textureFabric->assetPath = textureAssetPathToken;
textureFabric->resolvedPath = pxr::TfToken();
*texCoordIndexFabric = static_cast<int>(textureInfo.setIndex);
*wrapSFabric = textureInfo.wrapS;
*wrapTFabric = textureInfo.wrapT;
*offsetFabric = UsdUtil::glmToUsdVector(glm::fvec2(offset));
*rotationFabric = static_cast<float>(rotation);
*scaleFabric = UsdUtil::glmToUsdVector(glm::fvec2(scale));
texturesFabric[0].assetPath = _textureAssetPathTokenRed;
texturesFabric[0].resolvedPath = pxr::TfToken();
texturesFabric[1].assetPath = _textureAssetPathTokenRed;
texturesFabric[1].resolvedPath = pxr::TfToken();
}

bool FabricMaterial::stageDestroyed() {
Expand Down
6 changes: 6 additions & 0 deletions src/core/src/Tokens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ TF_DEFINE_PRIVATE_TOKENS(
UsdTokens,
(baseColorTex)
(cesium_base_color_texture)
(cesium_read_from_texture_array)
(cesium_texture_lookup)
(constant)
(doubleSided)
Expand Down Expand Up @@ -66,6 +67,7 @@ TF_DEFINE_PRIVATE_TOKENS(
((inputs_scale, "inputs:scale"))
((inputs_tex_coord_index, "inputs:tex_coord_index"))
((inputs_texture, "inputs:texture"))
((inputs_textures, "inputs:textures"))
((inputs_vertex_color_name, "inputs:vertex_color_name"))
((inputs_wrap_s, "inputs:wrap_s"))
((inputs_wrap_t, "inputs:wrap_t"))
Expand All @@ -90,6 +92,7 @@ __pragma(warning(pop))
namespace cesium::omniverse::FabricTokens {
const omni::fabric::TokenC baseColorTex = omni::fabric::asInt(pxr::UsdTokens->baseColorTex);
const omni::fabric::TokenC cesium_base_color_texture = omni::fabric::asInt(pxr::UsdTokens->cesium_base_color_texture);
const omni::fabric::TokenC cesium_read_from_texture_array = omni::fabric::asInt(pxr::UsdTokens->cesium_read_from_texture_array);
const omni::fabric::TokenC cesium_texture_lookup = omni::fabric::asInt(pxr::UsdTokens->cesium_texture_lookup);
const omni::fabric::TokenC constant = omni::fabric::asInt(pxr::UsdTokens->constant);
const omni::fabric::TokenC doubleSided = omni::fabric::asInt(pxr::UsdTokens->doubleSided);
Expand All @@ -116,6 +119,7 @@ const omni::fabric::TokenC inputs_roughness_factor = omni::fabric::asInt(pxr::Us
const omni::fabric::TokenC inputs_scale = omni::fabric::asInt(pxr::UsdTokens->inputs_scale);
const omni::fabric::TokenC inputs_tex_coord_index = omni::fabric::asInt(pxr::UsdTokens->inputs_tex_coord_index);
const omni::fabric::TokenC inputs_texture = omni::fabric::asInt(pxr::UsdTokens->inputs_texture);
const omni::fabric::TokenC inputs_textures = omni::fabric::asInt(pxr::UsdTokens->inputs_textures);
const omni::fabric::TokenC inputs_vertex_color_name = omni::fabric::asInt(pxr::UsdTokens->inputs_vertex_color_name);
const omni::fabric::TokenC inputs_wrap_s = omni::fabric::asInt(pxr::UsdTokens->inputs_wrap_s);
const omni::fabric::TokenC inputs_wrap_t = omni::fabric::asInt(pxr::UsdTokens->inputs_wrap_t);
Expand Down Expand Up @@ -157,6 +161,7 @@ const omni::fabric::TokenC _worldVisibility = omni::fabric::asInt(pxr::UsdTokens
namespace cesium::omniverse::UsdTokens {
const pxr::TfToken& baseColorTex = pxr::UsdTokens->baseColorTex;
const pxr::TfToken& cesium_base_color_texture = pxr::UsdTokens->cesium_base_color_texture;
const pxr::TfToken& cesium_read_from_texture_array = pxr::UsdTokens->cesium_read_from_texture_array;
const pxr::TfToken& cesium_texture_lookup = pxr::UsdTokens->cesium_texture_lookup;
const pxr::TfToken& constant = pxr::UsdTokens->constant;
const pxr::TfToken& doubleSided = pxr::UsdTokens->doubleSided;
Expand All @@ -183,6 +188,7 @@ const pxr::TfToken& inputs_roughness_factor = pxr::UsdTokens->inputs_roughness_f
const pxr::TfToken& inputs_scale = pxr::UsdTokens->inputs_scale;
const pxr::TfToken& inputs_tex_coord_index = pxr::UsdTokens->inputs_tex_coord_index;
const pxr::TfToken& inputs_texture = pxr::UsdTokens->inputs_texture;
const pxr::TfToken& inputs_textures = pxr::UsdTokens->inputs_textures;
const pxr::TfToken& inputs_vertex_color_name = pxr::UsdTokens->inputs_vertex_color_name;
const pxr::TfToken& inputs_wrap_s = pxr::UsdTokens->inputs_wrap_s;
const pxr::TfToken& inputs_wrap_t = pxr::UsdTokens->inputs_wrap_t;
Expand Down

0 comments on commit fe22312

Please sign in to comment.