Search code examples
phpimage-processinggltf3d-modelling

Render .glb / .gltf 3d model with PHP


Is there a possibility to create or modify .glb or .gltf files in PHP?

For example, I want to load a .glb file, modify it and save it again, similar to "Image Processing and Generation"-functions.

Pseudo code:

$model = @modelcreatefromglb("model.glb");
modelstring($model, 5, 0, 0, 0, "Hello world!");
modelglb($model, "model-with-text.glb");

I only found a small library for "blender" (.blend-Files), but this is not the desired way.


Solution

  • As far as I can tell, the PHP ecosystem doesn't really have many libraries for graphics APIs (OpenGL, Vulkan, Metal, etc.), and I couldn't find anything related to the glTF 2.0 format for the language. However, a .gltf file is just JSON, and you can parse its content with json_decode( $gltfContent ). Many changes, like changing materials or positions of objects, can be made just by modifying that JSON according to the glTF specification or quickstart guide. The vertex and texture data is usually in separate files (.bin, .png, .jpg) which would require more careful parsing to work with.

    A .glb is a binary file containing a small JSON header (equivalent content to a .gltf), followed by the binary vertex, animation, and texture data in one self-contained file. Parsing that is a bit more work but very doable. Here's example code in JavaScript, if that's helpful.