Skip to content

Instantly share code, notes, and snippets.

@tcannonfodder
Created July 28, 2016 03:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcannonfodder/65d68d7fc8038992de61a1f7116bb24d to your computer and use it in GitHub Desktop.
Save tcannonfodder/65d68d7fc8038992de61a1f7116bb24d to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using System;
namespace Telemachus
{
public class CameraCapture : MonoBehaviour
{
public RenderTexture overviewTexture;
Camera OVcamera = null;
public string cameraName = "";
public bool didRender;
public byte[] imageBytes = null;
public bool mutex = false;
void Start()
{
PluginLogger.debug("CAMERA CAPTURE STARTED");
}
void OnDisable()
{
PluginLogger.debug("WHY DISABLED?");
}
void OnDestroy()
{
PluginLogger.debug("WHY DESTROY?");
}
void Update()
{
PluginLogger.debug("UPDATE CAMERA");
}
void LateUpdate()
{
PluginLogger.debug("LATEUPDATE FOR CAMERA");
if (OVcamera == null)
{
PluginLogger.debug("NO CAMERA YET, FINDING: " + this.cameraName);
if(FlightCamera.fetch != null)
{
//OVcamera = FlightCamera.fetch.mainCamera;
}
foreach (Camera cam in Camera.allCameras)
{
if (cam.name == this.cameraName) { OVcamera = cam; }
}
}
if (OVcamera != null)
{
PluginLogger.debug("CAMERA FOUND, TAKING SCREENSHOT");
StartCoroutine(TakeScreenShot());
}
}
public void debugCameraDetails(Camera cam)
{
PluginLogger.debug("CAMERA: " + cam.name + " ; FAR: " + cam.far + "; FAR CLIP PLANE: " + cam.farClipPlane);
}
public IEnumerator TakeScreenShot()
{
if (mutex)
{
yield return true;
}
PluginLogger.debug("BYPASSED MUTEX");
mutex = true;
PluginLogger.debug("WAITING FOR END OF FRAME");
yield return new WaitForEndOfFrame();
PluginLogger.debug("GETTING CAMERA");
//Camera camOV = OVcamera;
var cameraContainer = new GameObject();
cameraContainer.name = "CameraCaptureTest" + cameraContainer.GetInstanceID();
Camera camOV = cameraContainer.AddComponent<Camera>();
// Just in case to support JSITransparentPod.
camOV.cullingMask &= ~(1 << 16 | 1 << 20);
camOV.CopyFrom(OVcamera);
camOV.enabled = false;
PluginLogger.debug("Swapping Textures");
RenderTexture currentRT = RenderTexture.active;
RenderTexture currentCameraTexture = camOV.targetTexture;
RenderTexture rt = new RenderTexture(256, 256, 24);
rt.Create();
float oldAspect = camOV.aspect;
camOV.aspect = 1.0f;
camOV.targetTexture = rt;
foreach (Camera cam in Camera.allCameras)
{
debugCameraDetails(cam);
}
PluginLogger.debug("MYCAM BELOW");
debugCameraDetails(camOV);
RenderTexture.active = camOV.targetTexture;
PluginLogger.debug("RENDERING");
camOV.Render();
PluginLogger.debug("READ APPLY 1");
Texture2D imageOverview = new Texture2D(camOV.targetTexture.width, camOV.targetTexture.height, TextureFormat.RGB24, false);
PluginLogger.debug("READ APPLY 2");
imageOverview.ReadPixels(new Rect(0, 0, camOV.targetTexture.width, camOV.targetTexture.height), 0, 0);
PluginLogger.debug("READ APPLY 3");
imageOverview.Apply();
PluginLogger.debug("RETURNING TEXTURE");
RenderTexture.active = currentRT;
camOV.targetTexture = currentCameraTexture;
camOV.aspect = oldAspect;
PluginLogger.debug("ENCODING TO PNG");
// Encode texture into PNG
this.imageBytes = imageOverview.EncodeToPNG();
this.didRender = true;
mutex = false;
Destroy(imageOverview);
Destroy(rt);
Destroy(cameraContainer);
}
}
}
[Telemachus Debug] CAMERA: GalaxyCamera ; FAR: 20; FAR CLIP PLANE: 20
[Telemachus Debug] CAMERA: Camera ScaledSpace ; FAR: 3E+07; FAR CLIP PLANE: 3E+07
[Telemachus Debug] CAMERA: Camera 01 ; FAR: 750000; FAR CLIP PLANE: 750000
[Telemachus Debug] CAMERA: Camera 00 ; FAR: 310; FAR CLIP PLANE: 310
[Telemachus Debug] CAMERA: UIVectorCamera ; FAR: 1000; FAR CLIP PLANE: 1000
[Telemachus Debug] CAMERA: Camera ; FAR: 1000; FAR CLIP PLANE: 1000
[Telemachus Debug] MYCAM BELOW
[Telemachus Debug] CAMERA: CameraCaptureTest-276836 ; FAR: 310; FAR CLIP PLANE: 310
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment