I’m trying to make a pipeline automation script which involves rendering some cubemaps.
I’ve used both my own custom function below, and the Unity example straight out of the scripting reference, both get the same results.
void GenerateCubemap() {
Debug.Log ("ReflectionGeneratorWizard: Rendering cubemap");
GameObject cameraObject = new GameObject("ReflectionGeneratorCamera");
Camera camera = cameraObject.AddComponent<Camera>();
camera.cullingMask = layerMask;
camera.near = 0.1f;
camera.far = 100.0f;
cameraObject.transform.position = origin.transform.position;
cameraObject.transform.rotation = Quaternion.identity;
cubemap = new Cubemap(size, TextureFormat.RGBA32, false);
camera.RenderToCubemap(cubemap);
DestroyImmediate(cameraObject);
}
The result that gets me is this:
The issue is that it’s rendering back faces instead of front faces.
One odd thing is that the example cubemap renderer script from the scripting reference gets the same results too. An even odder thing is that we were rendering other cubemaps just yesterday without a problem.
That same script works perfectly in a fresh project (just a few cubes in a scene and that editor script), so I’m relatively sure that the problem isn’t from there.
Is this something getting messed up in Unity’s internals? I’m using 3.5.5f2 on Windows, and the behaviour is identical on at least two workstations. I’ll give it a go on OS X shortly.
Unfortunately not. I tried to create a simple repro project, but in that project both my script and the one from the manual behave correctly. The project that this is in is pretty massive contains stuff I can’t put out in public, so making a repro project is unfortunately a lot of work.
I’ve got the same problem with Unity 4.3.1f1
Everything renders correctly from the camera, but in the texture generated by RenderToCubeMap, I’ve only got the backfaces.
I had this problem with Unity 5, but i had a solution, so I will post my solution here hoping it will help someone.
Turns out that the problem is using GL.invertCulling in scripts, if there is any script that’s using GL.invertCulling and you don’t need it just remove it, it exists usually in scripts such as PlanarReflection.cs and Water.cs, scripts that are used to render reflections.
if you don’t want to remove the script you can replace any instruction like GL.invertCulling = true; and GL.invertCulling = false; with the instruction GL.invertCulling = !GL.invertCulling;
the reason for this is that the reflection camera inverts culling in order to render front faces (because it’s a reflection camera, things for are the other way around, if it didn’t invert culling it will only render backfaces), and the the camera that will render the cubemap needs to do the same (a cube map is a reflection afterall), the problem happens when a reflection (water?) is rendered inside the cubemap, after rendering the reflection it will restore GL.invertCulling value to false while rendering the rest of the cube map, a move which make everything rendered afterwards culled the wrong way.
an alternate solution would be saving the old value before assigning true to GL.invertCulling, and then restore the old value instead of assigning false, but this solution makes the reflections inside the cubemap incorrect, e.g. if you had water in the scene, when you look at the water in the cubemap the reflecions in that water will look front-culled.