Natural Selection 2

Natural Selection 2

Not enough ratings
Alien Vision Editing
By Huze
This guide covers the basics of the alien vision shader and how you can edit it.
   
Award
Favorite
Favorited
Unfavorite
Setting up
This section covers how to set up your directory and shortcuts for modding. With this setup you won't be editing any official files directly. This is not required, but highly recommended.

Setting up your directory
  • Copy DarkVision.fx from
    C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\ns2\shaders
  • Create the "shaderMod\shaders" directory in your natural selection 2 directory.
    C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\shaderMod\shaders
  • Paste the shader into your newly created folder.

Creating your shortcut
  • Create a shortcut to NS2.exe
  • Edit the properties of the shortcut
  • Append ' -hotload -game shaderMod' to the end of the target
  • It should look like:
    "C:\Program Files (x86)\Steam\steamapps\common\natural selection 2\NS2.exe" -hotload -game shaderMod
When you launch the game via the shortcut you just created, everything in your 'shaders' directory will be loaded instead of the default.

Disable all consistency checking
  • Navigate to:
    %appdata%/natural selection 2/
  • Edit ConsistencyConfig.json (any editor will do, notepad is fine)
  • Remove all the files from consistency checks, or just replace with this:
    { "check": [ ] }
Picking Up the Pieces
Here is the lua code related to darkVision from Alien_Client.lua:420
"Player.screenEffects.darkVision:SetParameter(name, value)" is how the game code interacts with the shader.
local darkVisionFadeAmount = 1 local darkVisionFadeTime = 0.2 local darkVisionPulseTime = 4 if not self.darkVisionOn then darkVisionFadeAmount = math.max(1 - (Shared.GetTime() - self.darkVisionEndTime) / darkVisionFadeTime, 0) end if Player.screenEffects.darkVision then Player.screenEffects.darkVision:SetActive(self.darkVisionOn or darkVisionFadeAmount > 0) Player.screenEffects.darkVision:SetParameter("startTime", self.darkVisionTime) Player.screenEffects.darkVision:SetParameter("time", Shared.GetTime()) Player.screenEffects.darkVision:SetParameter("amount", darkVisionFadeAmount) end
If you decide to edit any lua, you're pretty much guaranteed to never pass consistency checks on any server. I don't recommend doing this.

The Pieces
You can choose any text editor to edit shaders. Personally I use notepad++[notepad-plus-plus.org].
Open DarkVision.fx and let's look at it.

You can see the 3 lua code parameters near the top of the file. These are used to create the "pulse" effect that happens when turning alien vision on.

The vertex shader:
VS_OUTPUT SFXBasicVS(VS_INPUT input)
Doesn't do anything.

The pixel shader:
float4 SFXDarkVisionPS(PS_INPUT input) : COLOR0
The pixel shader contains all the visuals that we want to edit.
  • inputPixel is the color value of the current pixel
  • depth1.r is the depth of the current pixel
  • depth1.r is the distance of the pixel from the screen. depth1.r < 0.5 roughly corresponds to the view model.
  • depth1.g is related to entities. depth1.g > 0.5 means the pixel belongs to an entity; other values mean the pixel belongs to the world.
  • depth2-5 are the depths of the neighboring pixels, used to figure out if a pixel is on the edge of an object.
Whatever color value is returned by the pixel shader will be the color of the pixel. Try messing around with some of the colors and values here.
Minimal Alien Vision Shader
Here is the pixel shader code for Minimal Alien Vision:
float4 SFXDarkVisionPS(PS_INPUT input) : COLOR0 { float2 texCoord = input.texCoord; float2 depth1 = tex2D(depthTextureSampler, input.texCoord).rg; float4 inputPixel = tex2D(baseTextureSampler, input.texCoord); const float offset = 0.0005 + depth1.g * 0.00001; float depth2 = tex2D(depthTextureSampler, input.texCoord + float2(-offset, -offset)).r; float depth3 = tex2D(depthTextureSampler, input.texCoord + float2(-offset, offset)).r; float depth4 = tex2D(depthTextureSampler, input.texCoord + float2(offset, -offset)).r; float depth5 = tex2D(depthTextureSampler, input.texCoord + float2(offset, offset)).r; float4 edgeColor; if (depth1.g > 0.5) // entities { float edge = (abs(depth2 - depth1.r) + abs(depth3 - depth1.r) + abs(depth4 - depth1.r) + abs(depth5 - depth1.r )); if (depth1.r < 0.4) // view model { edgeColor = float4(0.1, 0.1, 0.1, 0); return lerp(inputPixel, (edgeColor * edge), 0.5); } // world entitites edgeColor = float4(1.0, 0.05, 0.0, 0) * 8.0; float4 fog = float4(1, 1.0, 0, 0) * 8.0; float fogDensity = 0.001; float fogAmount = 1 - saturate(exp(-depth1.r * fogDensity)); return lerp(inputPixel, lerp(inputPixel, (edgeColor * edge) + (fog * fogAmount), 0.2 + edge), 1); } else // world geometry { float edge = abs(depth5 - depth1.r ); return lerp(inputPixel, edge, 0.01); } }

I've commented the important code blocks: entities, view model, world entities, and world geometry.
  • View model - light gray outline.
  • World Entities - bright orange outline and a foggy orange center
  • World Geometry - an extremely light edge highlight

The Result:


Feel free to copy the shader. Mess around with it and see what you can come up with! Post any questions in the comments and I'll do my best to answer and update the guide.
18 Comments
Huze  [author] 3 Apr, 2015 @ 5:47pm 
This guide is over 2 years old, so chances are it will be a bit dated. I haven't kept up with ns2, so I can't comment how accurate anything is.

The .fx file is an hlsl shader, not lua. At the time of the writing, I couldn't find much online as far as language constructs / syntax. It c-like syntax, but a lot is different. You may want to look at some other shaders in the ns2 directory and just learn from example.

Again, my knowledge of the code is pretty dated. As far as I know, the marine flashlight is just like any other light, so it wouldn't have it's own dedicated shader. You would have to mod the lua to apply a shader to the marine flashlight.
Metroidaron 3 Apr, 2015 @ 11:38am 
Also is the Marine Flashlight coded into the game as a shader? I would like to make a mod that replaces the flashlight with a "Nano Vision" shader that is sort of like the aliens, but I'm thinking something along the lines of Nano Vision from the game Syndacate. I think this would be cool to impliment one of three ways, when under the effects of a cat pack, as an upgrade, or as something the marines have from the beginning (and maybe the com can upgrade it to do more things)

I just think rines in the future will have something more advanced than a flashlight! :D :mac:
Metroidaron 3 Apr, 2015 @ 11:34am 
Hello, I am trying to delve into modding as a hobby, I do do programming, but mainly web based scripting. In this tutorial It says if you edit the Lua script you wont pass the checks on servers... so is the .fx file that controls the actual shader coded in Lua or another language? I ask because I am trying to find out wich language API I need to look up to start understanding the language and how it operates. THX! :) :alienlogo:
DeathJerky 24 Mar, 2013 @ 7:38pm 
Hey Huze, I've had a few friends asking me to post my vision up for them so they could use it. Would you mind if I provided people with a link to your guide since I did use your set up method, and to give you credit for helping me with it initially?
Huze  [author] 13 Mar, 2013 @ 7:30pm 
Yeah my code removed a lot of their stuff.
DeathJerky 10 Mar, 2013 @ 10:00pm 
Also, In case anyone was wondering, I messed with it a little and if you make edge = 0 when you reach the world entities part, it will allow you to see through the holes in the glass. Always bugged me a bit how the vision never let you see those holes before lol. WARNING: edge is what lets you see the world environment in the dark! turning this off will make your room go pitch black (excluding the orange glows on players/buildings) when all the lights go out.
DeathJerky 10 Mar, 2013 @ 9:55pm 
hmm possibly, Did you remove alot of their code then? Cause they had things like 'fadeout' and they didnt use return lerp(...); anymore it was all commented out in my default DarkVision.fx before I started editing.
ShnitzelKiller 10 Mar, 2013 @ 1:26pm 
could you use this to increase the fade distance with alien vision? Isn't that cheating?
Huze  [author] 10 Mar, 2013 @ 1:15pm 
Everything still looks fine to me. The DarkVision.fx file hasn't been changed since November. Maybe you messed up when you copied the code. Make sure you only replace the pixel shader.
Huze  [author] 10 Mar, 2013 @ 1:06pm 
Oh thanks, I'll check it out then.