Skip to content

Commit

Permalink
Merge branch 'usdz-asset-resolution' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
syoyo committed Apr 15, 2024
2 parents 4948e75 + cd08fff commit d1ba74d
Show file tree
Hide file tree
Showing 61 changed files with 27,421 additions and 3,852 deletions.
22 changes: 20 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ option(TINYUSDZ_BUILD_TESTS "Build tests" ${TINYUSDZ_DEFAULT_BUILD_TESTS})
option(TINYUSDZ_BUILD_BENCHMARKS
"Build some bechmark tests(e.g. internal C++ data structures)" OFF)
option(TINYUSDZ_BUILD_EXAMPLES
"Build examples(but not all examples area built in `examples` folder)"
"Build examples(but not all examples in `examples` folder are being built)"
${TINYUSDZ_DEFAULT_BUILD_EXAMPLES})
option(TINYUSDZ_WITH_BUILTIN_IMAGE_LOADER
"Build with built-in image loader(stb_image and fpng). When disabled, app need to provide image loader callback to load images."
Expand Down Expand Up @@ -108,6 +108,7 @@ option(TINYUSDZ_CXX_EXCEPTIONS

option(TINYUSDZ_WITH_USDMTLX "Build with MaterialX support" ON)
option(TINYUSDZ_WITH_JSON "Build with JSON serialization support" OFF)
option(TINYUSDZ_WITH_USD_TO_GLTF "Build with USD to glTF example" ON)
option(TINYUSDZ_WITH_USDOBJ "Build with usdObj support(import wavefront .obj)"
ON)
option(TINYUSDZ_WITH_USDFBX "Build with usdFbx support(import FBX .fbx)"
Expand Down Expand Up @@ -391,11 +392,25 @@ set(TINYUSDZ_SOURCES
${PROJECT_SOURCE_DIR}/src/image-loader.cc
${PROJECT_SOURCE_DIR}/src/pprinter.cc
${PROJECT_SOURCE_DIR}/src/stage.cc
${PROJECT_SOURCE_DIR}/src/stage.hh
${PROJECT_SOURCE_DIR}/src/tydra/facial.cc
${PROJECT_SOURCE_DIR}/src/tydra/facial.hh
${PROJECT_SOURCE_DIR}/src/tydra/prim-apply.cc
${PROJECT_SOURCE_DIR}/src/tydra/prim-apply.hh
${PROJECT_SOURCE_DIR}/src/tydra/scene-access.cc
${PROJECT_SOURCE_DIR}/src/tydra/scene-access.hh
${PROJECT_SOURCE_DIR}/src/tydra/attribute-eval.hh
${PROJECT_SOURCE_DIR}/src/tydra/attribute-eval.cc
${PROJECT_SOURCE_DIR}/src/tydra/attribute-eval-typed.cc
${PROJECT_SOURCE_DIR}/src/tydra/attribute-eval-typed-animatable.cc
${PROJECT_SOURCE_DIR}/src/tydra/attribute-eval-typed-fallback.cc
${PROJECT_SOURCE_DIR}/src/tydra/attribute-eval-typed-animatable-fallback.cc
${PROJECT_SOURCE_DIR}/src/tydra/obj-export.cc
${PROJECT_SOURCE_DIR}/src/tydra/shader-network.cc
${PROJECT_SOURCE_DIR}/src/tydra/render-data.cc)
${PROJECT_SOURCE_DIR}/src/tydra/shader-network.hh
${PROJECT_SOURCE_DIR}/src/tydra/render-data.cc
${PROJECT_SOURCE_DIR}/src/tydra/render-data.hh
)

if(TINYUSDZ_WITH_PXR_COMPAT_API)
list(APPEND TINYUSDZ_SOURCES ${PROJECT_SOURCE_DIR}/src/pxr-compat.cc)
Expand Down Expand Up @@ -1168,6 +1183,9 @@ if(TINYUSDZ_BUILD_EXAMPLES)
if (TINYUSDZ_WITH_JSON)
add_subdirectory(examples/usd_to_json)
endif()
if (TINYUSDZ_WITH_USD_TO_GLTF)
add_subdirectory(examples/usd_to_gltf)
endif()
if(TINYUSDZ_WITH_MODULE_USDA_WRITER)
add_subdirectory(examples/save_usda)
endif()
Expand Down
10 changes: 10 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ include src/crate-reader.hh
include src/crate-writer.cc
include src/crate-writer.hh
include src/define-type-trait.inc
include src/external/nanoflann.hpp
include src/external/nanoflann.COPYING
include src/external/filesystem/LICENSE
include src/external/filesystem/README.md
include src/external/filesystem/include/ghc/filesystem.hpp
Expand Down Expand Up @@ -173,6 +175,7 @@ include src/external/simple_match/include/simple_match/simple_match.hpp
include src/external/simple_match/include/simple_match/utility.hpp
include src/external/stb_image.h
include src/external/stb_image_resize.h
include src/external/stb_image_resize2.h
include src/external/stb_image_write.h
include src/external/stb_truetype.h
include src/external/string_id/LICENSE
Expand Down Expand Up @@ -469,6 +472,7 @@ include src/prim-reconstruct.cc
include src/prim-reconstruct.hh
include src/prim-types.cc
include src/prim-types.hh
include src/prim-type-macros.inc
include src/primvar.cc
include src/primvar.hh
include src/pxr-compat.cc
Expand Down Expand Up @@ -500,6 +504,12 @@ include src/tydra/render-data.cc
include src/tydra/render-data.hh
include src/tydra/scene-access.cc
include src/tydra/scene-access.hh
include src/tydra/attribute-eval.hh
include src/tydra/attribute-eval.cc
include src/tydra/attribute-eval-typed.cc
include src/tydra/attribute-eval-typed-fallack.cc
include src/tydra/attribute-eval-typed-animatable.cc
include src/tydra/attribute-eval-typed-animatable-fallback.cc
include src/tydra/shader-network.cc
include src/tydra/shader-network.hh
include src/tydra/facial.cc
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -525,3 +525,5 @@ Some helper code is licensed under MIT license.
* nanozlib: Apache 2.0 license. https://github.com/lighttransport/nanozlib
* lz4.py: MIT license. https://github.com/SE2Dev/PyCoD/blob/master/_lz4.py
* pugixml: MIT license. https://github.com/zeux/pugixml
* nanoflann: 2-clause BSD license. https://github.com/jlblancoc/nanoflann
* tinymeshutils: MIT license. https://github.com/syoyo/tinymeshutils
12 changes: 6 additions & 6 deletions examples/api_tutorial/api-tutorial-main.cc
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
// All-in-one TinyUSDZ core
#include <iostream>

// All-in-one TinyUSDZ core, but pprint feature is not included to speed up compilation.
#include "tinyusdz.hh"

// Import to_string() and operator<< features
#include <iostream>

#include "pprinter.hh"
#include "prim-pprint.hh"
#include "value-pprint.hh"

// Tydra is a collection of APIs to access/convert USD Prim data
// (e.g. Can get Attribute by name)
// See <tinyusdz>/examples/tydra_api for more Tydra API examples.
// (e.g. Get Prim's attribute by name)
// See <tinyusdz>/examples/tydra_api for more info about Tydra API.
#include "tydra/scene-access.hh"

//
// create a Scene
//
void CreateScene(tinyusdz::Stage *stage) {
// TinyUSDZ API does not use mutex, smart pointers(e.g. shared_ptr) and C++
// TinyUSDZ API does not use mutex, smart pointers(e.g. unique_ptr/shared_ptr) and C++
// exception. Also threading is opional in TinyUSDZ and currently
// multi-threading is not used. `value::token` implementation is simply copy
// string for each instances(like C++11 ABI std::string). (on the contrary,
Expand Down
35 changes: 29 additions & 6 deletions examples/tydra_api/tydra-api-main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <sstream>

#include "tinyusdz.hh"
#include "tydra/attribute-eval.hh"
#include "tydra/render-data.hh"
#include "tydra/scene-access.hh"
#include "tydra/shader-network.hh"
Expand Down Expand Up @@ -126,7 +127,7 @@ static void TraversePrimvarReader_float2(const tinyusdz::Stage &stage,

int main(int argc, char **argv) {
if (argc < 2) {
std::cout << "Need USD file with Material/Shader(e.g. `<tinyusdz>/models/texturescube.usda`\n" << std::endl;
std::cout << "Need USD file with Material/Shader(e.g. `<tinyusdz>/models/texturescube.usda`)\n" << std::endl;
return EXIT_FAILURE;
}

Expand Down Expand Up @@ -217,7 +218,7 @@ int main(int argc, char **argv) {
//return false;
}

return true;
return true;
};

void *userdata = nullptr;
Expand Down Expand Up @@ -358,15 +359,37 @@ int main(int argc, char **argv) {


//
// Find bound Material
// Get bound Material
//
std::cout << "FindBoundMaterial example -------------\n";
// - GetBoundMaterial:
// - Look Directly boundMaterial to the Prim.
// - If no material is asigned to the Prim, look into parent's BoundMaterial settings.
// - Also considers BindMaterial strength `bindMaterialAs` Prim metadatum
// - [ ] TODO: Consider materialBindingCollection
// - GetDirectlyBoundMaterial: Get BoundMaterial assigned to the Prim being queried.
//
// Usually GetBoundMaterial is what you want.
//
// See https://openusd.org/release/wp_usdshade.html for more details about Material assignment.
//
std::cout << "GetBoundMaterial example -------------\n";
for (const auto &item : meshmap) {
// FindBoundMaterial seaches bound material for parent GPrim.

tinyusdz::Path matPath;
const tinyusdz::Material *material{nullptr};
std::string err;
bool ret = tinyusdz::tydra::FindBoundMaterial(stage, tinyusdz::Path(item.first, ""), /* suffix */"", &matPath, &material, &err);

bool ret = tinyusdz::tydra::GetDirectlyBoundMaterial(stage, tinyusdz::Path(item.first, ""), /* purpose */"", &matPath, &material, &err);
std::cout << "Prim : " << item.first << "\n";
if (ret) {
std::cout << "has directlyBoundMaterial: " << matPath.full_path_name() << "\n";
} else if (err.empty()) {
std::cout << "no directlyBoundMaterial" << "\n";
} else {
std::cout << "GetDirectlyBoundMaterial failed: " << err << "\n";
}

ret = tinyusdz::tydra::GetBoundMaterial(stage, tinyusdz::Path(item.first, ""), /* purpose */"", &matPath, &material, &err);

if (ret) {
std::cout << item.first << " has bound Material. Material Path = " << matPath << "\n";
Expand Down
57 changes: 51 additions & 6 deletions examples/tydra_to_renderscene/to-renderscene-main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// Copyright 2022-Present Light Transport Entertainment Inc.

//
// Command-line check tool to convert USD Stage to RenderScene(glTF-like data
// Command-line tool to convert USD Stage to RenderScene(glTF-like data
// structure)
//
#include <algorithm>
#include <iostream>
#include <sstream>
#include <fstream>

#include "io-util.hh"
#include "pprinter.hh"
Expand All @@ -16,6 +17,7 @@
#include "tydra/render-data.hh"
#include "tydra/scene-access.hh"
#include "tydra/shader-network.hh"
#include "tydra/obj-export.hh"
#include "usdShade.hh"
#include "value-pprint.hh"
#include "value-types.hh"
Expand Down Expand Up @@ -48,12 +50,26 @@ using PrimvarReader_float2Map =

int main(int argc, char **argv) {
if (argc < 2) {
std::cout << "Need USD file.\n"
<< std::endl;
std::cout << "Usage: " << argv[0] << " input.usd [OPTIONS].\n";
std::cout << "\n\nOptions\n\n";
std::cout << " -t: Triangulate mesh\n";
std::cout << " --dumpobj: Dump mesh as wavefront .obj(for visual debugging)\n";
return EXIT_FAILURE;
}

std::string filepath = argv[1];
bool triangulate = true;
bool export_obj = false;
std::string filepath;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-t") == 0) {
triangulate = true;
} else if (strcmp(argv[i], "--dumpobj") == 0) {
export_obj = true;
} else {
filepath = argv[i];
}
}

std::string warn;
std::string err;

Expand Down Expand Up @@ -130,17 +146,22 @@ int main(int argc, char **argv) {
// RenderScene: Scene graph object which is suited for GL/Vulkan renderer
tinyusdz::tydra::RenderScene render_scene;
tinyusdz::tydra::RenderSceneConverter converter;
tinyusdz::tydra::RenderSceneConverterEnv env(stage);

std::cout << "Triangulate : " << (triangulate ? "true" : "false") << "\n";
env.mesh_config.triangulate = triangulate;

// Add base directory of .usd file to search path.
std::string usd_basedir = tinyusdz::io::GetBaseDir(filepath);
std::cout << "Add seach path: " << usd_basedir << "\n";

converter.set_search_paths({usd_basedir});
env.set_search_paths({usd_basedir});
// TODO: Set user-defined AssetResolutionResolver
// AssetResolutionResolver arr;
// converter.set_asset_resoluition_resolver(arr);

bool ret = converter.ConvertToRenderScene(stage, &render_scene);
double timecode = tinyusdz::value::TimeCode::Default();
bool ret = converter.ConvertToRenderScene(env, &render_scene);
if (!ret) {
std::cerr << "Failed to convert USD Stage to RenderScene: \n" << converter.GetError() << "\n";
return EXIT_FAILURE;
Expand All @@ -152,5 +173,29 @@ int main(int argc, char **argv) {

std::cout << DumpRenderScene(render_scene) << "\n";

if (export_obj) {
std::cout << "Dump RenderMesh as wavefront .obj\n";
for (size_t i = 0; i < render_scene.meshes.size(); i++) {
std::string obj_str;
std::string mtl_str;
if (!tinyusdz::tydra::export_to_obj(render_scene, i, obj_str, mtl_str, &warn, &err)) {
std::cerr << "obj export error: " << err << "\n";
exit(-1);
}

std::string obj_filename = std::to_string(i) + render_scene.meshes[i].prim_name + ".obj";
std::string mtl_filename = std::to_string(i) + render_scene.meshes[i].prim_name + ".mtl";
{
std::ofstream obj_ofs(obj_filename);
obj_ofs << obj_str;
}

{
std::ofstream mtl_ofs(mtl_filename);
mtl_ofs << mtl_str;
}
}
}

return EXIT_SUCCESS;
}
14 changes: 14 additions & 0 deletions examples/usd_to_gltf/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Assume this cmake is called from tinyusdz root(../../)
set(EXAMPLE_TARGET "usd_to_gltf")

set(TINYUSDZ_USD_TO_GLTF_CONVERT_SOURCES
to-gltf-main.cc
)

add_executable(${EXAMPLE_TARGET} ${TINYUSDZ_USD_TO_GLTF_CONVERT_SOURCES})
add_sanitizers(${EXAMPLE_TARGET})

target_include_directories(${EXAMPLE_TARGET} PRIVATE ${PROJECT_SOURCE_DIR}/src)
target_link_libraries(${EXAMPLE_TARGET} tinyusdz_static)

set_target_properties(${EXAMPLE_TARGET} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
22 changes: 22 additions & 0 deletions examples/usd_to_gltf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Simple USD to glTF converter

Simple USD to glTF converter through Tydra RenderScene API.

This example is just for illustration purpose of Tydra API usecase.
Not all features are supported.

## Status

* [x] Mesh geometry
* [x] Points, Normals, Texcoords
* [ ] Vertex weights
* [ ] Material
* [ ] Texture
* [ ] Skinning
* [ ] Skeleton
* [ ] BlendShapes(morph target in glTF)
* [ ] Animation
* [ ] Audio
* [ ] Sparse accessor

EoL.
Loading

0 comments on commit d1ba74d

Please sign in to comment.