Merge PR #11: camera fit
3d-model-renderer
A cross-platform (Linux, macOS, Windows) viewer for PMX / PMD (MikuMikuDance) and FBX models that lets you pose bones and drive vertex morphs (eyes, mouth, eyebrows) from an on-screen UI.
Built with C++17, OpenSceneGraph (rendering), Assimp (FBX and other formats) and Dear ImGui (UI).
How it works
.pmx ──► PmxLoader ─┐
.pmd ──► PmdLoader ─┼─► mr::Model ──► mr::Poser ──► ModelNode (OSG geometry + GLSL) ──► osgViewer
.fbx ──► Assimp ──┘ (one format) (morphs + skinning, ImGui overlay (bones, morphs, view)
CPU or GPU skinning)
- PMX and PMD use their own loaders because Assimp cannot read PMD and drops PMX morphs.
- Every format is converted into one internal model (right-handed, +Y up, facing +Z), so rendering, posing and morphing share a single code path.
- Bone rotations in the UI are in model-space axes at the bone pivot, so they behave the same for MMD and FBX rigs.
- Shaders are GLSL 1.20 so they run on macOS's legacy OpenGL context as well as Linux and Windows.
- Skinning has two paths, switchable under View: hardware (default; bone matrices go to the
vertex shader in a float texture) and software (
Poserskins every vertex on the CPU, using all cores). Hardware falls back to software if the GPU lacks vertex texture fetch or float textures. Morphs are applied on the CPU in both modes and only re-uploaded when morph weights change.
Supported: textures (incl. MMD sphere .sph/.spa and toon maps), vertex/group/bone/flip morphs,
BDEF1/2/4 weights (SDEF/QDEF approximated as linear blend), IK (CCD with MMD-style knee limits),
VMD bone/morph/IK-switch and camera playback with MMD's Bezier easing, and song playback
(.wav/.mp3/.flac via miniaudio) kept in sync with the timeline. Not yet:
VMD light/self-shadow keys, UV and material morphs.
Building
You need CMake ≥ 3.16, a C++17 compiler, OpenSceneGraph 3.6 and (optionally) Assimp 5.3 or newer. If no
suitable Assimp is installed, CMake downloads and builds Assimp 5.4.3 (-DMR_FETCH_ASSIMP=OFF turns that off).
Older system packages such as Ubuntu 22.04's Assimp 5.2 are ignored because they crash on some FBX files.
Dear ImGui is always downloaded.
macOS
xcode-select --install
brew install cmake ninja open-scene-graph assimp
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release
cmake --build build
./build/model_renderer path/to/model.pmx
macOS without Homebrew (e.g. macOS 12 and older)
Homebrew no longer supports older macOS releases. Everything can be built in a user directory without admin rights:
xcode-select --install
mkdir -p ~/Dev/mr-deps && cd ~/Dev/mr-deps
curl -fLO https://github.com/Kitware/CMake/releases/download/v3.31.6/cmake-3.31.6-macos-universal.tar.gz
tar xzf cmake-3.31.6-macos-universal.tar.gz
curl -fLO https://github.com/ninja-build/ninja/releases/download/v1.12.1/ninja-mac.zip
unzip ninja-mac.zip -d bin
export PATH=$PWD/cmake-3.31.6-macos-universal/CMake.app/Contents/bin:$PWD/bin:$PATH
curl -fL -o osg.tar.gz https://github.com/openscenegraph/OpenSceneGraph/archive/refs/tags/OpenSceneGraph-3.6.5.tar.gz
tar xzf osg.tar.gz
cmake -S OpenSceneGraph-OpenSceneGraph-3.6.5 -B osg-build -G Ninja -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=$PWD/osg-install -DCMAKE_INSTALL_NAME_DIR=$PWD/osg-install/lib \
-DBUILD_OSG_APPLICATIONS=OFF -DBUILD_OSG_EXAMPLES=OFF -DCMAKE_POLICY_VERSION_MINIMUM=3.5
cmake --build osg-build && cmake --install osg-build
cd path/to/3d-model-renderer
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_PREFIX_PATH=$HOME/Dev/mr-deps/osg-install -DMR_FETCH_ASSIMP=ON
cmake --build build
CMAKE_INSTALL_NAME_DIR matters: without it the OSG libraries are installed with bare names and the renderer fails at
startup with Library not loaded: libosgDB.161.dylib.
Linux (Debian / Ubuntu)
sudo apt install build-essential cmake ninja-build libopenscenegraph-dev libassimp-dev \
libgl1-mesa-dev fonts-noto-cjk xclip
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release
cmake --build build
./build/model_renderer path/to/model.pmx
Copy/paste in text fields uses xclip or xsel on X11 and wl-clipboard on Wayland.
Fedora: sudo dnf install cmake ninja-build gcc-c++ OpenSceneGraph-devel assimp-devel google-noto-sans-cjk-fonts.
Windows (Visual Studio 2022 + vcpkg)
git clone https://github.com/microsoft/vcpkg C:\vcpkg && C:\vcpkg\bootstrap-vcpkg.bat
C:\vcpkg\vcpkg install osg assimp --triplet x64-windows
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake
cmake --build build --config Release
build\Release\model_renderer.exe path\to\model.pmx
A Japanese font is picked up automatically (Hiragino on macOS, Meiryo/Yu Gothic on Windows, Noto CJK on Linux) so MMD bone and morph names display correctly.
Using it
The window is a dockable editor: Scene and Bones on the left, Morphs on the right,
the Timeline at the bottom and the 3D view in the middle. Drag a panel's tab to move, tab or
float it; the layout is saved to ~/.model_renderer_layout.ini (View > Reset panel layout
restores the default).
- Scenes: File > Save scene (Ctrl+S, Cmd+S on macOS) writes a
.mrscenefile with every model and stage (Position, Rotation, Scale, Stage, visibility), each model's dance, bone pose and morph edits, the camera motion, the song, the timeline frame, the 3D view's camera, the View settings and the Render size. File > Open scene... (Ctrl+O) opens it again, and New scene clears everything. Paths are stored relative to the scene file, so keep the scene next to (or above) itsmodels/folder and the whole folder can be moved or shared. The file is plain JSON. - File > Add model, motion or audio... (or Add media... in the Timeline) picks a model,
a dance
.vmd, a camera.vmdor a song (.wav/.mp3/.flac); camera files are detected automatically. A dance.vmdapplies to the model selected in the Scene panel. - Scene: several models can be loaded at once. The list selects which one the Bones, Morphs
and Timeline panels edit, with a visibility checkbox and
xto remove each one. Position, Rotation and Scale place the selected model. - Stages (Add stage...) are environment models such as a room or beach, as PMX, PMD or
DirectX
.x(the format most classic MMD stages and accessories use). They get no physics and don't move the camera when added. Files with "stage" or "ステージ" in the name, or very large models, are marked as stages automatically; the Stage checkbox changes it. - Bones: select a bone, then use the rotation sliders / move fields.
*marks posed bones. Bones the motion drives can still be offset. Bones in an IK chain follow their IK bone while View > Solve IK is on. - Morphs: sliders grouped as Eye / Mouth / Eyebrow / Other, as in MMD.
- Timeline: transport (play/pause, step, jump, frame field, loop, volume), a ruler to scrub
and one row of keyframes per camera, song, bone and morph track. Click a bone row to select
that bone. Ctrl+wheel zooms, Shift+wheel or middle drag pans. While the song plays its clock
drives the timeline. Untick Camera follow to orbit freely.
Camera fit... adapts a camera motion made for a different model: set Made for height (about
20 for standard MMD models, or Measure model... to read it from the model the motion came
with), then Fit to model scales the camera's heights to the selected model. Distance moves
every shot closer or farther and Offset X/Y/Z shifts the whole camera. With Follow model
placement on, moving, turning or scaling the model carries the camera with it. Saved in the scene;
--camera-made-for H --camera-fit,--camera-height S,--camera-distance Sand--camera-offset x,y,zdo the same from the command line. - Pop out (top-left of the 3D view, or View > Viewport in separate window) moves the 3D view into its own OS window, e.g. for a second monitor; close it or press Dock viewport back.
- File > Render... writes a PNG frame sequence at any resolution (presets up to 4K and
vertical), optionally with a transparent background. If
ffmpegis on thePATHit also encodesrender.mp4with the song. - Physics: models with rigid bodies and joints (hair, skirts, tails) are simulated with
Bullet, as in MMD. Toggle it in the Scene panel or View > Physics (
--physics offon the command line); Reset physics snaps everything back to the pose. Physics resets and settles when you jump on the timeline, and renders step it exactly one frame at a time. - Look panel (tabbed with Morphs): material style (MMD toon, Lambert, anime cel, soft
studio), light colour and direction, MMD edge outlines (from each PMX/PMD material's edge colour
and size), soft shadow mapping (self-shadows, shadows on stages, and a shadow-only floor when no
stage is visible), rim light, and one-click presets. Saved in
.mrscenefiles and used in final renders. Command line:--shading toon|lambert|cel|soft,--shadows on|off,--outlines on|off,--rim S. - Post-processing (Look panel): bloom, ambient occlusion, FXAA anti-aliasing, filmic tone
mapping and colour grading (exposure, contrast, saturation, temperature, vignette). All off by
default, so older GPUs pay nothing; the Anime and Studio presets turn some on. Also applied to
the pop-out window and final renders. Command line:
--bloom S,--ao on|off,--fxaa on|off,--filmic on|off. - View: floor grid with labelled X (red), Y (green) and Z (blue) axes (hidden in renders), skeleton overlay, wireframe, culling, shading style, outlines, shadows, GPU vs CPU skinning, UI scale (use 2 on Retina).
- Camera: left drag orbits, middle drag pans, right drag or wheel zooms, Space resets.
Command-line options (--help) allow scripted renders, e.g.
./build/model_renderer miku.pmd --morph あ=1 --morph まばたき=1 --bone 左腕=0,0,-40 \
--orbit 20,10 --hide-ui --screenshot out.png
--motion dance.vmd --camera camera.vmd --audio song.wav --play plays a full MMD scene
(--frame N jumps to a frame, --pop-out opens the separate viewport window);
--add model.pmx and --stage stage.pmx add more models (motions apply to the first);
--scale S and --stage-scale S resize the first model and the stages;
--no-grid hides the floor grid; model_renderer scene.mrscene opens a scene and
--save-scene FILE saves one after loading (--reload-test N reopens it N times and prints memory use);
--render DIR [--range A,B] [--render-size WxH] renders PNG frames without opening the UI flow;
tools/make_test_vmd.py --demo out.vmd writes a short demo motion. --skinning gpu|cpu picks the skinning path; --bench N prints load, frame and deformation times.
Tests
cmake -S . -B build -DMR_BUILD_TESTS=ON && cmake --build build && ctest --test-dir build
The test models in tests/data are generated by tools/make_test_models.py (PMX/PMD) and
tools/make_test_fbx.py (FBX, run with blender -b -P).
Layout
| Path | Contents |
|---|---|
src/model |
Internal model, PMX/PMD/Assimp loaders, Poser (morphs + skinning) |
src/render |
OSG scene node, shaders, texture loading |
src/ui |
ImGui ↔ OpenSceneGraph integration |
src/app |
Application, UI panels, file browser, command line |