浏览代码

Merge pull request #394 from Unity-Technologies/bent-normal-and-specular-occlusion

add bent normal support
/main
GitHub 7 年前
当前提交
77fe7881
共有 12 个文件被更改,包括 198 次插入119 次删除
  1. 8
      ScriptableRenderPipeline/Core/ShaderLibrary/Common.hlsl
  2. 4
      ScriptableRenderPipeline/HDRenderPipeline/Material/LayeredLit/Editor/LayeredLitUI.cs
  3. 27
      ScriptableRenderPipeline/HDRenderPipeline/Material/LayeredLit/LayeredLit.shader
  4. 25
      ScriptableRenderPipeline/HDRenderPipeline/Material/LayeredLit/LayeredLitTessellation.shader
  5. 9
      ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/Editor/BaseLitUI.cs
  6. 25
      ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/Editor/LitUI.cs
  7. 9
      ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/Lit.shader
  8. 141
      ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/LitData.hlsl
  9. 53
      ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/LitDataInternal.hlsl
  10. 2
      ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/LitProperties.hlsl
  11. 7
      ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/LitTessellation.shader
  12. 7
      ScriptableRenderPipeline/HDRenderPipeline/Material/MaterialUtilities.hlsl

8
ScriptableRenderPipeline/Core/ShaderLibrary/Common.hlsl


return saturate(x * FLT_MAX) * 2.0 - 1.0;
}
// Orthonormalize the basis vectors using the Gram-Schmidt process.
// We assume that the length of the surface normal is sufficiently close to 1.
// return orthonormalize tangent
float3 Orthonormalize(float3 tangent, float3 normal)
{
return normalize(tangent - dot(tangent, normal) * normal);
}
// ----------------------------------------------------------------------------
// Texture utilities
// ----------------------------------------------------------------------------

4
ScriptableRenderPipeline/HDRenderPipeline/Material/LayeredLit/Editor/LayeredLitUI.cs


if (normalMapSpace == NormalMapSpace.TangentSpace)
{
SetKeyword(material, "_NORMALMAP" + i, material.GetTexture(kNormalMap + i) || material.GetTexture(kDetailMap + i));
SetKeyword(material, "_BENTNORMALMAP" + i, material.GetTexture(kBentNormalMap + i));
SetKeyword(material, "_BENTNORMALMAP" + i, material.GetTexture(kBentNormalMapOS + i));
SetKeyword(material, "_SPECULAROCCLUSIONMAP" + i, material.GetTexture(kSpecularOcclusionMap + i));
SetKeyword(material, "_DETAIL_MAP" + i, material.GetTexture(kDetailMap + i));

27
ScriptableRenderPipeline/HDRenderPipeline/Material/LayeredLit/LayeredLit.shader


_MaskMap2("MaskMap2", 2D) = "white" {}
_MaskMap3("MaskMap3", 2D) = "white" {}
_SpecularOcclusionMap0("SpecularOcclusion0", 2D) = "white" {}
_SpecularOcclusionMap1("SpecularOcclusion1", 2D) = "white" {}
_SpecularOcclusionMap2("SpecularOcclusion2", 2D) = "white" {}
_SpecularOcclusionMap3("SpecularOcclusion3", 2D) = "white" {}
_NormalMap0("NormalMap0", 2D) = "bump" {}
_NormalMap1("NormalMap1", 2D) = "bump" {}
_NormalMap2("NormalMap2", 2D) = "bump" {}

_NormalScale1("_NormalScale1", Range(0.0, 2.0)) = 1
_NormalScale2("_NormalScale2", Range(0.0, 2.0)) = 1
_NormalScale3("_NormalScale3", Range(0.0, 2.0)) = 1
_BentNormalMap0("BentNormalMap0", 2D) = "bump" {}
_BentNormalMap1("BentNormalMap1", 2D) = "bump" {}
_BentNormalMap2("BentNormalMap2", 2D) = "bump" {}
_BentNormalMap3("BentNormalMap3", 2D) = "bump" {}
_BentNormalMapOS0("BentNormalMapOS0", 2D) = "white" {}
_BentNormalMapOS1("BentNormalMapOS1", 2D) = "white" {}
_BentNormalMapOS2("BentNormalMapOS2", 2D) = "white" {}
_BentNormalMapOS3("BentNormalMapOS3", 2D) = "white" {}
_HeightMap0("HeightMap0", 2D) = "black" {}
_HeightMap1("HeightMap1", 2D) = "black" {}

_AlphaCutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
_HorizonFade("Horizon fade", Range(0.0, 5.0)) = 1.0
// Stencil state
[HideInInspector] _StencilRef("_StencilRef", Int) = 2 // StencilLightingUsage.RegularLighting

#pragma shader_feature _NORMALMAP_TANGENT_SPACE2
#pragma shader_feature _NORMALMAP_TANGENT_SPACE3
#pragma shader_feature _ _REQUIRE_UV2 _REQUIRE_UV3
#pragma shader_feature _NORMALMAP0
#pragma shader_feature _NORMALMAP1
#pragma shader_feature _NORMALMAP2

#pragma shader_feature _MASKMAP2
#pragma shader_feature _MASKMAP3
#pragma shader_feature _SPECULAROCCLUSIONMAP0
#pragma shader_feature _SPECULAROCCLUSIONMAP1
#pragma shader_feature _SPECULAROCCLUSIONMAP2
#pragma shader_feature _SPECULAROCCLUSIONMAP3
#pragma shader_feature _BENTNORMALMAP0
#pragma shader_feature _BENTNORMALMAP1
#pragma shader_feature _BENTNORMALMAP2
#pragma shader_feature _BENTNORMALMAP3
#pragma shader_feature _EMISSIVE_COLOR_MAP
#pragma shader_feature _HEIGHTMAP0
#pragma shader_feature _HEIGHTMAP1

25
ScriptableRenderPipeline/HDRenderPipeline/Material/LayeredLit/LayeredLitTessellation.shader


_MaskMap2("MaskMap2", 2D) = "white" {}
_MaskMap3("MaskMap3", 2D) = "white" {}
_SpecularOcclusionMap0("SpecularOcclusion0", 2D) = "white" {}
_SpecularOcclusionMap1("SpecularOcclusion1", 2D) = "white" {}
_SpecularOcclusionMap2("SpecularOcclusion2", 2D) = "white" {}
_SpecularOcclusionMap3("SpecularOcclusion3", 2D) = "white" {}
_NormalMap0("NormalMap0", 2D) = "bump" {}
_NormalMap1("NormalMap1", 2D) = "bump" {}
_NormalMap2("NormalMap2", 2D) = "bump" {}

_NormalScale2("_NormalScale2", Range(0.0, 2.0)) = 1
_NormalScale3("_NormalScale3", Range(0.0, 2.0)) = 1
_BentNormalMap0("BentNormalMap0", 2D) = "bump" {}
_BentNormalMap1("BentNormalMap1", 2D) = "bump" {}
_BentNormalMap2("BentNormalMap2", 2D) = "bump" {}
_BentNormalMap3("BentNormalMap3", 2D) = "bump" {}
_BentNormalMapOS0("BentNormalMapOS0", 2D) = "white" {}
_BentNormalMapOS1("BentNormalMapOS1", 2D) = "white" {}
_BentNormalMapOS2("BentNormalMapOS2", 2D) = "white" {}
_BentNormalMapOS3("BentNormalMapOS3", 2D) = "white" {}
_HeightMap0("HeightMap0", 2D) = "black" {}
_HeightMap1("HeightMap1", 2D) = "black" {}
_HeightMap2("HeightMap2", 2D) = "black" {}

_AlphaCutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
_HorizonFade("Horizon fade", Range(0.0, 5.0)) = 1.0
// Stencil state
[HideInInspector] _StencilRef("_StencilRef", Int) = 2 // StencilLightingUsage.RegularLighting (fixed at compile time)

#pragma shader_feature _MASKMAP1
#pragma shader_feature _MASKMAP2
#pragma shader_feature _MASKMAP3
#pragma shader_feature _SPECULAROCCLUSIONMAP0
#pragma shader_feature _SPECULAROCCLUSIONMAP1
#pragma shader_feature _SPECULAROCCLUSIONMAP2
#pragma shader_feature _SPECULAROCCLUSIONMAP3
#pragma shader_feature _BENTNORMALMAP0
#pragma shader_feature _BENTNORMALMAP1
#pragma shader_feature _BENTNORMALMAP2
#pragma shader_feature _BENTNORMALMAP3
#pragma shader_feature _EMISSIVE_COLOR_MAP
#pragma shader_feature _HEIGHTMAP0
#pragma shader_feature _HEIGHTMAP1

9
ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/Editor/BaseLitUI.cs


// Material ID
public static GUIContent materialIDText = new GUIContent("Material type", "Subsurface Scattering: enable for translucent materials such as skin, vegetation, fruit, marble, wax and milk.");
public static GUIContent horizonFadeText = new GUIContent("Horizon Fade (Spec occlusion)", "horizon fade is use to control specular occlusion");
// Per pixel displacement
public static GUIContent enablePerPixelDisplacementText = new GUIContent("Enable Per Pixel Displacement", "Per pixel displacement work best with flat surfaces. This is an expensive features and should be enable wisely. Typical use case is paved road.");
public static GUIContent ppdMinSamplesText = new GUIContent("Minimum steps", "Minimum steps (texture sample) to use with per pixel displacement mapping");

protected const string kMaterialID = "_MaterialID";
protected const string kStencilRef = "_StencilRef";
protected MaterialProperty horizonFade = null;
protected const string kHorizonFade = "_HorizonFade";
// Wind
protected MaterialProperty windEnable = null;

// MaterialID
materialID = FindProperty(kMaterialID, props, false); // LayeredLit is force to be standard for now, so materialID could not exist
horizonFade = FindProperty(kHorizonFade, props);
// Per pixel displacement
enablePerPixelDisplacement = FindProperty(kEnablePerPixelDisplacement, props);
ppdMinSamples = FindProperty(kPpdMinSamples, props);

m_MaterialEditor.ShaderProperty(depthOffsetEnable, StylesBaseLit.depthOffsetEnableText);
EditorGUI.indentLevel--;
}
m_MaterialEditor.ShaderProperty(horizonFade, StylesBaseLit.horizonFadeText);
EditorGUI.indentLevel--;

25
ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/Editor/LitUI.cs


public static GUIContent normalMapSpaceText = new GUIContent("Normal Map space", "");
public static GUIContent normalMapText = new GUIContent("Normal Map", "Normal Map (BC7/BC5/DXT5(nm))");
public static GUIContent normalMapOSText = new GUIContent("Normal Map OS", "Normal Map (BC7/DXT1/RGB)");
public static GUIContent specularOcclusionMapText = new GUIContent("Specular Occlusion Map (RGBA)", "Specular Occlusion Map");
public static GUIContent bentNormalMapText = new GUIContent("Bent normal map", "Use only with indirect diffuse lighting (Lightmap/lightprobe) - Cosine weighted Bent Normal Map (average unoccluded direction) (BC7/BC5/DXT5(nm))");
public static GUIContent bentNormalMapOSText = new GUIContent("Bent normal map OS", "Use only with indirect diffuse lighting (Lightmap/lightprobe) - Bent Normal Map (BC7/DXT1/RGB)");
public static GUIContent heightMapText = new GUIContent("Height Map (R)", "Height Map.\nFor floating point textures, min, max and base value should be 0, 1 and 0.");
public static GUIContent heightMapCenterText = new GUIContent("Height Map Base", "Base of the heightmap in the texture (between 0 and 1)");

public static GUIContent tangentMapText = new GUIContent("Tangent Map", "Tangent Map (BC7/BC5/DXT5(nm))");
public static GUIContent tangentMapOSText = new GUIContent("Tangent Map OS", "Tangent Map (BC7/DXT1/RGB)");
public static GUIContent anisotropyText = new GUIContent("Anisotropy", "Anisotropy scale factor");
public static GUIContent anisotropyMapText = new GUIContent("Anisotropy Map (B)", "Anisotropy");
public static GUIContent anisotropyMapText = new GUIContent("Anisotropy Map (R)", "Anisotropy");
public static GUIContent UVBaseMappingText = new GUIContent("Base UV mapping", "");
public static GUIContent texWorldScaleText = new GUIContent("World scale", "Tiling factor applied to Planar/Trilinear mapping");

protected const string kSmoothnessRemapMax = "_SmoothnessRemapMax";
protected MaterialProperty[] maskMap = new MaterialProperty[kMaxLayerCount];
protected const string kMaskMap = "_MaskMap";
protected MaterialProperty[] specularOcclusionMap = new MaterialProperty[kMaxLayerCount];
protected const string kSpecularOcclusionMap = "_SpecularOcclusionMap";
protected MaterialProperty[] normalScale = new MaterialProperty[kMaxLayerCount];
protected const string kNormalScale = "_NormalScale";
protected MaterialProperty[] normalScale = new MaterialProperty[kMaxLayerCount];
protected const string kNormalScale = "_NormalScale";
protected MaterialProperty[] bentNormalMap = new MaterialProperty[kMaxLayerCount];
protected const string kBentNormalMap = "_BentNormalMap";
protected MaterialProperty[] bentNormalMapOS = new MaterialProperty[kMaxLayerCount];
protected const string kBentNormalMapOS = "_BentNormalMapOS";
protected MaterialProperty[] normalMapSpace = new MaterialProperty[kMaxLayerCount];
protected const string kNormalMapSpace = "_NormalMapSpace";
protected MaterialProperty[] heightMap = new MaterialProperty[kMaxLayerCount];

smoothnessRemapMin[i] = FindProperty(string.Format("{0}{1}", kSmoothnessRemapMin, m_PropertySuffixes[i]), props);
smoothnessRemapMax[i] = FindProperty(string.Format("{0}{1}", kSmoothnessRemapMax, m_PropertySuffixes[i]), props);
maskMap[i] = FindProperty(string.Format("{0}{1}", kMaskMap, m_PropertySuffixes[i]), props);
specularOcclusionMap[i] = FindProperty(string.Format("{0}{1}", kSpecularOcclusionMap, m_PropertySuffixes[i]), props);
bentNormalMap[i] = FindProperty(string.Format("{0}{1}", kBentNormalMap, m_PropertySuffixes[i]), props);
bentNormalMapOS[i] = FindProperty(string.Format("{0}{1}", kBentNormalMapOS, m_PropertySuffixes[i]), props);
normalMapSpace[i] = FindProperty(string.Format("{0}{1}", kNormalMapSpace, m_PropertySuffixes[i]), props);
heightMap[i] = FindProperty(string.Format("{0}{1}", kHeightMap, m_PropertySuffixes[i]), props);
heightAmplitude[i] = FindProperty(string.Format("{0}{1}", kHeightAmplitude, m_PropertySuffixes[i]), props);

m_MaterialEditor.TexturePropertySingleLine(Styles.maskMapSText, maskMap[layerIndex]);
m_MaterialEditor.TexturePropertySingleLine(Styles.specularOcclusionMapText, specularOcclusionMap[layerIndex]);
m_MaterialEditor.ShaderProperty(normalMapSpace[layerIndex], Styles.normalMapSpaceText);
// Triplanar only work with tangent space normal

if ((NormalMapSpace)normalMapSpace[layerIndex].floatValue == NormalMapSpace.TangentSpace)
{
m_MaterialEditor.TexturePropertySingleLine(Styles.normalMapText, normalMap[layerIndex], normalScale[layerIndex]);
m_MaterialEditor.TexturePropertySingleLine(Styles.bentNormalMapText, bentNormalMap[layerIndex]);
m_MaterialEditor.TexturePropertySingleLine(Styles.bentNormalMapOSText, bentNormalMapOS[layerIndex]);
}
m_MaterialEditor.TexturePropertySingleLine(Styles.heightMapText, heightMap[layerIndex]);

// With details map, we always use a normal map and Unity provide a default (0, 0, 1) normal map for it
SetKeyword(material, "_NORMALMAP", material.GetTexture(kNormalMap) || material.GetTexture(kDetailMap));
SetKeyword(material, "_TANGENTMAP", material.GetTexture(kTangentMap));
SetKeyword(material, "_BENTNORMALMAP", material.GetTexture(kBentNormalMap));
}
else // Object space
{

SetKeyword(material, "_BENTNORMALMAP", material.GetTexture(kBentNormalMapOS));
SetKeyword(material, "_SPECULAROCCLUSIONMAP", material.GetTexture(kSpecularOcclusionMap));
SetKeyword(material, "_EMISSIVE_COLOR_MAP", material.GetTexture(kEmissiveColorMap));
SetKeyword(material, "_HEIGHTMAP", material.GetTexture(kHeightMap));
SetKeyword(material, "_ANISOTROPYMAP", material.GetTexture(kAnisotropyMap));

9
ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/Lit.shader


_SmoothnessRemapMin("SmoothnessRemapMin", Float) = 0.0
_SmoothnessRemapMax("SmoothnessRemapMax", Float) = 1.0
_SpecularOcclusionMap("SpecularOcclusion", 2D) = "white" {}
_BentNormalMap("_BentNormalMap", 2D) = "bump" {}
_BentNormalMapOS("_BentNormalMapOS", 2D) = "white" {}
_HeightMap("HeightMap", 2D) = "black" {}
[HideInInspector] _HeightAmplitude("Height Amplitude", Float) = 0.01 // In world units. This will be computed in the UI.

[ToggleOff] _AlphaCutoffEnable("Alpha Cutoff Enable", Float) = 0.0
_AlphaCutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
_HorizonFade("Horizon fade", Range(0.0, 5.0)) = 1.0
// Stencil state
[HideInInspector] _StencilRef("_StencilRef", Int) = 2 // StencilLightingUsage.RegularLighting (fixed at compile time)

#pragma shader_feature _NORMALMAP
#pragma shader_feature _MASKMAP
#pragma shader_feature _SPECULAROCCLUSIONMAP
#pragma shader_feature _BENTNORMALMAP
#pragma shader_feature _EMISSIVE_COLOR_MAP
#pragma shader_feature _HEIGHTMAP
#pragma shader_feature _TANGENTMAP

141
ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/LitData.hlsl


#include "../MaterialUtilities.hlsl"
void GetBuiltinData(FragInputs input, SurfaceData surfaceData, float alpha, float depthOffset, out BuiltinData builtinData)
void GetBuiltinData(FragInputs input, SurfaceData surfaceData, float alpha, float3 bentNormalWS, float depthOffset, out BuiltinData builtinData)
{
// Builtin Data
builtinData.opacity = alpha;

// Note that data input above can be use to sample into lightmap (like normal)
builtinData.bakeDiffuseLighting = SampleBakedGI(input.positionWS, surfaceData.normalWS, input.texCoord1, input.texCoord2);
builtinData.bakeDiffuseLighting = SampleBakedGI(input.positionWS, bentNormalWS, input.texCoord1, input.texCoord2);
// It is safe to call this function here as surfaceData have been filled
// We want to know if we must enable transmission on GI for SSS material, if the material have no SSS, this code will be remove by the compiler.

#ifndef LAYERED_LIT_SHADER
// Want to use only one sampler for normalmap either we use OS or TS.
// Want to use only one sampler for normalmap/bentnormalmap either we use OS or TS. And either we have normal map or bent normal or both.
#define SAMPLER_NORMALMAP_IDX sampler_NormalMap
#if defined(_NORMALMAP)
#define SAMPLER_NORMALMAP_IDX sampler_NormalMap
#elif defined(_BENTNORMALMAP)
#define SAMPLER_NORMALMAP_IDX sampler_BentNormalMap
#endif
#define SAMPLER_NORMALMAP_IDX sampler_NormalMapOS
#if defined(_NORMALMAP)
#define SAMPLER_NORMALMAP_IDX sampler_NormalMapOS
#elif defined(_BENTNORMALMAP)
#define SAMPLER_NORMALMAP_IDX sampler_BentNormalMapOS
#endif
#define SAMPLER_SPECULAROCCLUSIONMAP_IDX sampler_SpecularOcclusionMap
#define SAMPLER_HEIGHTMAP_IDX sampler_HeightMap
// include LitDataInternal to define GetSurfaceData

#ifdef _MASKMAP
#define _MASKMAP_IDX
#endif
#ifdef _SPECULAROCCLUSIONMAP
#define _SPECULAROCCLUSIONMAP_IDX
#ifdef _BENTNORMALMAP
#define _BENTNORMALMAP_IDX
#endif
#include "LitDataInternal.hlsl"

ZERO_INITIALIZE(LayerTexCoord, layerTexCoord);
GetLayerTexCoord(input, layerTexCoord);
float depthOffset = ApplyPerPixelDisplacement(input, V, layerTexCoord);
#ifdef _DEPTHOFFSET_ON

// We perform the conversion to world of the normalTS outside of the GetSurfaceData
// so it allow us to correctly deal with detail normal map and optimize the code for the layered shaders
float3 normalTS;
float alpha = GetSurfaceData(input, layerTexCoord, surfaceData, normalTS);
GetNormalAndTangentWS(input, V, normalTS, surfaceData.normalWS, surfaceData.tangentWS);
// Done one time for all layered - cumulate with spec occ alpha for now
surfaceData.specularOcclusion *= GetHorizonOcclusion(V, surfaceData.normalWS, interpolatedVertexNormal, _HorizonFade);
float3 bentNormalTS;
float3 bentNormalWS;
float alpha = GetSurfaceData(input, layerTexCoord, surfaceData, normalTS, bentNormalTS);
GetNormalWS(input, V, normalTS, surfaceData.normalWS);
// Use bent normal to sample GI if available
surfaceData.specularOcclusion = 1.0;
#ifdef _BENTNORMALMAP
GetNormalWS(input, V, bentNormalTS, bentNormalWS);
// If we have bent normal and ambient occlusion, process a specular occlusion
surfaceData.specularOcclusion = 1.0; // TODO
#else
bentNormalWS = surfaceData.normalWS;
#endif
// This is use with anisotropic material
surfaceData.tangentWS = Orthonormalize(surfaceData.tangentWS, surfaceData.normalWS);
GetBuiltinData(input, surfaceData, alpha, depthOffset, builtinData);
GetBuiltinData(input, surfaceData, alpha, bentNormalWS, depthOffset, builtinData);
}
#else

#else
#define SAMPLER_NORMALMAP_IDX sampler_NormalMapOS2
#endif
#else
#elif defined(_NORMALMAP3)
#elif defined(_BENTNORMALMAP0)
#if defined(_NORMALMAP_TANGENT_SPACE0)
#define SAMPLER_NORMALMAP_IDX sampler_BentNormalMap0
#else
#define SAMPLER_NORMALMAP_IDX sampler_BentNormalMapOS0
#endif
#elif defined(_BENTNORMALMAP1)
#if defined(_NORMALMAP_TANGENT_SPACE1)
#define SAMPLER_NORMALMAP_IDX sampler_BentNormalMap1
#else
#define SAMPLER_NORMALMAP_IDX sampler_BentNormalMapOS1
#endif
#elif defined(_BENTNORMALMAP2)
#if defined(_NORMALMAP_TANGENT_SPACE2)
#define SAMPLER_NORMALMAP_IDX sampler_BentNormalMap2
#else
#define SAMPLER_NORMALMAP_IDX sampler_BentNormalMapOS2
#endif
#else
#if defined(_NORMALMAP_TANGENT_SPACE3)
#define SAMPLER_NORMALMAP_IDX sampler_BentNormalMap3
#else
#define SAMPLER_NORMALMAP_IDX sampler_BentNormalMapOS3
#endif
#endif
#if defined(_DETAIL_MAP0)

#define SAMPLER_MASKMAP_IDX sampler_MaskMap3
#endif
#if defined(_SPECULAROCCLUSIONMAP0)
#define SAMPLER_SPECULAROCCLUSIONMAP_IDX sampler_SpecularOcclusionMap0
#elif defined(_SPECULAROCCLUSIONMAP1)
#define SAMPLER_SPECULAROCCLUSIONMAP_IDX sampler_SpecularOcclusionMap1
#elif defined(_SPECULAROCCLUSIONMAP2)
#define SAMPLER_SPECULAROCCLUSIONMAP_IDX sampler_SpecularOcclusionMap2
#else
#define SAMPLER_SPECULAROCCLUSIONMAP_IDX sampler_SpecularOcclusionMap3
#endif
#if defined(_HEIGHTMAP0)
#define SAMPLER_HEIGHTMAP_IDX sampler_HeightMap0
#elif defined(_HEIGHTMAP1)

#ifdef _MASKMAP0
#define _MASKMAP_IDX
#endif
#ifdef _SPECULAROCCLUSIONMAP0
#define _SPECULAROCCLUSIONMAP_IDX
#ifdef _BENTNORMALMAP0
#define _BENTNORMALMAP_IDX
#endif
#include "LitDataInternal.hlsl"
#undef LAYER_INDEX

#undef _DETAIL_MAP_IDX
#undef _MASKMAP_IDX
#undef _SPECULAROCCLUSIONMAP_IDX
#undef _BENTNORMALMAP_IDX
#define LAYER_INDEX 1
#define ADD_IDX(Name) Name##1

#ifdef _MASKMAP1
#define _MASKMAP_IDX
#endif
#ifdef _SPECULAROCCLUSIONMAP1
#define _SPECULAROCCLUSIONMAP_IDX
#ifdef _BENTNORMALMAP1
#define _BENTNORMALMAP_IDX
#endif
#include "LitDataInternal.hlsl"
#undef LAYER_INDEX

#undef _DETAIL_MAP_IDX
#undef _MASKMAP_IDX
#undef _SPECULAROCCLUSIONMAP_IDX
#undef _BENTNORMALMAP_IDX
#define LAYER_INDEX 2
#define ADD_IDX(Name) Name##2

#ifdef _MASKMAP2
#define _MASKMAP_IDX
#endif
#ifdef _SPECULAROCCLUSIONMAP2
#define _SPECULAROCCLUSIONMAP_IDX
#ifdef _BENTNORMALMAP2
#define _BENTNORMALMAP_IDX
#endif
#include "LitDataInternal.hlsl"
#undef LAYER_INDEX

#undef _DETAIL_MAP_IDX
#undef _MASKMAP_IDX
#undef _SPECULAROCCLUSIONMAP_IDX
#undef _BENTNORMALMAP_IDX
#define LAYER_INDEX 3
#define ADD_IDX(Name) Name##3

#ifdef _MASKMAP3
#define _MASKMAP_IDX
#endif
#ifdef _SPECULAROCCLUSIONMAP3
#define _SPECULAROCCLUSIONMAP_IDX
#ifdef _BENTNORMALMAP3
#define _BENTNORMALMAP_IDX
#endif
#include "LitDataInternal.hlsl"
#undef LAYER_INDEX

#undef _DETAIL_MAP_IDX
#undef _MASKMAP_IDX
#undef _SPECULAROCCLUSIONMAP_IDX
#undef _BENTNORMALMAP_IDX
float3 BlendLayeredVector3(float3 x0, float3 x1, float3 x2, float3 x3, float weight[4])
{

outWeights[i] = 0.0f;
}
#if defined(_DENSITY_MODE)
// Note: blendMasks.argb because a is main layer
float4 opacityAsDensity = saturate((inputAlphaMask - (float4(1.0, 1.0, 1.0, 1.0) - blendMasks.argb)) * 20.0); // 20.0 is the number of steps in inputAlphaMask (Density mask. We decided 20 empirically)

SurfaceData surfaceData0, surfaceData1, surfaceData2, surfaceData3;
float3 normalTS0, normalTS1, normalTS2, normalTS3;
float alpha0 = GetSurfaceData0(input, layerTexCoord, surfaceData0, normalTS0);
float alpha1 = GetSurfaceData1(input, layerTexCoord, surfaceData1, normalTS1);
float alpha2 = GetSurfaceData2(input, layerTexCoord, surfaceData2, normalTS2);
float alpha3 = GetSurfaceData3(input, layerTexCoord, surfaceData3, normalTS3);
float3 bentNormalTS0, bentNormalTS1, bentNormalTS2, bentNormalTS3;
float alpha0 = GetSurfaceData0(input, layerTexCoord, surfaceData0, normalTS0, bentNormalTS0);
float alpha1 = GetSurfaceData1(input, layerTexCoord, surfaceData1, normalTS1, bentNormalTS1);
float alpha2 = GetSurfaceData2(input, layerTexCoord, surfaceData2, normalTS2, bentNormalTS2);
float alpha3 = GetSurfaceData3(input, layerTexCoord, surfaceData3, normalTS3, bentNormalTS3);
// Note: If per pixel displacement is enabled it mean we will fetch again the various heightmaps at the intersection location. Not sure the compiler can optimize.
float4 blendMasks = GetBlendMask(layerTexCoord, input.color);

clip(alpha - _AlphaCutoff);
#endif
float3 normalTS = float3(0.0, 0.0, 0.0);
float3 normalTS;
float3 bentNormalTS;
float3 bentNormalWS;
bentNormalTS = ComputeMainNormalInfluence(influenceMask, input, bentNormalTS0, bentNormalTS1, bentNormalTS2, bentNormalTS3, layerTexCoord, blendMasks.a, weights);
}
else
#endif

bentNormalTS = BlendLayeredVector3(bentNormalTS0, bentNormalTS1, bentNormalTS2, bentNormalTS3, weights);
}
surfaceData.perceptualSmoothness = SURFACEDATA_BLEND_SCALAR(surfaceData, perceptualSmoothness, weights);

surfaceData.coatCoverage = 0.0f;
surfaceData.coatIOR = 0.5;
GetNormalAndTangentWS(input, V, normalTS, surfaceData.normalWS, surfaceData.tangentWS);
// Done one time for all layered - cumulate with spec occ alpha for now
surfaceData.specularOcclusion = SURFACEDATA_BLEND_SCALAR(surfaceData, specularOcclusion, weights);
surfaceData.specularOcclusion *= GetHorizonOcclusion(V, surfaceData.normalWS, input.worldToTangent[2].xyz, _HorizonFade);
GetNormalWS(input, V, normalTS, surfaceData.normalWS);
// Use bent normal to sample GI if available
// If any layer use a bent normal map, then bentNormalTS contain the interpolated result of bentnormal and normalmap (in case no bent normal are available)
// Note: the code in LitDataInternal ensure that we fallback on normal map for layer that have no bentnormal
surfaceData.specularOcclusion = 1.0;
#if defined(_BENTNORMALMAP0) || defined(_BENTNORMALMAP1) || defined(_BENTNORMALMAP2) || defined(_BENTNORMALMAP3)
GetNormalWS(input, V, bentNormalTS, bentNormalWS);
// If we have bent normal and ambient occlusion, process a specular occlusion
surfaceData.specularOcclusion = 1.0; // TODO
#else // if no bent normal are available at all just keep the calculation fully
bentNormalWS = surfaceData.normalWS;
#endif
GetBuiltinData(input, surfaceData, alpha, depthOffset, builtinData);
GetBuiltinData(input, surfaceData, alpha, bentNormalWS, depthOffset, builtinData);
}
#endif // #ifndef LAYERED_LIT_SHADER

53
ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/LitDataInternal.hlsl


#endif
}
// Caution: Duplicate from GetBentNormalTS - keep in sync!
float3 ADD_IDX(GetNormalTS)(FragInputs input, LayerTexCoord layerTexCoord, float3 detailNormalTS, float detailMask)
{
float3 normalTS;

return normalTS;
}
// Caution: Duplicate from GetNormalTS - keep in sync!
float3 ADD_IDX(GetBentNormalTS)(FragInputs input, LayerTexCoord layerTexCoord, float3 normalTS, float3 detailNormalTS, float detailMask)
{
float3 bentNormalTS;
#ifdef _BENTNORMALMAP_IDX
#ifdef _NORMALMAP_TANGENT_SPACE_IDX
bentNormalTS = SAMPLE_UVMAPPING_NORMALMAP(ADD_IDX(_BentNormalMap), SAMPLER_NORMALMAP_IDX, ADD_IDX(layerTexCoord.base), ADD_IDX(_NormalScale));
#else // Object space
// We forbid scale in case of object space as it make no sense
// To be able to combine object space normal with detail map then later we will re-transform it to world space.
// Note: There is no such a thing like triplanar with object space normal, so we call directly 2D function
#ifdef SURFACE_GRADIENT
// /We need to decompress the normal ourselve here as UnpackNormalRGB will return a surface gradient
float3 normalOS = SAMPLE_TEXTURE2D(ADD_IDX(_BentNormalMapOS), SAMPLER_NORMALMAP_IDX, ADD_IDX(layerTexCoord.base).uv).xyz * 2.0 - 1.0;
// no need to renormalize normalOS for SurfaceGradientFromPerturbedNormal
bentNormalTS = SurfaceGradientFromPerturbedNormal(input.worldToTangent[2], TransformObjectToWorldDir(normalOS));
#else
float3 normalOS = UnpackNormalRGB(SAMPLE_TEXTURE2D(ADD_IDX(_BentNormalMapOS), SAMPLER_NORMALMAP_IDX, ADD_IDX(layerTexCoord.base).uv), 1.0);
bentNormalTS = TransformObjectToTangent(normalOS, input.worldToTangent);
#endif
#endif
#ifdef _DETAIL_MAP_IDX
#ifdef SURFACE_GRADIENT
bentNormalTS += detailNormalTS * detailMask;
#else
bentNormalTS = lerp(bentNormalTS, BlendNormalRNM(bentNormalTS, detailNormalTS), detailMask);
#endif
#endif
#else
// If there is no bent normal map provided, fallback on regular normal map
bentNormalTS = normalTS;
#endif
return bentNormalTS;
}
float ADD_IDX(GetSurfaceData)(FragInputs input, LayerTexCoord layerTexCoord, out SurfaceData surfaceData, out float3 normalTS)
float ADD_IDX(GetSurfaceData)(FragInputs input, LayerTexCoord layerTexCoord, out SurfaceData surfaceData, out float3 normalTS, out float3 bentNormalTS)
{
float alpha = SAMPLE_UVMAPPING_TEXTURE2D(ADD_IDX(_BaseColorMap), ADD_ZERO_IDX(sampler_BaseColorMap), ADD_IDX(layerTexCoord.base)).a * ADD_IDX(_BaseColor).a;

surfaceData.baseColor *= LerpWhiteTo(2.0 * saturate(detailAlbedo * ADD_IDX(_DetailAlbedoScale)), detailMask);
#endif
#ifdef _SPECULAROCCLUSIONMAP_IDX
// TODO: Do something. For now just take alpha channel
surfaceData.specularOcclusion = SAMPLE_UVMAPPING_TEXTURE2D(ADD_IDX(_SpecularOcclusionMap), SAMPLER_SPECULAROCCLUSIONMAP_IDX, ADD_IDX(layerTexCoord.base)).a;
#else
// The specular occlusion will be perform outside the internal loop
surfaceData.specularOcclusion = 1.0;
#endif
surfaceData.specularOcclusion = 1.0; // Will be setup outside of this function
bentNormalTS = ADD_IDX(GetBentNormalTS)(input, layerTexCoord, normalTS, detailNormalTS, detailMask);
#if defined(_MASKMAP_IDX)
surfaceData.perceptualSmoothness = SAMPLE_UVMAPPING_TEXTURE2D(ADD_IDX(_MaskMap), SAMPLER_MASKMAP_IDX, ADD_IDX(layerTexCoord.base)).a;

#endif
#ifdef _ANISOTROPYMAP
surfaceData.anisotropy = SAMPLE_UVMAPPING_TEXTURE2D(_AnisotropyMap, sampler_AnisotropyMap, layerTexCoord.base).b;
surfaceData.anisotropy = SAMPLE_UVMAPPING_TEXTURE2D(_AnisotropyMap, sampler_AnisotropyMap, layerTexCoord.base).r;
#else
surfaceData.anisotropy = 1.0;
#endif

2
ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/LitProperties.hlsl


float _AlphaCutoff;
float4 _DoubleSidedConstants;
float _HorizonFade;
float _PPDMaxSamples;
float _PPDMinSamples;
float _PPDLodThreshold;

7
ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/LitTessellation.shader


_SmoothnessRemapMin("SmoothnessRemapMin", Float) = 0.0
_SmoothnessRemapMax("SmoothnessRemapMax", Float) = 1.0
_SpecularOcclusionMap("SpecularOcclusion", 2D) = "white" {}
_BentNormalMap("_BentNormalMap", 2D) = "bump" {}
_BentNormalMapOS("_BentNormalMapOS", 2D) = "white" {}
_HeightMap("HeightMap", 2D) = "black" {}
[HideInInspector] _HeightAmplitude("Height Amplitude", Float) = 0.01 // In world units

[ToggleOff] _AlphaCutoffEnable("Alpha Cutoff Enable", Float) = 0.0
_AlphaCutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
_HorizonFade("Horizon fade", Range(0.0, 5.0)) = 1.0
// Stencil state
[HideInInspector] _StencilRef("_StencilRef", Int) = 2 // StencilLightingUsage.RegularLighting (fixed at compile time)

7
ScriptableRenderPipeline/HDRenderPipeline/Material/MaterialUtilities.hlsl


}
// This function convert the tangent space normal/tangent to world space and orthonormalize it + apply a correction of the normal if it is not pointing towards the near plane
void GetNormalAndTangentWS(FragInputs input, float3 V, float3 normalTS, inout float3 normalWS, inout float3 tangentWS)
void GetNormalWS(FragInputs input, float3 V, float3 normalTS, out float3 normalWS)
{
#ifdef SURFACE_GRADIENT
normalWS = SurfaceGradientResolveNormal(input.worldToTangent[2], normalTS);

#endif
// Orthonormalize the basis vectors using the Gram-Schmidt process.
// We assume that the length of the surface normal is sufficiently close to 1.
// This is use with anisotropic material
tangentWS = normalize(tangentWS - dot(tangentWS, normalWS) * normalWS);
}
正在加载...
取消
保存