Garry's Mod Wiki

Revision Difference

ENTITY:GetRenderMesh#515599

<function name="GetRenderMesh" parent="ENTITY" type="hook">⤶ <ishook>yes</ishook>⤶ <description>Specify a mesh that should be rendered instead of this SENT's model.</description>⤶ <realm>Client</realm>⤶ <predicted>No</predicted>⤶ <rets>⤶ <ret name="" type="table">A table containing the following keys:⤶ * <page>IMesh</page> Mesh - Required⤶ * <page>IMaterial</page> Material - Required⤶ * <page>VMatrix</page> Matrix - Optional</ret>⤶ </rets>⤶ </function>⤶ ⤶ <example>⤶ <description>A box that renders nicely with ambient lighting, projected textures, and bumpmaps. Performed in the most lines of code possible.</description>⤶ <code>⤶ AddCSLuaFile()⤶ ⤶ DEFINE_BASECLASS( "base_anim" )⤶ ⤶ ENT.PrintName = "Other Cube"⤶ ENT.Spawnable = true⤶ ⤶ ENT.Mins = Vector( -16, -16, -16 )⤶ ENT.Maxs = Vector( 16, 16, 16 )⤶ ⤶ ENT.Material = Material( "hunter/myplastic" )⤶ ⤶ function ENT:SpawnFunction( ply, tr, ClassName )⤶ local ent = ents.Create( ClassName )⤶ ent:SetPos( tr.HitPos + tr.HitNormal * 32 )⤶ ent:Spawn()⤶ return ent⤶ end⤶ ⤶ function ENT:Initialize()⤶ if CLIENT then⤶ self:CreateMesh()⤶ self:SetRenderBounds( self.Mins, self.Maxs )⤶ end⤶ ⤶ self:DrawShadow( false )⤶ end⤶ ⤶ function ENT:GetRenderMesh()⤶ return { Mesh = self.Mesh, Material = self.Material }⤶ end⤶ ⤶ function ENT:CreateMesh()⤶ self.Mesh = Mesh()⤶ ⤶ local positions = {⤶ Vector( -0.5, -0.5, -0.5 ),⤶ Vector( 0.5, -0.5, -0.5 ),⤶ Vector( -0.5, 0.5, -0.5 ),⤶ Vector( 0.5, 0.5, -0.5 ),⤶ Vector( -0.5, -0.5, 0.5 ),⤶ Vector( 0.5, -0.5, 0.5 ),⤶ Vector( -0.5, 0.5, 0.5 ),⤶ Vector( 0.5, 0.5, 0.5 ),⤶ };⤶ ⤶ local indices = {⤶ 1, 7, 5,⤶ 1, 3, 7,⤶ 6, 4, 2,⤶ 6, 8, 4,⤶ 1, 6, 2,⤶ 1, 5, 6,⤶ 3, 8, 7,⤶ 3, 4, 8,⤶ 1, 4, 3,⤶ 1, 2, 4,⤶ 5, 8, 6,⤶ 5, 7, 8,⤶ }⤶ ⤶ local normals = {⤶ Vector( -1, 0, 0 ),⤶ Vector( 1, 0, 0 ),⤶ Vector( 0, -1, 0 ),⤶ Vector( 0, 1, 0 ),⤶ Vector( 0, 0, -1 ),⤶ Vector( 0, 0, 1 ),⤶ }⤶ ⤶ local tangents = {⤶ { 0, 1, 0, -1 },⤶ { 0, 1, 0, -1 },⤶ { 0, 0, 1, -1 },⤶ { 1, 0, 0, -1 },⤶ { 1, 0, 0, -1 },⤶ { 0, 1, 0, -1 },⤶ }⤶ ⤶ local uCoords = {⤶ 0, 1, 0,⤶ 0, 1, 1,⤶ 0, 1, 0,⤶ 0, 1, 1,⤶ 0, 1, 0,⤶ 0, 1, 1,⤶ 0, 1, 0,⤶ 0, 1, 1,⤶ 0, 1, 0,⤶ 0, 1, 1,⤶ 0, 1, 0,⤶ 0, 1, 1,⤶ }⤶ ⤶ local vCoords = {⤶ 0, 1, 1,⤶ 0, 0, 1,⤶ 0, 1, 1,⤶ 0, 0, 1,⤶ 0, 1, 1,⤶ 0, 0, 1,⤶ 0, 1, 1,⤶ 0, 0, 1,⤶ 0, 1, 1,⤶ 0, 0, 1,⤶ 0, 1, 1,⤶ 0, 0, 1,⤶ }⤶ ⤶ local verts = {}⤶ local scale = self.Maxs - self.Mins⤶ ⤶ for vert_i = 1, #indices do⤶ local face_i = math.ceil( vert_i / 6 )⤶ ⤶ verts[vert_i] = {⤶ pos = positions[indices[vert_i]] * scale,⤶ normal = normals[face_i],⤶ u = uCoords[vert_i],⤶ v = vCoords[vert_i],⤶ userdata = tangents[face_i]⤶ }⤶ end⤶ ⤶ self.Mesh:BuildFromTriangles( verts )⤶ end⤶ </code>⤶ ⤶ </example>