Jump to content

Flight Scene Render Texture


Recommended Posts

Hi, I'm currently trying to implement some more realistic science. Particularly SSTV (Slow-Scan Television), as it was used in the early US Space Program. To do so I need to render the Screen to a RenderTexture to create the SSTV audio signal. Concerning the rendering to the RenderTexture I found this topic and looking at the Romfarer source helped a lot. But I'm stuck with one problem. Every aspect of the flight scene renders well except the procedural quad sphere terrain. It's just plain white as can be seen in the following Texture2D's saved as .png.

xEnAR5F.pngfetQuTV.png4nn33Fd.png

Thats my source:

	
public class SSTVCamera
{
private static string[] GameCameras = {
"Camera ScaledSpace", "Camera 01", "Camera 00"
};

private static int ScaledSpaceCameraIndex = 0;
private static int FarCameraIndex = 1;
private static int NearCameraIndex = 2;

private GameObject[] gameObjects;
private Camera[] cameras;

private RenderTexture renderTexture;

public SSTVCamera(int width, int height, int depth, RenderTextureFormat format) {
renderTexture = new RenderTexture(width, height, depth, format);
renderTexture.Create();

LoadCameras(60, 557059);
}

public Texture2D RenderToTexture(Vector3 position, Vector3 aim, Quaternion rotation, bool planetary) {
RenderTexture currentRenderTexture = RenderTexture.active;
RenderTexture.active = renderTexture;

if(planetary) {
cameras[ScaledSpaceCameraIndex].transform.position = position;
cameras[ScaledSpaceCameraIndex].transform.forward = aim;
cameras[ScaledSpaceCameraIndex].transform.localRotation = rotation;
cameras[ScaledSpaceCameraIndex].Render();
} else {
cameras[NearCameraIndex].transform.position = position;
cameras[NearCameraIndex].transform.forward = aim;
cameras[NearCameraIndex].transform.localRotation = rotation;

cameras[FarCameraIndex].transform.position = position;
cameras[FarCameraIndex].transform.forward = aim;
cameras[FarCameraIndex].transform.localRotation = rotation;

cameras[ScaledSpaceCameraIndex].transform.rotation = cameras[NearCameraIndex].transform.rotation;

cameras[ScaledSpaceCameraIndex].Render();
cameras[FarCameraIndex].Render();
cameras[NearCameraIndex].Render();
}

Texture2D texture = new Texture2D(renderTexture.width, renderTexture.height);
texture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);

RenderTexture.active = currentRenderTexture;
return texture;
}

private void LoadCameras(float fov, int cullingMask) {
gameObjects = new GameObject[GameCameras.Length];
cameras = new Camera[GameCameras.Length];

for(int i = 0; i < GameCameras.Length; i++) {
LoadCamera(GameCameras[i], ref gameObjects[i], ref cameras[i]);
}

cameras[ScaledSpaceCameraIndex].fov = fov;
cameras[FarCameraIndex].fov = fov;
cameras[NearCameraIndex].fov = fov;

cameras[FarCameraIndex].cullingMask = cullingMask;
cameras[NearCameraIndex].cullingMask = cullingMask;
}

private void LoadCamera(string name, ref GameObject gameObject, ref Camera camera) {
gameObject = new GameObject();
gameObject.name = name + " " + gameObject.GetInstanceID();

camera = gameObject.AddComponent<Camera>();
camera.CopyFrom(FindCamera(name));
camera.targetTexture = renderTexture;
camera.enabled = false;
}

private Camera FindCamera(string name) {
foreach(Camera camera in Camera.allCameras) {
if(camera.name == name)
return camera;
}
return null;
}

private void DestroyCameras() {
foreach(GameObject gameObject in gameObjects) {
GameObject.Destroy(gameObject);
}
}

}
}

Any help is greatly appreciated.

Link to comment
Share on other sites

Your problem is probably that the terrain is transparent. You can use a texture format without alpha if you want to make it visible. If you're only using it as the input for some computation then maybe that's not even necessary.

I've used RenderTextureFormat.RGB565 successfully but I don't think it's necessarily the RenderTexture that has to be the one without alpha.

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