zeux / meshoptimizer

Mesh optimization library that makes meshes smaller and faster to render
MIT License
5.69k stars 483 forks source link

_BATCHID for 3DTiles #354

Closed copycd closed 3 years ago

copycd commented 3 years ago

first, thanks to meshoptimizer.

while converting my glb file, i had got error that is lost _BATCHID attribute. i have written code for _BATCHID attribute error. i don't know how to write beautifully. please, understand.

cgltf.h typedef enum cgltf_attribute_type { cgltf_attribute_type_invalid, cgltf_attribute_type_position, cgltf_attribute_type_normal, cgltf_attribute_type_tangent, cgltf_attribute_type_texcoord, cgltf_attribute_type_color, cgltf_attribute_type_joints, cgltf_attribute_type_weights, // add. cgltf_attribute_type_batchid, } cgltf_attribute_type;

static void cgltf_parse_attribute_type(const char name, cgltf_attribute_type out_type, int out_index) { const char us = strchr(name, '_'); size_t len = us ? (size_t)(us - name) : strlen(name);

if (len == 8 && strncmp(name, "POSITION", 8) == 0)
{
    *out_type = cgltf_attribute_type_position;
}
else if (len == 6 && strncmp(name, "NORMAL", 6) == 0)
{
    *out_type = cgltf_attribute_type_normal;
}
else if (len == 7 && strncmp(name, "TANGENT", 7) == 0)
{
    *out_type = cgltf_attribute_type_tangent;
}
else if (len == 8 && strncmp(name, "TEXCOORD", 8) == 0)
{
    *out_type = cgltf_attribute_type_texcoord;
}
// add
else if (strncmp(name, "_BATCHID", 8) == 0)
{
    *out_type = cgltf_attribute_type_batchid;
}
else if (len == 5 && strncmp(name, "COLOR", 5) == 0)
{
    *out_type = cgltf_attribute_type_color;
}
else if (len == 6 && strncmp(name, "JOINTS", 6) == 0)
{
    *out_type = cgltf_attribute_type_joints;
}
else if (len == 7 && strncmp(name, "WEIGHTS", 7) == 0)
{
    *out_type = cgltf_attribute_type_weights;
}
else
{
    *out_type = cgltf_attribute_type_invalid;
}

if (us && *out_type != cgltf_attribute_type_invalid)
{
    *out_index = CGLTF_ATOI(us + 1);
}

}

write.cpp const char* attributeType(cgltf_attribute_type type) { switch (type) { case cgltf_attribute_type_position: return "POSITION"; case cgltf_attribute_type_normal: return "NORMAL"; case cgltf_attribute_type_tangent: return "TANGENT"; case cgltf_attribute_type_texcoord: return "TEXCOORD"; case cgltf_attribute_type_color: return "COLOR"; case cgltf_attribute_type_joints: return "JOINTS"; case cgltf_attribute_type_weights: return "WEIGHTS"; // add. case cgltf_attribute_type_batchid: return "_BATCHID"; default: return "ATTRIBUTE"; } }

void writeMeshAttributes(std::string& json, std::vector& views, std::string& json_accessors, size_t& accr_offset, const Mesh& mesh, int target, const QuantizationPosition& qp, const QuantizationTexture& qt, const Settings& settings) { . . . // add if (stream.type != cgltf_attribute_type_position && stream.type != cgltf_attribute_type_normal && stream.type != cgltf_attribute_type_tangent && stream.type != cgltf_attribute_typebatchid) { append(json, ""); append(json, size_t(stream.index)); } append(json, "\":"); append(json, vertex_accr); } }

stream.cpp StreamFormat writeVertexStream(std::string& bin, const Stream& stream, const QuantizationPosition& qp, const QuantizationTexture& qt, const Settings& settings) { . . . // add else if (stream.type == cgltf_attribute_type_batchid) { int components = 1; //if (!settings.quantize) if( true) { for (size_t i = 0; i < stream.data.size(); ++i) { const Attr& a = stream.data[i];

            uint16_t v[1] = {uint16_t(a.f[0])};
            bin.append(reinterpret_cast<const char*>(v), sizeof(v));
        }

        StreamFormat format = {cgltf_type_scalar, cgltf_component_type_r_16u, false, sizeof(uint16_t) * components};
        return format;
    }

    // I need to write for quantizing.
}

sample.zip }

zeux commented 3 years ago

I'm glad that it was pretty easy for you to add _BATCHID support. gltfpack only supports official glTF extensions, and a subset at that; _BATCHID is non-standard. I plan to look into a work-in-progress extension EXT_mesh_features in the future which might help, but that would happen at some point in the future, be separate from _BATCHID as that's a different extension, and potentially have some further restrictions on how the data is generated or transformed.