Skip to content
groverburger edited this page Dec 23, 2021 · 11 revisions

Model

Last updated for g3d version 1.5.1

A g3d object that lets you draw 3D meshes with position, rotation, and scale.
A Model can be created using model = g3d.newModel(mesh, texture, translation, rotation, scale).
Use the function model:draw() in love.draw to draw a model to the screen.

Model Properties

shader

Defaults to the built-in shader defined in shader.lua.
You can modify this to have the Model render itself with your own custom shader.

verts

Either computed from the obj given or set to the list of vertices given on creation of the Model.

texture

Either loaded from the path or set to the image given on creation of the Model.

mesh

A Love mesh automatically computed from the given verts, texture, and vertexFormat.

love.graphics.newMesh(self.vertexFormat, self.verts, "triangles")
mesh:setTexture(self.texture)

vertexFormat

The specified data that can be stored in each vertex, and in what datatype.
The default vertexFormat should serve the vast majority of use cases, so you shouldn't have to worry about this unless you understand how it works and want to customize it.
The default vertexFormat that comes preloaded in every g3d Model is listed below:

vertexFormat = {
    {"VertexPosition", "float", 3},
    {"VertexTexCoord", "float", 2},
    {"VertexNormal", "float", 3},
    {"VertexColor", "byte", 4},
}

Model Functions

g3d.newModel(mesh, texture, translation, rotation, scale)

Creates a new Model object.

Arguments:

  • mesh
    • either a path to an obj file or a custom list of vectors.
  • texture
    • either be a path to an image or a Love image object.
  • translation optional, defaults to {0,0,0}
    • a vector representing where the Model is in space.
  • rotation optional, defaults to {0,0,0}
    • a vector representing the rotation of the Model in space.
  • scale optional, defaults to {1,1,1}
    • a vector representing the scale of the Model in each axis.

Model:makeNormals(isFlipped)

Automatically adds normals to the Model's mesh.
The resulting normals are computed as face normals, instead of smoothed vertex normals.

Arguments:

  • isFlipped optional, defaults to false
    • a boolean that represents whether the normals should be flipped the opposite direction. Useful if your model has a different winding order from what g3d expects.

Model:setTransform(translation, rotation, scale)

The arguments to this function are vectors in the form of {x,y,z}.
Sets the model's translation, rotation, and scale - collectively called the Model's transform.

Arguments:

  • translation optional, defaults to {0,0,0}
    • a vector representing where the Model is in space.
  • rotation optional, defaults to {0,0,0}
    • a vector representing the rotation of the Model in space.
  • scale optional, defaults to {1,1,1}
    • a vector representing the scale of the Model in each axis.

Model:setTranslation(tx,ty,tz)

Set just the translation of this model.

Arguments:

  • tx
    • a number representing the x coordinate
  • ty
    • a number representing the y coordinate
  • tz
    • a number representing the z coordinate

Model:setRotation(rx,ry,rz)

Set just the rotation of this model using Euler angles.

Arguments:

  • rx
    • a number representing the x rotation
  • ry
    • a number representing the y rotation
  • rz
    • a number representing the z rotation

Model:setAxisAngleRotation(x,y,z,angle)

Set the model's rotation around the axis specified.

Arguments:

  • x
    • a number representing the x axis of rotation
  • y
    • a number representing the y axis of rotation
  • z
    • a number representing the z axis of rotation
  • angle
    • a number representing how much the model should rotate around the given axis, given in radians

Model:setQuaternionRotation(x,y,z,w)

Set just the rotation of this model using a quaternion.

Arguments:

  • x
    • a number representing the first component of the quaternion
  • y
    • a number representing the second component of the quaternion
  • z
    • a number representing the third component of the quaternion
  • w
    • a number representing the fourth component of the quaternion

Model:setScale(sx,sy,sz)

Set just the scale of this model.

Arguments:

  • sx
    • a number representing the scale in the x dimension
  • sy
    • a number representing the scale in the y dimension
  • sz
    • a number representing the scale in the z dimension

Model:draw(shader)

Draws the Model and updates all four uniform variables in g3d's vertex shader.

Arguments:

  • shader optional, defaults to g3d's build in shader
    • a shader with which the model will be drawn

Model:compress()

Compresses the model so that it takes less space in memory. This is mainly useful for models that have hundreds or thousands of triangles.

Note: This function requires FFI to be enabled. Also, models can no longer be used for collision detection after they have been compressed.