Weird lighting issues when renderig an additively loaded scene using a RenderTexture

Hey Folks!

While implementing native sharing for mobile platforms (not the problem here), I had the idea to create a scene containing a “mockup” of the player’s progress.

My approach here was using a camera with a RenderTexture in the mockup scene, so that the mockup isn’t actually rendered on-screen.

The code for the whole process is pretty short, and looks something like this:

private IEnumerator ShareProgress()
{
    yield return SceneManager.LoadSceneAsync("ProgressMockup", LoadSceneMode.Additive);

    // Wait for the frame to render the mockup properly
    yield return new WaitForEndOfFrame();

    // Create a Texture2D from the RenderTexture
    Texture2D screenshot = new Texture2D(progressMockupRenderTexture.width, progressMockupRenderTexture.height, TextureFormat.RGB24, false);
    RenderTexture.active = progressMockupRenderTexture;
    screenshot.ReadPixels(new Rect(0, 0, progressMockupRenderTexture.width, progressMockupRenderTexture.height), 0, 0);
    screenshot.Apply();

    RenderTexture.active = null;

    // Convert to PNG and save temporarily
    string filePath = Path.Combine(Application.temporaryCachePath, "SharedProgressMockup.png");
    File.WriteAllBytes(filePath, screenshot.EncodeToPNG());

    // Cleanup
    Destroy(screenshot);

    // Unload the Mockup scene
    SceneManager.UnloadSceneAsync("ProgressMockup");

    // Share via NativeShare
    new NativeShare()
        .AddFile(filePath)
        .SetSubject(LocalizationSettings.StringDatabase.GetLocalizedString(Constants.MOBILE_SHARE_TEXTS_TABLE_REFERENCE, "Subject"))
        .SetText(LocalizationSettings.StringDatabase.GetLocalizedString(Constants.MOBILE_SHARE_TEXTS_TABLE_REFERENCE, "Text"))
        .SetUrl("")
        .SetCallback((result, shareTarget) => Debug.Log("Share result: " + result + ", selected app: " + shareTarget))
        .Share();
}

While the actual capturing and sharing works great, there is something wrong with the rendering of the scene. The scene, like any other scene in the game, contains the same lighting settings and also has things like a Directional Light set up.

Here are the images (dont worry this is still early footage and not the final version, they’re also in german but of course that dosen’t change anything xD):

Editor (how it should look):

Rendered with a Directional Light (no fog, weird shadows, too dark)

Rendered without a Directional Light (no fog, correct shadows?, too dark)

I don’t really know what would be causing this. As visible in the editor the camera is just a prefab variant of the main camera, only change here is it rendering to the render texture.

As I said the lighting settings are also all the same. Even if I disabled the render texture and played that scene it still looks fine at runtime.

I appreciate any suggestions / solutions!

The “too dark” portion reminds me of this AFAIK still-extant bug… perhaps that’s what you’re seeing?

Editor has a bug with lighting if you auto-bake.

The current first scene will look correct, but when as you load or reload a scene, that subsequent scene will be darkish and potentially have other lighting issues as well.

You can verify that this is the case here by making a build to play outside of the Editor.

Turn off auto-bake and bake your lights if you wish to see correct lighting in the Unity Editor.

Firstly thanks for your response!

Thats what I originally thought of as well when I saw this, but sadly it happens at runtime on an android device too.

Maybe I’m entierly missing something with the lighting setup?
Like for example: Without the directional light, the shadows are still there and look correct too, shouldn’t this be impossible?

Of course the easiest solution would be to just load the scene in view and take a screenshot but this is just not elegant imo.

One thing I noticed is that when capturing with the scene in view, the render texture is also correct, which means the issue must be it not rendering the scene correctly when loaded additively, which is at least what I would expect.

Ah, looking above I don’t see you setting the additively-loaded scene to be the active scene… that will affect lighting.

From these docs:

I quote:

The active Scene is the Scene which will be used as the target for new GameObjects instantiated by scripts and from what Scene the lighting settings are used.

But remember you DO need that to happen AFTER the scene has finished loading but I think you yielding the AsyncObject will take care of that…

I added that, and in the Hierachy the menu scene is clearly not active for a frame but sadly still the same lighting issue.