浏览代码
- First version of sky renderer
- First version of sky renderer
- Added an Utilities class for re-usable methods in the renderloop (SetRenderTarget, Material creation, etc...)/main
Julien Ignace
8 年前
当前提交
fd04e67f
共有 8 个文件被更改,包括 326 次插入 和 138 次删除
-
2Assets/ScriptableRenderLoop/HDRenderLoop/Editor/HDRenderLoopInspector.cs
-
167Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs
-
16Assets/ScriptableRenderLoop/HDRenderLoop/Sky/Resources/SkyHDRI.shader
-
10Assets/ScriptableRenderLoop/ShaderLibrary/Common.hlsl
-
135Assets/ScriptableRenderLoop/HDRenderLoop/Sky/SkyRenderer.cs
-
12Assets/ScriptableRenderLoop/HDRenderLoop/Sky/SkyRenderer.cs.meta
-
110Assets/ScriptableRenderLoop/HDRenderLoop/Utilities.cs
-
12Assets/ScriptableRenderLoop/HDRenderLoop/Utilities.cs.meta
|
|||
using UnityEngine.Rendering; |
|||
using UnityEngine.Experimental.Rendering; |
|||
using System.Collections.Generic; |
|||
using System; |
|||
|
|||
namespace UnityEngine.Experimental.ScriptableRenderLoop |
|||
{ |
|||
[Serializable] |
|||
public class SkyParameters |
|||
{ |
|||
public Cubemap skyHDRI; |
|||
public float rotation = 0.0f; |
|||
public float exposure = 0.0f; |
|||
public float multiplier = 1.0f; |
|||
} |
|||
|
|||
public class SkyRenderer |
|||
{ |
|||
const int kSkyCubemapSize = 128; |
|||
|
|||
RenderTexture m_SkyboxCubemap = null; |
|||
|
|||
Material m_SkyboxMaterial = null; |
|||
Material m_SkyHDRIMaterial = null; |
|||
|
|||
Mesh BuildSkyMesh(Camera camera) |
|||
{ |
|||
Vector4 vertData0 = new Vector4(-1.0f, -1.0f, 1.0f, 1.0f); |
|||
Vector4 vertData1 = new Vector4(1.0f, -1.0f, 1.0f, 1.0f); |
|||
Vector4 vertData2 = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); |
|||
Vector4 vertData3 = new Vector4(-1.0f, 1.0f, 1.0f, 1.0f); |
|||
|
|||
Vector3[] vertData = new Vector3[4]; |
|||
vertData[0] = new Vector3(vertData0.x, vertData0.y, vertData0.z); |
|||
vertData[1] = new Vector3(vertData1.x, vertData1.y, vertData1.z); |
|||
vertData[2] = new Vector3(vertData2.x, vertData2.y, vertData2.z); |
|||
vertData[3] = new Vector3(vertData3.x, vertData3.y, vertData3.z); |
|||
|
|||
// Get view vector vased on the frustrum, i.e (invert transform frustrum get position etc...)
|
|||
Vector3[] eyeVectorData = new Vector3[4]; |
|||
|
|||
Matrix4x4 transformMatrix = camera.cameraToWorldMatrix * camera.projectionMatrix.inverse; |
|||
|
|||
Vector4 posWorldSpace0 = transformMatrix * vertData0; |
|||
Vector4 posWorldSpace1 = transformMatrix * vertData1; |
|||
Vector4 posWorldSpace2 = transformMatrix * vertData2; |
|||
Vector4 posWorldSpace3 = transformMatrix * vertData3; |
|||
|
|||
Vector3 temp = camera.GetComponent<Transform>().position; |
|||
Vector4 cameraPosition = new Vector4(temp.x, temp.y, temp.z, 0.0f); |
|||
|
|||
Vector4 direction0 = (posWorldSpace0 / posWorldSpace0.w - cameraPosition); |
|||
Vector4 direction1 = (posWorldSpace1 / posWorldSpace1.w - cameraPosition); |
|||
Vector4 direction2 = (posWorldSpace2 / posWorldSpace2.w - cameraPosition); |
|||
Vector4 direction3 = (posWorldSpace3 / posWorldSpace3.w - cameraPosition); |
|||
|
|||
if (SystemInfo.graphicsUVStartsAtTop) |
|||
{ |
|||
eyeVectorData[3] = new Vector3(direction0.x, direction0.y, direction0.z).normalized; |
|||
eyeVectorData[2] = new Vector3(direction1.x, direction1.y, direction1.z).normalized; |
|||
eyeVectorData[1] = new Vector3(direction2.x, direction2.y, direction2.z).normalized; |
|||
eyeVectorData[0] = new Vector3(direction3.x, direction3.y, direction3.z).normalized; |
|||
} |
|||
else |
|||
{ |
|||
eyeVectorData[0] = new Vector3(direction0.x, direction0.y, direction0.z).normalized; |
|||
eyeVectorData[1] = new Vector3(direction1.x, direction1.y, direction1.z).normalized; |
|||
eyeVectorData[2] = new Vector3(direction2.x, direction2.y, direction2.z).normalized; |
|||
eyeVectorData[3] = new Vector3(direction3.x, direction3.y, direction3.z).normalized; |
|||
} |
|||
|
|||
// Write out the mesh
|
|||
var triangles = new int[6] { 0, 1, 2, 2, 3, 0 }; |
|||
|
|||
return new Mesh |
|||
{ |
|||
vertices = vertData, |
|||
normals = eyeVectorData, |
|||
triangles = triangles |
|||
}; |
|||
} |
|||
|
|||
public void Rebuild() |
|||
{ |
|||
// TODO: We need to have an API to send our sky information to Enlighten. For now use a workaround through skybox/cubemap material...
|
|||
m_SkyboxMaterial = Utilities.CreateEngineMaterial("Skybox/Cubemap"); |
|||
RenderSettings.skybox = m_SkyboxMaterial; // Setup this material as the default to be use in RenderSettings
|
|||
RenderSettings.ambientIntensity = 1.0f; // fix this to 1, this parameter should not exist!
|
|||
RenderSettings.ambientMode = UnityEngine.Rendering.AmbientMode.Skybox; // Force skybox for our HDRI
|
|||
RenderSettings.reflectionIntensity = 1.0f; |
|||
|
|||
m_SkyHDRIMaterial = Utilities.CreateEngineMaterial("Hidden/HDRenderLoop/SkyHDRI"); |
|||
|
|||
m_SkyboxCubemap = new RenderTexture(kSkyCubemapSize, kSkyCubemapSize, 1, RenderTextureFormat.ARGBHalf); |
|||
m_SkyboxCubemap.dimension = TextureDimension.Cube; |
|||
m_SkyboxCubemap.Create(); |
|||
} |
|||
|
|||
public void OnDisable() |
|||
{ |
|||
Utilities.Destroy(m_SkyboxMaterial); |
|||
Utilities.Destroy(m_SkyHDRIMaterial); |
|||
|
|||
//m_SkyboxCubemap.Release();
|
|||
Utilities.Destroy(m_SkyboxCubemap); |
|||
|
|||
} |
|||
|
|||
public void RenderSky(Camera camera, SkyParameters skyParameters, RenderTargetIdentifier colorBuffer, RenderTargetIdentifier depthBuffer, RenderLoop renderLoop) |
|||
{ |
|||
// Render sky into a cubemap - doesn't happen every frame, can be control
|
|||
|
|||
// TODO: do a render to texture here
|
|||
|
|||
// Downsample the cubemap and provide it to Enlighten
|
|||
|
|||
// TODO: currently workaround is to set the cubemap in a Skybox/cubemap material
|
|||
//m_SkyboxMaterial.SetTexture(cubemap);
|
|||
|
|||
// Render the sky itself
|
|||
|
|||
Utilities.SetRenderTarget(renderLoop, colorBuffer, depthBuffer, "Sky Pass"); |
|||
|
|||
Mesh skyMesh = BuildSkyMesh(camera); |
|||
|
|||
m_SkyHDRIMaterial.SetTexture("_Cubemap", skyParameters.skyHDRI); |
|||
m_SkyHDRIMaterial.SetVector("_SkyParam", new Vector4(skyParameters.exposure, skyParameters.multiplier, skyParameters.rotation, 0.0f)); |
|||
|
|||
var cmd = new CommandBuffer { name = "Skybox" }; |
|||
cmd.DrawMesh(skyMesh, Matrix4x4.identity, m_SkyHDRIMaterial); |
|||
renderLoop.ExecuteCommandBuffer(cmd); |
|||
cmd.Dispose(); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: cf8a015aab8d3b643aa3ef3816f85447 |
|||
timeCreated: 1479314393 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
|
|||
using UnityEngine; |
|||
using UnityEngine.Rendering; |
|||
using UnityEngine.Experimental.Rendering; |
|||
|
|||
using UnityObject = UnityEngine.Object; |
|||
|
|||
namespace UnityEngine.Experimental.ScriptableRenderLoop |
|||
{ |
|||
[Flags] |
|||
public enum ClearFlag |
|||
{ |
|||
ClearNone = 0, |
|||
ClearColor = 1, |
|||
ClearDepth = 2 |
|||
} |
|||
|
|||
public class Utilities |
|||
{ |
|||
public const RendererConfiguration kRendererConfigurationBakedLighting = RendererConfiguration.PerObjectLightProbe | RendererConfiguration.PerObjectReflectionProbes | RendererConfiguration.PerObjectLightmaps | RendererConfiguration.PerObjectLightProbeProxyVolume; |
|||
|
|||
|
|||
// Render Target Management.
|
|||
public const ClearFlag kClearAll = ClearFlag.ClearDepth | ClearFlag.ClearColor; |
|||
|
|||
public static void SetRenderTarget(RenderLoop renderLoop, RenderTargetIdentifier depthBuffer, string name = "") |
|||
{ |
|||
var cmd = new CommandBuffer(); |
|||
cmd.name = name; |
|||
cmd.SetRenderTarget(depthBuffer); |
|||
renderLoop.ExecuteCommandBuffer(cmd); |
|||
cmd.Dispose(); |
|||
} |
|||
|
|||
public static void SetRenderTarget(RenderLoop renderLoop, RenderTargetIdentifier colorBuffer, RenderTargetIdentifier depthBuffer, string name = "") |
|||
{ |
|||
//RenderTargetIdentifier[] colorBuffers = { colorBuffer };
|
|||
SetRenderTarget(renderLoop, colorBuffer, depthBuffer, ClearFlag.ClearNone, new Color(0.0f, 0.0f, 0.0f, 0.0f), name); |
|||
} |
|||
|
|||
public static void SetRenderTarget(RenderLoop renderLoop, RenderTargetIdentifier colorBuffer, RenderTargetIdentifier depthBuffer, ClearFlag clearFlag, string name = "") |
|||
{ |
|||
//RenderTargetIdentifier[] colorBuffers = { colorBuffer };
|
|||
SetRenderTarget(renderLoop, colorBuffer, depthBuffer, clearFlag, new Color(0.0f, 0.0f, 0.0f, 0.0f), name); |
|||
} |
|||
|
|||
public static void SetRenderTarget(RenderLoop renderLoop, RenderTargetIdentifier colorBuffer, RenderTargetIdentifier depthBuffer, ClearFlag clearFlag, Color clearColor, string name = "") |
|||
{ |
|||
var cmd = new CommandBuffer(); |
|||
cmd.name = name; |
|||
cmd.SetRenderTarget(colorBuffer, depthBuffer); |
|||
if (clearFlag != ClearFlag.ClearNone) |
|||
cmd.ClearRenderTarget((clearFlag & ClearFlag.ClearDepth) != 0, (clearFlag & ClearFlag.ClearColor) != 0, clearColor); |
|||
renderLoop.ExecuteCommandBuffer(cmd); |
|||
cmd.Dispose(); |
|||
|
|||
//RenderTargetIdentifier[] colorBuffers = { colorBuffer };
|
|||
//SetRenderTarget(renderLoop, colorBuffers, depthBuffer, clearFlag, clearColor, name);
|
|||
} |
|||
|
|||
public static void SetRenderTarget(RenderLoop renderLoop, RenderTargetIdentifier[] colorBuffers, RenderTargetIdentifier depthBuffer, string name = "") |
|||
{ |
|||
SetRenderTarget(renderLoop, colorBuffers, depthBuffer, ClearFlag.ClearNone, Color.black, name); |
|||
} |
|||
|
|||
public static void SetRenderTarget(RenderLoop renderLoop, RenderTargetIdentifier[] colorBuffers, RenderTargetIdentifier depthBuffer, ClearFlag clearFlag = ClearFlag.ClearNone, string name = "") |
|||
{ |
|||
SetRenderTarget(renderLoop, colorBuffers, depthBuffer, clearFlag, Color.black, name); |
|||
} |
|||
|
|||
public static void SetRenderTarget(RenderLoop renderLoop, RenderTargetIdentifier[] colorBuffers, RenderTargetIdentifier depthBuffer, ClearFlag clearFlag, Color clearColor, string name = "" ) |
|||
{ |
|||
var cmd = new CommandBuffer(); |
|||
cmd.name = name; |
|||
cmd.SetRenderTarget(colorBuffers, depthBuffer); |
|||
if (clearFlag != ClearFlag.ClearNone) |
|||
cmd.ClearRenderTarget((clearFlag & ClearFlag.ClearDepth) != 0, (clearFlag & ClearFlag.ClearColor) != 0, clearColor); |
|||
renderLoop.ExecuteCommandBuffer(cmd); |
|||
cmd.Dispose(); |
|||
} |
|||
|
|||
// Miscellanous
|
|||
public static Material CreateEngineMaterial(string shaderPath) |
|||
{ |
|||
var mat = new Material(Shader.Find(shaderPath)) |
|||
{ |
|||
hideFlags = HideFlags.HideAndDontSave |
|||
}; |
|||
return mat; |
|||
} |
|||
|
|||
public static void Destroy(UnityObject obj) |
|||
{ |
|||
if (obj != null) |
|||
{ |
|||
#if UNITY_EDITOR
|
|||
if (Application.isPlaying) |
|||
UnityObject.Destroy(obj); |
|||
else |
|||
UnityObject.DestroyImmediate(obj); |
|||
#else
|
|||
UnityObject.Destroy(obj); |
|||
#endif
|
|||
} |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: b91b03c2b2270d648a49e0fb6880ca3f |
|||
timeCreated: 1479315130 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue