浏览代码

HDRenderloop: Setup minimal draw sky/HDRI (not working)

/main
Sebastien Lagarde 8 年前
当前提交
d8f13193
共有 11 个文件被更改,包括 229 次插入4 次删除
  1. 22
      Assets/ScriptableRenderLoop/HDRenderLoop/Editor/HDRenderLoopInspector.cs
  2. 2
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.asset.meta
  3. 88
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs
  4. 2
      Assets/ScriptableRenderLoop/ShaderLibrary/API/D3D11.hlsl
  5. 21
      Assets/ScriptableRenderLoop/ShaderLibrary/CommonLighting.hlsl
  6. 9
      Assets/ScriptableRenderLoop/HDRenderLoop/Sky.meta
  7. 9
      Assets/ScriptableRenderLoop/HDRenderLoop/Sky/Resources.meta
  8. 71
      Assets/ScriptableRenderLoop/HDRenderLoop/Sky/Resources/SkyHDRI.shader
  9. 9
      Assets/ScriptableRenderLoop/HDRenderLoop/Sky/Resources/SkyHDRI.shader.meta

22
Assets/ScriptableRenderLoop/HDRenderLoop/Editor/HDRenderLoopInspector.cs


public bool isDebugViewMaterialInit = false;
public GUIContent[] debugViewMaterialStrings = null;
public int[] debugViewMaterialValues = null;
public readonly GUIContent skyParameters = new GUIContent("Sky Parameters");
public readonly GUIContent skyExposure = new GUIContent("Sky Exposure");
public readonly GUIContent skyRotation = new GUIContent("Sky Rotation");
public readonly GUIContent skyMultiplier = new GUIContent("Sky Multiplier");
}
private static Styles s_Styles = null;

debugParameters.useForwardRenderingOnly = EditorGUILayout.Toggle(styles.useForwardRenderingOnly, debugParameters.useForwardRenderingOnly);
debugParameters.useDepthPrepass = EditorGUILayout.Toggle(styles.useDepthPrepass, debugParameters.useDepthPrepass);
if (EditorGUI.EndChangeCheck())
{
EditorUtility.SetDirty(renderLoop); // Repaint
}
EditorGUI.indentLevel--;
var skyParameters = renderLoop.skyParameters;
EditorGUILayout.LabelField(styles.skyParameters);
EditorGUI.indentLevel++;
EditorGUI.BeginChangeCheck();
skyParameters.skyHDRI = (Cubemap)EditorGUILayout.ObjectField("Cubemap", skyParameters.skyHDRI, typeof(Cubemap), false);
skyParameters.exposure = Mathf.Max(Mathf.Min(EditorGUILayout.FloatField(styles.skyExposure, skyParameters.exposure), 32), -32);
skyParameters.multiplier = Mathf.Max(EditorGUILayout.FloatField(styles.skyMultiplier, skyParameters.multiplier), 0);
skyParameters.rotation = Mathf.Max(Mathf.Min(EditorGUILayout.FloatField(styles.skyRotation, skyParameters.rotation), 360), 0);
if (EditorGUI.EndChangeCheck())
{
EditorUtility.SetDirty(renderLoop); // Repaint

2
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.asset.meta


fileFormatVersion: 2
guid: 2400b74f5ce370c4481e5dc417d03703
timeCreated: 1479232893
timeCreated: 1479240578
licenseType: Pro
NativeFormatImporter:
userData:

88
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs


BakeDiffuseLighting = 11,
}
public class SkyParameters
{
public Cubemap skyHDRI;
public float rotation;
public float exposure;
public float multiplier;
}
[SerializeField]
private SkyParameters m_SkyParameters = new SkyParameters();
public SkyParameters skyParameters
{
get { return m_SkyParameters; }
}
public class DebugParameters
{
// Material Debugging

[SerializeField]
TextureSettings m_TextureSettings = TextureSettings.Default;
Material m_SkyboxMaterial;
Material m_SkyHDRIMaterial;
Material m_DeferredMaterial;
Material m_FinalPassMaterial;

s_envLightList = new ComputeBuffer(MaxLights, System.Runtime.InteropServices.Marshal.SizeOf(typeof(EnvLightData)));
s_punctualShadowList = new ComputeBuffer(MaxShadows, System.Runtime.InteropServices.Marshal.SizeOf(typeof(PunctualShadowData)));
// 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 = 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 = CreateEngineMaterial("Hidden/HDRenderLoop/SkyHDRI");
m_DeferredMaterial = CreateEngineMaterial("Hidden/HDRenderLoop/Deferred");
m_FinalPassMaterial = CreateEngineMaterial("Hidden/HDRenderLoop/FinalPass");

s_areaLightList.Release();
s_envLightList.Release();
s_punctualShadowList.Release();
if (m_SkyboxMaterial) DestroyImmediate(m_SkyboxMaterial);
if (m_SkyHDRIMaterial) DestroyImmediate(m_SkyHDRIMaterial);
if (m_DeferredMaterial) DestroyImmediate(m_DeferredMaterial);
if (m_FinalPassMaterial) DestroyImmediate(m_FinalPassMaterial);

cmd.Dispose();
}
void RenderSky(Camera camera, 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
Vector3[] vertData = new Vector3[4];
vertData[0] = new Vector3(-1.0f, -1.0f, 0.0f);
vertData[1] = new Vector3(1.0f, -1.0f, 0.0f);
vertData[2] = new Vector3(1.0f, 1.0f, 0.0f);
vertData[3] = new Vector3(-1.0f, 1.0f, 0.0f);
Vector3[] eyeVectorData = new Vector3[4];
// camera.worldToCameraMatrix, camera.projectionMatrix
// Get view vector vased on the frustrum, i.e (invert transform frustrum get position etc...)
eyeVectorData[0] =
eyeVectorData[1] =
eyeVectorData[2] =
eyeVectorData[3] =
// Write out the mesh
var triangles = new int[4];
for (int i = 0; i < 4; i++)
{
triangles[i] = i;
}
Mesh mesh = new Mesh
{
vertices = vertData,
normals = eyeVectorData,
triangles = triangles
};
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(mesh, Matrix4x4.identity, m_SkyHDRIMaterial);
renderloop.ExecuteCommandBuffer(cmd);
cmd.Dispose();
*/
}
void RenderForward(CullResults cullResults, Camera camera, RenderLoop renderLoop)
{
// Bind material data

RenderDeferredLighting(camera, renderLoop);
RenderForward(cullResults, camera, renderLoop);
RenderSky(camera, renderLoop);
RenderForward(cullResults, camera, renderLoop); // Note: We want to render forward opaque before RenderSky, then RenderTransparent - can only do that once we have material.SetPass feature...
RenderVelocity(cullResults, camera, renderLoop);
RenderVelocity(cullResults, camera, renderLoop); // Note we may have to render velocity earlier if we do temporalAO, temporal volumetric etc... Mean we will not take into account forward opaque in case of deferred rendering ?
// TODO: Check with VFX team.
// Rendering distortion here have off course lot of artifact.

2
Assets/ScriptableRenderLoop/ShaderLibrary/API/D3D11.hlsl


#define UNITY_UV_STARTS_AT_TOP 1
#define UNITY_REVERSED_Z 1
#define UNITY_NEAR_CLIP_VALUE (1.0)
// This value will not go through any matrix projection convertion
#define UNITY_RAW_FAR_CLIP_VALUE (0.0)
#define FRONT_FACE_SEMATIC SV_IsFrontFace
#define FRONT_FACE_TYPE bool
#define IS_FRONT_VFACE(VAL, FRONT, BACK) ((VAL) ? (FRONT) : (BACK))

21
Assets/ScriptableRenderLoop/ShaderLibrary/CommonLighting.hlsl


#ifndef UNITY_COMMON_LIGHTING_INCLUDED
#define UNITY_COMMON_LIGHTING_INCLUDED
// These clamping function to max of floating point 16 bit are use to prevent INF in code in case of extreme value
float ClampToFloat16Max(float value)
{
return min(value, 65504.0);
}
float2 ClampToFloat16Max(float2 value)
{
return min(value, 65504.0);
}
float3 ClampToFloat16Max(float3 value)
{
return min(value, 65504.0);
}
float4 ClampToFloat16Max(float4 value)
{
return min(value, 65504.0);
}
// Ligthing convention
// Light direction is oriented backward (-Z). i.e in shader code, light direction is -lightData.forward

9
Assets/ScriptableRenderLoop/HDRenderLoop/Sky.meta


fileFormatVersion: 2
guid: d7469c05ebee66f4886264af0ab8bf2a
folderAsset: yes
timeCreated: 1479239906
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

9
Assets/ScriptableRenderLoop/HDRenderLoop/Sky/Resources.meta


fileFormatVersion: 2
guid: 4cdc45a9a65126a42aca158413a5b089
folderAsset: yes
timeCreated: 1479239906
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

71
Assets/ScriptableRenderLoop/HDRenderLoop/Sky/Resources/SkyHDRI.shader


Shader "Hidden/HDRenderLoop/SkyHDRI"
{
SubShader
{
Pass
{
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha // We will lerp only the values that are valid
HLSLPROGRAM
#pragma target 5.0
#pragma only_renderers d3d11 // TEMP: unitl we go futher in dev
#pragma vertex Vert
#pragma fragment Frag
#include "Common.hlsl"
TEXTURECUBE(_Cubemap);
SAMPLERCUBE(sampler_Cubemap);
float4 _SkyParam; // x exposure, y multiplier, z rotation
struct Attributes
{
float3 positionCS : POSITION;
float3 eyeVector : NORMAL;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float3 eyeVector : TEXCOORD0;
};
Varyings Vert(Attributes input)
{
// TODO: implement SV_vertexID full screen quad
Varyings output;
output.positionCS = float4(input.positionCS.xy, UNITY_RAW_FAR_CLIP_VALUE
#if UNITY_REVERSED_Z
+ 0.000001
#else
- 0.000001
#endif
, 1.0);
output.eyeVector = input.eyeVector;
return output;
}
float4 Frag(Varyings input) : SV_Target
{
float3 dir = normalize(input.eyeVector);
// Rotate direction
float phi = _SkyParam.z * PI / 180.0; // Convert to radiant
float cosPhi, sinPhi;
sincos(phi, cosPhi, sinPhi);
float3 rotDirX = float3(cosPhi, 0, sinPhi);
float3 rotDirY = float3(sinPhi, 0, -cosPhi);
dir = float3(dot(rotDirX, dir), dir.y, dot(rotDirY, dir));
return ClampToFloat16Max(SAMPLE_TEXTURECUBE_LOD(_Cubemap, sampler_Cubemap, dir, 0) * exp2(_SkyParam.x) * _SkyParam.y);
}
ENDHLSL
}
}
Fallback Off
}

9
Assets/ScriptableRenderLoop/HDRenderLoop/Sky/Resources/SkyHDRI.shader.meta


fileFormatVersion: 2
guid: 0e7a148852dbc8f47a32e829517574ff
timeCreated: 1479239909
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存