Skip to content

Commit 7ec5b1c

Browse files
committed
Default Shader Lua Method
Added `get_default_shaders` and `reset_shader` to easily revert a model to its vanilla shaders without the need to store default values in a table
1 parent 5b1c5ff commit 7ec5b1c

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

gamedata/scripts/lua_help_ex.script

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,8 @@
389389
// Shader/Textures
390390
function get_shaders(bool)
391391
function set_shader(number, string, string, bool)
392+
function get_default_shaders(bool)
393+
function reset_shader(number, bool)
392394

393395
get_shaders(hud_mode)
394396
hud_mode is optional and will default to false
@@ -406,10 +408,16 @@
406408
},
407409
},
408410

411+
get_default_shaders(hud_mode)
412+
returns similar table as get_shaders but containing the default shader and texture names, even after they were changed by set_shader
413+
409414
set_shader(id, shader, texture, hud_mode) can assign a new shader/texture to the submesh ID
410415
id can be -1 to apply the shader/texture to all submeshes at once
411416
shader/texture can be nil if you only want to apply one of them
412417
hud_mode is optional and will default to false
418+
419+
reset_shader(id, hud_mode)
420+
same as set_shader but resets to the default shader/texture values, so you don't need to store them in a table to reset them later
413421
}
414422

415423
class CArtefact : CGameObject {

src/xrGame/script_game_object.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,57 @@ luabind::object CScriptGameObject::GetShaders(bool bHud)
11471147
return table;
11481148
}
11491149

1150+
luabind::object CScriptGameObject::GetDefaultShaders(bool bHud)
1151+
{
1152+
IKinematics* k = nullptr;
1153+
1154+
if (bHud)
1155+
{
1156+
CActor* act = smart_cast<CActor*>(&object());
1157+
CHudItem* itm = smart_cast<CHudItem*>(&object());
1158+
if (itm)
1159+
k = itm->HudItemData()->m_model;
1160+
else if (act)
1161+
k = g_player_hud->m_model->dcast_PKinematics();
1162+
}
1163+
1164+
if (!k)
1165+
k = object().Visual()->dcast_PKinematics();
1166+
1167+
luabind::object table = luabind::newtable(ai().script_engine().lua());
1168+
1169+
if (!k)
1170+
{
1171+
table["error"] = true;
1172+
return table;
1173+
}
1174+
1175+
IRenderVisual* vis = k->dcast_RenderVisual();
1176+
xr_vector<IRenderVisual*>* children = vis->get_children();
1177+
1178+
if (!children)
1179+
{
1180+
luabind::object subtable = luabind::newtable(ai().script_engine().lua());
1181+
subtable["shader"] = vis->getDebugShaderDef();
1182+
subtable["texture"] = vis->getDebugTextureDef();
1183+
table[1] = subtable;
1184+
return table;
1185+
}
1186+
1187+
int i = 1;
1188+
1189+
for (auto* child : *children)
1190+
{
1191+
luabind::object subtable = luabind::newtable(ai().script_engine().lua());
1192+
subtable["shader"] = child->getDebugShaderDef();
1193+
subtable["texture"] = child->getDebugTextureDef();
1194+
table[i] = subtable;
1195+
++i;
1196+
}
1197+
1198+
return table;
1199+
}
1200+
11501201
void set_shader_tex(IRenderVisual* vis, int id, LPCSTR shader, LPCSTR texture)
11511202
{
11521203
xr_vector<IRenderVisual*>* children = vis->get_children();
@@ -1172,6 +1223,31 @@ void set_shader_tex(IRenderVisual* vis, int id, LPCSTR shader, LPCSTR texture)
11721223
children->at(id)->SetShaderTexture(shader, texture);
11731224
}
11741225

1226+
void reset_shader_tex(IRenderVisual* vis, int id)
1227+
{
1228+
xr_vector<IRenderVisual*>* children = vis->get_children();
1229+
1230+
if (!children)
1231+
{
1232+
vis->ResetShaderTexture();
1233+
return;
1234+
}
1235+
1236+
if (id == -1)
1237+
{
1238+
for (auto* child : *children)
1239+
{
1240+
child->ResetShaderTexture();
1241+
}
1242+
return;
1243+
}
1244+
1245+
id--;
1246+
1247+
if (id >= 0 && children->size() > id)
1248+
children->at(id)->ResetShaderTexture();
1249+
}
1250+
11751251
void CScriptGameObject::SetShaderTexture(int id, LPCSTR shader, LPCSTR texture, bool bHud)
11761252
{
11771253
IKinematics* k = nullptr;
@@ -1197,3 +1273,29 @@ void CScriptGameObject::SetShaderTexture(int id, LPCSTR shader, LPCSTR texture,
11971273

11981274
set_shader_tex(k->dcast_RenderVisual(), id, shader, texture);
11991275
}
1276+
1277+
void CScriptGameObject::ResetShaderTexture(int id, bool bHud)
1278+
{
1279+
IKinematics* k = nullptr;
1280+
1281+
if (bHud)
1282+
{
1283+
CActor* act = smart_cast<CActor*>(&object());
1284+
CHudItem* itm = smart_cast<CHudItem*>(&object());
1285+
if (itm)
1286+
k = itm->HudItemData()->m_model;
1287+
else if (act)
1288+
{
1289+
reset_shader_tex(g_player_hud->m_model->dcast_RenderVisual(), id);
1290+
reset_shader_tex(g_player_hud->m_model_2->dcast_RenderVisual(), id);
1291+
return;
1292+
}
1293+
}
1294+
1295+
if (!k)
1296+
k = object().Visual()->dcast_PKinematics();
1297+
1298+
if (!k) return;
1299+
1300+
reset_shader_tex(k->dcast_RenderVisual(), id);
1301+
}

src/xrGame/script_game_object.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,9 @@ class CScriptGameObject
11221122
//-Alundaio
11231123

11241124
::luabind::object GetShaders(bool bHud = false);
1125+
::luabind::object GetDefaultShaders(bool bHud = false);
11251126
void SetShaderTexture(int id, LPCSTR shader, LPCSTR texture, bool bHud = false);
1127+
void ResetShaderTexture(int id, bool bHud = false);
11261128

11271129
script_attachment* AddAttachment(u16 slot, LPCSTR model_name);
11281130
script_attachment* GetAttachment(u16 slot);

src/xrGame/script_game_object_script3.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,9 @@ class_<CScriptGameObject>& script_register_game_object2(class_<CScriptGameObject
638638
.def("remove_attachment", &CScriptGameObject::RemoveAttachment)
639639

640640
.def("get_shaders", &CScriptGameObject::GetShaders)
641+
.def("get_default_shaders", &CScriptGameObject::GetDefaultShaders)
641642
.def("set_shader", &CScriptGameObject::SetShaderTexture)
643+
.def("reset_shader", &CScriptGameObject::ResetShaderTexture)
642644
;
643645
return (instance);
644646
}

0 commit comments

Comments
 (0)