Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public final class GltfLoader
- Object
- GltfLoader
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.NORMALis read when present; otherwise flat per-triangle normals are computed so lit materials still shade correctly.TEXCOORD_0is read when present; otherwise zero texture coordinates are written.
Materials, textures, skinning and animation in the glTF are ignored by
load – it is a geometry loader. Apply a Material (and a Texture
loaded via the device) to the returned mesh as usual. To keep the whole file
– every part, the materials, the skeleton and the animation clips – use
loadScene(byte[]) and draw it with com.codename1.gaming.AnimatedModel.
Example:
byte[] glb = loadGlbBytes(); // 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);
Nested types
class GltfLoader.GltfModel | A loaded glTF model: its geometry plus the base-color texture extracted from the model’s first material, if any. |
class GltfLoader.GltfImageModel | A loaded glTF model in device-free form: its geometry plus the decoded base-color image extracted from the model’s first material, if any. |
Methods
Inherited methods
Method details
load
public static Mesh load(GraphicsDevice device, InputStream in)
throws IOExceptionParameters
deviceGraphicsDevice- the device that allocates the mesh buffers
inInputStream- a stream over
.glbor.gltfbytes
Returns
Throws
load
public static Mesh load(GraphicsDevice device, byte[] data).glb or .gltf bytes.Parameters
deviceGraphicsDevice- the device that allocates the mesh buffers
databyte[]- the raw model bytes (binary
.glbor JSON.gltf)
Returns
load
public static Mesh load(byte[] data).glb or .gltf bytes without a
GraphicsDevice, allocating the buffers directly. Behaves exactly like
load(GraphicsDevice, byte[]); useful for parsing geometry off the
render thread or handing meshes to non GPU consumers such as the AR
content pipeline.Parameters
databyte[]- the raw model bytes (binary
.glbor JSON.gltf)
Returns
load
public static Mesh load(InputStream in)
throws IOExceptionGraphicsDevice. The stream is closed.Parameters
inInputStream- a stream over
.glbor.gltfbytes
Returns
Throws
loadModel
public static GltfLoader.GltfModel loadModel(GraphicsDevice device, byte[] data).glb
or .gltf bytes. Use this (rather than load) when the model carries its
own texture and you want it applied automatically.Parameters
deviceGraphicsDevice- the device that allocates the mesh buffers and texture
databyte[]- the raw model bytes
Returns
loadModel
public static GltfLoader.GltfModel loadModel(GraphicsDevice device, InputStream in)
throws IOExceptionThrows
loadImageModel
public static GltfLoader.GltfImageModel loadImageModel(byte[] data).glb
or .gltf bytes without a GraphicsDevice. The image is decoded but not
uploaded to the GPU, so this works off the render thread and on non GPU
consumers such as the AR content pipeline.Parameters
databyte[]- the raw model bytes
Returns
loadImageModel
public static GltfLoader.GltfImageModel loadImageModel(InputStream in)
throws IOExceptionGraphicsDevice. The stream is closed.Throws
loadScene
public static GltfScene loadScene(byte[] data)Loads every node, primitive, material, skin and animation clip of a glTF
file, keeping the vertex data on the CPU. Use this for characters and
other multi-part or animated models; com.codename1.gaming.AnimatedModel
poses and draws the result.
Unlike load(byte[]) this reads every triangle primitive of every mesh
node, the base color factor and embedded base color image of each
material, every skin (joints and inverse bind matrices) and every
animation clip with LINEAR or STEP sampling. CUBICSPLINE samplers
are read as linear through their value keys. Morph targets are ignored.
Parameters
databyte[]- the bytes of a
.glbor.gltf(with embedded buffers) file
Returns
loadScene
public static GltfScene loadScene(InputStream in)
throws IOExceptionloadScene(byte[]).
The stream is closed.Parameters
inInputStream- a stream over
.glbor.gltfbytes