VR/Assets/Prefabs/OVR Scene Manager Overrides/Picture/WallArt.cs

81 lines
2.6 KiB
C#

using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
public class WallArt : MonoBehaviour
{
public GameObject outsideCameraPrefab;
public Transform window;
private Camera outsideCameraInstance;
public MeshRenderer meshRenderer;
private OVRCameraRig ovrCameraRig;
private RenderTexture renderTexture;
private Material material;
// Start is called before the first frame update
void Start()
{
var randomNumber = Random.Range(10000, 99999);
MarcsWebLogger.Log($"{randomNumber}-start");
renderTexture = new RenderTexture(256, 256, 0)
{
dimension = UnityEngine.Rendering.TextureDimension.Tex2D,
wrapMode = TextureWrapMode.Clamp,
filterMode = FilterMode.Bilinear,
enableRandomWrite = false,
useDynamicScale = false,
useMipMap = false,
depthStencilFormat = UnityEngine.Experimental.Rendering.GraphicsFormat.D24_UNorm_S8_UInt,
};
MarcsWebLogger.Log($"{randomNumber}-1");
GameObject camera = Instantiate(outsideCameraPrefab, transform.position, Quaternion.identity);
MarcsWebLogger.Log($"{randomNumber}-1.1");
outsideCameraInstance = camera.GetComponent<Camera>();
MarcsWebLogger.Log($"{randomNumber}-2");
try
{
// Attempt to set the target texture
outsideCameraInstance.targetTexture = renderTexture;
}
catch (System.Exception e)
{
// Handle any exceptions that may occur
Debug.LogError("An error occurred: " + e.Message);
}
MarcsWebLogger.Log($"{randomNumber}-3");
// Set the initial rotation of the camera
outsideCameraInstance.transform.rotation = Quaternion.Euler(
Random.Range(0f, 360f),
Random.Range(0f, 360f),
Random.Range(0f, 360f)
);
MarcsWebLogger.Log($"{randomNumber}-4");
material = new Material(Shader.Find("Standard"));
MarcsWebLogger.Log($"{randomNumber}-5");
material.mainTexture = renderTexture;
MarcsWebLogger.Log($"{randomNumber}-6");
meshRenderer.material = material;
MarcsWebLogger.Log($"{randomNumber}-7");
ovrCameraRig = FindObjectOfType<OVRCameraRig>();
}
// Update is called once per frame
void Update()
{
// Check if the cameraRig is assigned
if (ovrCameraRig == null) return;
outsideCameraInstance.transform.position = ovrCameraRig.centerEyeAnchor.position;
outsideCameraInstance.transform.LookAt(window);
}
}