Skip to content

Commit

Permalink
Merge pull request #9 from SerTheGreat/Dev
Browse files Browse the repository at this point in the history
Impoved greyout shader; Added options to disable greyout effects
  • Loading branch information
SerTheGreat committed Dec 2, 2015
2 parents 9ce7404 + b32c091 commit 37d92a8
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 242 deletions.
12 changes: 10 additions & 2 deletions G-Effects/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ public class Configuration
public float gLocStartCoeff = 1.1f; //How much more should our poor kerbal suffer after complete loss of vision to have a G-LOC
public float gDeathCoeff = 20.0f; //How much more should a kerbal suffer to die of a sustained over-g
public bool gDeathEnabled = false; //Will the critical conditions and g-deaths take place or not

public bool IVAOnly = false; //If set to true then g-effects will be rendered in IVA mode only
//Greyout is a post-processing effect. It may conflict with other post-processing effects like b/w cameras etc, so disable greyouts if necessary.
public bool IVAGreyout = true; //Greyout effect in IVA view
public bool mainCamGreyout = false; //mainCam is used in 3rd person view. The effect is disabled by default because it eats up stock reenty and mach visual effects
public String gLocScreenWarning = null; //Text of a warning displayed when a kerbal loses consience. Leave empty to disable.
public Color redoutRGB = Color.red; //Red, green, blue components of a redout color (in case you are certain that green men must have green blood, for example)
public int gLocFadeSpeed = 4; //Speed of fade-out visual effect when a kerbal is losing consciousness
public int breathThresholdTime = 8; //Time threshold in seconds for a kerbal needed to breathe after AGSM

public int breathThresholdTime = 8; //Time threshold in seconds for a kerbal needed to breathe after AGSM
public int maxBreaths = 6; //Maximum possible breath sounds to be played
public int minBreaths = 2; //Minimum breath sounds to be played
//You can disable specific sound effects by specifying 0 volumes.
Expand All @@ -38,7 +43,8 @@ public class Configuration
public float heartBeatVolume = 1.0f; //Volume of blood beating in kerbal's ears on negative over-G
public float femaleVoicePitch = 1.4f; //How much female kerbals' voice pitch is higher than males' one
public float breathSoundPitch = 1.8f; //Pitch of heavy breath's sounds
public bool enableLogging = false; //Enable this only in debug purposes as it floods the logs very much

public bool enableLogging = false; //Enable this only in debug purposes as it floods the logs very much

//Kerbal personal modifiers are used as multipliers for the gResistance parameter and also affect the speed of G effects accumulation
public float femaleModifier = 1; //How stronger are females than males
Expand Down Expand Up @@ -89,6 +95,8 @@ public void loadConfiguration(string root) {
} else KSPLog.print("" + root + " node not found");

bool.TryParse(nodes[0].GetValue("IVAOnly"), out IVAOnly);
bool.TryParse(nodes[0].GetValue("IVAGreyout"), out IVAGreyout);
bool.TryParse(nodes[0].GetValue("mainCamGreyout"), out mainCamGreyout);
gLocScreenWarning = nodes[0].GetValue("gLocScreenWarning");
string redoutColor = nodes[0].GetValue("redoutRGB");
string[] redoutComponents;
Expand Down
51 changes: 20 additions & 31 deletions G-Effects/G-Effects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ namespace G_Effects
[KSPAddon(KSPAddon.Startup.Flight, false)]
public class G_Effects : MonoBehaviour
{

//TODO find a way to disable EVA button on G-LOC
//TODO simulate orientation loss on G-LOC

GEffectsAPIImplementation gEffectsApiImpl = GEffectsAPIImplementation.instance();
KeepFit.KeepFitAPI keepFitAPI = new KeepFit.KeepFitAPI();
GrayoutCameraFilter flightCameraFilter;
GrayoutCameraFilter internalCameraFilter;
GreyoutCameraFilter flightCameraFilter;
GreyoutCameraFilter internalCameraFilter;

const string APP_NAME = "G-Effects";
const string CONTROL_LOCK_ID = "G_EFFECTS_LOCK";
Expand All @@ -49,6 +49,7 @@ public class G_Effects : MonoBehaviour
double downwardG;
double forwardG;
bool playEffects = false;
bool greyOutAllowed = false;
bool paused = false;
readonly static PortraitAgent PORTRAIT_AGENT = new PortraitAgent();

Expand All @@ -69,9 +70,6 @@ protected void Start()
if (keepFitAPI.initialize()) {
writeLog("KeepFit mod detected. Working in conjunction.");
}
/*string path = KSPUtil.ApplicationRootPath.Replace(@"\", "/") + "/GameData/G-Effects/blackout.png";
byte[] texture = File.ReadAllBytes(path);
blackoutTexture.LoadImage(texture);*/
if (blackoutTexture == null) {
blackoutTexture = GameDatabase.Instance.GetTexture("G-Effects/blackout", false);
}
Expand All @@ -82,8 +80,6 @@ protected void Start()
GameEvents.onGameUnpause.Add(onUnPause);
GameEvents.onCrewKilled.Add(onCrewKilled);
GameEvents.onVesselChange.Add(onVesselChange);
// Add another rendering queue hook for the GUI
//RenderingManager.AddToPostDrawQueue(4, new Callback(drawGUI));
PORTRAIT_AGENT.Start();
}

Expand Down Expand Up @@ -133,23 +129,15 @@ void resetValues() {
internalCameraFilter.setBypass(true);
}

GrayoutCameraFilter initializeCameraFilter(Camera camera) {
GrayoutCameraFilter filter = camera.gameObject.GetComponent<GrayoutCameraFilter>();
if (filter == null) {
filter = camera.gameObject.AddComponent<GrayoutCameraFilter>();
}
return filter;
}

protected void Awake() {
conf.loadConfiguration(APP_NAME.ToUpper());

if (!gAudio.isInitialized()) {
gAudio.initialize(conf.gruntsVolume, conf.breathVolume, conf.heartBeatVolume, conf.femaleVoicePitch, conf.breathSoundPitch);
}

flightCameraFilter = initializeCameraFilter(FlightCamera.fetch.mainCamera);
internalCameraFilter = initializeCameraFilter(InternalCamera.Instance.camera);
flightCameraFilter = GreyoutCameraFilter.initializeCameraFilter(FlightCamera.fetch.mainCamera, conf.mainCamGreyout);
internalCameraFilter = GreyoutCameraFilter.initializeCameraFilter(InternalCamera.Instance.camera, conf.IVAGreyout);
}

public void Update() {
Expand Down Expand Up @@ -201,6 +189,7 @@ public void Update() {
(!conf.IVAOnly || isIVA) &&
!MapView.MapIsEnabled;

greyOutAllowed = isIVA && conf.IVAGreyout || !isIVA && conf.mainCamGreyout;
flightCameraFilter.setBypass(isIVA || !playEffects);
internalCameraFilter.setBypass(!isIVA || !playEffects);
gAudio.setAudioEnabled(playEffects);
Expand Down Expand Up @@ -234,7 +223,7 @@ public void Update() {

gState.cumulativeG -= Math.Sign(gState.cumulativeG) * conf.gResistance * kerbalModifier;
//gAudio.applyFilter(1 - Mathf.Clamp01((float)(1.25 * Math.Pow(Math.Abs(gData.cumulativeG) / conf.MAX_CUMULATIVE_G, 2) - 0.2)));
doGrayout(gState);
doGreyout(gState);
if ((downwardG > conf.positiveThreshold) || (downwardG < conf.negativeThreshold) || (forwardG > conf.positiveThreshold) || (forwardG < conf.negativeThreshold)) {

double rebCompensation = conf.gResistance * kerbalModifier - conf.deltaGTolerance * conf.deltaGTolerance / kerbalModifier; //this is calculated so the rebound is in equilibrium with cumulativeG at the very point of G threshold
Expand Down Expand Up @@ -342,17 +331,6 @@ ProtoCrewMember bestCommander(ProtoCrewMember current, ProtoCrewMember candidate
}
}

void doGrayout(KerbalGState gState) {
float grayout = Mathf.Clamp(2 * gState.getSeverity(), 0f, 1.0f);
if (gState.cumulativeG > 0) {
flightCameraFilter.setMagnitude(grayout);
internalCameraFilter.setMagnitude(grayout);
} else {
flightCameraFilter.setMagnitude(0.0f);
internalCameraFilter.setMagnitude(0.0f);
}
}

void loseConsciousness(ProtoCrewMember crewMember, KerbalGState kerbalGData, bool isCommander, bool outputAllowed) {
kerbalGData.stopAGSM(0);
kerbalGData.resetBreath();
Expand Down Expand Up @@ -395,6 +373,17 @@ bool isRosterDead(ProtoCrewMember crewMember) {
return crewMember.rosterStatus.Equals(ProtoCrewMember.RosterStatus.Dead) || crewMember.rosterStatus.Equals(ProtoCrewMember.RosterStatus.Missing);
}

void doGreyout(KerbalGState gState) {
if (gState.cumulativeG > 0) {
float greyout = Mathf.Pow(Mathf.Clamp(gState.getSeverity() / 0.4f, 0f, 1.0f), 2); //Severity is divided by a percent of the total blackout at which greyout should be complete
flightCameraFilter.setMagnitude(greyout);
internalCameraFilter.setMagnitude(greyout);
} else {
flightCameraFilter.setMagnitude(0.0f);
internalCameraFilter.setMagnitude(0.0f);
}
}

void drawGEffects()
{
if (!playEffects) {
Expand Down Expand Up @@ -422,7 +411,7 @@ void drawGEffects()
colorFill.r = colorOut.r;
colorFill.g = colorOut.g;
colorFill.b = colorOut.b;
colorFill.a = (float)Math.Pow(severity, kerbalGData.cumulativeG > 0 ? 8 : 4); //this will intensify blackout/redout effect at the very end
colorFill.a = (float)Math.Pow(severity, 4); //this will intensify blackout/redout effect at the very end

//The following will fade out in overlay whatever is diplayed if losing consciousness or fade in on wake up
float fade = (float)(kerbalGData.gLocFadeAmount * kerbalGData.gLocFadeAmount) / (float)(MAX_GLOC_FADE * MAX_GLOC_FADE);
Expand Down
2 changes: 1 addition & 1 deletion G-Effects/G-Effects.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<Compile Include="G-Effects.cs" />
<Compile Include="GEffectsAPIImplementation.cs" />
<Compile Include="GEffectsAudio.cs" />
<Compile Include="GrayoutCameraFilter.cs" />
<Compile Include="GreyoutCameraFilter.cs" />
<Compile Include="KeepFit\KeepFitAPI.cs" />
<Compile Include="KerbalGState.cs" />
<Compile Include="PortraitAgent.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,35 @@ namespace G_Effects
/// <summary>
/// Camera filter that provides picture gray out effect
/// </summary>
public class GrayoutCameraFilter : MonoBehaviour
public class GreyoutCameraFilter : MonoBehaviour
{

static Material material;
bool bypass = true;
float magnitude = 0;

public GrayoutCameraFilter()
public GreyoutCameraFilter() : this(true) {
}

//In case actualEffect = false the filter is just a dummy
public GreyoutCameraFilter(bool actualEffect)
{
if (material == null) {
material = loadShader("grayout.shader");
if (actualEffect && material == null) {
material = loadShader("greyout.shader");
}
}

public static GreyoutCameraFilter initializeCameraFilter(Camera camera, bool actualFilter) {
GreyoutCameraFilter filter;
if (actualFilter) {
filter = camera.gameObject.GetComponent<GreyoutCameraFilter>();
if (filter == null) {
filter = camera.gameObject.AddComponent<GreyoutCameraFilter>();
}
} else {
filter = new GreyoutCameraFilter(false);
}
return filter;
}

public void setBypass(bool bypass) {
Expand Down
4 changes: 2 additions & 2 deletions G-Effects/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.2.3")]
[assembly: AssemblyFileVersion("0.2.3")]
[assembly: AssemblyVersion("0.3.1")]
[assembly: AssemblyFileVersion("0.3.1")]
47 changes: 0 additions & 47 deletions G-Effects/Shaders_source/grayout.shader

This file was deleted.

31 changes: 31 additions & 0 deletions G-Effects/Shaders_source/greyout.shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Shader "G-Effects/Greyout" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_Magnitude ("Magnitude", Range (0, 1)) = 0
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag

#include "UnityCG.cginc"

uniform sampler2D _MainTex;
uniform float _Magnitude;

float4 frag(v2f_img input) : COLOR {
float4 color = tex2D(_MainTex, input.uv);

float average = 0.299*color.r + 0.587*color.g + 0.114*color.b;
float3 greycolor = float3(average, average, average);

float4 result = color;
result.rgb = lerp(color.rgb, greycolor, _Magnitude);
return result;
}
ENDCG
}
}
Fallback "Diffuse"
}
3 changes: 3 additions & 0 deletions GameData/G-Effects/G-Effects.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ G-EFFECTS {
gDeathEnabled = true //Will the critical conditions and g-deaths take place or not

IVAOnly = false //If set to true visual and sound effects will be rendered in IVA mode only but physical effects like G-LOC and loss of control will remain in all views
//Greyout is a post-processing effect. It may conflict with other post-processing effects like b/w cameras etc, so disable greyouts if necessary.
IVAGreyout = true //Greyout effect in IVA view
mainCamGreyout = false //mainCam is used in 3rd person view. The effect is disabled by default because it eats up stock reenty and mach visual effects
gLocFadeSpeed = 4 //Speed of fade-out visual effect when a kerbal is losing consciousness
gLocScreenWarning = UNCONSCIOUS //Text of a warning displayed when a kerbal loses consciousness. Leave empty to disable.
redoutRGB = 180,0,0 //Red, green, blue components of redout color (you can change it even to greenout in case you are certain that green men must have green blood)
Expand Down
Binary file modified GameData/G-Effects/Plugins/G-Effects.dll
Binary file not shown.
Loading

0 comments on commit 37d92a8

Please sign in to comment.