Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support GeomSubset and USDPreviewSurface #19

Open
wants to merge 3 commits into
base: adsk/feature/archive-webgpu
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pxr/usdImaging/hdEmscripten/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pxr_library(hdEmscripten
usd
usdUtils
sdf
-Wl,--whole-archive usdShaders -Wl,--no-whole-archive

PUBLIC_CLASSES
webRenderDelegate
Expand All @@ -39,6 +40,7 @@ add_resources(sdf)
add_resources(hd)
add_resources(usdHydra)
add_resources(usdRender)
add_resources(usdShaders)
add_resources(usdVol)
add_resources(usdImaging)
add_resources(usd)
Expand Down
23 changes: 18 additions & 5 deletions pxr/usdImaging/hdEmscripten/js/ThreeJsRenderDelegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class TextureRegistry {
}

let filetype = undefined;
if (filename.indexOf('.png') >= filename.length - 5) {
if (filename.endsWith('.png')) {
filetype = 'image/png';
} else if (filename.indexOf('.jpg') >= filename.length - 5) {
} else if (filename.endsWith('.jpg')) {
filetype = 'image/jpeg';
} else if (filename.indexOf('.jpeg') >= filename.length - 5) {
} else if (filename.endsWith('.jpeg')) {
filetype = 'image/jpeg';
} else {
throw new Error('Unknown filetype');
Expand All @@ -37,6 +37,7 @@ class TextureRegistry {
return;
}

loadedFile = new Uint8Array(loadedFile.slice());
let blob = new Blob([loadedFile.slice()], {type: filetype});
let blobUrl = URL.createObjectURL(blob);

Expand Down Expand Up @@ -127,6 +128,18 @@ class HydraMesh {
this.updateOrder(this._normals, 'normal');
}

updateSubset(subsets) {
this._mesh.material = subsets.map(
({ materialId }) => this._interface.materials[materialId]?._material, // eslint-disable-line no-underscore-dangle
)

subsets.forEach(({ indices }, i) => {
indices.forEach((val) => {
this._geometry.addGroup(val * 3, 3, i)
})
})
}

// This is always called before prims are updated
setMaterial(materialId) {
console.log('Material: ' + materialId);
Expand All @@ -146,7 +159,7 @@ class HydraMesh {

if (interpolation === 'constant') {
this._mesh.material.color = new THREE.Color().fromArray(data);
} else if (interpolation === 'vertex') {
} else if (interpolation === 'vertex' || interpolation === 'varying') {
// Per-vertex buffer attribute
this._mesh.material.vertexColors = true;
if (wasDefaultMaterial) {
Expand All @@ -167,7 +180,7 @@ class HydraMesh {
if (interpolation === 'facevarying') {
// The UV buffer has already been prepared on the C++ side, so we just set it
this._geometry.setAttribute('uv', new THREE.Float32BufferAttribute(data.slice(), dimension));
} else if (interpolation === 'vertex') {
} else if (interpolation === 'vertex' || interpolation === 'varying') {
// We have per-vertex UVs, so we need to sort them accordingly
this._uvs = data.slice();
this.updateOrder(this._uvs, 'uv', 2);
Expand Down
22 changes: 22 additions & 0 deletions pxr/usdImaging/hdEmscripten/webRenderDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ const std::map<HdInterpolation, std::string> InterpolationStrings = {
{HdInterpolationInstance, "instance"}
};

const std::map<HdGeomSubset::Type, std::string> HdGeomSubsetTypeStrings = {
{HdGeomSubset::Type::TypeFaceSet, "typefaceset"},
};

void _runInMainThread(int funPointer) {
std::function<void()> *function = reinterpret_cast<std::function<void()>*>(funPointer);
(*function)();
Expand Down Expand Up @@ -169,6 +173,23 @@ class Emscripten_Rprim final : public HdMesh {
});
}

HdGeomSubsets geomSubsets = delegate->GetMeshTopology(id).GetGeomSubsets();
if (geomSubsets.size() > 0) {
runInMainThread([&]() {
val subsets = val::array();
int i = 0;
for (auto &geomSubset : geomSubsets) {
val subsetObj = val::object();
subsetObj.set("type", HdGeomSubsetTypeStrings.at(geomSubset.type));
subsetObj.set("id", geomSubset.id.GetString());
subsetObj.set("materialId", geomSubset.materialId.GetString());
subsetObj.set("indices", val(typed_memory_view(geomSubset.indices.size(), geomSubset.indices.cdata())));
subsets.set(i++, subsetObj);
}
_rPrim.call<void>("updateSubset", subsets);
});
}

*dirtyBits &= ~HdChangeTracker::AllSceneDirtyBits;
}

Expand Down Expand Up @@ -274,6 +295,7 @@ class Emscripten_Rprim final : public HdMesh {
_SendPrimvar(triangulated, primvar.name.GetString(), ip);
break;
}
case HdInterpolationVarying:
case HdInterpolationConstant:
case HdInterpolationVertex: {
_SendPrimvar(value, primvar.name.GetString(), ip);
Expand Down