- Home /
Getting real time cube maps to work with Agumented Reality games and optimizing them for mobile.
I'm trying to get real time cube maps to work with unity in order to reflect the environment the player is in off the object in the scene. When testing, the cubemaps work, but they reflect the default unity blue sky instead of the environment around the player.
I have set an AR camera as well as a plane that when tested does show what the camera of the phone sees, but the cubemaps don't register that. Also, I realize that realtime cubemaps are expensive but is there any way to optimize the scipt to run better on mobile? It was running just fine before applying the real time cube maps.
Here is the script if anyone can suggest any edits to it to achieve what I'm looking for:
using UnityEngine; using System.Collections;
public class RealtimeCubemap : MonoBehaviour {
public int cubemapSize = 16;
public bool oneFacePerFrame = false;
private Camera cam;
private RenderTexture rtex;
private GameObject go;
[ExecuteInEditMode]
void Start() {
// render all six faces at startup
UpdateCubemap(63);
}
void LateUpdate() {
if (oneFacePerFrame) {
int faceToRender = Time.frameCount % 6;
int faceMask = 1 << faceToRender;
UpdateCubemap(faceMask);
} else {
UpdateCubemap(63); // all six faces
}
}
void UpdateCubemap(int faceMask) {
if (!cam) {
go = new GameObject("CubemapCamera");
go.AddComponent(typeof(Camera));
go.hideFlags = HideFlags.HideAndDontSave;
go.transform.position = transform.position;
go.transform.rotation = Quaternion.identity;
cam = go.camera;
cam.farClipPlane = 100; // don't render very far into cubemap
cam.enabled = false;
}
if (!rtex) {
rtex = new RenderTexture(cubemapSize, cubemapSize, 16);
rtex.isCubemap = true;
rtex.hideFlags = HideFlags.HideAndDontSave;
renderer.sharedMaterial.SetTexture ("_Cube", rtex);
}
cam.transform.position = transform.position;
cam.RenderToCubemap(rtex, faceMask);
}
void OnDisable() {
DestroyImmediate (cam);
DestroyImmediate (rtex);
}
}
Thank you for any help.
Your answer
![](https://koobas.hobune.stream/wayback/20220613130040im_/https://answers.unity.com/themes/thub/images/avi.jpg)