Advertisement
Guest User

Panoramas Unity

a guest
Oct 28th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.05 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Collections.Generic;
  5.  
  6. public class LoadImages : MonoBehaviour {
  7.  
  8.     public string baseDir = @"C:\panoramic";
  9.     public List<KeyValuePair<int, Texture2D>> images = new List<KeyValuePair<int, Texture2D>>();
  10.  
  11.     private string[,] filepaths;
  12.     private int contadorDir = 0;
  13.     private GameObject cilindro;
  14.     private GameObject[] cilindroChildren = new GameObject[4];
  15.     private Renderer renderer;
  16.     private string[] dirs;
  17.     private GameObject loading;
  18.     private TextMesh loadingtext;
  19.     private int contadorLoaded = 0;
  20.     private WWW load;
  21.  
  22.     Texture2D tempTexture;
  23.  
  24.     // Use this for initialization
  25.     public IEnumerator Start()
  26.     {
  27.         cilindro = GameObject.Find("cilindro");
  28.         loading = GameObject.Find("loading");
  29.         loadingtext = loading.GetComponent<TextMesh>();
  30.         loadingtext.text = "Loading (0%)";
  31.         loading.SetActive(true);
  32.         cilindro.SetActive(false);
  33.  
  34.         //UnityEngine.VR.InputTracking.Recenter();
  35.         OVRManager.display.timeWarp = true;
  36.         OVRManager.display.RecenterPose();
  37.  
  38.         dirs = Directory.GetDirectories(baseDir);
  39.         filepaths = new string[dirs.Length, 4]; //Cada diretório / panorama contém 4 imagens
  40.         for (int i = 0; i < dirs.Length; i++)
  41.         {
  42.             filepaths[i, 0] = dirs[i] + "\\1.png";
  43.             filepaths[i, 1] = dirs[i] + "\\2.png";
  44.             filepaths[i, 2] = dirs[i] + "\\3.png";
  45.             filepaths[i, 3] = dirs[i] + "\\4.png";
  46.         }
  47.  
  48.         yield return StartCoroutine("LoadAll", filepaths);
  49.  
  50.         //Setar as primeiras texturas
  51.         setTextures();
  52.         loading.SetActive(false);
  53.         cilindro.SetActive(true);
  54.     }
  55.  
  56.     private void setTextures()
  57.     {
  58.  
  59.         loading.SetActive(true);
  60.         cilindro.SetActive(false);
  61.  
  62.         //UnityEngine.VR.InputTracking.Recenter();
  63.         OVRManager.display.RecenterPose();
  64.  
  65.         for (int i = 0; i < cilindro.transform.childCount; i++)
  66.         {
  67.             cilindroChildren[i] = cilindro.transform.GetChild(i).gameObject;
  68.             renderer = cilindroChildren[i].GetComponent<Renderer>();
  69.             renderer.material.mainTexture = images[(contadorDir * 4) + i].Value;
  70.             Debug.Log("Image " + (i + 1) + " set");
  71.         }
  72.  
  73.         Debug.Log("Textures set");
  74.  
  75.         loading.SetActive(false);
  76.         cilindro.SetActive(true);
  77.     }
  78.  
  79.  
  80.  
  81.     public IEnumerator LoadAll(string[,] filePaths)
  82.     {
  83.         Debug.Log("Load started..");
  84.         //Carregar as 4 texturas deste panorama
  85.  
  86.         for (int i = 0; i < dirs.Length; i++)
  87.         {
  88.             for (int j = 0; j < 4; j++)
  89.             {
  90.                 Debug.Log("Loading image " + (i+1) + ": " + (j + 1));
  91.                 loadingtext.text = "Loading.. (" + contadorLoaded * 100 / (dirs.Length * 4) + "%)";
  92.                 contadorLoaded++;
  93.                 load = new WWW("file:///" + filepaths[i, j]);
  94.                 yield return load;
  95.                 if (!string.IsNullOrEmpty(load.error))
  96.                 {
  97.                     Debug.LogWarning(filepaths[i, j] + " error");
  98.                 }
  99.                 else
  100.                 {
  101.                     tempTexture = new Texture2D(2048, 2048, TextureFormat.DXT1, true);
  102.                     load.LoadImageIntoTexture((Texture2D)tempTexture);
  103.                     tempTexture.filterMode = FilterMode.Bilinear;
  104.                     tempTexture.anisoLevel = 1;
  105.                     images.Add(new KeyValuePair<int, Texture2D>(i, tempTexture));
  106.                 }
  107.                 Debug.Log("Image " + (i+1) + ": "+ (j + 1) + " loaded");
  108.                
  109.             }
  110.         }
  111.            
  112.         Debug.Log("Load finished!");
  113.     }
  114.    
  115.     // Update is called once per frame
  116.     void Update()
  117.     {
  118.         if (Input.GetKeyUp(KeyCode.Return))
  119.         {
  120.             if (contadorDir < dirs.Length - 1)
  121.             {
  122.                 contadorDir++;
  123.             }else{
  124.                 contadorDir = 0;
  125.             }
  126.             setTextures();
  127.         }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement