浏览代码

Added a CommonSettings component to handle the choice of the SkyRenderer and later other global scene settings.

/main
Julien Ignace 8 年前
当前提交
799fb598
共有 17 个文件被更改,包括 306 次插入34 次删除
  1. 9
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs
  2. 1
      Assets/ScriptableRenderLoop/HDRenderLoop/Sky/HDRISky/HDRISkyParameters.cs
  3. 1
      Assets/ScriptableRenderLoop/HDRenderLoop/Sky/ProceduralSky/ProceduralSkyParameters.cs
  4. 40
      Assets/ScriptableRenderLoop/HDRenderLoop/Sky/SkyManager.cs
  5. 26
      Assets/ScriptableRenderLoop/HDRenderLoop/Sky/SkyParameters.cs
  6. 2
      Assets/ScriptableRenderLoop/HDRenderLoop/Sky/SkyParameters.cs.meta
  7. 12
      Assets/ScriptableRenderLoop/HDRenderLoop/Utilities.cs
  8. 27
      Assets/ScriptableRenderLoop/HDRenderLoop/Editor/HDRenderLoopMenuItems.cs
  9. 12
      Assets/ScriptableRenderLoop/HDRenderLoop/Editor/HDRenderLoopMenuItems.cs.meta
  10. 9
      Assets/ScriptableRenderLoop/HDRenderLoop/SceneSettings.meta
  11. 81
      Assets/ScriptableRenderLoop/HDRenderLoop/SceneSettings/CommonSettings.cs
  12. 12
      Assets/ScriptableRenderLoop/HDRenderLoop/SceneSettings/CommonSettings.cs.meta
  13. 9
      Assets/ScriptableRenderLoop/HDRenderLoop/SceneSettings/Editor.meta
  14. 87
      Assets/ScriptableRenderLoop/HDRenderLoop/SceneSettings/Editor/CommonSettingsEditor.cs
  15. 12
      Assets/ScriptableRenderLoop/HDRenderLoop/SceneSettings/Editor/CommonSettingsEditor.cs.meta

9
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs


#endif
SkyManager m_SkyManager = new SkyManager();
public SkyManager skyManager
{
get { return m_SkyManager; }
}
public SkyParameters skyParameters
public void InstantiateSkyRenderer(Type skyRendererType)
set { m_SkyManager.skyParameters = value; }
get { return m_SkyManager.skyParameters; }
m_SkyManager.InstantiateSkyRenderer(skyRendererType);
}
public class DebugParameters

1
Assets/ScriptableRenderLoop/HDRenderLoop/Sky/HDRISky/HDRISkyParameters.cs


namespace UnityEngine.Experimental.ScriptableRenderLoop
{
[DisallowMultipleComponent]
public class HDRISkyParameters
: SkyParameters
{

1
Assets/ScriptableRenderLoop/HDRenderLoop/Sky/ProceduralSky/ProceduralSkyParameters.cs


namespace UnityEngine.Experimental.ScriptableRenderLoop
{
[ExecuteInEditMode]
[DisallowMultipleComponent]
public class ProceduralSkyParameters
: SkyParameters
{

40
Assets/ScriptableRenderLoop/HDRenderLoop/Sky/SkyManager.cs


Mesh[] m_CubemapFaceMesh = new Mesh[6];
BuiltinSkyParameters m_BuiltinParameters = new BuiltinSkyParameters();
SkyRenderer m_Renderer = new HDRISkyRenderer();
SkyRenderer m_Renderer = null;
int m_SkyParametersHash = 0;
bool m_NeedUpdateEnvironment = false;

{
set
{
if (value == null || IsSkyParameterValid(value))
if(m_Renderer != null)
m_SkyParameters = value;
}
else
{
Debug.LogWarning("Sky renderer needs an instance of " + GetSkyParameterType().ToString() + " to be able to render.");
if (value == null || IsSkyParameterValid(value))
{
m_SkyParameters = value;
}
else
{
Debug.LogWarning("Sky renderer needs an instance of " + GetSkyParameterType().ToString() + " to be able to render.");
}
}
public void InstantiateSkyRenderer(Type skyRendererType)
{
if(skyRendererType == null)
{
m_Renderer = null;
}
else if (m_Renderer == null || m_Renderer.GetType() != skyRendererType)
{
m_Renderer = Activator.CreateInstance(skyRendererType) as SkyRenderer;
m_Renderer.Build();
}
}
protected Mesh BuildSkyMesh(Vector3 cameraPosition, Matrix4x4 cameraInvViewProjectionMatrix, bool forceUVBottom)

public void Build()
{
m_Renderer.Build();
if (m_Renderer != null)
m_Renderer.Build();
// TODO: We need to have an API to send our sky information to Enlighten. For now use a workaround through skybox/cubemap material...
m_StandardSkyboxMaterial = Utilities.CreateEngineMaterial("Skybox/Cubemap");

Utilities.Destroy(m_SkyboxCubemapRT);
Utilities.Destroy(m_SkyboxGGXCubemapRT);
m_Renderer.Cleanup();
if(m_Renderer != null)
m_Renderer.Cleanup();
}
public bool IsSkyValid()

public bool IsSkyParameterValid(SkyParameters parameters)
{
return m_Renderer.IsParameterValid(parameters);
return m_Renderer != null && m_Renderer.IsParameterValid(parameters);
return m_Renderer.GetSkyParameterType();
return (m_Renderer == null) ? null : m_Renderer.GetSkyParameterType();
}
public void RenderSky(Camera camera, Light sunLight, RenderTargetIdentifier colorBuffer, RenderTargetIdentifier depthBuffer, RenderLoop renderLoop)

26
Assets/ScriptableRenderLoop/HDRenderLoop/Sky/SkyParameters.cs


private FieldInfo[] m_Properties;
HDRenderLoop GetHDRenderLoop()
{
HDRenderLoop renderLoop = UnityEngine.Rendering.GraphicsSettings.GetRenderPipeline() as HDRenderLoop;
if (renderLoop == null)
{
Debug.LogWarning("SkyParameters component can only be used with HDRenderLoop custom RenderPipeline.");
return null;
}
return renderLoop;
}
HDRenderLoop renderLoop = GetHDRenderLoop();
HDRenderLoop renderLoop = Utilities.GetHDRenderLoop();
if (renderLoop == null)
{
return;

.GetFields(BindingFlags.Public | BindingFlags.Instance)
.ToArray();
if (renderLoop.skyParameters == null)
renderLoop.skyParameters = this;
else if (renderLoop.skyParameters != this)
if (renderLoop.skyManager.skyParameters == null || renderLoop.skyManager.skyParameters.GetType() != this.GetType()) // We allow override of parameters only if the type is different. It means that we changed the Sky Renderer and might need a new set of parameters.
renderLoop.skyManager.skyParameters = this;
else if (renderLoop.skyManager.skyParameters != this && renderLoop.skyManager.skyParameters.GetType() == this.GetType())
HDRenderLoop renderLoop = GetHDRenderLoop();
HDRenderLoop renderLoop = Utilities.GetHDRenderLoop();
if (renderLoop == null)
{
return;

if (renderLoop.skyParameters == this)
renderLoop.skyParameters = null;
if (renderLoop.skyManager.skyParameters == this)
renderLoop.skyManager.skyParameters = null;
}
public int GetHash()

2
Assets/ScriptableRenderLoop/HDRenderLoop/Sky/SkyParameters.cs.meta


fileFormatVersion: 2
guid: be80eb13382d90f4cb2738f69185b60c
timeCreated: 1481625888
timeCreated: 1481734433
licenseType: Pro
MonoImporter:
serializedVersion: 2

12
Assets/ScriptableRenderLoop/HDRenderLoop/Utilities.cs


else
m.DisableKeyword(keyword);
}
public static HDRenderLoop GetHDRenderLoop()
{
HDRenderLoop renderLoop = UnityEngine.Rendering.GraphicsSettings.GetRenderPipeline() as HDRenderLoop;
if (renderLoop == null)
{
Debug.LogWarning("SkyParameters component can only be used with HDRenderLoop custom RenderPipeline.");
return null;
}
return renderLoop;
}
}
}

27
Assets/ScriptableRenderLoop/HDRenderLoop/Editor/HDRenderLoopMenuItems.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace UnityEngine.Experimental.ScriptableRenderLoop
{
public class HDRenderLoopMenuItems
{
[UnityEditor.MenuItem("HDRenderLoop/Create Scene Settings")]
static void CreateSceneSettings()
{
CommonSettings[] settings = Object.FindObjectsOfType(typeof(CommonSettings)) as CommonSettings[];
if(settings.Length == 0)
{
GameObject go = new GameObject();
go.name = "SceneSettings";
go.AddComponent(typeof(CommonSettings));
}
else
{
Debug.LogWarning("SceneSettings has already been created.");
}
}
}
}

12
Assets/ScriptableRenderLoop/HDRenderLoop/Editor/HDRenderLoopMenuItems.cs.meta


fileFormatVersion: 2
guid: 19042aef0dcb65e4496d778558fefad9
timeCreated: 1481793704
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

9
Assets/ScriptableRenderLoop/HDRenderLoop/SceneSettings.meta


fileFormatVersion: 2
guid: 1b5e87e98ef1994498478f60a6dcdd65
folderAsset: yes
timeCreated: 1481725797
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

81
Assets/ScriptableRenderLoop/HDRenderLoop/SceneSettings/CommonSettings.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Linq;
using System.Reflection;
namespace UnityEngine.Experimental.ScriptableRenderLoop
{
[ExecuteInEditMode]
[DisallowMultipleComponent]
public class CommonSettings
: MonoBehaviour
{
[SerializeField]
private string m_SkyRendererTypeName = "";
public Type skyRendererType
{
set { m_SkyRendererTypeName = value != null ? value.FullName : ""; OnSkyRendererChanged(); }
get { return m_SkyRendererTypeName == "" ? null : Assembly.GetAssembly(typeof(CommonSettings)).GetType(m_SkyRendererTypeName); }
}
void OnEnable()
{
HDRenderLoop renderLoop = Utilities.GetHDRenderLoop();
if (renderLoop == null)
{
return;
}
OnSkyRendererChanged();
}
void OnDisable()
{
}
void OnSkyRendererChanged()
{
HDRenderLoop renderLoop = Utilities.GetHDRenderLoop();
if (renderLoop == null)
{
return;
}
renderLoop.InstantiateSkyRenderer(skyRendererType);
List<SkyParameters> result = new List<SkyParameters>();
gameObject.GetComponents<SkyParameters>(result);
Type skyParamType = renderLoop.skyManager.GetSkyParameterType();
// Disable all incompatible sky parameters and enable the compatible one
bool found = false;
foreach (SkyParameters param in result)
{
if (param.GetType() == skyParamType)
{
// This is a workaround the fact that we can't control the order in which components are initialized.
// So it can happen that a given SkyParameter is OnEnabled before the CommonSettings and so fail the setup because the SkyRenderer is not yet initialized.
// So we disable it to for OnEnable to be called again.
param.enabled = false;
param.enabled = true;
found = true;
}
else
{
param.enabled = false;
}
}
// If it does not exist, create the parameters
if (!found && skyParamType != null)
{
gameObject.AddComponent(skyParamType);
}
}
}
}

12
Assets/ScriptableRenderLoop/HDRenderLoop/SceneSettings/CommonSettings.cs.meta


fileFormatVersion: 2
guid: bc357c46587fc9d4cb8f311794d7d2f3
timeCreated: 1481734434
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

9
Assets/ScriptableRenderLoop/HDRenderLoop/SceneSettings/Editor.meta


fileFormatVersion: 2
guid: a1fd2e77167b0904e9c33020c13d1011
folderAsset: yes
timeCreated: 1481726125
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

87
Assets/ScriptableRenderLoop/HDRenderLoop/SceneSettings/Editor/CommonSettingsEditor.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using System.Linq;
using System.Reflection;
namespace UnityEngine.Experimental.ScriptableRenderLoop
{
[CustomEditor(typeof(CommonSettings))]
public class CommonSettingsEditor
: Editor
{
private class Styles
{
public readonly GUIContent none = new GUIContent("None");
public readonly GUIContent skyRenderer = new GUIContent("Sky Renderer");
}
private static Styles s_Styles = null;
private static Styles styles
{
get
{
if (s_Styles == null)
s_Styles = new Styles();
return s_Styles;
}
}
private List<Type> m_SkyRendererTypes;
private List<GUIContent> m_SkyRendererTypeNames = new List<GUIContent>();
private List<int> m_SkyRendererTypeValues = new List<int>();
void OnEnable()
{
m_SkyRendererTypes = Assembly.GetAssembly(typeof(SkyRenderer))
.GetTypes()
.Where(t => t.IsSubclassOf(typeof(SkyRenderer)) && !t.IsGenericType)
.ToList();
// Prepare the list of available SkyRenderers for the IntPopup
m_SkyRendererTypeNames.Clear();
m_SkyRendererTypeValues.Clear();
for(int i = 0 ; i < m_SkyRendererTypes.Count ; ++i)
{
string longName = m_SkyRendererTypes[i].ToString();
char[] separators = {'.'};
string[] tokens = longName.Split(separators);
m_SkyRendererTypeNames.Add(new GUIContent(tokens[tokens.Length - 1]));
m_SkyRendererTypeValues.Add(i);
}
// Add default null value.
m_SkyRendererTypeNames.Add(styles.none);
m_SkyRendererTypeValues.Add(m_SkyRendererTypeValues.Count);
m_SkyRendererTypes.Add(null);
}
public override void OnInspectorGUI()
{
serializedObject.Update();
CommonSettings settings = target as CommonSettings;
// Retrieve the index of the current SkyRenderer
int index = -1;
for(int i = 0 ; i < m_SkyRendererTypeValues.Count ; ++i )
{
if(m_SkyRendererTypes[i] == settings.skyRendererType)
{
index = i;
break;
}
}
EditorGUI.BeginChangeCheck();
int newValue = EditorGUILayout.IntPopup(styles.skyRenderer, index, m_SkyRendererTypeNames.ToArray(), m_SkyRendererTypeValues.ToArray());
if(EditorGUI.EndChangeCheck())
{
settings.skyRendererType = m_SkyRendererTypes[newValue];
}
serializedObject.ApplyModifiedProperties();
}
}
}

12
Assets/ScriptableRenderLoop/HDRenderLoop/SceneSettings/Editor/CommonSettingsEditor.cs.meta


fileFormatVersion: 2
guid: b020b2b47372e064bb3e3282b87df5d2
timeCreated: 1481726141
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存