浏览代码

Update ShaderLibrary and UnityStandard code

Several addition.
Add a forward and deferred pass not working with lighting
Add pack/unpack attribute
Reoarganize some file
better header dependecy
Remove platform, will not be used
/main
sebastienlagarde 8 年前
当前提交
940b6d13
共有 26 个文件被更改,包括 604 次插入422 次删除
  1. 17
      Assets/ScriptableRenderLoop/ShaderLibrary/Common.hlsl
  2. 75
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/Lighting/Lighting.hlsl
  3. 151
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/Material/Material.hlsl
  4. 5
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/ShaderVariables.hlsl
  5. 6
      Assets/ScriptableRenderLoop/UnityStandard/UnityStandardRenderLoop.cs
  6. 2
      Assets/TestScenes/ForwardRenderLoop/ForwardRenderLoop.unity
  7. 8
      Assets/ScriptableRenderLoop/ShaderLibrary/API/D3D11_1.hlsl
  8. 8
      Assets/ScriptableRenderLoop/ShaderLibrary/API/D3D11_1.hlsl.meta
  9. 38
      Assets/ScriptableRenderLoop/ShaderLibrary/CommonLighting.hlsl
  10. 8
      Assets/ScriptableRenderLoop/ShaderLibrary/CommonLighting.hlsl.meta
  11. 112
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/DisneyGGX.shader
  12. 9
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/DisneyGGX.shader.meta
  13. 44
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/Lighting/LightingDefinition.hlsl
  14. 8
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/Lighting/LightingDefinition.hlsl.meta
  15. 30
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/Material/CommonMaterial.hlsl
  16. 8
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/Material/CommonMaterial.hlsl.meta
  17. 120
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/Material/DisneyGGX.hlsl
  18. 8
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/Material/DisneyGGX.hlsl.meta
  19. 139
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/TemplateDisneyGGX.hlsl
  20. 8
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/TemplateDisneyGGX.hlsl.meta
  21. 9
      Assets/ScriptableRenderLoop/ShaderLibrary/Platform.meta
  22. 9
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/UnityStandard.shader.meta
  23. 8
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/VertexShader.hlsl.meta
  24. 34
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/VertexShader.hlsl
  25. 162
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/UnityStandard.shader

17
Assets/ScriptableRenderLoop/ShaderLibrary/Common.hlsl


// N: Normal vector
// H: Half vector
// Input/Outputs structs in PascalCase and prefixed by entry type
// struct AttributesDefault
// struct VaryingsDefault
// use input/output as variable name when using these structures
// Include platform header
#if defined(SHADER_API_XBOXONE)
#include "Platform/XboxOne.hlsl"
#endif
// Entry program name
// VertDefault
// FragDefault / FragForward / FragDeferred
// constant floating number written as 1.0 (not 1, not 1.0f, not 1.0h)
// uniform have g_ as prefix (still lowercaseThenCamelCase)
#elif defined(SHADER_API_XBOXONE)
#include "API/D3D11_1.hlsl"
#endif
// ----------------------------------------------------------------------------

75
Assets/ScriptableRenderLoop/UnityStandard/Shaders/Lighting/Lighting.hlsl


#ifndef UNITY_LIGHTING_INCLUDED
#define UNITY_LIGHTING_INCLUDED
struct PunctualLightData
{
float3 positionWS;
float invSqrAttenuationRadius;
float3 color;
float unused;
float3 forward;
float diffuseScale;
float3 up;
float specularScale;
float3 right;
float shadowDimmer;
float angleScale;
float angleOffset;
float2 unused2;
};
#include "../Material/Material.hlsl"
// Attenuation functions
// Simple forward loop architecture
// Ref: Moving Frostbite to PBR
float SmoothDistanceAttenuation(float squaredDistance, float invSqrAttenuationRadius)
StructuredBuffer<PunctualLightData> g_punctualLightData;
float g_lightCount;
// TODO: Think about how to apply Disney diffuse preconvolve on indirect diffuse => must be done during GBuffer layout! Else emissive will be fucked...
// That's mean we need to read DFG texture during Gbuffer...
void ForwardLighting( float3 V, float3 positionWS, BSDFData material,
out float4 diffuseLighting,
out float4 specularLighting)
float factor = squaredDistance * invSqrAttenuationRadius;
float smoothFactor = saturate(1.0f - factor * factor);
return smoothFactor * smoothFactor;
}
diffuseLighting = float4(0.0, 0.0, 0.0, 0.0);
specularLighting = float4(0.0, 0.0, 0.0, 0.0);
#define PUNCTUAL_LIGHT_THRESHOLD 0.01 // 1cm (in Unity 1 is 1m)
for (uint i = 0; i < (uint)g_lightCount; ++i)
{
float4 localDiffuseLighting;
float4 localSpecularLighting;
EvaluateBSDF_Punctual(V, positionWS, g_punctualLightData[i], material, localDiffuseLighting, localSpecularLighting);
diffuseLighting += localDiffuseLighting;
specularLighting += localSpecularLighting;
}
float GetDistanceAttenuation(float3 unL, float invSqrAttenuationRadius)
{
float sqrDist = dot(unL, unL);
float attenuation = 1.0f / (max(PUNCTUAL_LIGHT_THRESHOLD * PUNCTUAL_LIGHT_THRESHOLD, sqrDist));
// Non physically based hack to limit light influence to attenuationRadius.
attenuation *= SmoothDistanceAttenuation(sqrDist, invSqrAttenuationRadius);
return attenuation;
/*
for (int i = 0; i < 4; ++i)
{
float4 localDiffuseLighting;
float4 localSpecularLighting;
EvaluateBSDF_Area(V, positionWS, areaLightData[i], material, localDiffuseLighting, localSpecularLighting);
diffuseLighting += localDiffuseLighting;
specularLighting += localSpecularLighting;
}
*/
float GetAngleAttenuation(float3 L, float3 lightDir, float lightAngleScale, float lightAngleOffset)
{
float cd = dot(lightDir, L);
float attenuation = saturate(cd * lightAngleScale + lightAngleOffset);
// smooth the transition
attenuation *= attenuation;
return attenuation;
}
#endif

151
Assets/ScriptableRenderLoop/UnityStandard/Shaders/Material/Material.hlsl


#include "../../ShaderLibrary/Packing.hlsl"
#include "../../ShaderLibrary/BSDF.hlsl"
#include "../Lighting/Lighting.hlsl"
#define DisneyGGXSurfaceData SurfaceData
#define DisneyGGXBSDFData BSDFData
//-----------------------------------------------------------------------------
// SurfaceData and BSDFData
//-----------------------------------------------------------------------------
// Main structure that store the user data (i.e user input of master node in material graph)
struct DisneyGGXSurfaceData
{
float3 diffuseColor;
float occlusion;
float3 specularColor;
float smoothness;
float3 normal; // normal in world space
};
struct DisneyGGXBSDFData
{
float3 diffuseColor;
float occlusion;
float3 fresnel0;
float roughness;
float3 normalWS;
float perceptualRoughness;
};
//-----------------------------------------------------------------------------
// Parametrization function helpers
//-----------------------------------------------------------------------------
float PerceptualRoughnessToRoughness(float perceptualRoughness)
{
return perceptualRoughness * perceptualRoughness;
}
float RoughnessToPerceptualRoughness(float roughness)
{
return sqrt(roughness);
}
// Smoothness is the user facing name
// it should be perceptualSmoothness but we don't want the user to have to deal with this name
float SmoothnessToRoughness(float smoothness)
{
return (1 - smoothness) * (1 - smoothness);
}
float SmoothnessToPerceptualRoughness(float smoothness)
{
return (1 - smoothness);
}
#include "../../ShaderLibrary/CommonLighting.hlsl"
//-----------------------------------------------------------------------------
// conversion function for forward and deferred
//-----------------------------------------------------------------------------
#include "../Lighting/LightingDefinition.hlsl"
#include "CommonMaterial.hlsl"
DisneyGGXBSDFData ConvertSurfaceDataToBSDFData(DisneyGGXSurfaceData data)
{
DisneyGGXBSDFData output;
output.diffuseColor = data.diffuseColor;
output.occlusion = data.occlusion;
output.fresnel0 = data.specularColor;
output.roughness = SmoothnessToRoughness(data.smoothness);
output.normalWS = data.normal;
output.perceptualRoughness = SmoothnessToPerceptualRoughness(data.smoothness);
return output;
}
// This will encode UnityStandardData into GBuffer
void EncodeIntoGBuffer(DisneyGGXSurfaceData data, out half4 outGBuffer0, out half4 outGBuffer1, out half4 outGBuffer2)
{
// RT0: diffuse color (rgb), occlusion (a) - sRGB rendertarget
outGBuffer0 = half4(data.diffuseColor, data.occlusion);
// RT1: spec color (rgb), roughness (a) - sRGB rendertarget
outGBuffer1 = half4(data.specularColor, SmoothnessToRoughness(data.smoothness));
// RT2: normal (rgb), --unused, very low precision-- (a)
outGBuffer2 = half4(PackNormalCartesian(data.normal), 1.0f);
}
// This decode the Gbuffer in a BSDFData struct
DisneyGGXBSDFData DecodeFromGBuffer(half4 inGBuffer0, half4 inGBuffer1, half4 inGBuffer2)
{
BSDFData data;
data.diffuseColor = inGBuffer0.rgb;
data.occlusion = inGBuffer0.a;
data.fresnel0 = inGBuffer1.rgb;
data.roughness = inGBuffer1.a;
data.normalWS = UnpackNormalCartesian(inGBuffer2.rgb);
return data;
}
//-----------------------------------------------------------------------------
// EvaluateBSDF functions for each light type
//-----------------------------------------------------------------------------
void EvaluateBSDF_Punctual_DisneyGGX( float3 V, float3 positionWS, PunctualLightData light, DisneyGGXBSDFData material,
out float4 diffuseLighting,
out float4 specularLighting)
{
float3 unL = light.positionWS - positionWS;
float3 L = normalize(unL);
// Always done, directional have it neutral
float attenuation = GetDistanceAttenuation(unL, light.invSqrAttenuationRadius);
// Always done, point and dir have it neutral
attenuation *= GetAngleAttenuation(L, light.forward, light.angleScale, light.angleOffset);
float illuminance = saturate(dot(material.normalWS, L)) * attenuation;
diffuseLighting = float4(0.0f, 0.0f, 0.0f, 1.0f);
specularLighting = float4(0.0f, 0.0f, 0.0f, 1.0f);
if (illuminance > 0.0f)
{
float NdotV = abs(dot(material.normalWS, V)) + 1e-5f; // TODO: check Eric idea about doing that when writting into the GBuffer (with our forward decal)
float3 H = normalize(V + L);
float LdotH = saturate(dot(L, H));
float NdotH = saturate(dot(material.normalWS, H));
float NdotL = saturate(dot(material.normalWS, L));
float3 F = F_Schlick(material.fresnel0, LdotH);
float Vis = V_SmithJointGGX(NdotL, NdotV, material.roughness);
float D = D_GGX(NdotH, material.roughness);
specularLighting.rgb = F * Vis * D;
float disneyDiffuse = DisneyDiffuse(NdotV, NdotL, LdotH, material.perceptualRoughness);
diffuseLighting.rgb = material.diffuseColor * disneyDiffuse;
diffuseLighting.rgb *= light.color * illuminance;
specularLighting.rgb *= light.color * illuminance;
}
}
// Here we include all the different lighting model supported by the renderloop based on define done in .shader
#ifdef UNITY_MATERIAL_DISNEYGXX
#include "DisneyGGX.hlsl"
#endif
#endif // UNITY_MATERIAL_INCLUDED

5
Assets/ScriptableRenderLoop/UnityStandard/Shaders/ShaderVariables.hlsl


// CAUTION:
// Currently the shaders compiler always include regualr Unity shaderVariables, so I get a conflict here were UNITY_SHADER_VARIABLES_INCLUDED is already define, this need to be fixed.
// As I haven't change the variables name yet, I simply don't define anything, and I put the transform function at the end of the file outside the guard header.
// This need to be fixed.
#ifndef UNITY_SHADER_VARIABLES_INCLUDED
#define UNITY_SHADER_VARIABLES_INCLUDED

6
Assets/ScriptableRenderLoop/UnityStandard/UnityStandardRenderLoop.cs


//---------------------------------------------------------------------------------------------------------------------------------------------------
void UpdateLightConstants(ActiveLight[] activeLights /*, ref ShadowOutput shadow */)
{
for ( int nLight = 0; nLight < Math.Min(activeLights.Length, 4); nLight++ )
{
}
_LightCount
/*
int nNumLightsIncludingTooMany = 0;

2
Assets/TestScenes/ForwardRenderLoop/ForwardRenderLoop.unity


m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_Materials:
- {fileID: 2100000, guid: b4d74419a10f9614aaf2624ff3c83e80, type: 2}
- {fileID: 2100000, guid: 87c86082ee14fbd4b8426a8794f2060d, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0

8
Assets/ScriptableRenderLoop/ShaderLibrary/API/D3D11_1.hlsl


#ifndef UNITY_D3D11_INCLUDED
#define UNITY_D3D11_INCLUDED
// This file assume SHADER_API_D3D11 is defined
#define UNITY_UV_STARTS_AT_TOP 1
#endif // UNITY_D3D11_INCLUDED

8
Assets/ScriptableRenderLoop/ShaderLibrary/API/D3D11_1.hlsl.meta


fileFormatVersion: 2
guid: 9aa86e448ea738047916ec3dbc6e9e33
timeCreated: 1474465931
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

38
Assets/ScriptableRenderLoop/ShaderLibrary/CommonLighting.hlsl


#ifndef UNITY_COMMON_LIGHTING_INCLUDED
#define UNITY_COMMON_LIGHTING_INCLUDED
//-----------------------------------------------------------------------------
// Attenuation functions
//-----------------------------------------------------------------------------
// Ref: Moving Frostbite to PBR
float SmoothDistanceAttenuation(float squaredDistance, float invSqrAttenuationRadius)
{
float factor = squaredDistance * invSqrAttenuationRadius;
float smoothFactor = saturate(1.0f - factor * factor);
return smoothFactor * smoothFactor;
}
#define PUNCTUAL_LIGHT_THRESHOLD 0.01 // 1cm (in Unity 1 is 1m)
float GetDistanceAttenuation(float3 unL, float invSqrAttenuationRadius)
{
float sqrDist = dot(unL, unL);
float attenuation = 1.0f / (max(PUNCTUAL_LIGHT_THRESHOLD * PUNCTUAL_LIGHT_THRESHOLD, sqrDist));
// Non physically based hack to limit light influence to attenuationRadius.
attenuation *= SmoothDistanceAttenuation(sqrDist, invSqrAttenuationRadius);
return attenuation;
}
float GetAngleAttenuation(float3 L, float3 lightDir, float lightAngleScale, float lightAngleOffset)
{
float cd = dot(lightDir, L);
float attenuation = saturate(cd * lightAngleScale + lightAngleOffset);
// smooth the transition
attenuation *= attenuation;
return attenuation;
}
#endif // UNITY_COMMON_LIGHTING_INCLUDED

8
Assets/ScriptableRenderLoop/ShaderLibrary/CommonLighting.hlsl.meta


fileFormatVersion: 2
guid: 93bbf250faa37d446990b9501e7fb7e8
timeCreated: 1474465931
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

112
Assets/ScriptableRenderLoop/UnityStandard/Shaders/DisneyGGX.shader


Shader "Unity/DisneyGGX"
{
// TODO: Following set of parameters represent the parameters node inside the MaterialGraph.
// They are use to fill a SurfaceData. With a MaterialGraph these parameters will not be write here (?).
Properties
{
_DiffuseColor("Diffuse", Color) = (1,1,1,1)
_DiffuseMap("Diffuse", 2D) = "white" {}
_SpecColor("Specular", Color) = (0.04,0.04,0.04)
_SpecMap("Specular", 2D) = "white" {}
_Smoothness("Smoothness", Range(0.0, 1.0)) = 0.5
_SmoothnessMap("Smoothness", 2D) = "white" {}
_NormalMap("Normal Map", 2D) = "bump" {}
_OcclusionMap("Occlusion", 2D) = "white" {}
_Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
// Blending state
[HideInInspector] _Mode ("__mode", Float) = 0.0
[HideInInspector] _SrcBlend ("__src", Float) = 1.0
[HideInInspector] _DstBlend ("__dst", Float) = 0.0
[HideInInspector] _ZWrite ("__zw", Float) = 1.0
}
CGINCLUDE
ENDCG
SubShader
{
Tags { "RenderType"="Opaque" "PerformanceChecks"="False" }
LOD 300
// ------------------------------------------------------------------
// forward pass
Pass
{
Name "FORWARD"
Tags { "LightMode" = "Forward" }
Blend [_SrcBlend] [_DstBlend]
ZWrite [_ZWrite]
CGPROGRAM
#pragma target 5.0
#pragma only_renderers d3d11 // TEMP: unitl we go futher in dev
#pragma vertex VertDefault
#pragma fragment FragForward
#define UNITY_SHADERRENDERPASS UNITY_SHADERRENDERPASS_FORWARD
#include "TemplateDisneyGGX.hlsl"
float4 FragForward(PackedVaryings packedInput) : SV_Target
{
Varyings input = UnpackVaryings(packedInput);
float3 V = GetWorldSpaceNormalizeViewDir(input.positionWS);
float3 positionWS = input.positionWS;
SurfaceData surfaceData = GetSurfaceData(input);
BSDFData BSDFData = ConvertSurfaceDataToBSDFData(surfaceData);
float4 diffuseLighting;
float4 specularLighting;
ForwardLighting(V, positionWS, BSDFData, diffuseLighting, specularLighting);
return float4(diffuseLighting.rgb + specularLighting.rgb, 1.0);
}
ENDCG
}
// ------------------------------------------------------------------
// Deferred pass
Pass
{
Name "DEFERRED"
Tags { "LightMode" = "Deferred" }
CGPROGRAM
#pragma target 5.0
#pragma only_renderers d3d11 // TEMP: unitl we go futher in dev
#pragma vertex VertDefault
#pragma fragment FragDeferred
#define UNITY_SHADERRENDERPASS UNITY_SHADERRENDERPASS_DEFERRED
#include "TemplateDisneyGGX.hlsl"
void FragDeferred( PackedVaryings packedInput,
out half4 outGBuffer0 : SV_Target0,
out half4 outGBuffer1 : SV_Target1,
out half4 outGBuffer2 : SV_Target2,
out half4 outEmission : SV_Target3
)
{
Varyings input = UnpackVaryings(packedInput);
SurfaceData surfaceData = GetSurfaceData(input);
EncodeIntoGBuffer(surfaceData, outGBuffer0, outGBuffer1, outGBuffer2);
// Lightmap + Emisive lighting buffer
outEmission = float4(0, 0, 0, 1);
}
ENDCG
}
}
}

9
Assets/ScriptableRenderLoop/UnityStandard/Shaders/DisneyGGX.shader.meta


fileFormatVersion: 2
guid: e1a84346ee54f9f4993c2f05c59805a0
timeCreated: 1474456127
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

44
Assets/ScriptableRenderLoop/UnityStandard/Shaders/Lighting/LightingDefinition.hlsl


#ifndef UNITY_LIGHTING_DEFINITION_INCLUDED
#define UNITY_LIGHTING_DEFINITION_INCLUDED
//-----------------------------------------------------------------------------
// structure definition
//-----------------------------------------------------------------------------
struct PunctualLightData
{
float3 positionWS;
float invSqrAttenuationRadius;
float3 color;
float unused;
float3 forward;
float diffuseScale;
float3 up;
float specularScale;
float3 right;
float shadowDimmer;
float angleScale;
float angleOffset;
float2 unused2;
};
struct AreaLightData
{
float3 positionWS;
};
struct EnvLightData
{
float3 positionWS;
};
struct PlanarLightData
{
float3 positionWS;
};
#endif // UNITY_LIGHTING_DEFINITION_INCLUDED

8
Assets/ScriptableRenderLoop/UnityStandard/Shaders/Lighting/LightingDefinition.hlsl.meta


fileFormatVersion: 2
guid: 4177b2c24c1e70a44835076b95cd7420
timeCreated: 1474465931
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

30
Assets/ScriptableRenderLoop/UnityStandard/Shaders/Material/CommonMaterial.hlsl


#ifndef UNITY_COMMON_MATERIAL_INCLUDED
#define UNITY_COMMON_MATERIAL_INCLUDED
//-----------------------------------------------------------------------------
// Parametrization function helpers
//-----------------------------------------------------------------------------
float PerceptualRoughnessToRoughness(float perceptualRoughness)
{
return perceptualRoughness * perceptualRoughness;
}
float RoughnessToPerceptualRoughness(float roughness)
{
return sqrt(roughness);
}
// Smoothness is the user facing name
// it should be perceptualSmoothness but we don't want the user to have to deal with this name
float SmoothnessToRoughness(float smoothness)
{
return (1 - smoothness) * (1 - smoothness);
}
float SmoothnessToPerceptualRoughness(float smoothness)
{
return (1 - smoothness);
}
#endif // UNITY_COMMON_MATERIAL_INCLUDED

8
Assets/ScriptableRenderLoop/UnityStandard/Shaders/Material/CommonMaterial.hlsl.meta


fileFormatVersion: 2
guid: 71aa77c8ecebcc942b7914328d88f7d1
timeCreated: 1474465931
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

120
Assets/ScriptableRenderLoop/UnityStandard/Shaders/Material/DisneyGGX.hlsl


#ifndef UNITY_MATERIAL_DISNEYGGX_INCLUDED
#define UNITY_MATERIAL_DISNEYGGX_INCLUDED
//-----------------------------------------------------------------------------
// SurfaceData and BSDFData
//-----------------------------------------------------------------------------
// Main structure that store the user data (i.e user input of master node in material graph)
struct SurfaceData
{
float3 diffuseColor;
float occlusion;
float3 specularColor;
float smoothness;
float3 normal; // normal in world space
};
struct BSDFData
{
float3 diffuseColor;
float occlusion;
float3 fresnel0;
float roughness;
float3 normalWS;
float perceptualRoughness;
};
//-----------------------------------------------------------------------------
// conversion function for forward and deferred
//-----------------------------------------------------------------------------
BSDFData ConvertSurfaceDataToBSDFData(SurfaceData data)
{
BSDFData output;
output.diffuseColor = data.diffuseColor;
output.occlusion = data.occlusion;
output.fresnel0 = data.specularColor;
output.roughness = SmoothnessToRoughness(data.smoothness);
output.normalWS = data.normal;
output.perceptualRoughness = SmoothnessToPerceptualRoughness(data.smoothness);
return output;
}
// This will encode UnityStandardData into GBuffer
void EncodeIntoGBuffer(SurfaceData data, out half4 outGBuffer0, out half4 outGBuffer1, out half4 outGBuffer2)
{
// RT0: diffuse color (rgb), occlusion (a) - sRGB rendertarget
outGBuffer0 = half4(data.diffuseColor, data.occlusion);
// RT1: spec color (rgb), roughness (a) - sRGB rendertarget
outGBuffer1 = half4(data.specularColor, SmoothnessToRoughness(data.smoothness));
// RT2: normal (rgb), --unused, very low precision-- (a)
outGBuffer2 = half4(PackNormalCartesian(data.normal), 1.0f);
}
// This decode the Gbuffer in a BSDFData struct
BSDFData DecodeFromGBuffer(half4 inGBuffer0, half4 inGBuffer1, half4 inGBuffer2)
{
BSDFData data;
data.diffuseColor = inGBuffer0.rgb;
data.occlusion = inGBuffer0.a;
data.fresnel0 = inGBuffer1.rgb;
data.roughness = inGBuffer1.a;
data.normalWS = UnpackNormalCartesian(inGBuffer2.rgb);
return data;
}
//-----------------------------------------------------------------------------
// EvaluateBSDF functions for each light type
//-----------------------------------------------------------------------------
void EvaluateBSDF_Punctual( float3 V, float3 positionWS, PunctualLightData light, BSDFData material,
out float4 diffuseLighting,
out float4 specularLighting)
{
float3 unL = light.positionWS - positionWS;
float3 L = normalize(unL);
// Always done, directional have it neutral
float attenuation = GetDistanceAttenuation(unL, light.invSqrAttenuationRadius);
// Always done, point and dir have it neutral
attenuation *= GetAngleAttenuation(L, light.forward, light.angleScale, light.angleOffset);
float illuminance = saturate(dot(material.normalWS, L)) * attenuation;
diffuseLighting = float4(0.0f, 0.0f, 0.0f, 1.0f);
specularLighting = float4(0.0f, 0.0f, 0.0f, 1.0f);
if (illuminance > 0.0f)
{
float NdotV = abs(dot(material.normalWS, V)) + 1e-5f; // TODO: check Eric idea about doing that when writting into the GBuffer (with our forward decal)
float3 H = normalize(V + L);
float LdotH = saturate(dot(L, H));
float NdotH = saturate(dot(material.normalWS, H));
float NdotL = saturate(dot(material.normalWS, L));
float3 F = F_Schlick(material.fresnel0, LdotH);
float Vis = V_SmithJointGGX(NdotL, NdotV, material.roughness);
float D = D_GGX(NdotH, material.roughness);
specularLighting.rgb = F * Vis * D;
float disneyDiffuse = DisneyDiffuse(NdotV, NdotL, LdotH, material.perceptualRoughness);
diffuseLighting.rgb = material.diffuseColor * disneyDiffuse;
diffuseLighting.rgb *= light.color * illuminance;
specularLighting.rgb *= light.color * illuminance;
}
}
#endif // UNITY_MATERIAL_DISNEYGGX_INCLUDED

8
Assets/ScriptableRenderLoop/UnityStandard/Shaders/Material/DisneyGGX.hlsl.meta


fileFormatVersion: 2
guid: 3d0985c9290aa0847969ac858555e87a
timeCreated: 1474465931
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

139
Assets/ScriptableRenderLoop/UnityStandard/Shaders/TemplateDisneyGGX.hlsl


// GENERATED BY MATERIAL GRAPH
// No guard header!
#define UNITY_MATERIAL_DISNEYGXX // Need to be define before including Material.hlsl
#include "Lighting/Lighting.hlsl" // This include Material.hlsl
#include "ShaderVariables.hlsl"
// This files is generated by the material graph or written by hand
// Note for material graph:
// Material graph should generate the vertex shader output to add the variable that may be required
// For example if we require view vector in shader graph, the output must contain positionWS and we calcualte the view vector with it.
// Still some input are mandatory depends on the type of loop. positionWS is mandatory in this current framework. So the material graph should always generate it.
//-------------------------------------------------------------------------------------
// variable declaration
//-------------------------------------------------------------------------------------
// Set of users variables
float4 _DiffuseColor;
float4 _SpecColor;
float _Smoothness;
sampler2D _DiffuseMap;
sampler2D _NormalMap;
// ... Others
//-------------------------------------------------------------------------------------
// Lighting architecture
//-------------------------------------------------------------------------------------
// TODO: Check if we will have different Varyings based on different pass, not sure about that...
#if UNITY_SHADERRENDERPASS == UNITY_SHADERRENDERPASS_DEFERRED || UNITY_SHADERRENDERPASS == UNITY_SHADERRENDERPASS_FORWARD
// Forward
struct Attributes
{
float4 positionOS : POSITION; // TODO: why do we provide w here ? putting constant to 1 will save a float
half3 normalOS : NORMAL;
float2 uv0 : TEXCOORD0;
half4 tangentOS : TANGENT;
};
struct Varyings
{
float4 positionHS : SV_POSITION;
float3 positionWS : TEXCOORD0;
float2 texCoord0 : TEXCOORD1;
float4 tangentToWorld[3] : TEXCOORD2; // [3x3:tangentToWorld | 1x3:viewDirForParallax]
};
struct PackedVaryings
{
float4 positionHS : SV_Position;
float4 interpolators[5] : TEXCOORD0;
};
// Function to pack data to use as few interpolator as possible, the MaterialGraph should generate this functions
PackedVaryings PackVaryings(Varyings input)
{
PackedVaryings output;
output.positionHS = input.positionHS;
output.interpolators[0].xyz = input.positionWS.xyz;
output.interpolators[0].w = input.texCoord0.x;
output.interpolators[1] = input.tangentToWorld[0];
output.interpolators[2] = input.tangentToWorld[1];
output.interpolators[3] = input.tangentToWorld[2];
output.interpolators[4].x = input.texCoord0.y;
output.interpolators[4].yzw = float3(0.0, 0.0, 0.0);
return output;
}
Varyings UnpackVaryings(PackedVaryings input)
{
Varyings output;
output.positionHS = input.positionHS;
output.positionWS.xyz = input.interpolators[0].xyz;
output.texCoord0.x = input.interpolators[0].w;
output.texCoord0.y = input.interpolators[4].x;
output.tangentToWorld[0] = input.interpolators[1];
output.tangentToWorld[1] = input.interpolators[2];
output.tangentToWorld[2] = input.interpolators[3];
return output;
}
// TODO: Here we will also have all the vertex deformation (GPU skinning, vertex animation, morph target...) or we will need to generate a compute shaders instead (better! but require work to deal with unpacking like fp16)
PackedVaryings VertDefault(Attributes input)
{
Varyings output;
output.positionWS = TransformObjectToWorld(input.positionOS.xyz);
// TODO deal with camera center rendering and instancing (This is the reason why we always perform tow step transform to clip space + instancing matrix)
output.positionHS = TransformWorldToHClip(output.positionWS);
float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
output.texCoord0 = input.uv0;
// #ifdef _TANGENT_TO_WORLD
float4 tangentWS = float4(TransformObjectToWorldDir(input.tangentOS.xyz), input.tangentOS.w);
float3x3 tangentToWorld = CreateTangentToWorld(normalWS, tangentWS.xyz, tangentWS.w);
output.tangentToWorld[0].xyz = tangentToWorld[0];
output.tangentToWorld[1].xyz = tangentToWorld[1];
output.tangentToWorld[2].xyz = tangentToWorld[2];
// #else
// output.tangentToWorld[0].xyz = 0;
// output.tangentToWorld[1].xyz = 0;
// output.tangentToWorld[2].xyz = normalWS;
// #endif
output.tangentToWorld[0].w = 0;
output.tangentToWorld[1].w = 0;
output.tangentToWorld[2].w = 0;
return PackVaryings(output);
}
#endif
//-------------------------------------------------------------------------------------
// Fill SurfaceData function
//-------------------------------------------------------------------------------------
SurfaceData GetSurfaceData(Varyings input)
{
SurfaceData data;
data.diffuseColor = tex2D(_DiffuseMap, input.texCoord0) * _DiffuseColor;
data.occlusion = 1.0;
data.specularColor = _SpecColor;
data.smoothness = _Smoothness;
data.normal = UnpackNormalDXT5nm(tex2D(_NormalMap, input.texCoord0));
return data;
}

8
Assets/ScriptableRenderLoop/UnityStandard/Shaders/TemplateDisneyGGX.hlsl.meta


fileFormatVersion: 2
guid: bc4a80ef70e37814e94ea45fc03abdaa
timeCreated: 1474456127
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

9
Assets/ScriptableRenderLoop/ShaderLibrary/Platform.meta


fileFormatVersion: 2
guid: c50ac5c0368fe38449d335dad3e23260
folderAsset: yes
timeCreated: 1472140530
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

9
Assets/ScriptableRenderLoop/UnityStandard/Shaders/UnityStandard.shader.meta


fileFormatVersion: 2
guid: c846f5368b257ca49aa135748a1700e2
timeCreated: 1472205826
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

8
Assets/ScriptableRenderLoop/UnityStandard/Shaders/VertexShader.hlsl.meta


fileFormatVersion: 2
guid: 3cc412debf748e040aeabf1cb51d2515
timeCreated: 1472205826
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

34
Assets/ScriptableRenderLoop/UnityStandard/Shaders/VertexShader.hlsl


#ifndef UNITY_VERTEX_INCLUDED
#define UNITY_VERTEX_INCLUDED
#include "../Common.hlsl"
struct VertexOutputDeferred
{
float4 pos : SV_POSITION;
float4 tex : TEXCOORD0;
float4 tangentToWorldAndParallax[3] : TEXCOORD1; // [3x3:tangentToWorld | 1x3:viewDirForParallax]
};
VertexOutputDeferred vertDeferred (VertexInput v)
{
UNITY_SETUP_INSTANCE_ID(v);
VertexOutputDeferred o;
UNITY_INITIALIZE_OUTPUT(VertexOutputDeferred, o);
float4 posWorld = mul(unity_ObjectToWorld, v.vertex);
o.pos = UnityObjectToClipPos(v.vertex);
o.tex = TexCoords(v);
float3 normalWorld = UnityObjectToWorldNormal(v.normal);
float4 tangentWorld = float4(UnityObjectToWorldDir(v.tangent.xyz), v.tangent.w);
float3x3 tangentToWorld = CreateTangentToWorldPerVertex(normalWorld, tangentWorld.xyz, tangentWorld.w);
o.tangentToWorldAndParallax[0].xyz = tangentToWorld[0];
o.tangentToWorldAndParallax[1].xyz = tangentToWorld[1];
o.tangentToWorldAndParallax[2].xyz = tangentToWorld[2];
return o;
}
#endif // UNITY_VERTEX_INCLUDED

162
Assets/ScriptableRenderLoop/UnityStandard/Shaders/UnityStandard.shader


Shader "Unity/UnityStandard"
{
Properties
{
_DiffuseColor("Diffuse", Color) = (1,1,1,1)
_DiffuseMap("Diffuse", 2D) = "white" {}
_SpecColor("Specular", Color) = (0.04,0.04,0.04)
_SpecMap("Specular", 2D) = "white" {}
_Smoothness("Smoothness", Range(0.0, 1.0)) = 0.5
_SmoothnessMap("Smoothness", 2D) = "white" {}
_NormalMap("Normal Map", 2D) = "bump" {}
_OcclusionMap("Occlusion", 2D) = "white" {}
_Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
// Blending state
[HideInInspector] _Mode ("__mode", Float) = 0.0
[HideInInspector] _SrcBlend ("__src", Float) = 1.0
[HideInInspector] _DstBlend ("__dst", Float) = 0.0
[HideInInspector] _ZWrite ("__zw", Float) = 1.0
}
CGINCLUDE
#include "Material/Material.hlsl"
#include "ShaderVariables.hlsl"
ENDCG
SubShader
{
Tags { "RenderType"="Opaque" "PerformanceChecks"="False" }
LOD 300
// ------------------------------------------------------------------
// Base forward pass (directional light, emission, lightmaps, ...)
Pass
{
Name "FORWARD"
Tags { "LightMode" = "Forward" }
Blend [_SrcBlend] [_DstBlend]
ZWrite [_ZWrite]
CGPROGRAM
#pragma target 5.0
#pragma only_renderers d3d11 // TEMP: unitl we go futher in dev
#pragma vertex MainVS
#pragma fragment MainPS
float4 _DiffuseColor;
float4 _SpecColor;
float _Smoothness;
sampler2D _DiffuseMap;
sampler2D _NormalMap;
PunctualLightData _lightData[4];
float _LightCount;
//-------------------------------------------------------------------------------------
// Input functions
struct VSInput
{
float4 positionOS : POSITION; // TODO: why do we provide w here ? putting constant to 1 will save a float
half3 normalOS : NORMAL;
float2 uv0 : TEXCOORD0;
half4 tangentOS : TANGENT;
};
struct VSOutput
{
float4 positionHS : SV_POSITION;
float3 positionWS : TEXCOORD0;
float2 texCoord0 : TEXCOORD1;
float4 tangentToWorld[3] : TEXCOORD2; // [3x3:tangentToWorld | 1x3:viewDirForParallax]
};
VSOutput MainVS(VSInput input)
{
// TODO : here we must support anykind of vertex animation (GPU skinning, morphing) or better do it in compute.
VSOutput output;
output.positionWS = TransformObjectToWorld(input.positionOS.xyz);
// TODO deal with camera center rendering and instancing (This is the reason why we always perform tow step transform to clip space + instancing matrix)
output.positionHS = TransformWorldToHClip(output.positionWS);
float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
output.texCoord0 = input.uv0;
// #ifdef _TANGENT_TO_WORLD
float4 tangentWS = float4(TransformObjectToWorldDir(input.tangentOS.xyz), input.tangentOS.w);
float3x3 tangentToWorld = CreateTangentToWorld(normalWS, tangentWS.xyz, tangentWS.w);
output.tangentToWorld[0].xyz = tangentToWorld[0];
output.tangentToWorld[1].xyz = tangentToWorld[1];
output.tangentToWorld[2].xyz = tangentToWorld[2];
// #else
// output.tangentToWorld[0].xyz = 0;
// output.tangentToWorld[1].xyz = 0;
// output.tangentToWorld[2].xyz = normalWS;
// #endif
output.tangentToWorld[0].w = 0;
output.tangentToWorld[1].w = 0;
output.tangentToWorld[2].w = 0;
return output;
}
//-------------------------------------------------------------------------------------
// This function is either hand written or generate by the material graph
DisneyGGXSurfaceData GetSurfaceData(VSOutput input)
{
DisneyGGXSurfaceData data;
data.diffuseColor = tex2D(_DiffuseMap, input.texCoord0) * _DiffuseColor;
data.occlusion = 1.0;
data.specularColor = _SpecColor;
data.smoothness = _Smoothness;
data.normal = UnpackNormalDXT5nm(tex2D(_NormalMap, input.texCoord0));
return data;
}
//-------------------------------------------------------------------------------------
float4 MainPS(VSOutput input) : SV_Target
{
float3 V = GetWorldSpaceNormalizeViewDir(input.positionWS);
DisneyGGXSurfaceData surfaceData = GetSurfaceData(input);
DisneyGGXBSDFData BSDFData = ConvertSurfaceDataToBSDFData(surfaceData);
float4 outDiffuseLighting;
float4 outSpecularLighting;
for (int i = 0; i < _LightCount; ++i)
{
float4 diffuseLighting;
float4 specularLighting;
EvaluateBSDF_Punctual_DisneyGGX(V, input.positionWS, _lightData[i], BSDFData, diffuseLighting, specularLighting);
outDiffuseLighting += diffuseLighting;
outSpecularLighting += specularLighting;
}
return outDiffuseLighting + outSpecularLighting;
}
ENDCG
}
}
}
正在加载...
取消
保存