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

Fix the calculation of jointStride and weightStride of GLTF-Importer… #21

Merged
merged 1 commit into from
Nov 7, 2024

Conversation

HanetakaChou
Copy link
Contributor

@HanetakaChou HanetakaChou commented Nov 4, 2024

When I load one GLTF asset, the jointStride and weightStride can be zero!

We should change the stride manually, just like how we calcualte the indexStride when importing the IndexBuffer.

            if (joint_indices)
            {
                assert(joint_indices->count == positions->count);

                auto [jointSrc, jointStride] = cgltf_buffer_iterator(joint_indices, 0);
                vector<uint16_t, 4>* jointDst = buffers->jointData.data() + totalVertices;

                if (joint_indices->component_type == cgltf_component_type_r_8u)
                {
                    // !!! This line is what I add.
                    if (!jointStride) jointStride = sizeof(uint8_t) * 4;

                    for (size_t v_idx = 0; v_idx < joint_indices->count; v_idx++)
                    {
                        *jointDst = dm::vector<uint16_t, 4>(jointSrc[0], jointSrc[1], jointSrc[2], jointSrc[3]);

                        jointSrc += jointStride;
                        ++jointDst;
                    }
                }
                else
                {
                    assert(joint_indices->component_type == cgltf_component_type_r_16u);

                    // !!! This line is what I add.
                    if (!jointStride) jointStride = sizeof(uint16_t) * 4;

                    for (size_t v_idx = 0; v_idx < joint_indices->count; v_idx++)
                    {
                        const uint16_t* jointSrcUshort = (const uint16_t*)jointSrc;
                        *jointDst = dm::vector<uint16_t, 4>(jointSrcUshort[0], jointSrcUshort[1], jointSrcUshort[2], jointSrcUshort[3]);

                        jointSrc += jointStride;
                        ++jointDst;
                    }
                }
            }

            if (joint_weights)
            {
                assert(joint_weights->count == positions->count);

                auto [weightSrc, weightStride] = cgltf_buffer_iterator(joint_weights, 0);
                float4* weightDst = buffers->weightData.data() + totalVertices;

                if (joint_weights->component_type == cgltf_component_type_r_8u)
                {

                    // !!! This line is what I add.
                    if (!weightStride) weightStride = sizeof(uint8_t) * 4;

                    for (size_t v_idx = 0; v_idx < joint_indices->count; v_idx++)
                    {
                        *weightDst = dm::float4(
                            float(weightSrc[0]) / 255.f,
                            float(weightSrc[1]) / 255.f,
                            float(weightSrc[2]) / 255.f,
                            float(weightSrc[3]) / 255.f);

                        weightSrc += weightStride;
                        ++weightDst;
                    }
                }
                else if (joint_weights->component_type == cgltf_component_type_r_16u)
                {

                    // !!! This line is what I add.
                    if (!weightStride) weightStride = sizeof(uint16_t) * 4;

                    for (size_t v_idx = 0; v_idx < joint_indices->count; v_idx++)
                    {
                        const uint16_t* weightSrcUshort = (const uint16_t*)weightSrc;
                        *weightDst = dm::float4(
                            float(weightSrcUshort[0]) / 65535.f,
                            float(weightSrcUshort[1]) / 65535.f,
                            float(weightSrcUshort[2]) / 65535.f,
                            float(weightSrcUshort[3]) / 65535.f);
                        
                        weightSrc += weightStride;
                        ++weightDst;
                    }
                }
                else
                {
                    assert(joint_weights->component_type == cgltf_component_type_r_32f);

                    // !!! This line is what I add.
                    if (!weightStride) weightStride = sizeof(float) * 4;

                    for (size_t v_idx = 0; v_idx < joint_indices->count; v_idx++)
                    {
                        *weightDst = (const float*)weightSrc;

                        weightSrc += weightStride;
                        ++weightDst;
                    }
                }
            }

Copy link

github-actions bot commented Nov 4, 2024

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@HanetakaChou
Copy link
Contributor Author

I have read the CLA Document and I hereby sign the CLA

…in the same way as how we calculate the indexStride.
@HanetakaChou HanetakaChou changed the title Fix the calculattion of jointStride and weightStride of GLTF-Importer… Fix the calculation of jointStride and weightStride of GLTF-Importer… Nov 4, 2024
@HanetakaChou
Copy link
Contributor Author

@apanteleev

@HanetakaChou
Copy link
Contributor Author

HanetakaChou commented Nov 4, 2024

Here is the glTF Validator output of my glTF asset (NO ERROR! NO WARNING!):

{
    "uri": "/sparkle.gltf",
    "mimeType": "model/gltf+json",
    "validatorVersion": "2.0.0-dev.3.10",
    "validatedAt": "2024-11-04T21:46:11.944Z",
    "issues": {
        "numErrors": 0,
        "numWarnings": 0,
        "numInfos": 0,
        "numHints": 0,
        "messages": [],
        "truncated": false
    },
    "info": {
        "version": "2.0",
        "generator": "Khronos glTF Blender I/O v4.2.70",
        "resources": [
            {
                "pointer": "/buffers/0",
                "mimeType": "application/gltf-buffer",
                "storage": "external",
                "uri": "sparkle.bin",
                "byteLength": 3393892
            },
            {
                "pointer": "/images/0",
                "mimeType": "image/png",
                "storage": "external",
                "uri": "clothing.png",
                "image": {
                    "width": 2048,
                    "height": 1024,
                    "format": "rgb",
                    "primaries": "srgb",
                    "transfer": "srgb",
                    "bits": 8
                }
            },
            {
                "pointer": "/images/1",
                "mimeType": "image/png",
                "storage": "external",
                "uri": "hair.png",
                "image": {
                    "width": 1024,
                    "height": 1024,
                    "format": "rgb",
                    "primaries": "srgb",
                    "transfer": "srgb",
                    "bits": 8
                }
            },
            {
                "pointer": "/images/2",
                "mimeType": "image/png",
                "storage": "external",
                "uri": "face.png",
                "image": {
                    "width": 512,
                    "height": 512,
                    "format": "rgb",
                    "primaries": "srgb",
                    "transfer": "srgb",
                    "bits": 8
                }
            },
            {
                "pointer": "/images/3",
                "mimeType": "image/png",
                "storage": "external",
                "uri": "eye.png",
                "image": {
                    "width": 256,
                    "height": 256,
                    "format": "rgba",
                    "primaries": "srgb",
                    "transfer": "srgb",
                    "bits": 8
                }
            },
            {
                "pointer": "/images/4",
                "mimeType": "image/png",
                "storage": "external",
                "uri": "expression.png",
                "image": {
                    "width": 1024,
                    "height": 1024,
                    "format": "rgba",
                    "primaries": "srgb",
                    "transfer": "srgb",
                    "bits": 8
                }
            }
        ],
        "animationCount": 1,
        "materialCount": 5,
        "hasMorphTargets": false,
        "hasSkins": true,
        "hasTextures": true,
        "hasDefaultScene": true,
        "drawCallCount": 5,
        "totalVertexCount": 23551,
        "totalTriangleCount": 30122,
        "maxUVs": 1,
        "maxInfluences": 4,
        "maxAttributes": 5
    }
}

But donut does not work properly on my asset due to the incorrect jointStirde and weightStride.

@apanteleev apanteleev merged commit 924e13b into NVIDIAGameWorks:main Nov 7, 2024
1 check passed
@github-actions github-actions bot locked and limited conversation to collaborators Nov 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants