Skip to content

Commit

Permalink
Add support for tileset materials
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Jul 12, 2023
1 parent 80a0502 commit 8f67dae
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 20 deletions.
2 changes: 1 addition & 1 deletion exts/cesium.usd.plugins/schemas/cesium_schemas.usda
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class CesiumGeoreferencePrim "CesiumGeoreferencePrim" (

class CesiumTilesetPrim "CesiumTilesetPrim" (
doc = """A prim representing a tileset."""
inherits = </Boundable>
inherits = </Gprim>
customData = {
string className = "Tileset"

Expand Down
5 changes: 4 additions & 1 deletion src/core/include/cesium/omniverse/FabricResourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ class FabricResourceManager {
return instance;
}

bool shouldAcquireMaterial(const CesiumGltf::MeshPrimitive& primitive, bool hasImagery) const;
bool shouldAcquireMaterial(
const CesiumGltf::MeshPrimitive& primitive,
bool hasImagery,
const pxr::SdfPath& materialPath) const;

std::shared_ptr<FabricGeometry>
acquireGeometry(const CesiumGltf::Model& model, const CesiumGltf::MeshPrimitive& primitive, bool smoothNormals);
Expand Down
1 change: 1 addition & 0 deletions src/core/include/cesium/omniverse/OmniTileset.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class OmniTileset {
[[nodiscard]] float getMainThreadLoadingTimeLimit() const;
[[nodiscard]] bool getShowCreditsOnScreen() const;
[[nodiscard]] pxr::CesiumGeoreference getGeoreference() const;
[[nodiscard]] pxr::SdfPath getMaterialPath() const;

[[nodiscard]] int64_t getTilesetId() const;
[[nodiscard]] TilesetStatistics getStatistics() const;
Expand Down
4 changes: 3 additions & 1 deletion src/core/src/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "cesium/omniverse/OmniImagery.h"
#include "cesium/omniverse/OmniTileset.h"
#include "cesium/omniverse/TaskProcessor.h"
#include "cesium/omniverse/Tokens.h"
#include "cesium/omniverse/UsdUtil.h"

#ifdef CESIUM_OMNI_MSVC
Expand Down Expand Up @@ -317,7 +318,8 @@ void Context::processCesiumTilesetChanged(const ChangedPrim& changedPrim) {
name == pxr::CesiumTokens->cesiumCulledScreenSpaceError ||
name == pxr::CesiumTokens->cesiumSmoothNormals ||
name == pxr::CesiumTokens->cesiumMainThreadLoadingTimeLimit ||
name == pxr::CesiumTokens->cesiumShowCreditsOnScreen) {
name == pxr::CesiumTokens->cesiumShowCreditsOnScreen ||
name == UsdTokens::material_binding) {
tileset.value()->reload();
}
// clang-format on
Expand Down
24 changes: 16 additions & 8 deletions src/core/src/FabricPrepareRenderResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <Cesium3DTilesSelection/Tile.h>
#include <Cesium3DTilesSelection/Tileset.h>
#include <CesiumAsync/AsyncSystem.h>
#include <omni/fabric/FabricUSD.h>
#include <omni/ui/ImageProvider/DynamicTextureProvider.h>

namespace cesium::omniverse {
Expand Down Expand Up @@ -93,8 +94,11 @@ gatherMeshes(const OmniTileset& tileset, const glm::dmat4& tileTransform, const
return meshes;
}

std::vector<FabricMesh>
acquireFabricMeshes(const CesiumGltf::Model& model, const std::vector<MeshInfo>& meshes, bool hasImagery) {
std::vector<FabricMesh> acquireFabricMeshes(
const CesiumGltf::Model& model,
const std::vector<MeshInfo>& meshes,
bool hasImagery,
const OmniTileset& tileset) {
CESIUM_TRACE("FabricPrepareRenderResources::acquireFabricMeshes");
std::vector<FabricMesh> fabricMeshes;
fabricMeshes.reserve(meshes.size());
Expand All @@ -108,8 +112,8 @@ acquireFabricMeshes(const CesiumGltf::Model& model, const std::vector<MeshInfo>&
const auto fabricGeometry = fabricResourceManager.acquireGeometry(model, primitive, mesh.smoothNormals);
fabricMesh.geometry = fabricGeometry;

const auto shouldAcquireMaterial =
FabricResourceManager::getInstance().shouldAcquireMaterial(primitive, hasImagery);
const auto shouldAcquireMaterial = FabricResourceManager::getInstance().shouldAcquireMaterial(
primitive, hasImagery, tileset.getMaterialPath());

if (shouldAcquireMaterial) {
const auto materialInfo = GltfUtil::getMaterialInfo(model, primitive);
Expand Down Expand Up @@ -151,11 +155,13 @@ void setFabricMeshes(
const CesiumGltf::Model& model,
const std::vector<MeshInfo>& meshes,
std::vector<FabricMesh>& fabricMeshes,
bool hasImagery) {
bool hasImagery,
const OmniTileset& tileset) {
CESIUM_TRACE("FabricPrepareRenderResources::setFabricMeshes");
for (size_t i = 0; i < meshes.size(); i++) {
const auto& meshInfo = meshes[i];
const auto& primitive = model.meshes[meshInfo.meshId].primitives[meshInfo.primitiveId];
const auto& materialPath = tileset.getMaterialPath();

auto& mesh = fabricMeshes[i];
auto& geometry = mesh.geometry;
Expand All @@ -180,6 +186,8 @@ void setFabricMeshes(
if (baseColorTexture != nullptr && materialInfo.baseColorTexture.has_value()) {
material->setBaseColorTexture(baseColorTexture, materialInfo.baseColorTexture.value());
}
} else if (!materialPath.IsEmpty()) {
geometry->setMaterial(omni::fabric::Path(omni::fabric::asInt(materialPath)));
}
}
}
Expand Down Expand Up @@ -213,9 +221,9 @@ FabricPrepareRenderResources::prepareInLoadThread(

return asyncSystem
.runInMainThread(
[hasImagery, meshes = std::move(meshes), tileLoadResult = std::move(tileLoadResult)]() mutable {
[this, hasImagery, meshes = std::move(meshes), tileLoadResult = std::move(tileLoadResult)]() mutable {
const auto pModel = std::get_if<CesiumGltf::Model>(&tileLoadResult.contentKind);
auto fabricMeshes = acquireFabricMeshes(*pModel, meshes, hasImagery);
auto fabricMeshes = acquireFabricMeshes(*pModel, meshes, hasImagery, _tileset);
return IntermediateLoadThreadResult{
std::move(tileLoadResult),
std::move(meshes),
Expand Down Expand Up @@ -261,7 +269,7 @@ void* FabricPrepareRenderResources::prepareInMainThread(Cesium3DTilesSelection::

const auto& model = pRenderContent->getModel();

setFabricMeshes(model, meshes, fabricMeshes, hasImagery);
setFabricMeshes(model, meshes, fabricMeshes, hasImagery, _tileset);

return new TileRenderResources{
tileTransform,
Expand Down
9 changes: 8 additions & 1 deletion src/core/src/FabricResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,18 @@ FabricResourceManager::FabricResourceManager() {

FabricResourceManager::~FabricResourceManager() = default;

bool FabricResourceManager::shouldAcquireMaterial(const CesiumGltf::MeshPrimitive& primitive, bool hasImagery) const {
bool FabricResourceManager::shouldAcquireMaterial(
const CesiumGltf::MeshPrimitive& primitive,
bool hasImagery,
const pxr::SdfPath& materialPath) const {
if (_disableMaterials) {
return false;
}

if (!materialPath.IsEmpty()) {
return false;
}

return hasImagery || GltfUtil::hasMaterial(primitive);
}

Expand Down
11 changes: 11 additions & 0 deletions src/core/src/OmniTileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <pxr/usd/usd/prim.h>
#include <pxr/usd/usd/stage.h>
#include <pxr/usd/usdGeom/boundable.h>
#include <pxr/usd/usdShade/materialBindingAPI.h>

namespace cesium::omniverse {

Expand Down Expand Up @@ -243,6 +244,16 @@ pxr::CesiumGeoreference OmniTileset::getGeoreference() const {
return UsdUtil::getCesiumGeoreference(georeferencePath);
}

pxr::SdfPath OmniTileset::getMaterialPath() const {
auto tileset = UsdUtil::getCesiumTileset(_tilesetPath);

const auto materialBindingApi = pxr::UsdShadeMaterialBindingAPI(tileset);
const auto materialBinding = materialBindingApi.GetDirectBinding();
const auto& materialPath = materialBinding.GetMaterialPath();

return materialPath;
}

int64_t OmniTileset::getTilesetId() const {
return _tilesetId;
}
Expand Down
40 changes: 40 additions & 0 deletions src/plugins/CesiumUsdSchemas/generatedSchema.usda.in
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,25 @@ class CesiumTilesetPrim "CesiumTilesetPrim" (
displayName = "URL"
doc = "The URL of this tileset's tileset.json file. Usually blank if this is an ion asset."
)
uniform bool doubleSided = 0 (
doc = """Although some renderers treat all parametric or polygonal
surfaces as if they were effectively laminae with outward-facing
normals on both sides, some renderers derive significant optimizations
by considering these surfaces to have only a single outward side,
typically determined by control-point winding order and/or
orientation. By doing so they can perform \"backface culling\" to
avoid drawing the many polygons of most closed surfaces that face away
from the viewer.

However, it is often advantageous to model thin objects such as paper
and cloth as single, open surfaces that must be viewable from both
sides, always. Setting a gprim's doubleSided attribute to
\\c true instructs all renderers to disable optimizations such as
backface culling for the gprim, and attempt (not all renderers are able
to do so, but the USD reference GL renderer always will) to provide
forward-facing normals on each side of the surface for lighting
calculations."""
)
float3[] extent (
doc = """Extent is a three dimensional range measuring the geometric
extent of the authored gprim in its own local space (i.e. its own
Expand All @@ -184,6 +203,27 @@ class CesiumTilesetPrim "CesiumTilesetPrim" (
the extent of all children, as they will be pruned from BBox computation
during traversal."""
)
uniform token orientation = "rightHanded" (
allowedTokens = ["rightHanded", "leftHanded"]
doc = """Orientation specifies whether the gprim's surface normal
should be computed using the right hand rule, or the left hand rule.
Please see for a deeper explanation and
generalization of orientation to composed scenes with transformation
hierarchies."""
)
color3f[] primvars:displayColor (
doc = '''It is useful to have an "official" colorSet that can be used
as a display or modeling color, even in the absence of any specified
shader for a gprim. DisplayColor serves this role; because it is a
UsdGeomPrimvar, it can also be used as a gprim override for any shader
that consumes a displayColor parameter.'''
)
float[] primvars:displayOpacity (
doc = """Companion to displayColor that specifies opacity, broken
out as an independent attribute rather than an rgba color, both so that
each can be independently overridden, and because shaders rarely consume
rgba parameters."""
)
rel proxyPrim (
doc = '''The proxyPrim relationship allows us to link a
prim whose purpose is "render" to its (single target)
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/CesiumUsdSchemas/plugInfo.json.in
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
},
"autoGenerated": true,
"bases": [
"UsdGeomBoundable"
"UsdGeomGprim"
],
"schemaKind": "concreteTyped"
}
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/CesiumUsdSchemas/src/CesiumUsdSchemas/tileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ PXR_NAMESPACE_OPEN_SCOPE
TF_REGISTRY_FUNCTION(TfType)
{
TfType::Define<CesiumTileset,
TfType::Bases< UsdGeomBoundable > >();
TfType::Bases< UsdGeomGprim > >();

// Register the usd prim typename as an alias under UsdSchemaBase. This
// enables one to call
Expand Down Expand Up @@ -455,7 +455,7 @@ CesiumTileset::GetSchemaAttributeNames(bool includeInherited)
};
static TfTokenVector allNames =
_ConcatenateAttributeNames(
UsdGeomBoundable::GetSchemaAttributeNames(true),
UsdGeomGprim::GetSchemaAttributeNames(true),
localNames);

if (includeInherited)
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/CesiumUsdSchemas/src/CesiumUsdSchemas/tileset.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "pxr/pxr.h"
#include ".//api.h"
#include "pxr/usd/usdGeom/boundable.h"
#include "pxr/usd/usdGeom/gprim.h"
#include "pxr/usd/usd/prim.h"
#include "pxr/usd/usd/stage.h"
#include ".//tokens.h"
Expand Down Expand Up @@ -36,7 +36,7 @@ class SdfAssetPath;
/// So to set an attribute to the value "rightHanded", use CesiumTokens->rightHanded
/// as the value.
///
class CesiumTileset : public UsdGeomBoundable
class CesiumTileset : public UsdGeomGprim
{
public:
/// Compile time constant representing what kind of schema this class is.
Expand All @@ -49,15 +49,15 @@ class CesiumTileset : public UsdGeomBoundable
/// for a \em valid \p prim, but will not immediately throw an error for
/// an invalid \p prim
explicit CesiumTileset(const UsdPrim& prim=UsdPrim())
: UsdGeomBoundable(prim)
: UsdGeomGprim(prim)
{
}

/// Construct a CesiumTileset on the prim held by \p schemaObj .
/// Should be preferred over CesiumTileset(schemaObj.GetPrim()),
/// as it preserves SchemaBase state.
explicit CesiumTileset(const UsdSchemaBase& schemaObj)
: UsdGeomBoundable(schemaObj)
: UsdGeomGprim(schemaObj)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void wrapCesiumTileset()
{
typedef CesiumTileset This;

class_<This, bases<UsdGeomBoundable> >
class_<This, bases<UsdGeomGprim> >
cls("Tileset");

cls
Expand Down

0 comments on commit 8f67dae

Please sign in to comment.