// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
using FlaxEditor.GUI.ContextMenu;
using FlaxEditor.Viewport.Cameras;
using FlaxEngine;
using Object = FlaxEngine.Object;
namespace FlaxEditor.Viewport.Previews
{
///
/// Generic asset preview editor viewport base class.
///
///
public abstract class AssetPreview : EditorViewport
{
private ContextMenuButton _showDefaultSceneButton;
///
/// The preview light. Allows to modify rendering settings.
///
public DirectionalLight PreviewLight;
///
/// The env probe. Allows to modify rendering settings.
///
public EnvironmentProbe EnvProbe;
///
/// The sky. Allows to modify rendering settings.
///
public Sky Sky;
///
/// The sky light. Allows to modify rendering settings.
///
public SkyLight SkyLight;
///
/// Gets the post fx volume. Allows to modify rendering settings.
///
public PostFxVolume PostFxVolume;
///
/// Gets or sets a value indicating whether show default scene actors (sky, env probe, skylight, directional light, etc.).
///
public bool ShowDefaultSceneActors
{
get => PreviewLight.IsActive;
set
{
if (ShowDefaultSceneActors != value)
{
PreviewLight.IsActive = value;
EnvProbe.IsActive = value;
Sky.IsActive = value;
SkyLight.IsActive = value;
if (_showDefaultSceneButton != null)
_showDefaultSceneButton.Checked = value;
}
}
}
///
/// Initializes a new instance of the class.
///
/// If set to true use widgets for viewport, otherwise hide them.
/// The initial orbit radius.
protected AssetPreview(bool useWidgets, float orbitRadius = 50.0f)
: this(useWidgets, new ArcBallCamera(Vector3.Zero, orbitRadius))
{
}
///
/// Initializes a new instance of the class.
///
/// If set to true use widgets for viewport, otherwise hide them.
/// The camera controller.
protected AssetPreview(bool useWidgets, ViewportCamera camera)
: base(Object.New(), camera, useWidgets)
{
Task.ViewFlags = ViewFlags.DefaultAssetPreview;
Task.AllowGlobalCustomPostFx = false;
var orbitRadius = 200.0f;
if (camera is ArcBallCamera arcBallCamera)
orbitRadius = arcBallCamera.OrbitRadius;
camera.SerArcBallView(new Quaternion(-0.08f, -0.92f, 0.31f, -0.23f), Vector3.Zero, orbitRadius);
if (useWidgets)
{
// Show Default Scene
_showDefaultSceneButton = ViewWidgetShowMenu.AddButton("Default Scene", () => ShowDefaultSceneActors = !ShowDefaultSceneActors);
_showDefaultSceneButton.Checked = true;
}
// Setup preview scene
PreviewLight = new DirectionalLight();
PreviewLight.Brightness = 8.0f;
PreviewLight.ShadowsMode = ShadowsCastingMode.None;
PreviewLight.Orientation = Quaternion.Euler(new Vector3(52.1477f, -109.109f, -111.739f));
//
EnvProbe = new EnvironmentProbe();
EnvProbe.AutoUpdate = false;
EnvProbe.CustomProbe = FlaxEngine.Content.LoadAsyncInternal(EditorAssets.DefaultSkyCubeTexture);
//
Sky = new Sky();
Sky.SunLight = PreviewLight;
Sky.SunPower = 9.0f;
//
SkyLight = new SkyLight();
SkyLight.Mode = SkyLight.Modes.CustomTexture;
SkyLight.Brightness = 2.1f;
SkyLight.CustomTexture = EnvProbe.CustomProbe;
//
PostFxVolume = new PostFxVolume();
PostFxVolume.IsBounded = false;
// Link actors for rendering
Task.ActorsSource = ActorsSources.CustomActors;
Task.AddCustomActor(PreviewLight);
Task.AddCustomActor(EnvProbe);
Task.AddCustomActor(Sky);
Task.AddCustomActor(SkyLight);
Task.AddCustomActor(PostFxVolume);
}
///
public override bool HasLoadedAssets => base.HasLoadedAssets && Sky.HasContentLoaded && EnvProbe.Probe.IsLoaded && PostFxVolume.HasContentLoaded;
///
public override void OnDestroy()
{
// Ensure to cleanup created actor objects
Object.Destroy(ref PreviewLight);
Object.Destroy(ref EnvProbe);
Object.Destroy(ref Sky);
Object.Destroy(ref SkyLight);
Object.Destroy(ref PostFxVolume);
base.OnDestroy();
}
}
}