Garry's Mod Wiki

Revision Difference

mesh.Begin#549837

<function name="Begin" parent="mesh" type="libraryfunc">⤶ ⤶ <function name="Begin" parent="mesh" type="libraryfunc">⤶ <description>Starts a new dynamic mesh. If an <page>IMesh</page> is passed, it will use that mesh instead.</description> <realm>Client</realm> <args> <arg name="mesh" type="IMesh" default="nil">Mesh to build. This argument can be removed if you wish to build a "dynamic" mesh. See examples below.</arg> <arg name="primitiveType" type="number">Primitive type, see <page>Enums/MATERIAL</page>.</arg> <arg name="primiteCount" type="number">The amount of primitives.</arg> </args> </function> <example> <description>Draws a triangle near Vector( 0, 0, 0 ) in the map using a dynamic mesh.</description> <code>⤶ ⤶ <note> Calling a vertex buffer seemly over 64 vertexes & using a iMesh object will lead to a crash. Avoid using the static mesh method below if it's a complex object.</note>⤶ ⤶ <code>⤶ local mat = Material( "editor/wireframe" ) -- The material (a wireframe) local verts = { -- A table of 3 vertices that form a triangle { pos = Vector( 0, 0, 0 ), u = 0, v = 0 }, -- Vertex 1 { pos = Vector( 10, 0, 0 ), u = 1, v = 0 }, -- Vertex 2 { pos = Vector( 10, 10, 0 ), u = 1, v = 1 }, -- Vertex 3 } hook.Add( "PostDrawOpaqueRenderables", "MeshLibTest", function() render.SetMaterial( mat ) -- Apply the material mesh.Begin( MATERIAL_TRIANGLES, 1 ) -- Begin writing to the dynamic mesh for i = 1, #verts do mesh.Position( verts[i].pos ) -- Set the position mesh.TexCoord( 0, verts[i].u, verts[i].v ) -- Set the texture UV coordinates mesh.AdvanceVertex() -- Write the vertex end mesh.End() -- Finish writing the mesh and draw it end ) </code> </example> ⤶ Precaching a mesh with a IMesh can be used for simple objects.⤶ ⤶ <note> Calling a vertex buffer seemly over 64 vertexes & using a iMesh object will lead to a crash. Avoid using the static mesh method below if it's a complex object.</note>⤶ ⤶ <example>⤶ <description>Draws a triangle near Vector( 0, 0, 0 ) in the map using a static mesh.</description>⤶ <example>⤶ <description>Draws a triangle near Vector( 0, 0, 0 ) in the map using a static mesh.</description>⤶ <code>⤶ ⤶ <code>⤶ local mat = Material( "editor/wireframe" ) -- The material (a wireframe) local obj = Mesh() -- Create the IMesh object local verts = { -- A table of 3 vertices that form a triangle { pos = Vector( 0, 0, 0 ), u = 0, v = 0 }, -- Vertex 1 { pos = Vector( 10, 0, 0 ), u = 1, v = 0 }, -- Vertex 2 { pos = Vector( 10, 10, 0 ), u = 1, v = 1 }, -- Vertex 3 } mesh.Begin( obj, MATERIAL_TRIANGLES, 1 ) -- Begin writing to the static mesh for i = 1, #verts do mesh.Position( verts[i].pos ) -- Set the position mesh.TexCoord( 0, verts[i].u, verts[i].v ) -- Set the texture UV coordinates mesh.AdvanceVertex() -- Write the vertex end mesh.End() -- Finish writing to the IMesh hook.Add( "PostDrawOpaqueRenderables", "MeshLibTest", function() render.SetMaterial( mat ) -- Apply the material obj:Draw() -- Draw the mesh end ) </code> </example>