Skip to content

Commit

Permalink
Start implementing
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Oct 6, 2023
1 parent 8a803a9 commit 67dbdb9
Show file tree
Hide file tree
Showing 16 changed files with 218 additions and 107 deletions.
6 changes: 4 additions & 2 deletions src/core/include/cesium/omniverse/FabricGeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ class FabricGeometry {
const glm::dmat4& nodeTransform,
const CesiumGltf::Model& model,
const CesiumGltf::MeshPrimitive& primitive,
bool smoothNormals,
bool hasImagery);
bool smoothNormals);

void setActive(bool active);
void setVisibility(bool visible);
Expand All @@ -49,6 +48,9 @@ class FabricGeometry {
const FabricGeometryDefinition _geometryDefinition;
const bool _debugRandomColors;
const long _stageId;

// TODO: Need function that can map glTF set index to primvar st index. Needed for both regular texcoords and imagery texcoords.
// TODO: this makes the material -> primvar association work
};

} // namespace cesium::omniverse
4 changes: 2 additions & 2 deletions src/core/include/cesium/omniverse/FabricGeometryDefinition.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ class FabricGeometryDefinition {
const CesiumGltf::MeshPrimitive& primitive,
bool smoothNormals);

[[nodiscard]] bool hasTexcoords() const;
[[nodiscard]] bool hasNormals() const;
[[nodiscard]] bool hasVertexColors() const;
[[nodiscard]] bool getDoubleSided() const;
[[nodiscard]] uint64_t getTexcoordSetCount() const;

bool operator==(const FabricGeometryDefinition& other) const;

private:
bool _hasTexcoords{false};
bool _hasNormals{false};
bool _hasVertexColors{false};
bool _doubleSided{false};
uint64_t _texcoordSetCount{0};
};

} // namespace cesium::omniverse
4 changes: 2 additions & 2 deletions src/core/include/cesium/omniverse/FabricMaterial.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class FabricMaterial {
~FabricMaterial();

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

void clearMaterial();
void clearBaseColorTexture();
void clearBaseColorTexture(uint64_t index);

void setActive(bool active);

Expand Down
6 changes: 3 additions & 3 deletions src/core/include/cesium/omniverse/FabricMaterialDefinition.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ struct MaterialInfo;

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

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

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

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

} // namespace cesium::omniverse
2 changes: 1 addition & 1 deletion src/core/include/cesium/omniverse/FabricResourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class FabricResourceManager {
long stageId);

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

std::shared_ptr<FabricTexture> acquireTexture();

Expand Down
3 changes: 3 additions & 0 deletions src/core/include/cesium/omniverse/GltfUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,7 @@ bool hasImageryTexcoords(const CesiumGltf::Model& model, const CesiumGltf::MeshP
bool hasVertexColors(const CesiumGltf::Model& model, const CesiumGltf::MeshPrimitive& primitive, uint64_t setIndex);
bool hasMaterial(const CesiumGltf::MeshPrimitive& primitive);

std::vector<uint64_t> getTexcoordSetIndexes(const CesiumGltf::Model& model, const CesiumGltf::MeshPrimitive& primitive);
std::vector<uint64_t> getImageryTexcoordSetIndexes(const CesiumGltf::Model& model, const CesiumGltf::MeshPrimitive& primitive);

} // namespace cesium::omniverse::GltfUtil
3 changes: 3 additions & 0 deletions src/core/include/cesium/omniverse/OmniTileset.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <vector>

namespace Cesium3DTilesSelection {
class RasterOverlay;
class Tileset;
class ViewState;
class ViewUpdateResult;
Expand Down Expand Up @@ -72,6 +73,8 @@ class OmniTileset {

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

private:
Expand Down
25 changes: 24 additions & 1 deletion src/core/include/cesium/omniverse/Tokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,16 @@ __pragma(warning(push)) __pragma(warning(disable : 4003))
((primvars_displayColor, "primvars:displayColor")) \
((primvars_displayOpacity, "primvars:displayOpacity")) \
((primvars_normals, "primvars:normals")) \
((primvars_st, "primvars:st")) \
((primvars_st_0, "primvars:st_0")) \
((primvars_st_1, "primvars:st_1")) \
((primvars_st_2, "primvars:st_2")) \
((primvars_st_3, "primvars:st_3")) \
((primvars_st_4, "primvars:st_4")) \
((primvars_st_5, "primvars:st_5")) \
((primvars_st_6, "primvars:st_6")) \
((primvars_st_7, "primvars:st_7")) \
((primvars_st_8, "primvars:st_8")) \
((primvars_st_9, "primvars:st_9")) \
((primvars_vertexColor, "primvars:vertexColor")) \
((xformOp_transform_cesium, "xformOp:transform:cesium"))

Expand Down Expand Up @@ -104,6 +113,20 @@ __pragma(warning(pop))

namespace cesium::omniverse::FabricTokens {
FABRIC_DECLARE_TOKENS(USD_TOKENS);

const std::array<const omni::fabric::TokenC, 10> primvars_st_n = {{
primvars_st_0,
primvars_st_1,
primvars_st_2,
primvars_st_3,
primvars_st_4,
primvars_st_5,
primvars_st_6,
primvars_st_7,
primvars_st_8,
primvars_st_9,
}};

}

namespace cesium::omniverse::FabricTypes {
Expand Down
56 changes: 33 additions & 23 deletions src/core/src/FabricGeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ void FabricGeometry::setMaterial(const omni::fabric::Path& materialPath) {
}

void FabricGeometry::initialize() {
const auto hasTexcoords = _geometryDefinition.hasTexcoords();
const auto hasNormals = _geometryDefinition.hasNormals();
const auto hasVertexColors = _geometryDefinition.hasVertexColors();
const auto doubleSided = _geometryDefinition.getDoubleSided();
const auto texcoordSetCount = _geometryDefinition.getTexcoordSetCount();

auto srw = UsdUtil::getFabricStageReaderWriter();

Expand All @@ -132,8 +132,10 @@ void FabricGeometry::initialize() {
attributes.addAttribute(FabricTypes::subdivisionScheme, FabricTokens::subdivisionScheme);
attributes.addAttribute(FabricTypes::material_binding, FabricTokens::material_binding);

if (hasTexcoords) {
attributes.addAttribute(FabricTypes::primvars_st, FabricTokens::primvars_st);
assert(texcoordSetCount <= FabricTokens::primvars_st_n.size());

for (uint64_t i = 0; i < texcoordSetCount; i++) {
attributes.addAttribute(FabricTypes::primvars_st, FabricTokens::primvars_st_n[i]);
}

if (hasNormals) {
Expand All @@ -156,15 +158,17 @@ void FabricGeometry::initialize() {

// Initialize primvars
size_t primvarsCount = 0;
size_t primvarIndexSt = 0;
size_t primvarIndexNormal = 0;
size_t primvarIndexVertexColor = 0;

const size_t primvarIndexDisplayColor = primvarsCount++;
const size_t primvarIndexDisplayOpacity = primvarsCount++;

if (hasTexcoords) {
primvarIndexSt = primvarsCount++;
std::vector<uint64_t> primvarIndexStArray;
primvarIndexStArray.reserve(texcoordSetCount);

for (uint64_t i = 0; i < texcoordSetCount; i++) {
primvarIndexStArray.push_back(primvarsCount++);
}

if (hasNormals) {
Expand All @@ -191,9 +195,9 @@ void FabricGeometry::initialize() {
primvarInterpolationsFabric[primvarIndexDisplayColor] = FabricTokens::constant;
primvarInterpolationsFabric[primvarIndexDisplayOpacity] = FabricTokens::constant;

if (hasTexcoords) {
primvarsFabric[primvarIndexSt] = FabricTokens::primvars_st;
primvarInterpolationsFabric[primvarIndexSt] = FabricTokens::vertex;
for (uint64_t i = 0; i < texcoordSetCount; i++) {
primvarsFabric[primvarIndexStArray[i]] = FabricTokens::primvars_st_n[i];
primvarInterpolationsFabric[primvarIndexStArray[i]] = FabricTokens::vertex;
}

if (hasNormals) {
Expand All @@ -208,9 +212,9 @@ void FabricGeometry::initialize() {
}

void FabricGeometry::reset() {
const auto hasTexcoords = _geometryDefinition.hasTexcoords();
const auto hasNormals = _geometryDefinition.hasNormals();
const auto hasVertexColors = _geometryDefinition.hasVertexColors();
const auto texcoordSetCount = _geometryDefinition.getTexcoordSetCount();

auto srw = UsdUtil::getFabricStageReaderWriter();

Expand Down Expand Up @@ -243,8 +247,8 @@ void FabricGeometry::reset() {
srw.setArrayAttributeSize(_path, FabricTokens::faceVertexIndices, 0);
srw.setArrayAttributeSize(_path, FabricTokens::points, 0);

if (hasTexcoords) {
srw.setArrayAttributeSize(_path, FabricTokens::primvars_st, 0);
for (uint64_t i = 0; i < texcoordSetCount; i++) {
srw.setArrayAttributeSize(_path, FabricTokens::primvars_st_n[i], 0);
}

if (hasNormals) {
Expand All @@ -263,25 +267,23 @@ void FabricGeometry::setGeometry(
const glm::dmat4& nodeTransform,
const CesiumGltf::Model& model,
const CesiumGltf::MeshPrimitive& primitive,
bool smoothNormals,
bool hasImagery) {
bool smoothNormals) {

if (stageDestroyed()) {
return;
}

const auto hasTexcoords = _geometryDefinition.hasTexcoords();
const auto hasNormals = _geometryDefinition.hasNormals();
const auto hasVertexColors = _geometryDefinition.hasVertexColors();
const auto& texcoordSetIndexes = GltfUtil::getTexcoordSetIndexes(model, primitive);
const auto& imageryTexcoordSetIndexes = GltfUtil::getImageryTexcoordSetIndexes(model, primitive);

auto srw = UsdUtil::getFabricStageReaderWriter();

const auto positions = GltfUtil::getPositions(model, primitive);
const auto indices = GltfUtil::getIndices(model, primitive, positions);
const auto normals = GltfUtil::getNormals(model, primitive, positions, indices, smoothNormals);
const auto vertexColors = GltfUtil::getVertexColors(model, primitive, 0);
const auto texcoords_0 = GltfUtil::getTexcoords(model, primitive, 0);
const auto imageryTexcoords = GltfUtil::getImageryTexcoords(model, primitive, 0);
const auto extent = GltfUtil::getExtent(model, primitive);
const auto faceVertexCounts = GltfUtil::getFaceVertexCounts(indices);

Expand Down Expand Up @@ -398,14 +400,22 @@ void FabricGeometry::setGeometry(
indices.fill(faceVertexIndicesFabric);
positions.fill(pointsFabric);

if (hasTexcoords) {
const auto& texcoords = hasImagery ? imageryTexcoords : texcoords_0;

srw.setArrayAttributeSize(_path, FabricTokens::primvars_st, texcoords.size());
const auto fillTexcoords = [&](const omni::fabric::Token& primvarStToken, const TexcoordsAccessor& texcoords) {
srw.setArrayAttributeSize(_path, primvarStToken, texcoords.size());
auto stFabric = srw.getArrayAttributeWr<glm::fvec2>(_path, primvarStToken);
texcoords.fill(stFabric);
};

auto stFabric = srw.getArrayAttributeWr<glm::fvec2>(_path, FabricTokens::primvars_st);
for (uint64_t i = 0; i < texcoordSetIndexes.size(); i++) {
const auto& primvarStToken = FabricTokens::primvars_st_n[i];
const auto texcoords = GltfUtil::getTexcoords(model, primitive, texcoordSetIndexes[i]);
fillTexcoords(primvarStToken, texcoords);
}

texcoords.fill(stFabric);
for (uint64_t i = 0; i < imageryTexcoordSetIndexes.size(); i++) {
const auto& primvarStToken = FabricTokens::primvars_st_n[texcoordSetIndexes.size() + i];
const auto texcoords = GltfUtil::getImageryTexcoords(model, primitive, imageryTexcoordSetIndexes[i]);
fillTexcoords(primvarStToken, texcoords);
}

if (hasNormals) {
Expand Down
24 changes: 10 additions & 14 deletions src/core/src/FabricGeometryDefinition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,12 @@ FabricGeometryDefinition::FabricGeometryDefinition(
const CesiumGltf::Model& model,
const CesiumGltf::MeshPrimitive& primitive,
bool smoothNormals) {

const auto hasPrimitiveSt = GltfUtil::hasTexcoords(model, primitive, 0);
const auto hasImagerySt = GltfUtil::hasImageryTexcoords(model, primitive, 0);

const auto materialInfo = GltfUtil::getMaterialInfo(model, primitive);

_hasTexcoords = hasPrimitiveSt || hasImagerySt;
_hasNormals = GltfUtil::hasNormals(model, primitive, smoothNormals);
_hasVertexColors = GltfUtil::hasVertexColors(model, primitive, 0);
_doubleSided = materialInfo.doubleSided;
}

bool FabricGeometryDefinition::hasTexcoords() const {
return _hasTexcoords;
_texcoordSetCount = GltfUtil::getTexcoordSetIndexes(model, primitive).size() +
GltfUtil::getImageryTexcoordSetIndexes(model, primitive).size();
}

bool FabricGeometryDefinition::hasNormals() const {
Expand All @@ -43,11 +35,11 @@ bool FabricGeometryDefinition::getDoubleSided() const {
return _doubleSided;
}

bool FabricGeometryDefinition::operator==(const FabricGeometryDefinition& other) const {
if (_hasTexcoords != other._hasTexcoords) {
return false;
}
[[nodiscard]] uint64_t FabricGeometryDefinition::getTexcoordSetCount() const {
return _texcoordSetCount;
}

bool FabricGeometryDefinition::operator==(const FabricGeometryDefinition& other) const {
if (_hasNormals != other._hasNormals) {
return false;
}
Expand All @@ -60,6 +52,10 @@ bool FabricGeometryDefinition::operator==(const FabricGeometryDefinition& other)
return false;
}

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

return true;
}

Expand Down
Loading

0 comments on commit 67dbdb9

Please sign in to comment.