Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*************************************************************************************\
- * Alien Vision Shader for Natural Selection 2 *
- * To be used with Dark Vision Mod for Natural Selection 2 *
- * Steam Workshop: http://steamcommunity.com/sharedfiles/filedetails/?id=108510422 *
- * Forum: http://forums.unknownworlds.com/discussion/comment/2066954/#Comment_2066954 *
- * Example Video: http://www.youtube.com/watch?v=Q7TNh9Cbngg *
- * *
- * Author: Ineluki80 *
- * Date: 2013-01-26 *
- * Version: v0.2 *
- * *
- * Features: *
- * - Insectoide compound eyes using a rhombohedral instead of a hexagonal lattice *
- * - sonar sight using sound pulses with exponential decay and distance based *
- * resolution *
- * - importance highlighting for biological and technological compounds *
- \*************************************************************************************/
- struct VS_INPUT
- {
- float3 ssPosition : POSITION;
- float2 texCoord : TEXCOORD0;
- float4 color : COLOR0;
- };
- struct VS_OUTPUT
- {
- float4 ssPosition : POSITION;
- float2 texCoord : TEXCOORD0;
- float4 color : COLOR0;
- };
- struct PS_INPUT
- {
- float2 texCoord : TEXCOORD0;
- float4 color : COLOR0;
- };
- texture baseTexture;
- texture depthTexture;
- float time;
- float startTime;
- float amount;
- sampler baseTextureSampler = sampler_state
- {
- texture = (baseTexture);
- AddressU = Wrap;
- AddressV = Wrap;
- MinFilter = Linear;
- MagFilter = Linear;
- MipFilter = Linear;
- SRGBTexture = False;
- };
- sampler depthTextureSampler = sampler_state
- {
- texture = (depthTexture);
- AddressU = Clamp;
- AddressV = Clamp;
- MinFilter = Linear;
- MagFilter = Linear;
- MipFilter = None;
- SRGBTexture = False;
- };
- /**
- * Vertex shader.
- */
- VS_OUTPUT SFXBasicVS(VS_INPUT input)
- {
- VS_OUTPUT output;
- output.ssPosition = float4(input.ssPosition, 1);
- output.texCoord = input.texCoord;
- output.color = input.color;
- return output;
- }
- float4 SFXDarkVisionPS(PS_INPUT input) : COLOR0
- {
- // Get the rendered image as a texture
- float4 inputPixel = tex2D(baseTextureSampler, input.texCoord); // this is the rendered image which we want to pixelshade
- float4 outputPixel = float4(inputPixel.r, inputPixel.g, inputPixel.b, 0.0); // here we get the pixel colours from the rendered image
- // insectoid eyes
- const float grain = 50.0; // granularity of the eyes .. how many cells per row and column
- const float4 delta = float4(-1.0,0.0,1.0,0.0)/3; // offset for the different colours channels
- float x = (input.texCoord.x) * grain; // X and Y coordinates within the granularity
- float y = (input.texCoord.y) * grain;
- float4 eyePattern = 0.5+0.8*pow(fmod(delta+x+y,1.0)*fmod(grain+delta+x-y,1.0),1.3); // creates a rectangular grid, slightly dislocated for all colour channels
- // Edge Highlighting
- float2 depthMap = tex2D(depthTextureSampler, input.texCoord).rg; // this texture contains the depth information in r and importance factor in g
- float offset = 0.0010; // offset is the difference between two pixels for which we do an edge test.
- float4 ETest = float4(
- tex2D(depthTextureSampler, input.texCoord + float2(-offset,0)).r, // we get the depth value of each pixel around the one we want to shade
- tex2D(depthTextureSampler, input.texCoord + float2(+offset,0)).r, // above in -x, now in +x direction
- tex2D(depthTextureSampler, input.texCoord + float2(0,-offset)).r, // now in -y, next in +y direction
- tex2D(depthTextureSampler, input.texCoord + float2(0,+offset)).r
- );
- const float falloff = 10.0 / log(2); // edge intensity is halved for each 10 units,
- // so it's 1/4 for 20 units, 1/8 for 30 units, ...
- float edge = saturate ( // edge is a value which highlights each pixel with values between 0 and 1
- length(ETest-depthMap.r)*exp(-depthMap.r/falloff) // an edge is a pixel which distance to its neighbours scaled by the
- ); // exponential perception factor is in the order of magnitude 1 or bigger
- // Sonar effect // By applying a time modified pulse using a slater function, we can
- float sonar = 0.02+0.70*exp(-abs(depthMap.r-fmod(50*(time-startTime),100.0))/10.0); // emulate a sonar. Sound travels 50 units per second, pulses are 10 units long
- // and travel 100 units before the next pulse is emitted.
- // Minimum brightness is 2%, maximum brightness is 72%.
- // Importance highlighting
- float4 edgeColor; // This will hold the colour of the edge based on information content.
- if ( depthMap.g > 0.5 && depthMap.r > 0.4 ) { // If the pixel belongs to something important (bio, tech, marines, etc)
- edgeColor = float4( 2.97, 0.37, -0.13, 0.0 ) * depthMap.g; // and the distance is bigger than 0.4 units (so we ignore our snoute).
- outputPixel = outputPixel + 0.1*edgeColor; // We set the edge colour to a bright orange weighted by importance, and
- edge = edge * 5.0; // overlay the object colour with that orange, too. Finally we add some
- } else { // glow effect to the edges by oversaturating them via a huge factor.
- edgeColor = float4( 1.0, 1.0, 1.0, 0.0 ); // If the object is not important, edge colour is plain white.
- }
- // Adding all information together
- return outputPixel*eyePattern + edge*edgeColor*sonar; // We overlay the rendered scene with our eyePattern and add coloured edges.
- // Finally, we return the calculated colour as result.
- }
- technique SFXDarkVision
- {
- pass p0
- {
- ZEnable = False;
- ZWriteEnable = False;
- VertexShader = compile vs_2_0 SFXBasicVS();
- PixelShader = compile ps_2_0 SFXDarkVisionPS();
- CullMode = None;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment