该项目的目的是同时测试和演示来自 Unity DOTS 技术堆栈的多个新包。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

362 行
15 KiB

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.HighDefinition;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;
using Unity.DebugDisplay;
using Unity.Sample.Core;
public static class RenderSettings
{
#pragma warning disable 649
[ConfigVar(Name = "show.quality", DefaultValue = "0", Description = "Show quality setting debug overlay")]
static ConfigVar showQuality;
[ConfigVar(Name = "r.quality", DefaultValue = "Ultra", Description = "Overall rendering quality", Flags = ConfigVar.Flags.Save)]
public static ConfigVar rQuality;
[ConfigVar(Name = "r.vsync", DefaultValue = "1", Description = "Number of v-blanks to wait for each frame. 0 means no sync", Flags = ConfigVar.Flags.Save)]
public static ConfigVar rVSync;
[ConfigVar(Name = "r.fullscreen", DefaultValue = "3", Description = "Full screen mode (0: exclusive, 1: full, 3: windowed)", Flags = ConfigVar.Flags.Save)]
public static ConfigVar rFullscreen;
[ConfigVar(Name = "r.aamode", DefaultValue = "fxaa", Description = "AA mode: off, fxaa, smaa, taa", Flags = ConfigVar.Flags.Save)]
public static ConfigVar rAAMode;
[ConfigVar(Name = "r.aaquality", DefaultValue = "high", Description = "AA quality: low, med, high", Flags = ConfigVar.Flags.Save)]
public static ConfigVar rAAQuality;
[ConfigVar(Name = "r.bloom", DefaultValue = "1", Description = "Enable bloom", Flags = ConfigVar.Flags.Save)]
public static ConfigVar rBloom;
[ConfigVar(Name = "r.motionblur", DefaultValue = "1", Description = "Enable motion blur", Flags = ConfigVar.Flags.Save)]
public static ConfigVar rMotionBlur;
[ConfigVar(Name = "r.ssao", DefaultValue = "1", Description = "Enable ssao", Flags = ConfigVar.Flags.Save)]
public static ConfigVar rSSAO;
[ConfigVar(Name = "r.grain", DefaultValue = "1", Description = "Enable grain", Flags = ConfigVar.Flags.Save)]
public static ConfigVar rGrain;
[ConfigVar(Name = "r.ssr", DefaultValue = "1", Description = "Enable screen space reflections", Flags = ConfigVar.Flags.Save)]
public static ConfigVar rSSR;
[ConfigVar(Name = "r.sss", DefaultValue = "1", Description = "Enable subsurface scattering", Flags = ConfigVar.Flags.Save)]
public static ConfigVar rSSS;
[ConfigVar(Name = "r.roughrefraction", DefaultValue = "1", Description = "Enable rough refraction", Flags = ConfigVar.Flags.Save)]
public static ConfigVar rRoughRefraction;
[ConfigVar(Name = "r.distortion", DefaultValue = "1", Description = "Enable distortion", Flags = ConfigVar.Flags.Save)]
public static ConfigVar rDistortion;
[ConfigVar(Name = "r.shadowdistmult", DefaultValue = "1.0", Description = "Shadow distance multiplier", Flags = ConfigVar.Flags.Save)]
public static ConfigVar rShadowDistMult;
[ConfigVar(Name = "r.decaldist", DefaultValue = "200", Description = "Decal draw distance", Flags = ConfigVar.Flags.Save)]
public static ConfigVar rDecalDist;
[ConfigVar(Name = "r.gamma", DefaultValue = "1", Description = "User gamma correction", Flags = ConfigVar.Flags.Save)]
public static ConfigVar rGamma;
[ConfigVar(Name = "r.resolution", DefaultValue = "", Description = "Screen resolution", Flags = ConfigVar.Flags.Save)]
public static ConfigVar rResolution;
[ConfigVar(Name = "r.latesync", DefaultValue = "1", Description = "Sync with render thread late", Flags = ConfigVar.Flags.None)]
public static ConfigVar rLateSync;
[ConfigVar(Name = "r.occlusionthreshold", DefaultValue = "50", Description = "Occlusion threshold", Flags = ConfigVar.Flags.None)]
public static ConfigVar rOcclusionThreshold;
#pragma warning restore 649
public static void Init()
{
Console.AddCommand("r_resolution", CmdResolution, "Display or set resolution, e.g. 1280x720");
Console.AddCommand("r_quality", CmdQuality, "Set the render quality");
Console.AddCommand("r_maxqueue", CmdMaxQueue, "Max queued frames");
Console.AddCommand("r_srpbatching", CmdSrpBatching, "Use 0 or 1 to disable or enable SRP batching");
// Default to reasonable window
if (rResolution.Value == "")
rResolution.Value = "1280x720@" + Screen.currentResolution.refreshRate;
currentQualityIdx = default;
currentResX = default;
currentResY = default;
currentResRate = default;
}
/*
* TODO:
* SSS Sample count
* MSAA Sample count
* */
static int currentResX, currentResY, currentResRate;
static int currentQualityIdx;
public static void Update()
{
if (rLateSync.ChangeCheck())
{
GraphicsDeviceSettings.waitForPresentSyncPoint = rLateSync.IntValue > 0 ? WaitForPresentSyncPoint.EndFrame : WaitForPresentSyncPoint.BeginFrame;
}
if (rOcclusionThreshold.ChangeCheck())
{
//HDRenderPipeline.s_OcclusionThreshold = rOcclusionThreshold.FloatValue;
var hdpipe = RenderPipelineManager.currentPipeline as HDRenderPipeline;
if (hdpipe != null)
{
var ty = hdpipe.GetType();
var prop = ty.GetField("s_OcclusionThreshold");
if (prop != null)
prop.SetValue(null, rOcclusionThreshold.FloatValue);
}
}
bool updateAAFlags = false;
bool updateFrameSettings = false;
if (rResolution.ChangeCheck())
CmdResolution(new string[] { rResolution.Value });
else
{
if (currentResX != Screen.width || currentResY != Screen.height || currentResRate != Screen.currentResolution.refreshRate)
{
currentResX = Screen.width;
currentResY = Screen.height;
currentResRate = Screen.currentResolution.refreshRate;
rResolution.Value = currentResX + "x" + currentResY + "@" + currentResRate;
}
}
if (rQuality.ChangeCheck())
{
var names = QualitySettings.names;
bool set = false;
for (int i = 0; i < names.Length; i++)
{
if (rQuality.Value == names[i])
{
QualitySettings.SetQualityLevel(i);
set = true;
break;
}
}
if (!set)
{
rQuality.Value = QualitySettings.names[QualitySettings.GetQualityLevel()];
Console.Write("Unknown quality setting. Reverting to " + rQuality.Value);
}
}
else
{
var currentIdx = QualitySettings.GetQualityLevel();
if (currentQualityIdx != currentIdx)
{
currentQualityIdx = currentIdx;
rQuality.Value = QualitySettings.names[currentIdx];
}
}
if (showQuality.IntValue > 0)
DrawQualityOverlay();
if (rVSync.ChangeCheck())
QualitySettings.vSyncCount = rVSync.IntValue;
if (rFullscreen.ChangeCheck())
Screen.fullScreenMode = (FullScreenMode)rFullscreen.IntValue;
// if (rNewPrepareLights.ChangeCheck())
// LightLoop.useNewPrepareLights = (rNewPrepareLights.IntValue != 0);
if (rAAMode.ChangeCheck())
updateAAFlags = true;
if (rAAQuality.ChangeCheck())
updateAAFlags = true;
// Post effect flags
// Disabled when we removed the changes to HDRP
//if (rBloom.ChangeCheck())
// Bloom.globalEnable = rBloom.IntValue > 0;
if (rMotionBlur.ChangeCheck())
{
// Disabled when we removed the changes to HDRP
// MotionBlur.globalEnable = rMotionBlur.IntValue > 0;
updateFrameSettings = true;
}
if (rSSAO.ChangeCheck())
{
// Disabled when we removed the changes to HDRP
//AmbientOcclusion.globalEnable = rSSAO.IntValue > 0;
updateFrameSettings = true;
}
// Disabled when we removed the changes to HDRP
//if (rGrain.ChangeCheck())
// FilmGrain.globalEnable = rGrain.IntValue > 0;
if (rSSR.ChangeCheck())
{
// Disabled when we removed the changes to HDRP
//ScreenSpaceReflection.globalEnable = rSSR.IntValue > 0;
updateFrameSettings = true;
}
if (rSSS.ChangeCheck())
updateFrameSettings = true;
if (rRoughRefraction.ChangeCheck())
updateFrameSettings = true;
if (rDistortion.ChangeCheck())
updateFrameSettings = true;
// Disabled when we removed the changes to HDRP
//if (rShadowDistMult.ChangeCheck())
// HDShadowSettings.shadowDistanceMultiplier = Mathf.Clamp(rShadowDistMult.FloatValue, 0.5f, 4.0f);
// Disabled when we removed the changes to HDRP
//if (rDecalDist.ChangeCheck())
//{
// var hdasset = GraphicsSettings.renderPipelineAsset as HDRenderPipelineAsset;
// hdasset.m_RenderPipelineSettings.decalSettings.drawDistance = rDecalDist.IntValue;
//}
// Disabled when we removed the changes to HDRP
//if (rGamma.ChangeCheck())
// ColorGrading.globalGamma = Mathf.Clamp(rGamma.FloatValue, 0.1f, 5.0f);
if (updateAAFlags)
UpdateAAFlags(GameApp.CameraStack.TopCamera());
if (updateFrameSettings)
UpdateFrameSettings(GameApp.CameraStack.TopCamera());
}
internal static void UpdateCameraSettings(Camera cam)
{
UpdateAAFlags(cam);
UpdateFrameSettings(cam);
}
static void DrawQualityOverlay()
{
Overlay.Managed.Write(2, 2, "Quality Settings");
Overlay.Managed.Write(2, 4, "Current camera: {0}", GameApp.CameraStack.TopCamera() != null ? GameApp.CameraStack.TopCamera().name : "(null)");
//Overlay.Managed.Write(2, 5, "Post volume: {0}", "");// Game.game.TopCamera() != null ? Game.game.TopCamera().name : "(null)");
Overlay.Managed.Write(2, 7, "r.aamode: {0}", rAAMode.Value);
Overlay.Managed.Write(2, 8, "r.aaquality: {0}", rAAQuality.Value);
Overlay.Managed.Write(2, 9, "r.bloom: {0}", rBloom.IntValue);
Overlay.Managed.Write(2, 10, "r.motionblur: {0}", rMotionBlur.IntValue);
Overlay.Managed.Write(2, 11, "r.ssao: {0}", rSSAO.IntValue);
Overlay.Managed.Write(2, 12, "r.grain: {0}", rGrain.IntValue);
Overlay.Managed.Write(2, 13, "r.ssr: {0}", rSSR.IntValue);
Overlay.Managed.Write(2, 15, "r.sss: {0}", rSSS.IntValue);
Overlay.Managed.Write(2, 16, "r.roughrefraction: {0}", rRoughRefraction.IntValue);
Overlay.Managed.Write(2, 17, "r.distortion: {0}", rDistortion.IntValue);
Overlay.Managed.Write(2, 19, "r.decaldist: {0}", rDecalDist.IntValue);
Overlay.Managed.Write(2, 20, "r.shadowdistmult: {0}", rShadowDistMult.FloatValue);
}
static void UpdateFrameSettings(Camera c)
{
var hdCam = c.GetComponent<HDAdditionalCameraData>();
if (hdCam == null)
return;
hdCam.renderingPathCustomFrameSettings.SetEnabled(FrameSettingsField.SubsurfaceScattering, rSSS.IntValue > 0);
hdCam.renderingPathCustomFrameSettings.SetEnabled(FrameSettingsField.MotionVectors, rMotionBlur.IntValue > 0);
hdCam.renderingPathCustomFrameSettings.SetEnabled(FrameSettingsField.ObjectMotionVectors, rMotionBlur.IntValue > 0);
hdCam.renderingPathCustomFrameSettings.SetEnabled(FrameSettingsField.SSAO, rSSAO.IntValue > 0);
hdCam.renderingPathCustomFrameSettings.SetEnabled(FrameSettingsField.SSR, rSSR.IntValue > 0);
hdCam.renderingPathCustomFrameSettings.SetEnabled(FrameSettingsField.RoughRefraction, rRoughRefraction.IntValue > 0);
hdCam.renderingPathCustomFrameSettings.SetEnabled(FrameSettingsField.Distortion, rDistortion.IntValue > 0);
}
static void UpdateAAFlags(Camera c)
{
if (c == null)
return;
var hdCam = c.GetComponent<HDAdditionalCameraData>();
if (hdCam == null)
return;
if (rAAMode.Value == "off")
hdCam.antialiasing = HDAdditionalCameraData.AntialiasingMode.None;
else if (rAAMode.Value == "fxaa")
hdCam.antialiasing = HDAdditionalCameraData.AntialiasingMode.FastApproximateAntialiasing;
else if (rAAMode.Value == "taa")
hdCam.antialiasing = HDAdditionalCameraData.AntialiasingMode.TemporalAntialiasing;
else
GameDebug.Log("Unknown aa mode: " + rAAMode.Value);
}
static void CmdResolution(string[] arguments)
{
var resolutions = Screen.resolutions;
if (arguments.Length > 0)
{
var wantedResolution = arguments[0];
int[] res = new int[3];
int a = 0;
for (int i = 0; i < wantedResolution.Length; i++)
{
var c = wantedResolution[i];
if (c >= '0' && c <= '9')
res[a] = res[a] * 10 + (c - '0');
else if ((c == 'x' || c == '@') && a < 2)
a++;
else
break;
}
int width = res[0];
int height = res[1];
int refresh = res[2] > 0 ? res[2] : Screen.currentResolution.refreshRate;
if (width > 100 && height > 100)
{
Screen.SetResolution(width, height, Screen.fullScreenMode, refresh);
return;
}
else
{
Console.Write("Invalid resolution. Use <width>x<height>[@<refresh>] with width and height > 100");
}
}
Console.Write("Resolutions supported by monitor:");
for (var i = 0; i < resolutions.Length; i++)
{
var r = resolutions[i];
Console.Write(r.width + "x" + r.height + "@" + r.refreshRate);
}
Console.Write("Fullscreen-mode: " + (int)Screen.fullScreenMode + "(" + Screen.fullScreenMode.ToString() + ")");
Console.Write("Current window resolution: " + Screen.width + "x" + Screen.height);
Console.Write("Current screen resolution: " + Screen.currentResolution.width + "x" + Screen.currentResolution.height + "@" + Screen.currentResolution.refreshRate);
}
static void CmdQuality(string[] arguments)
{
var names = QualitySettings.names;
Console.Write("Current setting: " + names[QualitySettings.GetQualityLevel()]);
Console.Write("Known settings:");
for (var i = 0; i < names.Length; i++)
{
Console.Write(" " + names[i]);
}
}
static void CmdMaxQueue(string[] arguments)
{
if (arguments.Length != 1)
{
Console.Write("Max queued frames: " + QualitySettings.maxQueuedFrames);
return;
}
QualitySettings.maxQueuedFrames = int.Parse(arguments[0]);
}
static void CmdSrpBatching(string[] args)
{
GraphicsSettings.useScriptableRenderPipelineBatching = (args.Length == 1 && args[0] == "1");
Console.Write("SrpBatching " + (GraphicsSettings.useScriptableRenderPipelineBatching ? "on" : "off"));
}
}