add saving frames button
This commit is contained in:
parent
3f3a21401c
commit
3c48ee28f9
@ -1,5 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.IO;
|
||||
|
||||
public class main : MonoBehaviour
|
||||
{
|
||||
@ -48,6 +50,13 @@ public class main : MonoBehaviour
|
||||
return list[index];
|
||||
}
|
||||
|
||||
Vector2Int textureCameraSize = new Vector2Int(800, 600);
|
||||
GameObject textureCamera;
|
||||
int ScreenshotsPerSecond = 1;
|
||||
int FrameCounter = 0;
|
||||
|
||||
bool isSavinFrames = false;
|
||||
|
||||
|
||||
void Start()
|
||||
{
|
||||
@ -58,6 +67,16 @@ public class main : MonoBehaviour
|
||||
mainCamera.transform.LookAt(new Vector3(m_camLookAtX, m_camLookAtY, m_camLookAtZ), new Vector3(0.0f, 1.0f, 0.0f));
|
||||
mainCamera.name = "MainCamera";
|
||||
|
||||
textureCamera = new GameObject();
|
||||
textureCamera.AddComponent<Camera>();
|
||||
textureCamera.transform.position = new Vector3(m_camPosX, m_camPosY, m_camPosZ);
|
||||
textureCamera.transform.LookAt(new Vector3(m_camLookAtX, m_camLookAtY, m_camLookAtZ), new Vector3(0.0f, 1.0f, 0.0f));
|
||||
textureCamera.name = "TextureCamera";
|
||||
//Debug.Log(textureCamera.GetComponent<Camera>().targetTexture);
|
||||
Rect rect = new Rect(0, 0, textureCameraSize.x, textureCameraSize.y);
|
||||
RenderTexture renderTexture = new RenderTexture(textureCameraSize.x, textureCameraSize.y, 24);
|
||||
textureCamera.GetComponent<Camera>().targetTexture = renderTexture;
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
//directional light
|
||||
@ -101,7 +120,8 @@ public class main : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool exists = Directory.Exists(Application.dataPath + "/capturedframe");
|
||||
if (!exists) Directory.CreateDirectory(Application.dataPath + "/capturedframe");
|
||||
}
|
||||
|
||||
void Update()
|
||||
@ -127,6 +147,12 @@ public class main : MonoBehaviour
|
||||
treesZSlider = 0;
|
||||
treesXSlider = 0;
|
||||
directionalLight.GetComponent<Light>().intensity = lightIntensity;
|
||||
|
||||
textureCamera.GetComponent<Camera>().fieldOfView = m_FieldOfView;
|
||||
textureCamera.GetComponent<Camera>().transform.position = new Vector3(newCameraPosX, newCameraPosY, newCameraPosZ);
|
||||
textureCamera.GetComponent<Camera>().transform.LookAt(new Vector3(500f + m_camLookAtX, 1f + m_camLookAtY, 500f + m_camLookAtZ));
|
||||
|
||||
//textureCamera.transform.RotateAround(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f), 1);
|
||||
}
|
||||
|
||||
void OnGUI()
|
||||
@ -164,5 +190,41 @@ public class main : MonoBehaviour
|
||||
GUI.Label(new Rect(10, 250, 60, 20), "LightSlider");
|
||||
lightIntensity = GUI.HorizontalSlider(new Rect(70, 250, 150, 20), lightIntensity, 0.0f, 10.0f);
|
||||
|
||||
if (GUI.Button(new Rect(10, 280, 200, 30), isSavinFrames ? "Stop saving frames" : "Start saving frames"))
|
||||
{
|
||||
if (!isSavinFrames) StartCoroutine("CaptureAndSaveFrames");
|
||||
else StopCoroutine("CaptureAndSaveFrames");
|
||||
isSavinFrames = !isSavinFrames;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
IEnumerator CaptureAndSaveFrames()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
yield return new WaitForEndOfFrame();
|
||||
|
||||
RenderTexture currentRT = RenderTexture.active;
|
||||
|
||||
RenderTexture.active = textureCamera.GetComponent<Camera>().targetTexture;
|
||||
|
||||
textureCamera.GetComponent<Camera>().Render();
|
||||
|
||||
Texture2D offscreenTexture = new Texture2D(textureCamera.GetComponent<Camera>().targetTexture.width, textureCamera.GetComponent<Camera>().targetTexture.height, TextureFormat.RGB24, false);
|
||||
offscreenTexture.ReadPixels(new Rect(0, 0, textureCamera.GetComponent<Camera>().targetTexture.width, textureCamera.GetComponent<Camera>().targetTexture.height), 0, 0, false);
|
||||
offscreenTexture.Apply();
|
||||
|
||||
RenderTexture.active = currentRT;
|
||||
++FrameCounter;
|
||||
|
||||
byte[] bytes = offscreenTexture.EncodeToPNG();
|
||||
File.WriteAllBytes(Application.dataPath + "/capturedframe/capturedframe" + FrameCounter.ToString() + ".png", bytes);
|
||||
Debug.Log(Application.dataPath + "/capturedframe/capturedframe" + FrameCounter.ToString() + ".png");
|
||||
|
||||
UnityEngine.Object.Destroy(offscreenTexture);
|
||||
|
||||
yield return new WaitForSeconds(1.0f / ScreenshotsPerSecond);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user