231 lines
9.4 KiB
C#
231 lines
9.4 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using System.Collections;
|
|
using System.IO;
|
|
|
|
public class main : MonoBehaviour
|
|
{
|
|
//camera GUI
|
|
float m_FieldOfView = 60.0f;
|
|
float m_camPosX = 0.0f;
|
|
float m_camPosY = 0.0f;
|
|
float m_camPosZ = 0.0f;
|
|
float m_camLookAtX = 0.0f;
|
|
float m_camLookAtY = 0.0f;
|
|
float m_camLookAtZ = 0.0f;
|
|
|
|
float newCameraPosX = 0.0f;
|
|
float newCameraPosY = 0.0f;
|
|
float newCameraPosZ = 0.0f;
|
|
|
|
float camera_rotation = 1.1f;
|
|
float camera_r = 20f;
|
|
|
|
Vector3 lightDir = new Vector3(10.0f, 10.0f, 10.0f);
|
|
|
|
//components
|
|
GameObject mainCamera;
|
|
GameObject directionalLight;
|
|
int numOfTrees;
|
|
int numOfRows;
|
|
|
|
//prefabs
|
|
public GameObject tree1;
|
|
public GameObject tree2;
|
|
public GameObject tree3;
|
|
public GameObject tree4;
|
|
public GameObject tree5;
|
|
public GameObject tree6;
|
|
public System.Random random = new System.Random();
|
|
public List<GameObject> renderedTrees = new List<GameObject>();
|
|
|
|
float treesZSlider = 0.0f;
|
|
float treesXSlider = 0.0f;
|
|
|
|
float lightIntensity = 2.0f;
|
|
GameObject selectRandomTree()
|
|
{
|
|
var list = new List<GameObject> { tree1, tree2, tree3, tree4, tree5, tree6 };
|
|
var index = UnityEngine.Random.Range(0, list.Count - 1);
|
|
return list[index];
|
|
}
|
|
|
|
Vector2Int textureCameraSize = new Vector2Int(800, 600);
|
|
GameObject textureCamera;
|
|
int ScreenshotsPerSecond = 1;
|
|
int FrameCounter = 0;
|
|
|
|
bool isSavinFrames = false;
|
|
|
|
|
|
void Start()
|
|
{
|
|
//camera
|
|
mainCamera = new GameObject();
|
|
mainCamera.AddComponent<Camera>();
|
|
mainCamera.transform.position = new Vector3(m_camPosX, m_camPosY, m_camPosZ);
|
|
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
|
|
directionalLight = new GameObject();
|
|
directionalLight.AddComponent<Light>();
|
|
directionalLight.name = "Directional Light";
|
|
directionalLight.GetComponent<Light>().type = LightType.Directional;
|
|
directionalLight.GetComponent<Light>().shadows = LightShadows.Soft;
|
|
directionalLight.GetComponent<Light>().color = Color.white;
|
|
directionalLight.GetComponent<Light>().useColorTemperature = true;
|
|
directionalLight.GetComponent<Light>().colorTemperature = 5900;
|
|
directionalLight.transform.position = new Vector3(10, 10.0f, 10.0f);
|
|
directionalLight.transform.Rotate(lightDir.x, lightDir.y, lightDir.z, Space.Self);
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
// create prefabs
|
|
|
|
int positionShift_x = 500;
|
|
int positionShift_z = 500;
|
|
int treesNumber = 50;
|
|
int rowsNumber = 10;
|
|
int gridShiftX = 10;
|
|
int gridShiftZ = 10;
|
|
|
|
int randomShiftX;
|
|
int randomShiftZ;
|
|
numOfTrees = treesNumber;
|
|
numOfRows = rowsNumber;
|
|
|
|
int maxShiftValue = 5;
|
|
|
|
for (int i=0; i<treesNumber * gridShiftX; i+=gridShiftX)
|
|
{
|
|
for(int k=0; k<rowsNumber * gridShiftZ; k+=gridShiftZ)
|
|
{
|
|
randomShiftX = random.Next(0, maxShiftValue);
|
|
randomShiftZ = random.Next(0, maxShiftValue);
|
|
|
|
renderedTrees.Add(Instantiate(selectRandomTree(), new Vector3(i + positionShift_x + randomShiftX, 1, k + positionShift_z + randomShiftZ), Quaternion.identity));
|
|
}
|
|
}
|
|
|
|
bool exists = Directory.Exists(Application.dataPath + "/capturedframe");
|
|
if (!exists) Directory.CreateDirectory(Application.dataPath + "/capturedframe");
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
mainCamera.GetComponent<Camera>().fieldOfView = m_FieldOfView;
|
|
newCameraPosX = camera_r * Mathf.Cos(camera_rotation) + 500f + m_camPosX;
|
|
newCameraPosY = 10f + m_camPosY;
|
|
newCameraPosZ = camera_r * Mathf.Sin(camera_rotation) + 500f + m_camPosZ;
|
|
mainCamera.GetComponent<Camera>().transform.position = new Vector3(newCameraPosX, newCameraPosY, newCameraPosZ);
|
|
mainCamera.GetComponent<Camera>().transform.LookAt(new Vector3(500f + m_camLookAtX, 1f + m_camLookAtY, 500f + m_camLookAtZ));
|
|
camera_rotation += 0.001f;
|
|
int index = 0;
|
|
|
|
renderedTrees.ForEach(elem => {
|
|
var currentPosition = elem.transform.position;
|
|
float shiftValueZ = treesZSlider * ((index % numOfRows) - numOfRows/2);
|
|
float shiftValueX = treesXSlider * ( index / numOfRows - numOfTrees/2);
|
|
elem.transform.position = new Vector3(currentPosition.x + shiftValueX, currentPosition.y, currentPosition.z + shiftValueZ);
|
|
index++;
|
|
});
|
|
|
|
|
|
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()
|
|
{
|
|
GUI.Label(new Rect(10, 20, 60, 20), "FOV");
|
|
m_FieldOfView = GUI.HorizontalSlider(new Rect(70, 20, 150, 20), m_FieldOfView, 20.0f, 150.0f);
|
|
|
|
GUI.Label(new Rect(10, 45, 60, 20), "camPosX");
|
|
m_camPosX = GUI.HorizontalSlider(new Rect(70, 45, 150, 20), m_camPosX, -300.0f, 300.0f);
|
|
|
|
GUI.Label(new Rect(10, 70, 60, 20), "camPosY");
|
|
m_camPosY = GUI.HorizontalSlider(new Rect(70, 70, 150, 20), m_camPosY, -300.0f, 300.0f);
|
|
|
|
GUI.Label(new Rect(10, 95, 60, 20), "camPosZ");
|
|
m_camPosZ = GUI.HorizontalSlider(new Rect(70, 95, 150, 20), m_camPosZ, -300.0f, 300.0f);
|
|
|
|
GUI.Label(new Rect(10, 120, 60, 20), "LookAtX");
|
|
m_camLookAtX = GUI.HorizontalSlider(new Rect(70, 120, 150, 20), m_camLookAtX, -300.0f, 300.0f);
|
|
|
|
GUI.Label(new Rect(10, 150, 60, 20), "LookAtY");
|
|
m_camLookAtY = GUI.HorizontalSlider(new Rect(70, 145, 150, 20), m_camLookAtY, -300.0f, 300.0f);
|
|
|
|
GUI.Label(new Rect(10, 170, 60, 20), "LookAtZ");
|
|
m_camLookAtZ = GUI.HorizontalSlider(new Rect(70, 170, 150, 20), m_camLookAtZ, -300.0f, 300.0f);
|
|
|
|
GUI.Label(new Rect(10, 190, 60, 20), "CameraR");
|
|
camera_r = GUI.HorizontalSlider(new Rect(70, 190, 150, 20), camera_r, -150.0f, 150.0f);
|
|
|
|
GUI.Label(new Rect(10, 210, 60, 20), "TreesZSlider");
|
|
treesZSlider = GUI.HorizontalSlider(new Rect(70, 210, 150, 20), treesZSlider, -30.0f, 30.0f);
|
|
|
|
GUI.Label(new Rect(10, 230, 60, 20), "TreesXSlider");
|
|
treesXSlider = GUI.HorizontalSlider(new Rect(70, 230, 150, 20), treesXSlider, -30.0f, 30.0f);
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|