Garry's Mod Wiki

Revision Difference

util.GetModelMeshes#564573

<function name="GetModelMeshes" parent="util" type="libraryfunc"> <description> Returns a table of visual meshes of given model. <note>This does not work on brush models (`*number` models)</note>⤶ See also <page>ENTITY:GetRenderMesh</page>. ⤶ Retrieves vertex, triangle, and bone data for the visual meshes of a given model. ⤶ <note>⤶ This does not work on brush models (Models with names in the format `*number`) </note>⤶ </description> <realm>Shared</realm> <args> <arg name="model" type="string">The full path to a model to get the visual meshes of.</arg>⤶ <arg name="lod" type="number" default="0">Which LOD to retrieve. 0 is the best quality, increasing the number lowers the model quaility.</arg>⤶ <arg name="bodygroupMask" type="number" default="0">Bodygroup combination for the model. This can be in format of `"000000"` where each number represents a bodygroup option.</arg> <arg name="model" type="string">⤶ The full path to the model to get the visual meshes of. </arg> <arg name="lod" type="number" default="0">⤶ Which of the model's Level of Detail (LOD) models to retrieve.⤶ ⤶ `0` is the best quality with higher numbers progressively lowering the quality.⤶ </arg>⤶ <arg name="bodygroupMask" type="number" default="0">⤶ The combination of bodygroups to retrieve meshes for.⤶ ⤶ For more information, see <page>Entity:SetBodyGroups</page>⤶ </arg>⤶ </args> <rets> <ret name="" type="table"> A table of tables with the following format:⤶ * <page>string</page> material - The material of the specific mesh⤶ * <page>table</page> triangles - A table of <page>Structures/MeshVertex</page>es ready to be fed into <page>IMesh:BuildFromTriangles</page>⤶ * <page>table</page> verticies - A table of <page>Structures/MeshVertex</page>es representing all the vertices of the mesh. This table is used internally to generate the "triangles" table.⤶ ⤶ Each <page>Structures/MeshVertex</page> returned also has an extra table of tables field called "weights" with the following data:⤶ * <page>number</page> bone - The bone this vertex is attached to⤶ * <page>number</page> weight - How "strong" this vertex is attached to the bone. A vertex can be attached to multiple bones at once.⤶ <ret name="modelMeshes" type="table"> A sequential table of <page text="ModelMeshData Structures">Structures/ModelMeshData</page>⤶ ⤶ Each index in this table corresponds to a mesh within the model passed as an argument to this function.⤶ </ret> <ret name="" type="table"> A table of tables containing the model bind pose (where the keys are the bone ID) with the following contents:⤶ * <page>number</page> parent - The ID of the parent bone. * <page>VMatrix</page> matrix - The bone's bind transform in model (not bone) space. ⤶ <ret name="modelBindPoses" type="table"> A sequential table of <page text="BoneBindPose Structures">Structures/BoneBindPose</page>⤶ ⤶ This tables indices are bone IDs for the <page>Structures/BoneBindPose</page> stored at each index. </ret> </rets> </function> <example> <description>Simple example usage on a Scripted Entity.</description>⤶ <description>⤶ A snippet of Entity code that renders a wireframe helicopter bomb by retrieving the model's mesh and using it to create a new mesh.⤶ </description>⤶ <code> -- The default material to render with in case we for some reason don't have one local myMaterial = Material( "models/wireframe" ) -- models/debug/debugwhite function ENT:CreateMesh() -- Destroy any previous meshes if ( self.Mesh ) then self.Mesh:Destroy() end -- Get a list of all meshes of a model local visualMeshes = util.GetModelMeshes( "models/combine_helicopter/helicopter_bomb01.mdl" ) -- Check if the model even exists⤶ -- Make sure the model exists before continuing⤶ if ( !visualMeshes ) then return end -- Select the first mesh local visualMesh = visualMeshes[ 1 ] -- Set the material to draw the mesh with from the model data myMaterial = Material( visualMesh.material ) -- You can apply any changes to visualMesh.verticies table here, distorting the mesh -- or any other changes you can come up with -- Create and build the mesh self.Mesh = Mesh() self.Mesh:BuildFromTriangles( visualMesh.triangles ) end -- A special hook to override the normal mesh for rendering function ENT:GetRenderMesh() -- If the mesh doesn't exist, create it! if ( !self.Mesh ) then return self:CreateMesh() end return { Mesh = self.Mesh, Material = myMaterial } end function ENT:Draw() -- Draw the entity's model normally, this calls GetRenderMesh at some point⤶ -- Draw the entity's model normally, this will internally call ENT:GetRenderMesh()⤶ self:DrawModel() end </code> </example>