HWRM 2.0 - Per-ship shader controls system

I typed all of this up for EvilleJedi (he asked for it), but decided to just post it all here…

Look at the lines commented-out in Hgn_BC.ship

The short version is, you define up to 32 ‘channels’ of data. A channel can be any value - though in some cases from 0->1 is best. You set the values using calls in the .ship (or using a LUA call) - and you can also assign channel values to named shader inputs - effectively letting you send data to the ship’s materials, and edit it via LUA.

BUT - it gets better.

You can also declare a navlight as using a channel, and it will scale/color based on that channel - so you can do a ‘red alert’ look, indeed a dying look, etc…

In the navlight style you can now use:

colorChannel = XX (channel number) - mults color by value.scaleChannel = XX (changes size of light) - mults scale by channel value.

SobGroup_SetShaderValues(
1 - SobGroup - “MySobGroup”, etc
2 - Scope - just use 17 (trust me)
3 - Effect Base Ships? - 0 for no, 1 for yes.
4 - SubSys filter - “” for no Subs, “*” for all, “XXX” for the named system to target
5 - Base Channel - Channel number to start at… 0->31
6,7,8,etc - Values for each channel from base onwards
)

2 Likes

Need clarification on how the ShaderProp is mapped to a channel ( see PM)
Lines from the HGN_BC

–setShaderChannels(NewShipType, 0, 1.0, 1.0, 0.0, 0.0);
–addShaderProp(NewShipType, “SOB_surfGlow”, 2, 2);

setShaderChannels(NewShipType, [shader channel], [channel parameter #1], [channel parameter #2],[…],[…], [channel parameter #n]);
addShaderProp(NewShipType, [shader parameter name], [???], [???]);

I would think that setShaderChannels creates a channel with values and addShaderProp maps the channel to a shader parameter

Another question is the initial level of performance hit on these functions. For example, if I applied to all ships that turns nav lights off by switching tactics to aggressive or evasive, obviously complex effects will have a performance penalty, but it looks like the only way to hook up the shader to a change in value or change in ship state is through a ship .lua ?

also is there any way to retrieve the current channel value?

setShaderChannels(ship, base channel, value for base, value for base+1, value for base+2, etc)…

So - setShaderChannels(sh, 0, 1, 1, 0, 0) means C0 = 1, C1 = 1, C2 = 0, C3 = 0

addShaderProp(ship, shader param, base channel, channel count)

As stated for Hgn_BC, that would make the BC’s lights all disappear - because C2 and C3 are both zero…

I can’t really guess the perf hit - this hasn’t been stress tested. It should be rather cheap, tbh. Lots of routines do heavy lifting on sobgroups, even large ones - And since the ‘hard’ stuff is done at load time in the .ship, just changing the values should be extremely cheap. That was the plan :wink:

No - channel values are write-only. This was by design. Otherwise we’re talking about having 32 ‘random’ values for every ship that anything can use… which smells like all sorts of trouble and non-MP safe insanity.

1 Like

I am missing something here, a trigger for the state change. Using the example of the Battlecruiser with the lights out for an example, where/what is the trigger for changing this state?

Could you do a gamerule level script to set ships glows to “off” when it’s EMP’d, for example?

Shaders are scary voodoo mixed with maths @bitvenom, what could possibly go wrong here? :stuck_out_tongue:

EDIT:

If anyone else is wondering, I used notepad++ to help me find where the SOB_surfGlow lives, it’s in *\shaders\gl_surf\sob_bay.surf, sob_ship.surf and sob_ship_glow.surf

I got this working from within a ship’s custom code! :smiley:

I don’t understand the guts of it yet, but I had to have this line in the ship file:

addShaderProp(NewShipType, "SOB_surfGlow", 2, 2);

and this line in the custom code!

SobGroup_SetShaderValues(CustomGroup,17,1,0,0, 1.0, 1.0, 0.0, 0.0)

It’s worth noting that the fourth parameter, the Subsystem Filter, needs the quotation marks!

Are we able to set more than one shader prop? My big plan with this is Borgification, which would be showing/hiding a DIFF, GLOW and NORM layer on top of the normal ship texture. As well as fiddling with the glow on demand… :thinking:

3 Likes

Assimilation you say… Hmmmmm. :slight_smile:

I’m still missing something actually…

local ShipHP = SobGroup_HealthPercentage(CustomGroup)

if ShipHP > 0.05 then

	SobGroup_SetShaderValues(CustomGroup,17,1,"*",0, 1.0, 1.0, 1.0, 1.0)

elseif ShipHP < 0.05 and ShipHP > 0.04 then

	SobGroup_SetShaderValues(CustomGroup,17,1,"*",0, 1.0, 1.0, 0.0, 0.0)

elseif ShipHP < 0.04 and ShipHP > 0.03 then

	SobGroup_SetShaderValues(CustomGroup,17,1,"*",0, 1.0, 1.0, 1.0, 1.0)

elseif ShipHP < 0.03 and ShipHP > 0.02 then

	SobGroup_SetShaderValues(CustomGroup,17,1,"*",0, 1.0, 1.0, 0.0, 0.0)

elseif ShipHP < 0.02 and ShipHP > 0.01 then

	SobGroup_SetShaderValues(CustomGroup,17,1,"*",0, 1.0, 1.0, 1.0, 1.0)

elseif ShipHP < 0.01 then

	SobGroup_SetShaderValues(CustomGroup,17,1,"*",0, 1.0, 1.0, 0.0, 0.0)

end

This doesn’t change the glow of the ship… it should be flickering just before it dies :confused:

//EDIT
In fact… I think I was just wrong… addShaderProp turns off the lights on its own without the sobgroup function doing anything… gah >_<

VERY briefly (crazy busy today)…

When you assign the shader prop you are mapping some channels to a specific Surface property. You can’t edit those real-time (the mappings). When you construct the ship, the same place you assign channels->surface props, you are setting the birth/default values for those channels.

So -

setShaderChannels(NewShipType, 0, 1.0, 1.0, 1.1, 0.25);
setShaderChannels(NewShipType, 2, 1.1, 0.25);
-- These both set channel 2 & 3 to 1.1, 0.25 respectively
-- one just starts at 0, also settings 0 & 1 to 1.0
-- You can have 32 channels total, set to whatever you want.
-- Don't forget, NAV_LIGHTS can also bind to channels
-- for example NL for birth, NL for red alert, NL for near death, etc.

addShaderProp(NewShipType, "SOB_surfGlow", 2, 2);

If you look at the defaults for SOB_surfGlow, you’ll see that they can be a few things, but most often (except when a thruster surface), they are 1.1, 0.25 (the .surf declares the default when mapping, the .prog uses them, which I believe resets them (I should confirm that!) - and in the case of ships, material props are sob_share_materials.imp

Now, elsewhere in code you can call SobGroup_SetShaderValues as you have been, and get some results (I tested this a fair bit…) - but your call is a bit wasteful:

SobGroup_SetShaderValues(CustomGroup,17,1,"*",2, 0.0, 0.0)

Is better, skip setting 0 and 1.

5 Likes

Ok, so I am getting this conceptually now, I really am. It is however still not working for me. Before I bug BitVenom with essentially a repeated post here are all the parts:

For the ship file:

setShaderChannels(NewShipType, 2, 1.1, 0.25);
addShaderProp(NewShipType, "SOB_surfGlow", 2, 2);
addCustomCode(NewShipType,"data:Ship/fed_TNG_flagship/fed_TNG_flagship.lua","","Create_fed_TNG_flagship","Update_fed_TNG_flagship","Destroy_fed_TNG_flagship","fed_TNG_flagship",0.5)

For the {shipname}.lua file:

function Create_fed_TNG_flagship(CustomGroup, playerIndex, shipID)	

SobGroup_SetShaderValues(CustomGroup,17,1,"*", 2, 0.0, 0.0)

end

function Update_fed_TNG_flagship(CustomGroup, playerIndex, shipID)
end

function Destroy_fed_TNG_flagship(CustomGroup, playerIndex, shipID)
end

With just these two parts I expect that if I run the game, the fed_TNG_flagship will have its glows disabled. It however does not have disabled glows. I can add a print statement in with the create function so I know it’s being called properly. This should be relatively simple for anyone to reproduce… can someone please give it a shot? I want to make sure I’m not doing something stupid :sweat:

If the SobGroup_SetShaderValues code isn’t connected for custom code, I imagine that would just crash, right? I may be able to debug this in the near future… It should work.

2 Likes

The (ship) surface declares it as 1.5, 0.5, and the program sets it to 1.0, 0.25 (and I can confirm that is what is finally passed to the shader).


I’m going to take a delve into this…

Just did some testing:

hgn_battlecruiser.ship

setShaderChannels(NewShipType, 2, 0.0, 0.0);
addShaderProp(NewShipType, "SOB_surfGlow", 2, 2);
addCustomCode(NewShipType,"data:shaderprops.lua","","","Update_ShaderProps","","Hgn_Battlecruiser",0.1);

shaderprops.lua

function Update_ShaderProps(CustomGroup, playerIndex, shipID)
    -- abs(0.5-fract(inTime.x*0.125))*2.0; // Auto-cycle fade for testing
    local gt = Universe_GameTime() * 0.125
    local fract = gt - floor(gt)
    local power = abs(0.5 - fract) * 2.0;
    
    print("setting value: " .. power)
    SobGroup_SetShaderValues(CustomGroup,17,1,"*", 2, power, 0.25)
end

Result (with a modifed shader fragment):

image

The values aren’t updating via custom code. I am getting print values though.

2 Likes

Have you checked to see if SobGroup_SetShaderValues works when called from a gamerule rather than from inside the CustomCode?

Not yet…

Edit:

According to the log, the function still is being called…

Maybe a parameter is wrong?

My faith in my ability to follow instructions is restored at least :sweat_smile:

1 Like

If you screw up the parameters it should log warnings/errors - does it?

AH - I think I see the issue…

The Sobgroup you pass in is by NAME. CustomGroup isn’t a name?

2 Likes

What’s a Sobgroup-safe command you know works in Custom code? I want to look at how it gets the group…

3 Likes

No errors/warnings.


Well:

print("setting value for " .. CustomGroup .. " (shipID: " .. shipID .. "): " .. power)

results in:

setting value for Hgn_Battlecruiser (shipID: 592): 0.2500002384185791

(Also, sSobGroupName in addCustomCode)


Just one example from the Kushan drone frigate:

SobGroup_CreateShip(CustomGroup, "kus_drone"..k)

HwRM.log:

Wed Dec 07 16:48:14 2016
Version 2.1, Build Number 31, Changelist Number 1237961
Loaded Archive: 'Homeworld2.big'
Failed to Load Archive '..\..\DATAUPDATES\UpdateHomeworld2.big'
Loaded Archive: 'HW1Ships.big'
Failed to Load Archive '..\..\DATAUPDATES\UpdateHW1Ships.big'
Loaded Archive: 'HW2Ships.big'
Failed to Load Archive '..\..\DATAUPDATES\UpdateHW2Ships.big'
Loaded Archive: 'HWBackgrounds.big'
Failed to Load Archive '..\..\DATAUPDATES\UpdateHWBackgrounds.big'
UTIL -- filepath failure, path doesn't exists 'C:\Program Files (x86)\Steam\steamapps\common\Homeworld\HomeworldRM\data\locale\English'
Loaded Archive: 'English.big'
Failed to Load Archive '..\..\DATAUPDATES\UpdateEnglish.big'
Failed to Load Archive '..\..\DATAUPDATES\Compatibility.big'
Uing ..profiles\ for profiles folder
GAME -- Using player profile radar3301
Changing from a 32 bit colour depth in winNT (6.1 build 7600),
Display: (0, 0, 800, 600) - (286, 110)
GL Info: 3.3 - 3.3.0 - Build 20.19.15.4474
GL Vendor: Intel
GL Renderer: Intel(R) HD Graphics 5500
GL Part ID: 5500
Loaded Archive: 'EnglishSpeech.big'
Failed to Load Archive '..\..\DATAUPDATES\UpdateEnglishSpeech.big'
Loaded Archive: 'Music.big'
Failed to Load Archive '..\..\DATAUPDATES\UpdateMusic.big'
SOUND -- created destination [ fdaudio ], handle [ 4 ] with [ 48 ] channels created
SOUND -- created destination [ fda streamer ], handle [ 5 ] with [ 8 ] channels created
SOUND -- created destination [ dxa streamer ], handle [ 0 ] with [ 8 ] channels created
SOUND -- created destination [ dxaudio ], handle [ 1 ] with [ 48 ] channels created
Build name:
Data path : C:\Program Files (x86)\Steam\steamapps\common\Homeworld\HomeworldRM\data
Font Load: data:UI\Font\Blender.rcf, 800,600
Font Attr: 14, Biu
No mapping for font 'Blender' - using 'default'
Resetting fp PC control word.
CmdLine: -overridebigfile -luatrace -nomovies -allowProgramReloads 1
12 Races Discovered
Race Filtering: DEATHMATCH rules - @Deathmatch,Extras
$4951 loaded
$2735 loaded
$4950 loaded
Escort Formation loaded
$2737 loaded
$2736 loaded
Goose Formation loaded
Hexagon Formation loaded
$4953 loaded
$4955 loaded
$5484 loaded
$5480 loaded
$5482 loaded
$4954 loaded
Wedge Formation loaded
$4952 loaded
3D X Formation loaded
Starting Level: DATA:\LevelData\Multiplayer\DeathMatchHW2\2p_hostilities_end.level
Font Load: data:UI\Font\SmallFonts7.rcf, 800,600
Font Attr: 14, Biu
Cannot overwrite function Create_Hgn_Carrier
Cannot overwrite function Update_Hgn_Carrier
Unable to retrieve UI (animated) texture ()
Error, LoadScreen failed to screen of type GameBalanceScreen when loading section GameBalanceScreen from file DATA:\UI\NewUI\Developer\GameBalanceScreen.lua
Error, LoadScreen failed to screen of type DeveloperScreen when loading section DeveloperScreen from file DATA:\UI\NewUI\Developer\DeveloperScreen.lua
persistant data loading: trying to put a non dock capable ship in parade (Hgn_Probe), has the NIS put crap in the player's fleet again?
persistant data loading: trying to put a non dock capable ship in parade (Hgn_Probe), has the NIS put crap in the player's fleet again?
persistant data loading: trying to put a non dock capable ship in parade (Hgn_Probe), has the NIS put crap in the player's fleet again?
persistant data loading: trying to put a non dock capable ship in parade (Hgn_Probe), has the NIS put crap in the player's fleet again?
persistant data loading: trying to put a non dock capable ship in parade (Hgn_Probe), has the NIS put crap in the player's fleet again?
persistant data loading: trying to put a non dock capable ship in parade (Hgn_Probe), has the NIS put crap in the player's fleet again?
persistant data loading: trying to put a non dock capable ship in parade (Hgn_Probe), has the NIS put crap in the player's fleet again?
persistant data loading: trying to put a non dock capable ship in parade (Hgn_Probe), has the NIS put crap in the player's fleet again?
build available
generic build available
build available
generic build available
build available
generic build available
build available
generic build available
build available
utility build available HGN_PROBE  UTILITY
build available
generic build available
carrier open
setting value: 0.8749999850988388
setting value for Hgn_Battlecruiser (shipID: 592): 0.7499999701976776
setting value for Hgn_Battlecruiser (shipID: 608): 0.7499999701976776
setting value for Hgn_Battlecruiser (shipID: 624): 0.7499999701976776
setting value for Hgn_Battlecruiser (shipID: 640): 0.7499999701976776
setting value: 0.7499999701976776
setting value: 0.6250000894069672
setting value for Hgn_Battlecruiser (shipID: 592): 0.5000002086162567
setting value for Hgn_Battlecruiser (shipID: 608): 0.5000002086162567
setting value for Hgn_Battlecruiser (shipID: 624): 0.5000002086162567
setting value for Hgn_Battlecruiser (shipID: 640): 0.5000002086162567
setting value: 0.5000002086162567
setting value: 0.3750002980232239
setting value for Hgn_Battlecruiser (shipID: 592): 0.2500004172325134
setting value for Hgn_Battlecruiser (shipID: 608): 0.2500004172325134
setting value for Hgn_Battlecruiser (shipID: 624): 0.2500004172325134
setting value for Hgn_Battlecruiser (shipID: 640): 0.2500004172325134
setting value: 0.2500004172325134
setting value: 0.125000536441803
setting value for Hgn_Battlecruiser (shipID: 592): 6.556510925292969e-007
setting value for Hgn_Battlecruiser (shipID: 608): 6.556510925292969e-007
setting value for Hgn_Battlecruiser (shipID: 624): 6.556510925292969e-007
setting value for Hgn_Battlecruiser (shipID: 640): 6.556510925292969e-007
setting value: 6.556510925292969e-007
setting value: 0.1249997615814209
setting value for Hgn_Battlecruiser (shipID: 592): 0.2500002384185791
setting value for Hgn_Battlecruiser (shipID: 608): 0.2500002384185791
setting value for Hgn_Battlecruiser (shipID: 624): 0.2500002384185791
setting value for Hgn_Battlecruiser (shipID: 640): 0.2500002384185791
setting value: 0.2500002384185791
setting value: 0.3750007152557373
setting value for Hgn_Battlecruiser (shipID: 592): 0.5000011920928955
setting value for Hgn_Battlecruiser (shipID: 608): 0.5000011920928955
setting value for Hgn_Battlecruiser (shipID: 624): 0.5000011920928955
setting value for Hgn_Battlecruiser (shipID: 640): 0.5000011920928955
setting value: 0.5000011920928955
setting value: 0.6250016689300537
setting value for Hgn_Battlecruiser (shipID: 592): 0.7500021457672119
setting value for Hgn_Battlecruiser (shipID: 608): 0.7500021457672119
setting value for Hgn_Battlecruiser (shipID: 624): 0.7500021457672119
setting value for Hgn_Battlecruiser (shipID: 640): 0.7500021457672119
setting value: 0.7500021457672119
1 Like

Try screwing up the call - to see if you see a log for that… ?

Like, instead of CustomGroup, just use 0
Or instead of “*”, use 0 - or an in-place array, etc.

Knowing that you can see the error log for that call is important…

4 Likes

So this is interesting…

I have a rule set up and the custom code set up… Set both the SobGroup and the Target(?) to 0 in both places, and got this error:

From the rule:

setting value: 0.8749999850988388
luasobgroupactions 4956:  SobGroup_SetShaderValues: could not find sobgroup (0)
parameter: 0
stack traceback:
   1:  function `Update_ShaderProps' at line 66 [string ""]

From the custom code:

setting value for Hgn_Battlecruiser (shipID: 592): 0.7499999701976776
luasobgroupactions 4956:  SobGroup_SetShaderValues: could not find sobgroup (0)
parameter: bad argument #1 to `?' (string expected, got table)
parameter: unable to recover; exiting
Lua -- Critical error-- FATAL EXIT --luamem/103:!--stack trace--
Display: (0, 0, 100, 100) - (3, 26)

I’ll try setting only the target next. Stay tuned…


Edit:

Only setting the target to 0 has no effect. Setting it to an empty array ({}):

setting value: 0.8749999850988388
luasobgroupactions 4943:  SobGroup_SetShaderValues : Incorrect type for parameter 4 - Subsystem mask name
parameter: Player_Ships0
stack traceback:
   1:  function `Update_ShaderProps' at line 66 [string ""]
Display: (0, 0, 100, 100) - (3, 26)

Not sure why it says the parameter is Player_Ships0; perhaps there’s a bug in the type-checking/error reporting section of that function?

Of particular note here, is that it failed in the rule, before it had a chance to get to the custom code…


Edit 2:

Allowing the Rule to process normally, it fails at the custom code:

setting value for Hgn_Battlecruiser (shipID: 592): 0.7499999701976776
luasobgroupactions 4943:  SobGroup_SetShaderValues : Incorrect type for parameter 4 - Subsystem mask name
parameter: bad argument #1 to `?' (string expected, got table)
parameter: unable to recover; exiting
Lua -- Critical error-- FATAL EXIT --luamem/103:!--stack trace--
Display: (0, 0, 100, 100) - (3, 26)

(any further test requests on my part are going to have to wait until tomorrow)

2 Likes