Class GltfLoader

java.lang.Object
com.codename1.gpu.GltfLoader

public final class GltfLoader extends Object

Loads a Mesh from a glTF 2.0 model so applications can render real authored geometry rather than only the built in Primitives. Both the binary container (.glb) and the JSON form (.gltf) are supported; for the JSON form, buffers must be embedded as data: URIs (external .bin side files are not fetched). The first triangle primitive of the first mesh is read.

The loader produces the engine's standard VertexFormat.POSITION_NORMAL_TEXCOORD layout so the result drops straight into any built in Material:

  • POSITION (required) is read as the vertex position.
  • NORMAL is read when present; otherwise flat per-triangle normals are computed so lit materials still shade correctly.
  • TEXCOORD_0 is read when present; otherwise zero texture coordinates are written.

Materials, textures, skinning and animation in the glTF are ignored -- this is a geometry loader. Apply a Material (and a Texture loaded via the device) to the returned mesh as usual.

Example:

byte[] glb = ...; // bytes of a .glb model
Mesh mesh = GltfLoader.load(device, glb);
Material material = new Material(Material.Type.PHONG).setTexture(texture);
device.draw(mesh, material, modelMatrix);
  • Method Details

    • load

      public static Mesh load(GraphicsDevice device, java.io.InputStream in) throws java.io.IOException

      Reads all bytes from the stream and loads the model. The stream is closed.

      Parameters
      • device: the device that allocates the mesh buffers

      • in: a stream over .glb or .gltf bytes

      Returns

      the loaded mesh

      Throws:
      java.io.IOException
    • load

      public static Mesh load(GraphicsDevice device, byte[] data)

      Loads a model from in-memory .glb or .gltf bytes.

      Parameters
      • device: the device that allocates the mesh buffers

      • data: the raw model bytes (binary .glb or JSON .gltf)

      Returns

      the loaded mesh

    • loadModel

      public static GltfLoader.GltfModel loadModel(GraphicsDevice device, byte[] data)

      Loads a model together with its base-color texture from in-memory .glb or .gltf bytes. Use this (rather than load) when the model carries its own texture and you want it applied automatically.

      Parameters
      • device: the device that allocates the mesh buffers and texture

      • data: the raw model bytes

      Returns

      the loaded mesh plus its base-color texture (null texture if the model has none)

    • loadModel

      public static GltfLoader.GltfModel loadModel(GraphicsDevice device, java.io.InputStream in) throws java.io.IOException
      Reads all bytes from the stream and loads the model with its base-color texture. The stream is closed.
      Throws:
      java.io.IOException