Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System;
- public class PictureTaker : MonoBehaviour {
- public Camera VRCamLeft;
- public Camera VRCamRight;
- public Camera SnapShotCam;
- List<SnapShot> snapsTakenThisLevel;
- List<string> subjectsInLastSnap;
- public RenderTexture overviewTexture;
- public string path = "";
- //Stores info about a snapshot taken in game
- private class SnapShot {
- public string fileName;
- public string level;
- public List<string> capturedSubjects;
- public SnapShot (string file, string curLevel) {
- fileName = file;
- level = curLevel;
- }
- }
- // Used for initialization
- void Awake () {
- snapsTakenThisLevel = new List<SnapShot> ();
- subjectsInLastSnap = new List<string> ();
- SnapShotCam.gameObject.SetActive (false);
- }
- //Called once per frame
- void Update () {
- //for debugging, prints file name of last snap
- if (Input.GetButtonDown ("Jump")) {
- Debug.Log (snapsTakenThisLevel[snapsTakenThisLevel.Count -1].fileName);
- }
- }
- // LateUpdate is called once per frame after update
- void LateUpdate () {
- //If player clicks, take a snapshot
- if(Input.GetButtonDown("Fire1")){
- Transform middleCam = VRCamLeft.transform;
- middleCam.position = GetCenterOfCameras ();
- Ray ray = new Ray(middleCam.position, middleCam.forward);
- RaycastHit hit;
- Debug.DrawRay(ray.origin, ray.direction * 20, Color.red);
- //If a snap target is hit, add it to the subjects in last snap list, else clear it
- if (Physics.Raycast(ray, out hit, 25)) {
- if(hit.collider.gameObject.tag == "SnapTarget" && hit.collider != null) {
- string newSubject = hit.collider.gameObject.name;
- subjectsInLastSnap.Add(newSubject);
- Debug.Log ("PictureTaker:LateUpdate - Captured :" + hit.collider.gameObject.name);
- }
- else{
- subjectsInLastSnap.Clear();
- }
- }
- StartCoroutine(TakeScreenShot());
- Debug.Log("PictureTaker:LateUpdate - Took Snap!");
- }
- }
- //Create a new Vector3 inbetween both cameras
- Vector3 GetCenterOfCameras () {
- Vector3 newPos;
- newPos.y = (VRCamLeft.transform.position.y + VRCamRight.transform.position.y)/2;
- newPos.x = (VRCamLeft.transform.position.x + VRCamRight.transform.position.x)/2;
- newPos.z = (VRCamLeft.transform.position.z + VRCamRight.transform.position.z)/2;
- return newPos;
- }
- //Rotate snapshot cam to something close to where it should be
- Quaternion GetNewCameraRotation () {
- Quaternion newRotation = VRCamLeft.transform.rotation;
- return newRotation;
- }
- //Set filename for screenshots
- string fileName(int width, int height) {
- return string.Format("screen_{0}x{1}_{2}.png",
- width, height,
- System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
- }
- //Take screenshot from snapcam and save to persistant filepath
- public IEnumerator TakeScreenShot() {
- yield return new WaitForEndOfFrame();
- SnapShotCam.gameObject.SetActive (true);
- SnapShotCam.transform.position = GetCenterOfCameras ();
- SnapShotCam.transform.rotation = GetNewCameraRotation ();
- RenderTexture currentRT = RenderTexture.active;
- RenderTexture.active = SnapShotCam.targetTexture;
- SnapShotCam.Render();
- Texture2D imageOverview = new Texture2D(SnapShotCam.targetTexture.width, SnapShotCam.targetTexture.height, TextureFormat.RGB24, false);
- imageOverview.ReadPixels(new Rect(0, 0, SnapShotCam.targetTexture.width, SnapShotCam.targetTexture.height), 0, 0);
- imageOverview.Apply();
- RenderTexture.active = currentRT;
- // Encode texture into PNG
- byte[] bytes = imageOverview.EncodeToPNG();
- // Save in memory
- string filename = fileName(Convert.ToInt32(imageOverview.width), Convert.ToInt32(imageOverview.height));
- path = Application.persistentDataPath + "/Snapshots/" + filename;
- System.IO.File.WriteAllBytes(path, bytes);
- //Create game data
- SnapShot newSnap = new SnapShot (filename, "TestLevel");
- newSnap.capturedSubjects = subjectsInLastSnap;
- snapsTakenThisLevel.Add (newSnap);
- }
- //Saves the game relevant information about a photo
- void SaveSnapInfo () {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement