fix gc bug
This commit is contained in:
115
internal/gltf.c
115
internal/gltf.c
@@ -86,9 +86,12 @@ static JSValue make_texture_info(JSContext *js, cgltf_texture_view *tv, cgltf_da
|
||||
JS_SetPropertyStr(js, o.val, "texture", JS_NewInt32(js, (int)(tv->texture - data->textures)));
|
||||
JS_SetPropertyStr(js, o.val, "texcoord", JS_NewInt32(js, tv->texcoord));
|
||||
if (tv->has_transform) {
|
||||
JSValue t;
|
||||
JS_ROOT(tr, JS_NewObject(js));
|
||||
JS_SetPropertyStr(js, tr.val, "offset", make_float_array(js, tv->transform.offset, 2));
|
||||
JS_SetPropertyStr(js, tr.val, "scale", make_float_array(js, tv->transform.scale, 2));
|
||||
t = make_float_array(js, tv->transform.offset, 2);
|
||||
JS_SetPropertyStr(js, tr.val, "offset", t);
|
||||
t = make_float_array(js, tv->transform.scale, 2);
|
||||
JS_SetPropertyStr(js, tr.val, "scale", t);
|
||||
JS_SetPropertyStr(js, tr.val, "rotation", JS_NewFloat64(js, tv->transform.rotation));
|
||||
JS_SetPropertyStr(js, o.val, "transform", tr.val);
|
||||
}
|
||||
@@ -118,14 +121,17 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *
|
||||
JS_ROOT(obj, JS_NewObject(js));
|
||||
|
||||
// Buffers
|
||||
JSValue tmp;
|
||||
JS_ROOT(buffers_arr, JS_NewArray(js));
|
||||
for (cgltf_size i = 0; i < data->buffers_count; i++) {
|
||||
cgltf_buffer *buf = &data->buffers[i];
|
||||
JS_ROOT(b, JS_NewObject(js));
|
||||
if (buf->data && buf->size > 0)
|
||||
JS_SetPropertyStr(js, b.val, "blob", js_new_blob_stoned_copy(js, buf->data, buf->size));
|
||||
else
|
||||
if (buf->data && buf->size > 0) {
|
||||
tmp = js_new_blob_stoned_copy(js, buf->data, buf->size);
|
||||
JS_SetPropertyStr(js, b.val, "blob", tmp);
|
||||
} else {
|
||||
JS_SetPropertyStr(js, b.val, "blob", JS_NULL);
|
||||
}
|
||||
JS_SetPropertyStr(js, b.val, "byte_length", JS_NewInt64(js, buf->size));
|
||||
JS_SetPropertyNumber(js, buffers_arr.val, i, b.val);
|
||||
}
|
||||
@@ -143,7 +149,8 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *
|
||||
const char *usage = "unknown";
|
||||
if (bv->type == cgltf_buffer_view_type_vertices) usage = "vertex";
|
||||
else if (bv->type == cgltf_buffer_view_type_indices) usage = "index";
|
||||
JS_SetPropertyStr(js, v.val, "usage", JS_NewString(js, usage));
|
||||
tmp = JS_NewString(js, usage);
|
||||
JS_SetPropertyStr(js, v.val, "usage", tmp);
|
||||
JS_SetPropertyNumber(js, views_arr.val, i, v.val);
|
||||
}
|
||||
JS_SetPropertyStr(js, obj.val, "views", views_arr.val);
|
||||
@@ -159,18 +166,22 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *
|
||||
JS_SetPropertyStr(js, a.val, "view", JS_NULL);
|
||||
JS_SetPropertyStr(js, a.val, "byte_offset", JS_NewInt64(js, acc->offset));
|
||||
JS_SetPropertyStr(js, a.val, "count", JS_NewInt64(js, acc->count));
|
||||
JS_SetPropertyStr(js, a.val, "component_type", JS_NewString(js, component_type_str(acc->component_type)));
|
||||
JS_SetPropertyStr(js, a.val, "type", JS_NewString(js, type_str(acc->type)));
|
||||
tmp = JS_NewString(js, component_type_str(acc->component_type));
|
||||
JS_SetPropertyStr(js, a.val, "component_type", tmp);
|
||||
tmp = JS_NewString(js, type_str(acc->type));
|
||||
JS_SetPropertyStr(js, a.val, "type", tmp);
|
||||
JS_SetPropertyStr(js, a.val, "normalized", JS_NewBool(js, acc->normalized));
|
||||
if (acc->has_min) {
|
||||
int n = cgltf_num_components(acc->type);
|
||||
JS_SetPropertyStr(js, a.val, "min", make_float_array(js, acc->min, n));
|
||||
tmp = make_float_array(js, acc->min, n);
|
||||
JS_SetPropertyStr(js, a.val, "min", tmp);
|
||||
} else {
|
||||
JS_SetPropertyStr(js, a.val, "min", JS_NULL);
|
||||
}
|
||||
if (acc->has_max) {
|
||||
int n = cgltf_num_components(acc->type);
|
||||
JS_SetPropertyStr(js, a.val, "max", make_float_array(js, acc->max, n));
|
||||
tmp = make_float_array(js, acc->max, n);
|
||||
JS_SetPropertyStr(js, a.val, "max", tmp);
|
||||
} else {
|
||||
JS_SetPropertyStr(js, a.val, "max", JS_NULL);
|
||||
}
|
||||
@@ -183,13 +194,15 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *
|
||||
for (cgltf_size mi = 0; mi < data->meshes_count; mi++) {
|
||||
cgltf_mesh *mesh = &data->meshes[mi];
|
||||
JS_ROOT(m, JS_NewObject(js));
|
||||
JS_SetPropertyStr(js, m.val, "name", mesh->name ? JS_NewString(js, mesh->name) : JS_NULL);
|
||||
tmp = mesh->name ? JS_NewString(js, mesh->name) : JS_NULL;
|
||||
JS_SetPropertyStr(js, m.val, "name", tmp);
|
||||
|
||||
JS_ROOT(prims_arr, JS_NewArray(js));
|
||||
for (cgltf_size pi = 0; pi < mesh->primitives_count; pi++) {
|
||||
cgltf_primitive *prim = &mesh->primitives[pi];
|
||||
JS_ROOT(p, JS_NewObject(js));
|
||||
JS_SetPropertyStr(js, p.val, "topology", JS_NewString(js, topology_str(prim->type)));
|
||||
tmp = JS_NewString(js, topology_str(prim->type));
|
||||
JS_SetPropertyStr(js, p.val, "topology", tmp);
|
||||
|
||||
JS_ROOT(attrs, JS_NewObject(js));
|
||||
for (cgltf_size ai = 0; ai < prim->attributes_count; ai++) {
|
||||
@@ -222,13 +235,17 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *
|
||||
cgltf_image *img = &data->images[i];
|
||||
JS_ROOT(im, JS_NewObject(js));
|
||||
if (img->buffer_view) {
|
||||
JS_SetPropertyStr(js, im.val, "kind", JS_NewString(js, "buffer_view"));
|
||||
tmp = JS_NewString(js, "buffer_view");
|
||||
JS_SetPropertyStr(js, im.val, "kind", tmp);
|
||||
JS_SetPropertyStr(js, im.val, "view", JS_NewInt32(js, (int)(img->buffer_view - data->buffer_views)));
|
||||
} else if (img->uri) {
|
||||
JS_SetPropertyStr(js, im.val, "kind", JS_NewString(js, "uri"));
|
||||
JS_SetPropertyStr(js, im.val, "uri", JS_NewString(js, img->uri));
|
||||
tmp = JS_NewString(js, "uri");
|
||||
JS_SetPropertyStr(js, im.val, "kind", tmp);
|
||||
tmp = JS_NewString(js, img->uri);
|
||||
JS_SetPropertyStr(js, im.val, "uri", tmp);
|
||||
}
|
||||
JS_SetPropertyStr(js, im.val, "mime", img->mime_type ? JS_NewString(js, img->mime_type) : JS_NULL);
|
||||
tmp = img->mime_type ? JS_NewString(js, img->mime_type) : JS_NULL;
|
||||
JS_SetPropertyStr(js, im.val, "mime", tmp);
|
||||
JS_SetPropertyNumber(js, images_arr.val, i, im.val);
|
||||
}
|
||||
JS_SetPropertyStr(js, obj.val, "images", images_arr.val);
|
||||
@@ -262,27 +279,36 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *
|
||||
for (cgltf_size i = 0; i < data->materials_count; i++) {
|
||||
cgltf_material *mat = &data->materials[i];
|
||||
JS_ROOT(m, JS_NewObject(js));
|
||||
JS_SetPropertyStr(js, m.val, "name", mat->name ? JS_NewString(js, mat->name) : JS_NULL);
|
||||
tmp = mat->name ? JS_NewString(js, mat->name) : JS_NULL;
|
||||
JS_SetPropertyStr(js, m.val, "name", tmp);
|
||||
|
||||
JS_ROOT(pbr, JS_NewObject(js));
|
||||
if (mat->has_pbr_metallic_roughness) {
|
||||
cgltf_pbr_metallic_roughness *pmr = &mat->pbr_metallic_roughness;
|
||||
JS_SetPropertyStr(js, pbr.val, "base_color_factor", make_float_array(js, pmr->base_color_factor, 4));
|
||||
JS_SetPropertyStr(js, pbr.val, "base_color_texture", make_texture_info(js, &pmr->base_color_texture, data));
|
||||
tmp = make_float_array(js, pmr->base_color_factor, 4);
|
||||
JS_SetPropertyStr(js, pbr.val, "base_color_factor", tmp);
|
||||
tmp = make_texture_info(js, &pmr->base_color_texture, data);
|
||||
JS_SetPropertyStr(js, pbr.val, "base_color_texture", tmp);
|
||||
JS_SetPropertyStr(js, pbr.val, "metallic_factor", JS_NewFloat64(js, pmr->metallic_factor));
|
||||
JS_SetPropertyStr(js, pbr.val, "roughness_factor", JS_NewFloat64(js, pmr->roughness_factor));
|
||||
JS_SetPropertyStr(js, pbr.val, "metallic_roughness_texture", make_texture_info(js, &pmr->metallic_roughness_texture, data));
|
||||
tmp = make_texture_info(js, &pmr->metallic_roughness_texture, data);
|
||||
JS_SetPropertyStr(js, pbr.val, "metallic_roughness_texture", tmp);
|
||||
}
|
||||
JS_SetPropertyStr(js, pbr.val, "normal_texture", make_texture_info(js, &mat->normal_texture, data));
|
||||
JS_SetPropertyStr(js, pbr.val, "occlusion_texture", make_texture_info(js, &mat->occlusion_texture, data));
|
||||
JS_SetPropertyStr(js, pbr.val, "emissive_factor", make_float_array(js, mat->emissive_factor, 3));
|
||||
JS_SetPropertyStr(js, pbr.val, "emissive_texture", make_texture_info(js, &mat->emissive_texture, data));
|
||||
tmp = make_texture_info(js, &mat->normal_texture, data);
|
||||
JS_SetPropertyStr(js, pbr.val, "normal_texture", tmp);
|
||||
tmp = make_texture_info(js, &mat->occlusion_texture, data);
|
||||
JS_SetPropertyStr(js, pbr.val, "occlusion_texture", tmp);
|
||||
tmp = make_float_array(js, mat->emissive_factor, 3);
|
||||
JS_SetPropertyStr(js, pbr.val, "emissive_factor", tmp);
|
||||
tmp = make_texture_info(js, &mat->emissive_texture, data);
|
||||
JS_SetPropertyStr(js, pbr.val, "emissive_texture", tmp);
|
||||
JS_SetPropertyStr(js, m.val, "pbr", pbr.val);
|
||||
|
||||
const char *alpha_mode = "OPAQUE";
|
||||
if (mat->alpha_mode == cgltf_alpha_mode_mask) alpha_mode = "MASK";
|
||||
else if (mat->alpha_mode == cgltf_alpha_mode_blend) alpha_mode = "BLEND";
|
||||
JS_SetPropertyStr(js, m.val, "alpha_mode", JS_NewString(js, alpha_mode));
|
||||
tmp = JS_NewString(js, alpha_mode);
|
||||
JS_SetPropertyStr(js, m.val, "alpha_mode", tmp);
|
||||
JS_SetPropertyStr(js, m.val, "alpha_cutoff", JS_NewFloat64(js, mat->alpha_cutoff));
|
||||
JS_SetPropertyStr(js, m.val, "double_sided", JS_NewBool(js, mat->double_sided));
|
||||
JS_SetPropertyStr(js, m.val, "unlit", JS_NewBool(js, mat->unlit));
|
||||
@@ -296,7 +322,8 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *
|
||||
for (cgltf_size i = 0; i < data->nodes_count; i++) {
|
||||
cgltf_node *node = &data->nodes[i];
|
||||
JS_ROOT(n, JS_NewObject(js));
|
||||
JS_SetPropertyStr(js, n.val, "name", node->name ? JS_NewString(js, node->name) : JS_NULL);
|
||||
tmp = node->name ? JS_NewString(js, node->name) : JS_NULL;
|
||||
JS_SetPropertyStr(js, n.val, "name", tmp);
|
||||
JS_SetPropertyStr(js, n.val, "mesh", node->mesh ? JS_NewInt32(js, (int)(node->mesh - data->meshes)) : JS_NULL);
|
||||
|
||||
JS_ROOT(children, JS_NewArray(js));
|
||||
@@ -305,15 +332,19 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *
|
||||
JS_SetPropertyStr(js, n.val, "children", children.val);
|
||||
|
||||
if (node->has_matrix) {
|
||||
JS_SetPropertyStr(js, n.val, "matrix", make_float_array(js, node->matrix, 16));
|
||||
tmp = make_float_array(js, node->matrix, 16);
|
||||
JS_SetPropertyStr(js, n.val, "matrix", tmp);
|
||||
JS_SetPropertyStr(js, n.val, "translation", JS_NULL);
|
||||
JS_SetPropertyStr(js, n.val, "rotation", JS_NULL);
|
||||
JS_SetPropertyStr(js, n.val, "scale", JS_NULL);
|
||||
} else {
|
||||
JS_SetPropertyStr(js, n.val, "matrix", JS_NULL);
|
||||
JS_SetPropertyStr(js, n.val, "translation", make_float_array(js, node->translation, 3));
|
||||
JS_SetPropertyStr(js, n.val, "rotation", make_float_array(js, node->rotation, 4));
|
||||
JS_SetPropertyStr(js, n.val, "scale", make_float_array(js, node->scale, 3));
|
||||
tmp = make_float_array(js, node->translation, 3);
|
||||
JS_SetPropertyStr(js, n.val, "translation", tmp);
|
||||
tmp = make_float_array(js, node->rotation, 4);
|
||||
JS_SetPropertyStr(js, n.val, "rotation", tmp);
|
||||
tmp = make_float_array(js, node->scale, 3);
|
||||
JS_SetPropertyStr(js, n.val, "scale", tmp);
|
||||
}
|
||||
|
||||
JS_SetPropertyStr(js, n.val, "skin", node->skin ? JS_NewInt32(js, (int)(node->skin - data->skins)) : JS_NULL);
|
||||
@@ -340,7 +371,8 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *
|
||||
for (cgltf_size ai = 0; ai < data->animations_count; ai++) {
|
||||
cgltf_animation *anim = &data->animations[ai];
|
||||
JS_ROOT(a, JS_NewObject(js));
|
||||
JS_SetPropertyStr(js, a.val, "name", anim->name ? JS_NewString(js, anim->name) : JS_NULL);
|
||||
tmp = anim->name ? JS_NewString(js, anim->name) : JS_NULL;
|
||||
JS_SetPropertyStr(js, a.val, "name", tmp);
|
||||
|
||||
JS_ROOT(samps, JS_NewArray(js));
|
||||
for (cgltf_size si = 0; si < anim->samplers_count; si++) {
|
||||
@@ -351,7 +383,8 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *
|
||||
const char *interp = "LINEAR";
|
||||
if (samp->interpolation == cgltf_interpolation_type_step) interp = "STEP";
|
||||
else if (samp->interpolation == cgltf_interpolation_type_cubic_spline) interp = "CUBICSPLINE";
|
||||
JS_SetPropertyStr(js, s.val, "interpolation", JS_NewString(js, interp));
|
||||
tmp = JS_NewString(js, interp);
|
||||
JS_SetPropertyStr(js, s.val, "interpolation", tmp);
|
||||
JS_SetPropertyNumber(js, samps.val, si, s.val);
|
||||
}
|
||||
JS_SetPropertyStr(js, a.val, "samplers", samps.val);
|
||||
@@ -376,7 +409,8 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *
|
||||
case cgltf_animation_path_type_weights: path = "weights"; break;
|
||||
default: break;
|
||||
}
|
||||
JS_SetPropertyStr(js, target.val, "path", JS_NewString(js, path));
|
||||
tmp = JS_NewString(js, path);
|
||||
JS_SetPropertyStr(js, target.val, "path", tmp);
|
||||
JS_SetPropertyStr(js, c.val, "target", target.val);
|
||||
JS_SetPropertyNumber(js, channels.val, ci, c.val);
|
||||
}
|
||||
@@ -391,7 +425,8 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *
|
||||
for (cgltf_size i = 0; i < data->skins_count; i++) {
|
||||
cgltf_skin *skin = &data->skins[i];
|
||||
JS_ROOT(s, JS_NewObject(js));
|
||||
JS_SetPropertyStr(js, s.val, "name", skin->name ? JS_NewString(js, skin->name) : JS_NULL);
|
||||
tmp = skin->name ? JS_NewString(js, skin->name) : JS_NULL;
|
||||
JS_SetPropertyStr(js, s.val, "name", tmp);
|
||||
JS_ROOT(joints, JS_NewArray(js));
|
||||
for (cgltf_size ji = 0; ji < skin->joints_count; ji++)
|
||||
JS_SetPropertyNumber(js, joints.val, ji, JS_NewInt32(js, (int)(skin->joints[ji] - data->nodes)));
|
||||
@@ -405,12 +440,16 @@ JSValue js_gltf_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *
|
||||
// Extensions
|
||||
JS_ROOT(exts, JS_NewObject(js));
|
||||
JS_ROOT(used, JS_NewArray(js));
|
||||
for (cgltf_size i = 0; i < data->extensions_used_count; i++)
|
||||
JS_SetPropertyNumber(js, used.val, i, JS_NewString(js, data->extensions_used[i]));
|
||||
for (cgltf_size i = 0; i < data->extensions_used_count; i++) {
|
||||
tmp = JS_NewString(js, data->extensions_used[i]);
|
||||
JS_SetPropertyNumber(js, used.val, i, tmp);
|
||||
}
|
||||
JS_SetPropertyStr(js, exts.val, "used", used.val);
|
||||
JS_ROOT(required, JS_NewArray(js));
|
||||
for (cgltf_size i = 0; i < data->extensions_required_count; i++)
|
||||
JS_SetPropertyNumber(js, required.val, i, JS_NewString(js, data->extensions_required[i]));
|
||||
for (cgltf_size i = 0; i < data->extensions_required_count; i++) {
|
||||
tmp = JS_NewString(js, data->extensions_required[i]);
|
||||
JS_SetPropertyNumber(js, required.val, i, tmp);
|
||||
}
|
||||
JS_SetPropertyStr(js, exts.val, "required", required.val);
|
||||
JS_SetPropertyStr(js, obj.val, "extensions", exts.val);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user