基础HUD修改
< Zh
Jump to navigation
Jump to search

This page is Machine translated
It is not recommended to use machine translation without any corrections.
If the article is not corrected in the long term, it will be removed.
Also, please make sure the article complies with the alternate languages guide.
It is not recommended to use machine translation without any corrections.
If the article is not corrected in the long term, it will be removed.
Also, please make sure the article complies with the alternate languages guide.
本教程将展示如何通过添加图形来修改HUD。虽然为HUD添加图形并不困难,但建议您熟悉材质创建 流程。
导入HUD材质
一般来说,为HUD添加图形与在Source引擎游戏中添加材质的流程相同。不同之处在于我们需要在.tga
文件附带的.txt
文件中为vtex 提供额外参数。
以下是一个配置示例:
"UnlitGeneric" { "nomip" "1" "nocompress" "1" "nolod" "1" }
此处我们通过参数告诉vtex:
- $nomip
- 不生成mipmap层级,因为HUD不需要多级细节
- $nocompress
- 禁用纹理压缩,防止图形出现伪影
- $nolod
- 在低版本DirectX中不使用低质量纹理,性能影响可忽略
配置完成后运行vtex工具。建议将生成的图形放在/materials/HUD/
目录(不存在则需创建)。
.vmt 文件配置示例:
"UnlitGeneric" { "$basetexture" "HUD/你的图形名称" "$translucent" "1" "$translucency" "1" "$ignorez" "1" }
添加HUD元素类
在Visual Studio中搜索class CHud
,建议备份原始文件。我们需要通过paint
函数绘制图形,并使用ConVar控制显示状态。
类定义示例:
#include "hudelement.h"
#include <vgui_controls/Panel.h>
using namespace vgui;
class CHudImport : public CHudElement, public Panel
{
DECLARE_CLASS_SIMPLE( CHudImport, Panel );
public:
CHudImport( const char *pElementName );
void togglePrint();
virtual void OnThink();
protected:
virtual void Paint();
int m_nImport;
};
在hud_import.cpp
中:
#include "hud.h"
#include "cbase.h"
#include "hud_import.h"
#include "iclientmode.h"
#include "hud_macros.h"
#include "vgui_controls/controls.h"
#include "vgui/ISurface.h"
#include "tier0/memdbgon.h"
using namespace vgui;
DECLARE_HUDELEMENT( CHudImport );
构造函数实现:
CHudImport::CHudImport( const char *pElementName ) : CHudElement( pElementName ), BaseClass( NULL, "HudImport" )
{
Panel *pParent = g_pClientMode->GetViewport();
SetParent( pParent );
SetVisible( false );
SetAlpha( 255 );
// 创建纹理
m_nImport = surface()->CreateNewTextureID();
surface()->DrawSetTextureFile( m_nImport, "HUD/import" , true, true);
SetHiddenBits( HIDEHUD_PLAYERDEAD | HIDEHUD_NEEDSUIT );
}
绘制函数:
void CHudImport::Paint()
{
SetPaintBorderEnabled(false);
surface()->DrawSetTexture( m_nImport );
surface()->DrawTexturedRect( 2, 2, 128, 128 );
}
待完善: 请熟悉HUD修改的开发者补充说明边框处理机制
定义ConVar
在cpp文件顶部添加:
static ConVar show_beta("show_beta", "0", 0, "在右上角切换测试版图标显示");
可见性控制函数:
void CHudImport::togglePrint()
{
if (!show_beta.GetBool())
this->SetVisible(false);
else
this->SetVisible(true);
}
void CHudImport::OnThink()
{
togglePrint();
BaseClass::OnThink();
}
编辑HudLayout.res
在mod目录的/scripts/HudLayout.res
中添加:
HudImport { "fieldName" "HudImport" "xpos" "r86" "ypos" "6" "wide" "80" "tall" "34" "visible" "0" "enabled" "1" "PaintBackgroundType" "2" }
更多VGUI方案信息请参考VGUI文档 。
动画纹理实现
若要实现动画效果,在材质配置文件中添加:
"UnlitGeneric" { "$basetexture" "hud/你的动画名称" "$translucent" "1" "Proxies" { "AnimatedTexture" { "animatedtexturevar" "$basetexture" "animatedtextureframenumvar" "$frame" "animatedtextureframerate" 10 } } }
注意事项: 1. 使用VTEX工具处理序列帧(配置"startframe"/"endframe") 2. 确保.txt文件与.tga序列位于materialsrc目录 3. .vmt必须与.vtf同名