Jump to content

Creating a camera causes transparency


Recommended Posts

A problem has been reported to me on the PWB Fuel Balancer, and apparently the NavHud mod, where things become a bit transparent.

I've isolated it to the following code, which is run upon entry to the Flight scene in the Start method.  I'm at a loss, this is an area outside my experience, and any help would be appreciated:

[KSPAddon(KSPAddon.Startup.Flight, false)]
public class InFlightMarkerCam : MonoBehaviour
{
	private GameObject _markerCamObject;
	internal static Camera MarkerCam;


	public void Start()
	{
		// When the following line is commented out, no transparency
		CreateMarkerCam();
	}

	private void CreateMarkerCam()
	{
		if (null != _markerCamObject) return;
		// print("Setting up the inflight MarkerCamObject");
		_markerCamObject = new GameObject("MarkerCamObject");
		_markerCamObject.transform.parent = FlightCamera.fetch.cameras[0].gameObject.transform;//Camera.mainCamera.gameObject.transform; // Set the new camera to be a child of the main camera  
		MarkerCam = _markerCamObject.AddComponent<Camera>();

		// Change a few things - the depth needs to be higher so it renders over the top
		MarkerCam.name = "markerCam";
		MarkerCam.depth = Camera.main.depth + 10;
		MarkerCam.clearFlags = CameraClearFlags.Depth;
		// Add a behaviour so we can get the MarkerCam to come around and change itself when the main camera changes
		_markerCamObject.AddComponent<MarkerCamBehaviour>(); // TODO can this be removed?

		// Set the culling mask. 
		MarkerCam.cullingMask = 1 << 17;
	}

 

Here is an example of the problem:

IyqtpTy.jpg

Link to comment
Share on other sites

It's clearing the depth texture before rendering, so it no longer knows what is in front of what (that's a simplification, it likely only applies to some aspects of what it is rendering) so you get transparency. 

Is there any reason why the clear flag is set to Depth instead of Don't Clear?

Edited by DMagic
Link to comment
Share on other sites

Adding a bit of potentially relevant information -- I have noticed similar occurrences whenever I enable HDR for KSP cameras (for...reasons.....).  Something in the KSP shaders doesn't play nicely with HDR, and any surfaces hit by sunlight become partially transparent.

Disabling of HDR, or replacing all Stock shaders with Unity Standard shader fixes the transparency issues (but of course presents other shader related problems...).

I'm not seeing your camera as being set to HDR... but perhaps that is the default behaviour of newly instantiated cameras?  IDK;  might not hurt to try explicitly setting it to false in the method that creates the camera object.

Link to comment
Share on other sites

3 hours ago, DMagic said:

It's clearing the depth texture before rendering, so it no longer knows what is in front of what (that's a simplification, it likely only applies to some aspects of what it is rendering) so you get transparency. 

Is there any reason why the clear flag is set to Depth instead of Don't Clear?

No idea, this is old code.

I assume I should change that to DontClear. ?

1 hour ago, Shadowmage said:

not seeing your camera as being set to HDR... but perhaps that is the default behaviour of newly instantiated cameras?  IDK;  might not hurt to try explicitly setting it to false in the method that creates the camera object.

What is HDR?

Link to comment
Share on other sites

I didn't notice transparency in navhud, though I didn't look very closely, but one thing I did notice is that it seemed to crank up the brightness of EVE clouds dramatically when switched on. Like some part of the post-processing to even out the image brightness was being overridden or disabled (or possibly multiplied, who knows), but only on atmospheres. Once in space, and not looking at Kerbin, it was fine. Been like that as long as I can remember.

Link to comment
Share on other sites

1 hour ago, linuxgurugamer said:

What is HDR?

HDR = High Dynamic Range coloring.  Uses floating-point buffers for rendered color data rather than byte/fixed based (very basic over-simplification of its description).  I've no idea why it actually messes with KSP rendering shaders, I only know that it does and that its effect was very similar to what you had reported.

Link to comment
Share on other sites

1 hour ago, linuxgurugamer said:

Once I have this fixed, I'll take a look at Navhud

Kerbal Flight Indicators is a sort of alternative NavHud, I just pointed it out because it seems to have a different  CameraClearFlags mode ("Nothing" instead of "Depth") but still has the transparency.

Edited by Zah
Link to comment
Share on other sites

8 hours ago, DMagic said:

Is there any reason why the clear flag is set to Depth instead of Don't Clear?

The enum for the clear flag is:

    public enum CameraClearFlags
    {
        Skybox = 1,
        Color = 2,
        SolidColor = 2,
        Depth = 3,
        Nothing = 4
    }

So, I set it to Nothing, and there was no change

Next, I set hdr = false

and That fixed it

6 hours ago, Shadowmage said:

Disabling of HDR, or replacing all Stock shaders with Unity Standard shader fixes the transparency issues (but of course presents other shader related problems...).

Thank  you @Shadowmage

However, hdr is obsolete, the following has replaced it:

MarkerCam.allowHDR = false;

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...