FBX2glTF

repository·main·Indexed 25 days ago

https://github.com/facebookincubator/fbx2gltf

A command-line utility and Node.js wrapper (version 0.9.7-p1) used to convert 3D assets from Autodesk's FBX format to glTF 2.0 (.gltf or .glb). It supports Draco compression, animation baking, and material mapping from FBX (including Stingray PBS and 3ds Max Physical materials) to glTF PBR. The tool provides a Node.js API via a `convert` function and includes pre-compiled binaries for macOS, Linux, and Windows.

Tokens
5.9K
Snippets
7
Records
25
Agent score
81%

What's inside FBX2glTF

  1. How to embed buffers in non-binary glTF using --embed

    main

    The --embed flag allows you to create a single distributable glTF file without using the binary .glb format. It achieves this by encoding the binary buffer(s) as base64-encoded data:// URIs within the JSON.

    Note: This method is slow and results in much larger files compared to the .glb format, but it is useful for loaders that do not support .glb files.

  2. How animations are converted in FBX2glTF

    main

    FBX2glTF uses a "baking" method for animations. It steps through the animation's time interval keyframe by keyframe, calculates the local transform of each node, and records any changes in rotation, translation, or scale.

    Key features and behaviors:

    • Completeness: Every animation in the FBX file is converted to an animation in the glTF file.
    • Blend Shapes: Supported on a per-mesh basis; clips can vary the weights associated with each shape.
    • Trade-offs: Baking is simple and precise but can lead to very large file sizes, especially with complex animation rigs due to the data explosion of recorded keyframes.
  3. Apply Draco compression to mesh geometry

    main

    FBX2glTF can optionally apply Draco compression to geometric data (vertex indices, positions, normals, per-vertex color, etc.). This is highly effective for reducing file size, particularly for static models.

    Requirements:

    • Enabling this adds a dependency on the KHR_draco_geometry_compression glTF extension.
    • The target glTF viewer must be able to decompress Draco data.
  4. How materials are converted in FBX2glTF

    main

    The conversion process maps FBX material models (primarily Lambert and Phong) to glTF 2.0's Physically-Based Rendering (PBR) model. Because these models are inherently incompatible, some settings transfer automatically while others are handled differently.

    Automatically supported settings:

    • Emissive constants and textures
    • Occlusion maps
    • Normal maps

    Traditional settings (Lambert/Phong) mapping notes: Settings like Ambient, Diffuse, Specular, and Shininess (which can be constants or textures) do not have a direct 1:1 mapping in PBR, as PBR relies on fundamental attributes like roughness and metalness.

  5. How texture coordinate flipping works

    main

    glTF uses a coordinate system where (0, 0) is the top left, whereas FBX often assumes (0, 0) is the bottom left.

    • --flip-v (the default behavior) applies y -> (1.0 - y) to all V texture coordinates to ensure spec-compliant glTF.
    • --no-flip-v disables this behavior, which is useful if your textures are already pre-flipped or if you are working in a glTF-centric coordinate system.
    • --flip-u applies x -> (1.0 - x) to all U texture coordinates.
  6. How to trim glTF file size using --keep-attribute

    main

    If an FBX contains superfluous vertex attributes, you can use the -k or --keep-attribute flag to reduce the output file size. When this flag is used, you must specify it repeatedly for every attribute you wish to retain.

    Supported arguments:

    • position
    • normal
    • tangent
    • binormial
    • color
    • uv0
    • uv1
  7. Build FBX2glTF on Linux and macOS

    main

    To build from source on Unix-like systems, you must have build essentials (gcc or clang), cmake, python 3.* (with pip3), and zstd installed.

    Note: This project currently only supports FBX SDK version 2019.2. The build system will fail if other versions are used.

    # 1. Set environment variables for SDK location and Conan configuration
    if [[ "$OSTYPE" == "darwin"* ]]; then
        export CONAN_CONFIG="-s compiler=apple-clang -s compiler.version=10.0 -s compiler.libcxx=libc++"
        export FBXSDK_TARBALL="https://github.com/zellski/FBXSDK-Darwin/archive/2019.2.tar.gz"
    elif [[ "$OSTYPE" == "linux"* ]]; then
        export CONAN_CONFIG="-s compiler.libcxx=libstdc++11"
        export FBXSDK_TARBALL="https://github.com/zellski/FBXSDK-Linux/archive/2019.2.tar.gz"
    else
        echo "This snippet only handles Mac OS X and Linux."
    fi
    
    # 2. Fetch the project
    git clone https://github.com/facebookincubator/FBX2glTF.git
    cd FBX2glTF
    
    # 3. Fetch, unpack, and decompress the FBX SDK
    curl -sL "${FBXSDK_TARBALL}" | tar xz --strip-components=1 --include */sdk/
    # Then decompress the contents
    zstd -d -r --rm sdk
    
    # 4. Install and configure Conan
    pip3 install conan
    conan remote add --force bincrafters https://api.bintray.com/conan/bincrafters/public-conan
    
    # 5. Initialize and run the build
    conan install . -i build -s build_type=Release ${CONAN_CONFIG}
    conan build . -bf build
  8. Run FBX2glTF to convert models

    main

    FBX2glTF is a command-line tool used to convert Autodesk FBX 3D models into the glTF 2.0 format. You can run it by providing the path to an FBX file as a positional argument, or by using explicit flags for input and output files.

    # Simple conversion
    > FBX2glTF ~/models/butterfly.fbx
    
    # Complex pipeline with specific input, output, and compression
    > FBX2glTF --binary --draco --verbose \
              --input ~/models/source/butterfly.fbx \
              --output ~/models/target/butterfly.glb
  9. Handle binary (GLB) output in convert()

    main

    When using the convert function, you can control whether the output is a GLB (binary) or glTF (JSON) file using the opts array:

    1. Explicit GLB: Provide a destination filename ending in .glb. If you also include --binary or -b in opts, the tool ensures consistency.
    2. Explicit glTF: Provide a destination filename ending in .gltf.
    3. Automatic GLB: If you include --binary or -b in the opts array, the function will automatically ensure the destination extension is .glb.
    4. Automatic glTF: If no extension is provided for destFile, it defaults to .gltf.
  10. How GltfModel manages glTF resources

    main

    The GltfModel class represents the complete glTF 2.0 structure. Because glTF relies on index-based referencing (e.g., an accessor pointing to the $n^{th}$ buffer view), the model uses a Holder<T> pattern to manage resource lifecycles and automatic index assignment.

    Resource Management with Holder<T>

    Each resource type (e.g., BufferData, AccessorData, MeshData) is stored within a Holder<T>. When you add a new resource via a hold() method:

    1. The resource is assigned a unique index (ix) based on its position in the collection.
    2. It is stored as a std::shared_ptr<T> within the holder.
    3. The holder ensures that all resources live as long as the GltfModel instance exists.

    While the model uses shared_ptr internally for ownership, it is intended that consumers pass around raw references (T&) or pointers (T*) during a single conversion run, as the GltfModel is designed to outlive the conversion activity.

  11. Understand Metallic-Roughness material resolution

    main

    FBX2glTF uses specific resolvers to map FBX materials to the glTF Metallic-Roughness model. The FbxRoughMetMaterialInfo structure holds the extracted properties, including baseColor, metallic, roughness, and emissive values, along with their corresponding texture maps (e.g., texNormal, texBaseColor, texMetallic).

    Two primary resolvers handle this mapping:

    1. FbxStingrayPBSMaterialResolver: Resolves Stingray PBS materials from FBX files.
    2. Fbx3dsMaxPhysicalMaterialResolver: Resolves 3ds Max Physical materials from FBX files.

    Both resolvers output an FbxRoughMetMaterialInfo object containing the processed material data and texture pointers.