Hello guys
I am trying to use the Unity3D reference posted source for realtime updating cubemaps, available here: Unity - Scripting API: Camera.RenderToCubemap
(The second source).
But I don’t know how to setup. I tried to put this script on my mesh which also have Mesh Renderer, and then I changed the material to Reflective/Diffuse.
But this does not want to render cubemaps and apply on the mesh. How can I fix this? Thanks.
do you have unity pro?
This seems to happen if you save the scene first and then hit play… usually fixes it for me…
Yes I have Unity Pro with also Android Pro, which is the target platform. I am going to try it without save before playing. My biggest problem is that I cannot stop my finger to press Ctrl S, it’s impossible! haha, I want to ask, does this work also android? Thanks guys, I will let you know in less then 1 minute!
No I don’t know why, but it didn’t worked!
Now I get this error:
and
What is?
using UnityEngine;
using System;
// Attach this script to an object that uses a Reflective shader.
// Realtime reflective cubemaps!
[ExecuteInEditMode()]
class RenderCubemapReflection : MonoBehaviour
{
public int cubemapSize = 128;
public float nearClip = 0.01f;
public float farClip = 500;
public bool oneFacePerFrame = false;
public LayerMask layerMask;
private Camera cam;
private RenderTexture rtex;
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)
{
GameObject go = new GameObject("CubemapCamera", typeof(Camera));
go.hideFlags = HideFlags.HideAndDontSave;
go.transform.position = transform.position;
go.transform.rotation = Quaternion.identity;
cam = go.camera;
cam.cullingMask = layerMask;
cam.nearClipPlane = nearClip;
cam.farClipPlane = farClip;
cam.enabled = false;
}
if (!rtex)
{
rtex = new RenderTexture(cubemapSize, cubemapSize, 16);
rtex.isPowerOfTwo = true;
rtex.isCubemap = true;
rtex.hideFlags = HideFlags.HideAndDontSave;
foreach (Renderer r in GetComponentsInChildren<Renderer>())
{
foreach (Material m in r.sharedMaterials)
{
if (m.HasProperty("_Cube"))
{
m.SetTexture("_Cube", rtex);
}
}
}
}
cam.transform.position = transform.position;
cam.RenderToCubemap(rtex, faceMask);
}
void OnDisable()
{
DestroyImmediate(cam);
DestroyImmediate(rtex);
}
}
Here is the script that i use for my realtime reflections… just attach it to your object (or an object in a prefab)… it also handles culling and allows you to set the distance of the reflection…
Wow, amazing, thank you so much, this is looking working, also in Edit Mode, I am going to set the values and test it, thanks!!!