WaterChicken

FakeRenderCubemapFace

Jul 11th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.19 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class FakeRenderCubemapFace : MonoBehaviour {
  5.     private Texture2D texture;
  6.     public Cubemap cubemap;
  7.     public CubemapFace cubemapFace;
  8.     public float updateFrequency = .5f;
  9.     private float lastUpdate = 0f;
  10.     private Rect sourceRect;
  11.  
  12.     // Use this for initialization
  13.     void Start() {
  14.         float x = 0;
  15.         float y = cubemap.height;
  16.         if (cubemapFace == CubemapFace.PositiveZ) {
  17.             x = cubemap.width;
  18.         }
  19.         if (cubemapFace == CubemapFace.PositiveX) {
  20.             x = cubemap.width * 2;
  21.         }
  22.         if (cubemapFace == CubemapFace.NegativeZ) {
  23.             x = cubemap.width * 3;
  24.         }
  25.         if (cubemapFace == CubemapFace.PositiveY) {
  26.             y = cubemap.width * 2;
  27.         }
  28.         if (cubemapFace == CubemapFace.NegativeY) {
  29.             y = 0;
  30.         }
  31.         texture = new Texture2D (cubemap.width, cubemap.height);
  32.         sourceRect = new Rect (x, y, cubemap.width, cubemap.height);
  33.         camera.pixelRect = sourceRect;
  34.     }
  35.  
  36.     void Update() {
  37.         if (Time.time > lastUpdate + updateFrequency) {
  38.             camera.enabled = true;
  39.             lastUpdate = Time.time;
  40.         }
  41.     }
  42.    
  43.     void OnPostRender() {
  44.         texture.ReadPixels (sourceRect, 0, 0);
  45.         cubemap.SetPixels (texture.GetPixels (), cubemapFace);
  46.         camera.enabled = false;
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment