The glTF file is the JSON part of a glTF representation of a 3D scene. This function creates the R structure corresponding to one, and writes the binary buffer file for it.

as.gltf(x, ...)

# S3 method for default
as.gltf(x, y = NULL, z = NULL, vertices,
                            material = NULL,
                            normals = NULL,
                            texcoords = NULL,
                            points = NULL, segments = NULL,
                            triangles = NULL,
                            quads = NULL,
                            transform = diag(4),
                            extras = NULL,
                            ...,
                            rglscene = list(),
                            previous = Gltf$new(),
                            newScene = FALSE,
                            parentNode = NULL,
                            dir = tempdir(),
                            scale = c(1,1,1))

# S3 method for rglscene
as.gltf(x, ..., previous = Gltf$new(),
                            newScene = FALSE)
# S3 method for mesh3d
as.gltf(x, ...)

Arguments

x

An object to convert to a "gltf" object.

y, z

In the default method, combined with x to make coordinates. Any reasonable way of defining the coordinates is acceptable. See the function xyz.coords for details.

vertices

A 3 or 4 row matrix of Euclidean or homogeneous coordinates; takes precedence over x, y, z.

material

material properties for rendering

normals

normals at each vertex as a 3 or 4 column matrix

texcoords

texture coordinates at each vertex as a 2 column matrix

points

vector of indices of vertices to draw as points

segments

2 x n matrix of indices of vertices to draw as segments

triangles

3 x n matrix of indices of vertices to draw as triangles

quads

4 x n matrix of indices of vertices to draw as quads

transform

4 x 4 matrix associated with this object (e.g. a subscene)

extras

A list to attach as extras component to the result

...

Other parameters passed to the default method.

rglscene

The RGL scene this came from, e.g. to look up defaults

previous

Optionally a previously produced "gltf" object; the new geometry will be added to it.

newScene

logical; if TRUE, add a new scene to previous, otherwise try to add to the existing scene.

parentNode

If not NULL, add the new object as a child of this node, otherwise add to the first node of the default scene.

dir

Where to write the binary buffer file.

scale

Rescaling in the enclosing subscene.

Details

as.gltf is a generic function.

The method for "rglscene" objects can handle most objects produced by scene3d, but not all objects will be handled. In particular:

  • Lights, text, bounding box decorations and backgrounds are saved in “extra” fields, so they can be read by rgl2gltf, but most other software will ignore them or only display some parts.

  • Most material properties are also stored in “extra” fields.

There are methods for many individual types of "rglobject", but these are intended for internal use.

Value

A "gltf" object.

References

The specification of the glTF format: https://www.khronos.org/registry/glTF/specs/2.0/glTF-2.0.html

Author

Duncan Murdoch

Examples

cube <- rgl::rotate3d(rgl::cube3d(col = "red"), -pi/10, 1,0,0)
gltf <- as.gltf(cube)
rgl::plot3d(gltf)
gltf$closeBuffers()