Garry's Mod Wiki

IMesh:DrawSkinned

  IMesh:DrawSkinned( table bones, boolean multiply = false )

Description

Renders the mesh with the active matrix and given bone matrices.

Arguments

1 sequential table<VMatrix> bones
A list of matrices to use as bones. Up to 52 of them.
2 boolean multiply = false
If set, multiplies given matrices with currently active model matrix (cam.GetModelMatrix). This incurs a performance hit.

Example

Example usage. Draws a dancing cube using skeletal animation at maps 0, 0, 0 position. (gm_construct's spawn)

local mat = Material( "models/debug/debugwhite" ) local ZMin, ZMax = -10, 10 local pos = Vector( 0, 0, 0 ) local function SetPosZ( i ) pos[3] = math.Remap( math.sin( ( CurTime() * 7 ) + ( i / 2 ) ), -1, 1, ZMin, ZMax ) return pos end local s = 50 local v = { Vector( -s, -s, -s ), Vector( s, -s, -s ), Vector( s, s, -s ), Vector( -s, s, -s ), Vector( -s, -s, s ), Vector( s, -s, s ), Vector( s, s, s ), Vector( -s, s, s ), } local tris = { {1, 2, 3}, {1, 3, 4}, {5, 8, 7}, {5, 7, 6}, {1, 5, 6}, {1, 6, 2}, {2, 6, 7}, {2, 7, 3}, {3, 7, 8}, {3, 8, 4}, {4, 8, 5}, {4, 5, 1} } local normals = { Vector( 0, 0,-1 ), Vector( 0, 0,-1 ), Vector( 0, 0, 1 ), Vector( 0, 0, 1 ), Vector( 0,-1, 0 ), Vector( 0,-1, 0 ), Vector( 1, 0, 0 ), Vector( 1, 0, 0 ), Vector( 0, 1, 0 ), Vector( 0, 1, 0 ), Vector( -1, 0, 0 ), Vector( -1, 0, 0 ) } local imesh = Mesh( mat, 2 ) mesh.Begin( imesh, MATERIAL_TRIANGLES, 12 ) for ti, t in ipairs( tris ) do local n = normals[ti] for _, i in ipairs( t ) do mesh.Position( v[i] ) mesh.BoneData( 0, i, 1 ) mesh.BoneData( 1, i, 0 ) mesh.Normal( n ) mesh.Color( 255, 255, 255, 255 ) mesh.AdvanceVertex() end end mesh.End() local boneCount = 8 local boneTable = {} for i = 1, boneCount do boneTable[i] = Matrix() end hook.Add("PostDrawOpaqueRenderables", "Cube", function() -- Update bone matrices for i = 1, boneCount do boneTable[i]:Identity() boneTable[i]:Translate( SetPosZ( i ) ) end -- Setup model lighting render.SetLocalModelLights() -- Reset any previously set local model lights render.ResetModelLighting( 0.5, 0.5, 0.5 ) render.SetModelLighting( BOX_FRONT, 1, 1, 1 ) render.SetModelLighting( BOX_LEFT, 1, 0, 0 ) render.SetModelLighting( BOX_RIGHT, 0, 1, 0 ) render.SetModelLighting( BOX_TOP, 0, 0, 1 ) -- Set the render material render.SetMaterial( mat ) -- Draw the mesh with the bone table imesh:DrawSkinned( boneTable ) end)