Skip to content

Commit 83b4ebc

Browse files
committed
Merge branch 'threejs' into dev
2 parents 2f42a76 + c30b0ec commit 83b4ebc

File tree

4 files changed

+2851
-8
lines changed

4 files changed

+2851
-8
lines changed

sandbox/threejs/CMakeLists.txt

+2-4
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ list(
3131
add_executable(${BUILD_TARGET} ${SOURCES})
3232
add_sanitizers(${BUILD_TARGET})
3333

34-
set(EXT_COMPILE_OPTIONS "-sTOTAL_MEMORY=1024MB")
35-
3634
target_compile_options(${BUILD_TARGET} PRIVATE ${EXT_COMPILE_OPTIONS})
3735

3836
# tinyusdz dir
@@ -50,11 +48,11 @@ if (EMSCRIPTEN)
5048
${BUILD_TARGET}
5149
PROPERTIES OUTPUT_NAME tinyusdz
5250
SUFFIX ".js"
53-
RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/dist)
51+
RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/simple)
5452
#set(CMAKE_EXECUTABLE_SUFFIX ".html")
5553
endif()
5654

57-
set_target_properties(${BUILD_TARGET} PROPERTIES LINK_FLAGS "-s INITIAL_MEMORY=128MB -s TOTAL_MEMORY=1GB -sTOTAL_STACK=256MB -sASSERTIONS -s ALLOW_MEMORY_GROWTH=1 -s WASM=1 -sMODULARIZE -sEXPORT_ES6=1 --bind")
55+
set_target_properties(${BUILD_TARGET} PROPERTIES LINK_FLAGS "-s INITIAL_MEMORY=128MB -s TOTAL_MEMORY=512MB -sTOTAL_STACK=128MB -sASSERTIONS -s ALLOW_MEMORY_GROWTH=1 -s WASM=1 -sMODULARIZE -sEXPORT_ES6=1 --bind")
5856

5957
# ENVIRONMENT=web
6058
# SINGLE_FILE=1

sandbox/threejs/binding.cc

+135
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,47 @@
88

99
using namespace emscripten;
1010

11+
namespace detail {
12+
13+
// To RGBA
14+
bool ToRGBA(const std::vector<uint8_t> &src, int channels,
15+
std::vector<uint8_t> &dst) {
16+
17+
uint32_t npixels = src.size() / channels;
18+
dst.resize(npixels * 4);
19+
20+
if (channels == 1) { // grayscale
21+
for (size_t i = 0; i < npixels; i++) {
22+
dst[4 * i + 0] = src[i];
23+
dst[4 * i + 1] = src[i];
24+
dst[4 * i + 2] = src[i];
25+
dst[4 * i + 3] = 1.0f;
26+
}
27+
} else if (channels == 2) { // assume luminance + alpha
28+
for (size_t i = 0; i < npixels; i++) {
29+
dst[4 * i + 0] = src[2*i+0];
30+
dst[4 * i + 1] = src[2*i+0];
31+
dst[4 * i + 2] = src[2*i+0];
32+
dst[4 * i + 3] = src[2*i+1];
33+
}
34+
} else if (channels == 3) {
35+
for (size_t i = 0; i < npixels; i++) {
36+
dst[4 * i + 0] = src[3*i+0];
37+
dst[4 * i + 1] = src[3*i+1];
38+
dst[4 * i + 2] = src[3*i+2];
39+
dst[4 * i + 3] = 1.0f;
40+
}
41+
} else if (channels == 4) {
42+
dst = src;
43+
} else {
44+
return false;
45+
}
46+
47+
return true;
48+
}
49+
50+
}
51+
1152
///
1253
/// Simple C++ wrapper class for Emscripten
1354
///
@@ -29,6 +70,8 @@ class TinyUSDZLoader {
2970

3071
tinyusdz::tydra::RenderSceneConverterEnv env(stage);
3172

73+
env.material_config.preserve_texel_bitdepth = true;
74+
3275
if (is_usdz) {
3376
// Setup AssetResolutionResolver to read a asset(file) from memory.
3477
bool asset_on_memory =
@@ -69,6 +112,81 @@ class TinyUSDZLoader {
69112
return render_scene_.meshes.size();
70113
}
71114

115+
emscripten::val getMaterial(int mat_id) const {
116+
117+
emscripten::val mat = emscripten::val::object();
118+
119+
if (!loaded_) {
120+
return mat;
121+
}
122+
123+
if (mat_id >= render_scene_.materials.size()) {
124+
return mat;
125+
}
126+
127+
const auto &m = render_scene_.materials[mat_id];
128+
129+
if (m.surfaceShader.diffuseColor.is_texture()) {
130+
mat.set("diffuseColorTextureId", m.surfaceShader.diffuseColor.texture_id);
131+
} else {
132+
// TODO
133+
//mat.set("diffuseColor", m.surfaceShader.diffuseColor);
134+
}
135+
136+
return mat;
137+
}
138+
139+
emscripten::val getTexture(int tex_id) const {
140+
141+
emscripten::val tex = emscripten::val::object();
142+
143+
if (!loaded_) {
144+
return tex;
145+
}
146+
147+
if (tex_id >= render_scene_.textures.size()) {
148+
return tex;
149+
}
150+
151+
const auto &t = render_scene_.textures[tex_id];
152+
153+
tex.set("textureImageId", int(t.texture_image_id));
154+
//tex.set("wrapS", to_string(t.wrapS));
155+
//tex.set("wrapT", to_string(t.wrapT));
156+
// TOOD: bias, scale, rot/scale/trans
157+
158+
return tex;
159+
}
160+
161+
emscripten::val getImage(int img_id) const {
162+
163+
emscripten::val img = emscripten::val::object();
164+
165+
if (!loaded_) {
166+
return img;
167+
}
168+
169+
if (img_id >= render_scene_.images.size()) {
170+
return img;
171+
}
172+
173+
const auto &i = render_scene_.images[img_id];
174+
175+
if ((i.buffer_id >= 0) && (i.buffer_id < render_scene_.buffers.size())) {
176+
const auto &b = render_scene_.buffers[i.buffer_id];
177+
178+
// TODO: Support HDR
179+
180+
img.set("data", emscripten::typed_memory_view(b.data.size(), b.data.data()));
181+
img.set("width", int(i.width));
182+
img.set("height", int(i.height));
183+
img.set("channels", int(i.channels));
184+
}
185+
186+
187+
return img;
188+
}
189+
72190
emscripten::val getMesh(int mesh_id) const {
73191
emscripten::val mesh = emscripten::val::object();
74192

@@ -94,6 +212,20 @@ class TinyUSDZLoader {
94212
// vec3
95213
mesh.set("points", emscripten::typed_memory_view(rmesh.points.size() * 3, points_ptr));
96214

215+
{
216+
// slot 0 hardcoded.
217+
uint32_t uvSlotId = 0;
218+
if (rmesh.texcoords.count(uvSlotId)) {
219+
const float *uvs_ptr = reinterpret_cast<const float *>(rmesh.texcoords.at(uvSlotId).data.data());
220+
221+
// assume vec2
222+
mesh.set("texcoords", emscripten::typed_memory_view(rmesh.texcoords.at(uvSlotId).vertex_count() * 2, uvs_ptr));
223+
}
224+
}
225+
226+
mesh.set("materialId", rmesh.material_id);
227+
228+
97229
return mesh;
98230
}
99231

@@ -122,6 +254,9 @@ EMSCRIPTEN_BINDINGS(tinyusdz_module) {
122254
.constructor<const std::string &>()
123255
.function("getMesh", &TinyUSDZLoader::getMesh)
124256
.function("numMeshes", &TinyUSDZLoader::numMeshes)
257+
.function("getMaterial", &TinyUSDZLoader::getMaterial)
258+
.function("getTexture", &TinyUSDZLoader::getTexture)
259+
.function("getImage", &TinyUSDZLoader::getImage)
125260
.function("ok", &TinyUSDZLoader::ok)
126261
.function("error", &TinyUSDZLoader::error);
127262
}

0 commit comments

Comments
 (0)