Advertisement
AnthonyGraceffa

PT01

Mar 10th, 2015
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.13 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5.  
  6. public class PictureTaker : MonoBehaviour {
  7.  
  8.     public Camera VRCamLeft;
  9.     public Camera VRCamRight;
  10.     public Camera SnapShotCam;
  11.     List<SnapShot> snapsTakenThisLevel;
  12.     List<string> subjectsInLastSnap;
  13.  
  14.     public RenderTexture overviewTexture;
  15.     public string path = "";
  16.  
  17.     //Stores info about a snapshot taken in game
  18.     private class SnapShot {
  19.         public string fileName;
  20.         public string level;
  21.         public List<string> capturedSubjects;
  22.  
  23.         public SnapShot (string file, string curLevel) {
  24.             fileName = file;
  25.             level = curLevel;
  26.         }
  27.  
  28.     }
  29.     // Used for initialization
  30.     void Awake () {
  31.         snapsTakenThisLevel = new List<SnapShot> ();
  32.         subjectsInLastSnap = new List<string> ();
  33.  
  34.         SnapShotCam.gameObject.SetActive (false);
  35.  
  36.     }
  37.  
  38.     //Called once per frame
  39.     void Update () {
  40.         //for debugging, prints file name of last snap
  41.         if (Input.GetButtonDown ("Jump")) {
  42.             Debug.Log (snapsTakenThisLevel[snapsTakenThisLevel.Count -1].fileName);
  43.         }
  44.     }
  45.  
  46.     // LateUpdate is called once per frame after update
  47.     void LateUpdate () {
  48.  
  49.         //If player clicks, take a snapshot
  50.         if(Input.GetButtonDown("Fire1")){
  51.             Transform middleCam = VRCamLeft.transform;
  52.             middleCam.position = GetCenterOfCameras ();
  53.             Ray ray = new Ray(middleCam.position, middleCam.forward);
  54.             RaycastHit hit;
  55.             Debug.DrawRay(ray.origin, ray.direction * 20, Color.red);
  56.  
  57.             //If a snap target is hit, add it to the subjects in last snap list, else clear it
  58.             if (Physics.Raycast(ray, out hit, 25)) {
  59.                 if(hit.collider.gameObject.tag == "SnapTarget" && hit.collider != null) {
  60.                     string newSubject = hit.collider.gameObject.name;
  61.                     subjectsInLastSnap.Add(newSubject);
  62.  
  63.                     Debug.Log ("PictureTaker:LateUpdate - Captured :" + hit.collider.gameObject.name);
  64.  
  65.                 }
  66.                 else{
  67.                     subjectsInLastSnap.Clear();
  68.                 }
  69.  
  70.             }
  71.             StartCoroutine(TakeScreenShot());
  72.             Debug.Log("PictureTaker:LateUpdate - Took Snap!");
  73.         }
  74.     }
  75.  
  76.     //Create a new Vector3 inbetween both cameras
  77.     Vector3 GetCenterOfCameras () {
  78.         Vector3 newPos;
  79.         newPos.y = (VRCamLeft.transform.position.y + VRCamRight.transform.position.y)/2;
  80.         newPos.x = (VRCamLeft.transform.position.x + VRCamRight.transform.position.x)/2;
  81.         newPos.z = (VRCamLeft.transform.position.z + VRCamRight.transform.position.z)/2;
  82.         return newPos;
  83.     }
  84.  
  85.     //Rotate snapshot cam to something close to where it should be
  86.     Quaternion GetNewCameraRotation () {
  87.         Quaternion newRotation = VRCamLeft.transform.rotation;
  88.         return newRotation;
  89.     }
  90.    
  91.     //Set filename for screenshots
  92.     string fileName(int width, int height) {
  93.         return string.Format("screen_{0}x{1}_{2}.png",
  94.                              width, height,
  95.                              System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
  96.     }
  97.  
  98.     //Take screenshot from snapcam and save to persistant filepath
  99.     public IEnumerator TakeScreenShot() {
  100.         yield return new WaitForEndOfFrame();
  101.  
  102.         SnapShotCam.gameObject.SetActive (true);
  103.         SnapShotCam.transform.position = GetCenterOfCameras ();
  104.         SnapShotCam.transform.rotation = GetNewCameraRotation ();
  105.  
  106.        
  107.         RenderTexture currentRT = RenderTexture.active;
  108.        
  109.         RenderTexture.active = SnapShotCam.targetTexture;
  110.         SnapShotCam.Render();
  111.         Texture2D imageOverview = new Texture2D(SnapShotCam.targetTexture.width, SnapShotCam.targetTexture.height, TextureFormat.RGB24, false);
  112.         imageOverview.ReadPixels(new Rect(0, 0, SnapShotCam.targetTexture.width, SnapShotCam.targetTexture.height), 0, 0);
  113.         imageOverview.Apply();
  114.        
  115.         RenderTexture.active = currentRT;
  116.        
  117.        
  118.         // Encode texture into PNG
  119.         byte[] bytes = imageOverview.EncodeToPNG();
  120.        
  121.         // Save in memory
  122.         string filename = fileName(Convert.ToInt32(imageOverview.width), Convert.ToInt32(imageOverview.height));
  123.         path = Application.persistentDataPath + "/Snapshots/" + filename;
  124.  
  125.  
  126.         System.IO.File.WriteAllBytes(path, bytes);
  127.  
  128.         //Create game data
  129.         SnapShot newSnap = new SnapShot (filename, "TestLevel");
  130.         newSnap.capturedSubjects = subjectsInLastSnap;
  131.         snapsTakenThisLevel.Add (newSnap);
  132.     }
  133.  
  134.     //Saves the game relevant information about a photo
  135.     void SaveSnapInfo () {
  136.  
  137.     }
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement