Why is my preview scene showing nothing?

Hi, I’m working on an EditorWindow that shows a preview of an object that can be saved as an asset. My problem is that the generated object is not showing in my preview, but the object was generated properly.

This is how I generate the object. I made sure to save the mesh and game object to the project, so I can check if the object shows in the Game view and Scene view correctly. The objects show up correctly when put into a scene, but interestingly the mesh is missing from the game object (I assume this is due to the way I’m saving them to the assets folder).

var meshObject = new GameObject("preview-mesh");
var meshRenderer = meshObject.AddComponent<MeshRenderer>()!;
meshRenderer.material = AssetDatabase.LoadAssetAtPath<Material>("Assets/Ghi3/GameWorld/Editor/DefaultMaterial.mat");
var meshFilter = meshObject.AddComponent<MeshFilter>()!;
var terrainResolutionWidth = (byte)_terrainResolutionWidth.value;
var terrainResolutionDepth = (byte)_terrainResolutionDepth.value;
var heightMap = GameWorldGenerator.GenerateHeightMap(terrainResolutionWidth, terrainResolutionDepth);
meshFilter.sharedMesh = GameWorldGenerator.GenerateMesh(heightMap);
AssetDatabase.CreateAsset(meshFilter.sharedMesh, "Assets/Scenes/mesh.asset");
PrefabUtility.SaveAsPrefabAsset(meshObject, "Assets/Scenes/mesh.prefab");
_preview.Preview(meshObject);
_preview.Render();

_preview.Preview() and _preview.Render() look like this:

public void Preview([NotNull] GameObject gameObject)
{
    if (gameObject == null)
        throw new ArgumentNullException(nameof(gameObject));
            
    var previewScene = _previewCamera.scene;
    var cameraObject = _previewCamera.gameObject;

    foreach (var rootGameObject in previewScene.GetRootGameObjects()!)
        if (rootGameObject != cameraObject)
            UnityEngine.Object.DestroyImmediate(rootGameObject);
            
    SceneManager.MoveGameObjectToScene(gameObject, _previewCamera.scene);
    // This capsule is also not showing!
    SceneManager.MoveGameObjectToScene(
        GameObject.CreatePrimitive(PrimitiveType.Capsule),
         _previewCamera.scene);
    // TODO: Center camera on gameObject.
}

public void Render()
{
    _previewCamera.Render();
    Graphics.CopyTexture(_previewCamera.targetTexture, _previewImage.image);
}

I set up the camera and scene in the constructor of the custom VisualElement:

public GameWorldPreview()
{
    UxmlUtil.LoadUxml(this); // This loads a UXML file into this.
    _previewImage = this.RequireElement<Image>("mesh-preview"); // This gets an Image with the name "mesh-preview" from the UXML tree.
    var graphicsFormat = GraphicsFormatUtility.GetGraphicsFormat(
        RenderTextureFormat.ARGBFloat,
        RenderTextureReadWrite.Default);
    _previewImage.image = new Texture2D(
        width: 200,
        height: 200,
        graphicsFormat,
        mipCount: 1,
        TextureCreationFlags.DontInitializePixels);
    var cameraObject = new GameObject();
    _previewCamera = cameraObject.AddComponent<Camera>()!;
    SetUpPreviewScene(graphicsFormat, cameraObject);
    // TODO: Dynamically update the size of the preview image with schedule.Execute(...).Every(...).
}

private void SetUpPreviewScene(GraphicsFormat graphicsFormat, [NotNull] GameObject cameraObject)
{
    var previewScene = EditorSceneManager.NewPreviewScene();
    _previewCamera.cameraType = CameraType.Preview;
    _previewCamera.aspect = 1f;
    _previewCamera.orthographicSize = 100f;
    _previewCamera.backgroundColor = Color.blue;
    _previewCamera.scene = previewScene;
    _previewCamera.targetTexture = new RenderTexture(
        width: 200,
        height: 200,
        colorFormat: graphicsFormat,
        depthStencilFormat: GraphicsFormat.None,
        mipCount: 1);
    cameraObject.transform!.position = Vector3.back * 10f + Vector3.up;
    SceneManager.MoveGameObjectToScene(cameraObject, previewScene);
}

Can anyone help me understand what’s going on here?

Edit: I forgot some details.
I’m working with Unity 2022.3.15f1.
After adding them to the scene, my camera has a position of 0, 1, -10 and the object is at 0, 0, 0.

For anyone wondering about this exact problem: In my case the problem wasn’t the scene, it was the RenderTexture I used on my camera. I didn’t want to set the depth in the constructor, because I don’t fully understand what it does, so I opted to use another constructor. This seems to create a RenderTexture with depth = 0 that shows an empty skybox in my case. When I changed the constructor to use depth = 24, it showed my object as expected. Curiously, when setting the depth to 16, it doesn’t show anything (clear).