Jump to content

Applying effects to the flight camera


Recommended Posts

Hello!

I'm an oldie to programming but very much a newbie to C# and developing plugins for KSP. At the moment I am working on a mod that will simulate the effects on a pilot of g-forces, for example, red-outs and black-outs and FOV changes in response to different g-force situations.

The only part that is eluding me is how to apply simple effects to the flight camera, the FOV changes are easy enough but I could really use some advice on the best way of applying simple color filtering to the camera. For example, if I create a red Texture2D and apply different levels of opacity to it how do I then go about placing this texture as the last thing drawn by the camera on each frame?

I am most grateful for any direction that anyone could kindly give :)

Link to comment
Share on other sites

I've no idea myself, but the code from this wonderful mod may well point you in the right direction: http://forum.kerbalspaceprogram.com/threads/101681-0-25-WIP-MovieTime-v0-1-%28Nov-30%29-Olde-Timey-Movie-and-TV-Effects

Thanks for the pointer, I'll take a look!

- - - Updated - - -

After looking through the MovieTime mod code I was lead to this function in Unity which looks like the key:

MonoBehaviour.OnRenderImage(RenderTexture,RenderTexture)

Hopefully, I'll be able to work this one out!

Thanks again for the pointer, lo-fi.

Link to comment
Share on other sites

Most likely you want to look at the "RenderingManager.AddToPreDrawQueue" or "RenderingManager.AddToPostDrawQueue". As this should allow you to do drawing. Check the Unity documentation on how to draw GUI elements on how to draw a full screen colored overlay. (Don't think you need to create a texture to do this)

Link to comment
Share on other sites

Most likely you want to look at the "RenderingManager.AddToPreDrawQueue" or "RenderingManager.AddToPostDrawQueue". As this should allow you to do drawing. Check the Unity documentation on how to draw GUI elements on how to draw a full screen colored overlay. (Don't think you need to create a texture to do this)

Thank you for your input, Daid. It definitely seems to be easier using your method, I came up with the following code snippet which ended up needing a texture. I just created a 32x32 solid white png for that:


using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace G_Force
{
[KSPAddon(KSPAddon.Startup.Flight, true)]
public class G_Force : MonoBehaviour
{
double currentG;
float initialFOV;
Color colorOut;
float alpha;
Texture2D whiteTexture = new Texture2D(32, 32, TextureFormat.ARGB32, false);

protected void Start()
{
DontDestroyOnLoad(this);
initialFOV = FlightCamera.fetch.fovDefault;
string path = KSPUtil.ApplicationRootPath.Replace(@"\", "/") + "/GameData/G-Force/White32x32.png";
byte[] texture = File.ReadAllBytes(path);
whiteTexture.LoadImage(texture);
RenderingManager.AddToPostDrawQueue(3, new Callback(postDraw));
}

void postDraw()
{
ScreenMessages.PostScreenMessage(currentG.ToString() + " / " + alpha.ToString());
GUI.color = colorOut;
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), whiteTexture);
GUI.color = Color.white;
}

public void Update()
{
currentG = FlightGlobals.ActiveVessel.geeForce;

if (currentG > 1)
{
colorOut = Color.black;
alpha = (float)currentG / 9;
}
else
{
colorOut = Color.red;
alpha = 1 - (float)currentG;
}

colorOut.A(alpha);
}
}
}

Unfortunately, the alpha component does not seem to be taking any effect. I only get solid black or solid red.

My best guess so far, from googleing the Unity pages, is that I can only use alpha transparency if I use a shader. This is new territory for me though and I'm not sure if I'm barking up the wrong tree for this problem.

Thanks for all the pointers everyone, I appreciate it.

- - - Updated - - -

I found a mistake in my code, I was doing:

colorOut.A(alpha)

...which didn't work. I needed to do:

colorOut.a = alpha

Which did work!

Edited by russnash37
Link to comment
Share on other sites

You could do it fairly easily using the MovieTime shader. Omit the vignette and overlay textures (and set their amounts to 1). Set the Monochrome variable to 1 and then set the MonoColor variable to the color you want to tint to (ie, red). Maybe the Brightness variable as well.

It's essentially the same as what I'm doing with the night vision shader, but with a different color and without the overlays (though a growing tunnel-vision vignette would add to the black out/red out effect).

Shaders were a mystery to me when I started this, but this resource in particular: http://it-ebooks.info/book/2954/ was extremely helpful. It didn't hurt that one of the examples was exactly what I wanted.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...