浏览代码

Moving the default specular value from Lit.hlsl to CommonMaterial.hlsl

Writing the GetSurfaceAndBuiltinData for the fabric shader
Correcting problems in the custom inspector of the Fabric Material
Adding Emissive support for the fabric shader
Correction problems in the Fabric.hlsl shader
/main
Anis Benyoub 6 年前
当前提交
03507596
共有 7 个文件被更改,包括 579 次插入210 次删除
  1. 6
      com.unity.render-pipelines.core/CoreRP/ShaderLibrary/CommonMaterial.hlsl
  2. 549
      com.unity.render-pipelines.high-definition/HDRP/Editor/Material/Fabric/FabricUI.cs
  3. 5
      com.unity.render-pipelines.high-definition/HDRP/Material/Fabric/Fabric.hlsl
  4. 69
      com.unity.render-pipelines.high-definition/HDRP/Material/Fabric/Fabric.shader
  5. 141
      com.unity.render-pipelines.high-definition/HDRP/Material/Fabric/FabricData.hlsl
  6. 17
      com.unity.render-pipelines.high-definition/HDRP/Material/Fabric/FabricProperties.hlsl
  7. 2
      com.unity.render-pipelines.high-definition/HDRP/Material/Lit/Lit.hlsl

6
com.unity.render-pipelines.core/CoreRP/ShaderLibrary/CommonMaterial.hlsl


#define UNITY_COMMON_MATERIAL_INCLUDED
//-----------------------------------------------------------------------------
// Define constants
//-----------------------------------------------------------------------------
#define DEFAULT_SPECULAR_VALUE 0.04
//-----------------------------------------------------------------------------
// Helper functions for roughness
//-----------------------------------------------------------------------------

549
com.unity.render-pipelines.high-definition/HDRP/Editor/Material/Fabric/FabricUI.cs


{
protected static class Styles
{
// Fields
public static GUIContent fabricTypeText = new GUIContent("Fabric Type", "");
public static string emissiveLabelText = "Emissive Inputs";
public static GUIContent fabricTypeText = new GUIContent("Fabric Type", "");
// Primary UV mapping
public static GUIContent UVBaseMappingText = new GUIContent("Base UV mapping", "");
// Base Color
// Fuzz Tint
// Smoothness
// AO
// Mask
// Normal 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 bentNormalMapText = new GUIContent("Bent normal map", "Use only with indirect diffuse lighting (Lightmap/light-probe) - Cosine weighted Bent Normal Map (average un-occluded direction) (BC7/BC5/DXT5(nm))");
public static GUIContent tangentMapText = new GUIContent("Tangent Map", "Tangent Map (BC7/BC5/DXT5(nm))");
// Tangent map
// public static GUIContent tangentMapText = new GUIContent("Tangent Map", "Tangent Map (BC7/BC5/DXT5(nm))");
// Anisotropy
public static GUIContent UVBaseMappingText = new GUIContent("Base UV mapping", "");
// Details
// Detail map
public static GUIContent detailMaskText = new GUIContent("Fuzz Detail (RG)", "Fuzz Detail");
public static GUIContent detailMaskText = new GUIContent("Detail Mask (R)", "Detail Mask");
public static GUIContent detailFuzz1Text = new GUIContent("Fuzz Detail 1", "Fuzz Detail factor");
public static GUIContent detailAOScaleText = new GUIContent("Detail AO", "Detail AO Scale factor");
public static GUIContent detailNormalScaleText = new GUIContent("Detail NormalScale", "Normal Scale factor");

// Subsurface
// Diffusion
// Transmission
public static GUIContent transmissionToggleText = new GUIContent("Transmission Enabled", "Enable/Disable the transmission");
// Subsurface scattering
public static GUIContent subsurfaceToggleText = new GUIContent("Subsurface Enabled", "Enable/Disable the subsurface");
// Thickness
// Specular occlusion
public static GUIContent enableSpecularOcclusionText = new GUIContent("Enable Specular Occlusion from Bent normal", "Require cosine weighted bent normal and cosine weighted ambient occlusion. Specular occlusion for reflection probe");
public static GUIContent specularOcclusionWarning = new GUIContent("Require a cosine weighted bent normal and ambient occlusion maps");
}
// Emissive
public static GUIContent UVMappingEmissiveText = new GUIContent("Emissive UV mapping", "");
public static GUIContent emissiveText = new GUIContent("Emissive Map + Color", "Emissive Map + Color (linear RGB) in nits unit");
public static GUIContent albedoAffectEmissiveText = new GUIContent("Albedo Affect Emissive", "Specifies whether or not the emissive color is multiplied by the albedo.");
public enum FabricType
{
Silk,
CottonWool,
}
public enum UVBaseMapping
{
UV0,
UV1,
UV2,
UV3
// Specular occlusion
// public static GUIContent enableSpecularOcclusionText = new GUIContent("Enable Specular Occlusion from Bent normal", "Require cosine weighted bent normal and cosine weighted ambient occlusion. Specular occlusion for reflection probe");
// public static GUIContent specularOcclusionWarning = new GUIContent("Require a cosine weighted bent normal and ambient occlusion maps");
public enum UVDetailMapping
{
UV0,
UV1,
UV2,
UV3
}
// Fabric Type
protected MaterialProperty fabricType = null;
protected const string kFabricType = "_FabricType";
// Base UV set & mask
// Base color
// Base color map
// Smoothness
// Mask map
protected MaterialProperty maskMap = null;
protected const string kMaskMap = "_MaskMap";
// Smoothness remapping
// AO remapping
protected MaterialProperty maskMap = null;
protected const string kMaskMap = "_MaskMap";
// Normal Scale & Map
protected MaterialProperty bentNormalMap = null;
protected const string kBentNormalMap = "_BentNormalMap";
// protected MaterialProperty bentNormalMap = null;
// protected const string kBentNormalMap = "_BentNormalMap";
// Fuzz Tint
protected MaterialProperty fabricType = null;
protected const string kFabricType = "_FabricType";
// Diffusion profile
// Transmission
protected MaterialProperty enableTransmission = null;
protected const string kEnableTransmission = "_EnableTransmission";
// Subsurface scattering
protected MaterialProperty enableSubsurfaceScattering = null;
protected const string kEnableSubsurfaceScattering = "_EnableSubsurfaceScattering";
// Thickness
protected MaterialProperty thickness = null;
protected const string kThickness = "_Thickness";
protected MaterialProperty thicknessMap = null;

// UV Detail Set & Mask
protected MaterialProperty UVDetailsMappingMask = null;
protected const string kUVDetailsMappingMask = "_UVDetailsMappingMask";
protected MaterialProperty UVMappingMaskDetail = null;
protected const string kUVMappingMaskDetail = "_UVMappingMaskDetail";
// Detail Map
// Detail Mask
// Link detail with base
protected MaterialProperty detailFuzz1 = null;
protected const string kDetailFuzz1 = "_DetailFuzz1";
// Detail adjusting
protected MaterialProperty detailAOScale = null;
protected const string kDetailAOScale = "_DetailAOScale";
protected MaterialProperty detailNormalScale = null;

protected MaterialProperty tangentMap = null;
protected const string kTangentMap = "_TangentMap";
// protected MaterialProperty tangentMap = null;
// protected const string kTangentMap = "_TangentMap";
protected MaterialProperty enableSpecularOcclusion = null;
protected const string kEnableSpecularOcclusion = "_EnableSpecularOcclusion";
// UV Emissive Set & Mask
protected MaterialProperty UVEmissive = null;
protected const string kUVEmissive = "_UVEmissive";
protected MaterialProperty UVMappingMaskEmissive = null;
protected const string kUVMappingMaskEmissive = "_UVMappingMaskEmissive";
// Emissive
protected MaterialProperty emissiveColor = null;
protected const string kEmissiveColor = "_EmissiveColor";
protected MaterialProperty emissiveColorMap = null;
protected const string kEmissiveColorMap = "_EmissiveColorMap";
// protected MaterialProperty enableSpecularOcclusion = null;
// protected const string kEnableSpecularOcclusion = "_EnableSpecularOcclusion";
protected MaterialProperty enableSubsurfaceScattering = null;
protected const string kEnableSubsurfaceScattering = "_EnableSubsurfaceScattering";
protected MaterialProperty enableTransmission = null;
protected const string kEnableTransmission = "_EnableTransmission";
// Fabric Type
fabricType = FindProperty(kFabricType, props);
// Base UV set & mask
// Base Color & Map
// Smoothness
// Mask and remapping values
maskMap = FindProperty(kMaskMap, props);
maskMap = FindProperty(kMaskMap, props);
// Normal map and scale
bentNormalMap = FindProperty(kBentNormalMap, props);
// bentNormalMap = FindProperty(kBentNormalMap, props);
// Fuzz tint
fabricType = FindProperty(kFabricType, props);
// Diffusion profile
diffusionProfileID = FindProperty(kDiffusionProfileID, props);
// Transmission
enableTransmission = FindProperty(kEnableTransmission, props);
diffusionProfileID = FindProperty(kDiffusionProfileID, props);
enableSubsurfaceScattering = FindProperty(kEnableSubsurfaceScattering, props);
// Thickness
// Details
// Details Set and Mask
UVDetailsMappingMask = FindProperty(kUVDetailsMappingMask, props);
linkDetailsWithBase = FindProperty(kLinkDetailsWithBase, props);
UVMappingMaskDetail = FindProperty(kUVMappingMaskDetail, props);
// Detail map and rmapping
detailFuzz1 = FindProperty(kDetailFuzz1, props);
linkDetailsWithBase = FindProperty(kLinkDetailsWithBase, props);
tangentMap = FindProperty(kTangentMap, props);
// tangentMap = FindProperty(kTangentMap, props);
// toggle
enableSubsurfaceScattering = FindProperty(kEnableSubsurfaceScattering, props);
enableTransmission = FindProperty(kEnableTransmission, props);
// UV Emissive set & Mask
UVEmissive = FindProperty(kUVEmissive, props);
UVMappingMaskEmissive = FindProperty(kUVMappingMaskEmissive, props);
// Emissive Data
emissiveColor = FindProperty(kEmissiveColor, props);
emissiveColorMap = FindProperty(kEmissiveColorMap, props);
// Specular occlusion
// enableSpecularOcclusion = FindProperty(kEnableSpecularOcclusion, props);
protected void ShaderSSSAndTransmissionInputGUI(Material material)
public enum FabricType
var hdPipeline = RenderPipelineManager.currentPipeline as HDRenderPipeline;
Silk,
CottonWool,
}
if (hdPipeline == null)
return;
var diffusionProfileSettings = hdPipeline.diffusionProfileSettings;
if (hdPipeline.IsInternalDiffusionProfile(diffusionProfileSettings))
{
EditorGUILayout.HelpBox("No diffusion profile Settings have been assigned to the render pipeline asset.", MessageType.Warning);
return;
}
// TODO: Optimize me
var profiles = diffusionProfileSettings.profiles;
var names = new GUIContent[profiles.Length + 1];
names[0] = new GUIContent("None");
var values = new int[names.Length];
values[0] = DiffusionProfileConstants.DIFFUSION_PROFILE_NEUTRAL_ID;
public enum UVBaseMapping
{
UV0,
UV1,
UV2,
UV3
}
for (int i = 0; i < profiles.Length; i++)
{
names[i + 1] = new GUIContent(profiles[i].name);
values[i + 1] = i + 1;
}
public enum UVDetailMapping
{
UV0,
UV1,
UV2,
UV3
}
using (var scope = new EditorGUI.ChangeCheckScope())
{
int profileID = (int)diffusionProfileID.floatValue;
public enum UVEmissiveMapping
{
UV0,
UV1,
UV2,
UV3
}
using (new EditorGUILayout.HorizontalScope())
{
EditorGUILayout.PrefixLabel(Styles.diffusionProfileText);
protected void BaseUVMappingInputGUI()
{
m_MaterialEditor.ShaderProperty(UVBase, Styles.UVBaseMappingText);
using (new EditorGUILayout.HorizontalScope())
{
profileID = EditorGUILayout.IntPopup(profileID, names, values);
UVBaseMapping uvBaseMapping = (UVBaseMapping)UVBase.floatValue;
if (GUILayout.Button("Goto", EditorStyles.miniButton, GUILayout.Width(50f)))
Selection.activeObject = diffusionProfileSettings;
}
}
float X, Y, Z, W;
X = (uvBaseMapping == UVBaseMapping.UV0) ? 1.0f : 0.0f;
Y = (uvBaseMapping == UVBaseMapping.UV1) ? 1.0f : 0.0f;
Z = (uvBaseMapping == UVBaseMapping.UV2) ? 1.0f : 0.0f;
W = (uvBaseMapping == UVBaseMapping.UV3) ? 1.0f : 0.0f;
if (scope.changed)
diffusionProfileID.floatValue = profileID;
}
UVMappingMask.colorValue = new Color(X, Y, Z, W);
/*
if ((int)materialID.floatValue == (int)MaterialId.LitSSS)
{
m_MaterialEditor.ShaderProperty(subsurfaceMask[layerIndex], Styles.subsurfaceMaskText);
m_MaterialEditor.TexturePropertySingleLine(Styles.subsurfaceMaskMapText, subsurfaceMaskMap[layerIndex]);
}
if ((int)materialID.floatValue == (int)BaseLitGUI.MaterialId.LitTranslucent ||
((int)materialID.floatValue == (int)BaseLitGUI.MaterialId.LitSSS && transmissionEnable.floatValue > 0.0f))
{
m_MaterialEditor.TexturePropertySingleLine(Styles.thicknessMapText, thicknessMap[layerIndex]);
if (thicknessMap[layerIndex].textureValue != null)
{
// Display the remap of texture values.
Vector2 remap = thicknessRemap[layerIndex].vectorValue;
EditorGUI.BeginChangeCheck();
EditorGUILayout.MinMaxSlider(Styles.thicknessRemapText, ref remap.x, ref remap.y, 0.0f, 1.0f);
if (EditorGUI.EndChangeCheck())
{
thicknessRemap[layerIndex].vectorValue = remap;
}
}
else
{
// Allow the user to set the constant value of thickness if no thickness map is provided.
m_MaterialEditor.ShaderProperty(thickness[layerIndex], Styles.thicknessText);
}
}
*/
m_MaterialEditor.TextureScaleOffsetProperty(baseColorMap);
protected void ShaderAnisoInputGUI()
protected void BaseInputGUI(Material material)
m_MaterialEditor.TexturePropertySingleLine(Styles.tangentMapText, tangentMap);
m_MaterialEditor.ShaderProperty(anisotropy, Styles.anisotropyText);
m_MaterialEditor.TexturePropertySingleLine(Styles.anisotropyMapText, anisotropyMap);
}
protected override void MaterialPropertiesGUI(Material material)
{
GUILayout.Label("Fabric Options", EditorStyles.boldLabel);
// The set of inputs offered for customizing the material
EditorGUILayout.LabelField(Styles.InputsText, EditorStyles.boldLabel);
m_MaterialEditor.ShaderProperty(fabricType, Styles.fabricTypeText);
EditorGUI.indentLevel--;
m_MaterialEditor.ShaderProperty(fuzzTint, Styles.fuzzTintText);
EditorGUILayout.LabelField(Styles.InputsText, EditorStyles.boldLabel);
EditorGUI.indentLevel++;
// Define the UV mapping for the base textures
BaseUVMappingInputGUI();
// The base color map and matching base color value
// If no mask texture was provided, we display the smoothness value
else
// If we have a mask map, we do not use values but remapping fields instead
m_MaterialEditor.TexturePropertySingleLine(Styles.maskMapSpecularText, maskMap);
if (maskMap.textureValue != null)
{
float remapMin = smoothnessRemapMin.floatValue;
float remapMax = smoothnessRemapMax.floatValue;

}
}
m_MaterialEditor.TexturePropertySingleLine(Styles.maskMapSpecularText, maskMap);
// The primal normal map field
m_MaterialEditor.TexturePropertySingleLine(Styles.bentNormalMapText, bentNormalMap);
ShaderSSSAndTransmissionInputGUI(material);
ShaderAnisoInputGUI();
// The fuzz tint value (that affects the color of the specular lighting term)
m_MaterialEditor.ShaderProperty(fuzzTint, Styles.fuzzTintText);
EditorGUILayout.Space();
// m_MaterialEditor.TexturePropertySingleLine(Styles.bentNormalMapText, bentNormalMap);
EditorGUI.BeginChangeCheck();
m_MaterialEditor.ShaderProperty(UVBase, Styles.UVBaseMappingText);
// The diffusion/transmission/subsurface gui
ShaderSSSAndTransmissionInputGUI(material);
UVBaseMapping uvBaseMapping = (UVBaseMapping)UVBase.floatValue;
// Anisotropy GUI
ShaderAnisoInputGUI(material);
float X, Y, Z, W;
X = (uvBaseMapping == UVBaseMapping.UV0) ? 1.0f : 0.0f;
Y = (uvBaseMapping == UVBaseMapping.UV1) ? 1.0f : 0.0f;
Z = (uvBaseMapping == UVBaseMapping.UV2) ? 1.0f : 0.0f;
W = (uvBaseMapping == UVBaseMapping.UV3) ? 1.0f : 0.0f;
EditorGUI.indentLevel--;
}
UVMappingMask.colorValue = new Color(X, Y, Z, W);
m_MaterialEditor.TextureScaleOffsetProperty(baseColorMap);
EditorGUI.indentLevel--;
EditorGUILayout.Space();
protected void DetailsInput(Material material)
{
m_MaterialEditor.TexturePropertySingleLine(Styles.detailMaskText, detailMask);
m_MaterialEditor.TexturePropertySingleLine(Styles.detailMaskText, detailMask);
if (material.GetTexture(kDetailMap))
{

// Setup the UVSet for detail, if planar/triplanar is use for base, it will override the mapping of detail (See shader code)
float X, Y, Z, W;
UVDetailsMappingMask.colorValue = new Color(X, Y, Z, W);
UVMappingMaskDetail.colorValue = new Color(X, Y, Z, W);
EditorGUI.indentLevel++;
m_MaterialEditor.ShaderProperty(linkDetailsWithBase, Styles.linkDetailsWithBaseText);

m_MaterialEditor.ShaderProperty(detailFuzz1, Styles.detailFuzz1Text);
//m_MaterialEditor.ShaderProperty(detailFuzz1, Styles.detailFuzz1Text);
m_MaterialEditor.ShaderProperty(detailAOScale, Styles.detailAOScaleText);
m_MaterialEditor.ShaderProperty(detailNormalScale, Styles.detailNormalScaleText);
m_MaterialEditor.ShaderProperty(detailSmoothnessScale, Styles.detailSmoothnessScaleText);

}
protected void ShaderSSSAndTransmissionInputGUI(Material material)
{
var hdPipeline = RenderPipelineManager.currentPipeline as HDRenderPipeline;
if (enableSpecularOcclusion != null)
if (hdPipeline == null)
return;
var diffusionProfileSettings = hdPipeline.diffusionProfileSettings;
if (hdPipeline.IsInternalDiffusionProfile(diffusionProfileSettings))
m_MaterialEditor.ShaderProperty(enableSpecularOcclusion, Styles.enableSpecularOcclusionText);
}
EditorGUILayout.HelpBox("No diffusion profile Settings have been assigned to the render pipeline asset.", MessageType.Warning);
return;
}
// Enable transmission toggle
m_MaterialEditor.ShaderProperty(enableTransmission, Styles.transmissionToggleText);
// Subsurface toggle and options
m_MaterialEditor.ShaderProperty(enableSubsurfaceScattering, Styles.subsurfaceToggleText);
if (enableSubsurfaceScattering.floatValue == 1.0f)
{
m_MaterialEditor.ShaderProperty(subsurfaceMask, Styles.subsurfaceMaskText);
m_MaterialEditor.TexturePropertySingleLine(Styles.subsurfaceMaskMapText, subsurfaceMaskMap);
}
// The thickness sub-menu is toggled if either the transmission or subsurface are requested
if (enableSubsurfaceScattering.floatValue == 1.0f || enableTransmission.floatValue == 1.0f)
{
m_MaterialEditor.TexturePropertySingleLine(Styles.thicknessMapText, thicknessMap);
if (thicknessMap.textureValue != null)
{
// Display the remap of texture values.
Vector2 remap = thicknessRemap.vectorValue;
EditorGUI.BeginChangeCheck();
EditorGUILayout.MinMaxSlider(Styles.thicknessRemapText, ref remap.x, ref remap.y, 0.0f, 1.0f);
if (EditorGUI.EndChangeCheck())
{
thicknessRemap.vectorValue = remap;
}
}
else
{
// Allow the user to set the constant value of thickness if no thickness map is provided.
m_MaterialEditor.ShaderProperty(thickness, Styles.thicknessText);
}
}
// We only need to display the diffusion profile if we have either transmission or diffusion
// TODO: Optimize me
if (enableSubsurfaceScattering.floatValue == 1.0f || enableTransmission.floatValue == 1.0f)
{
var profiles = diffusionProfileSettings.profiles;
var names = new GUIContent[profiles.Length + 1];
names[0] = new GUIContent("None");
var values = new int[names.Length];
values[0] = DiffusionProfileConstants.DIFFUSION_PROFILE_NEUTRAL_ID;
for (int i = 0; i < profiles.Length; i++)
{
names[i + 1] = new GUIContent(profiles[i].name);
values[i + 1] = i + 1;
}
using (var scope = new EditorGUI.ChangeCheckScope())
{
int profileID = (int)diffusionProfileID.floatValue;
using (new EditorGUILayout.HorizontalScope())
{
EditorGUILayout.PrefixLabel(Styles.diffusionProfileText);
using (new EditorGUILayout.HorizontalScope())
{
profileID = EditorGUILayout.IntPopup(profileID, names, values);
if (GUILayout.Button("Goto", EditorStyles.miniButton, GUILayout.Width(50f)))
Selection.activeObject = diffusionProfileSettings;
}
}
if (scope.changed)
diffusionProfileID.floatValue = profileID;
}
}
}
protected void EmissiveInputGUI(Material material)
{
EditorGUILayout.Space();
EditorGUILayout.LabelField(Styles.emissiveLabelText, EditorStyles.boldLabel);
EditorGUI.indentLevel++;
m_MaterialEditor.TexturePropertySingleLine(Styles.emissiveText, emissiveColorMap, emissiveColor);
if (material.GetTexture(kEmissiveColorMap))
{
EditorGUI.indentLevel++;
m_MaterialEditor.ShaderProperty(UVEmissive, Styles.UVMappingEmissiveText);
UVBaseMapping uvEmissiveMapping = (UVBaseMapping)UVEmissive.floatValue;
float X, Y, Z, W;
X = (uvEmissiveMapping == UVBaseMapping.UV0) ? 1.0f : 0.0f;
Y = (uvEmissiveMapping == UVBaseMapping.UV1) ? 1.0f : 0.0f;
Z = (uvEmissiveMapping == UVBaseMapping.UV2) ? 1.0f : 0.0f;
W = (uvEmissiveMapping == UVBaseMapping.UV3) ? 1.0f : 0.0f;
UVMappingMaskEmissive.colorValue = new Color(X, Y, Z, W);
m_MaterialEditor.TextureScaleOffsetProperty(emissiveColorMap);
EditorGUI.indentLevel--;
}
EditorGUI.indentLevel--;
}
protected void ShaderAnisoInputGUI(Material material)
{
// We only have anisotropy for the silk fabric
FabricType fabricType = (FabricType)material.GetFloat(kFabricType);
if(fabricType == FabricType.Silk)
{
//m_MaterialEditor.TexturePropertySingleLine(Styles.tangentMapText, tangentMap);
m_MaterialEditor.ShaderProperty(anisotropy, Styles.anisotropyText);
m_MaterialEditor.TexturePropertySingleLine(Styles.anisotropyMapText, anisotropyMap);
}
}
protected override void MaterialPropertiesGUI(Material material)
{
GUILayout.Label("Fabric Options", EditorStyles.boldLabel);
// The generic type of the fabric (either cotton/wool or silk)
EditorGUI.indentLevel++;
m_MaterialEditor.ShaderProperty(fabricType, Styles.fabricTypeText);
EditorGUI.indentLevel--;
// Base GUI
EditorGUI.indentLevel++;
BaseInputGUI(material);
EditorGUI.indentLevel--;
EditorGUILayout.Space();
// Emissive GUI
EditorGUI.indentLevel++;
EmissiveInputGUI(material);
EditorGUI.indentLevel--;
EditorGUILayout.Space();
// Details Input
EditorGUI.indentLevel++;
DetailsInput(material);
EditorGUI.indentLevel--;
}
protected override void VertexAnimationPropertiesGUI()

// With details map, we always use a normal map and Unity provide a default (0, 0, 1) normal map for it
CoreUtils.SetKeyword(material, "_NORMALMAP", material.GetTexture(kNormalMap) || material.GetTexture(kDetailMap));
CoreUtils.SetKeyword(material, "_TANGENTMAP", material.GetTexture(kTangentMap));
CoreUtils.SetKeyword(material, "_BENTNORMALMAP", material.GetTexture(kBentNormalMap));
// However, the tangent map flag is only bound to the presence of a tangent map
// CoreUtils.SetKeyword(material, "_TANGENTMAP", material.GetTexture(kTangentMap));
// For the moment, we do not support the bent normal map
// CoreUtils.SetKeyword(material, "_BENTNORMALMAP", material.GetTexture(kBentNormalMap));
CoreUtils.SetKeyword(material, "_ENABLESPECULAROCCLUSION", material.GetFloat(kEnableSpecularOcclusion) > 0.0f);
// We do not support specular occlusion for the moment
// CoreUtils.SetKeyword(material, "_ENABLESPECULAROCCLUSION", material.GetFloat(kEnableSpecularOcclusion) > 0.0f);
CoreUtils.SetKeyword(material, "_EMISSIVE_COLOR_MAP", material.GetTexture(kEmissiveColorMap));
bool needUV2 = (UVDetailMapping)material.GetFloat(kUVDetail) == UVDetailMapping.UV2 || (UVBaseMapping)material.GetFloat(kUVBase) == UVBaseMapping.UV2;
bool needUV3 = (UVDetailMapping)material.GetFloat(kUVDetail) == UVDetailMapping.UV3 || (UVBaseMapping)material.GetFloat(kUVBase) == UVBaseMapping.UV3;
// Require and set
bool needUV2 = (UVDetailMapping)material.GetFloat(kUVDetail) == UVDetailMapping.UV2 || (UVBaseMapping)material.GetFloat(kUVBase) == UVBaseMapping.UV2 || (UVBaseMapping)material.GetFloat(kUVEmissive) == UVBaseMapping.UV2;
bool needUV3 = (UVDetailMapping)material.GetFloat(kUVDetail) == UVDetailMapping.UV3 || (UVBaseMapping)material.GetFloat(kUVBase) == UVBaseMapping.UV3 || (UVBaseMapping)material.GetFloat(kUVEmissive) == UVBaseMapping.UV2;
if (needUV3)
{

material.DisableKeyword("_REQUIRE_UV3");
}
// Fetch the fabric type
FabricType fabricType = (FabricType)material.GetFloat(kFabricType);
// If the material is of type cotton/wool we inject it! Otherwise it is necessarily of silk/anisotropy type (we don't inject it to save keywords)
CoreUtils.SetKeyword(material, "_MATERIAL_FEATURE_COTTON_WOOL", fabricType == FabricType.CottonWool);
FabricType fabricType = (FabricType)material.GetFloat(kFabricType);
CoreUtils.SetKeyword(material, "_MATERIAL_FEATURE_ANISOTROPY", fabricType == FabricType.Silk);
}
}
} // namespace UnityEditor

5
com.unity.render-pipelines.high-definition/HDRP/Material/Fabric/Fabric.hlsl


bsdfData.ambientOcclusion = surfaceData.ambientOcclusion;
bsdfData.fuzzTint = surfaceData.fuzzTint;
bsdfData.fresnel0 = DEFAULT_SPECULAR_VALUE;
// Note: we have ZERO_INITIALIZE the struct so bsdfData.anisotropy == 0.0
// Note: DIFFUSION_PROFILE_NEUTRAL_ID is 0

FillMaterialTransmission(surfaceData.diffusionProfile, surfaceData.thickness, bsdfData);
}
if (HasFlag(surfaceData.materialFeatures, MATERIALFEATUREFLAGS_FABRIC_SILK))
if (!HasFlag(surfaceData.materialFeatures, MATERIALFEATUREFLAGS_FABRIC_COTTON_WOOL))
{
FillMaterialAnisotropy(surfaceData.anisotropy, surfaceData.tangentWS, cross(surfaceData.normalWS, surfaceData.tangentWS), bsdfData);
}

// Note: diffuseLighting is multiply by color in PostEvaluateBSDF
diffuseLighting = DisneyDiffuse(NdotV, NdotL, LdotV, bsdfData.perceptualRoughness);
}
specularLighting *= bsdfData.fuzzTint;
}
//-----------------------------------------------------------------------------

69
com.unity.render-pipelines.high-definition/HDRP/Material/Fabric/Fabric.shader


// Reminder. Color here are in linear but the UI (color picker) do the conversion sRGB to linear
// Be careful, do not change the name here to _Color. It will conflict with the "fake" parameters (see end of properties) required for GI.
// Fabric type
[Enum(Silk, 0, CottonWool, 1)] _FabricType("Fabric Type", Float) = 0
// Value used to define which uv channel is used for the first set of textures
[Enum(UV0, 0, UV1, 1, UV2, 2, UV3, 3)] _UVBase("UV Set for base", Float) = 0
[HideInInspector] _UVMappingMask("_UVMappingMask", Color) = (1, 0, 0, 0)
// Base color
// Normal map
_NormalMap("NormalMap", 2D) = "bump" {} // Tangent space normal map
_NormalScale("_NormalScale", Range(0.0, 2.0)) = 1
// TODO
// _TangentMap("TangentMap", 2D) = "bump" {}
// Smoothness values (overriden by the mask map)
// The mask texture and the matching remapping values for it
_AORemapMin("AORemapMin", Float) = 0.0
_AORemapMax("AORemapMax", Float) = 1.0
_AORemapMin("AORemapMin", Float) = 0.0
_AORemapMax("AORemapMax", Float) = 1.0
_NormalMap("NormalMap", 2D) = "bump" {} // Tangent space normal map
_NormalScale("_NormalScale", Range(0.0, 2.0)) = 1
// TODO
//_BentNormalMap("_BentNormalMap", 2D) = "bump" {}
_BentNormalMap("_BentNormalMap", 2D) = "bump" {}
// Fuzz Tint
[Enum(Silk, 0, CottonWool, 1)] _FabricType("Fabric Type", Float) = 0
// Detail Data
[Enum(UV0, 0, UV1, 1, UV2, 2, UV3, 3)] _UVDetail("UV Set for detail", Float) = 0
[HideInInspector] _UVMappingMaskDetail("_UVMappingMaskDetail", Color) = (1, 0, 0, 0)
_DetailFuzz1("_DetailFuzz1", Range(0.0, 1.0)) = 1
[ToggleUI] _LinkDetailsWithBase("LinkDetailsWithBase", Float) = 1.0
[Enum(UV0, 0, UV1, 1, UV2, 2, UV3, 3)] _UVDetail("UV Set for detail", Float) = 0
[HideInInspector] _UVDetailsMappingMask("_UVDetailsMappingMask", Color) = (1, 0, 0, 0)
[ToggleUI] _LinkDetailsWithBase("LinkDetailsWithBase", Float) = 1.0
// Emissive Data
[Enum(UV0, 0, UV1, 1, UV2, 2, UV3, 3)] _UVEmissive("UV Set for emissive", Float) = 0
[HideInInspector] _UVMappingMaskEmissive("_UVMappingMaskEmissive", Color) = (1, 0, 0, 0)
[HDR] _EmissiveColor("EmissiveColor", Color) = (0, 0, 0)
_EmissiveColorMap("EmissiveColorMap", 2D) = "white" {}
[ToggleUI] _AlbedoAffectEmissive("Albedo Affect Emissive", Float) = 0.0
_TangentMap("TangentMap", 2D) = "bump" {}
// Anisotropy Data
// Diffusion Data
// Transmission Data
[ToggleUI] _EnableTransmission("_EnableTransmission", Float) = 0.0
// Subsurface Data
[ToggleUI] _EnableSubsurfaceScattering("_EnableSubsurfaceScattering", Float) = 0.0
// Thickness Data
[Enum(UV0, 0, UV1, 1, UV2, 2, UV3, 3)] _UVBase("UV Set for base", Float) = 0
[HideInInspector] _UVMappingMask("_UVMappingMask", Color) = (1, 0, 0, 0)
[ToggleUI] _EnableSpecularOcclusion("Enable specular occlusion", Float) = 0.0
[ToggleUI] _EnableSubsurfaceScattering("_EnableSubsurfaceScattering", Float) = 0.0
[ToggleUI] _EnableTransmission("_EnableTransmission", Float) = 0.0
//[ToggleUI] _EnableSpecularOcclusion("Enable specular occlusion", Float) = 0.0
// Transparency
[ToggleUI] _PreRefractionPass("PreRefractionPass", Float) = 0.0

[ToggleUI] _EnableFogOnTransparent("Enable Fog", Float) = 1.0
[ToggleUI] _EnableBlendModePreserveSpecularLighting("Enable Blend Mode Preserve Specular Lighting", Float) = 1.0
_EmissionColor("Color", Color) = (1, 1, 1)
// HACK: GI Baking system relies on some properties existing in the shader ("_MainTex", "_Cutoff" and "_Color") for opacity handling, so we need to store our version of those parameters in the hard-coded name the GI baking system recognizes.
_MainTex("Albedo", 2D) = "white" {}
_Color("Color", Color) = (1,1,1,1)

#pragma shader_feature _DETAIL_MAP
#pragma shader_feature _SUBSURFACE_MASK_MAP
#pragma shader_feature _THICKNESSMAP
#pragma shader_feature _EMISSIVE_COLOR_MAP
// Keyword for transparent
#pragma shader_feature _SURFACE_TYPE_TRANSPARENT

// MaterialFeature are used as shader feature to allow compiler to optimize properly
#pragma shader_feature _MATERIAL_FEATURE_SUBSURFACE_SCATTERING
#pragma shader_feature _MATERIAL_FEATURE_TRANSMISSION
#pragma shader_feature _MATERIAL_FEATURE_ANISOTROPY // Anisotropy is for Silk
#pragma shader_feature _MATERIAL_FEATURE_COTTON_WOOL
//enable GPU instancing support
#pragma multi_compile_instancing

141
com.unity.render-pipelines.high-definition/HDRP/Material/Fabric/FabricData.hlsl


//-------------------------------------------------------------------------------------
// Fill SurfaceData/Builtin data function
//-------------------------------------------------------------------------------------
#include "CoreRP/ShaderLibrary/Sampling/SampleUVMapping.hlsl"
#include "HDRP/Material/MaterialUtilities.hlsl"
void GetSurfaceAndBuiltinData(FragInputs input, float3 V, inout PositionInputs posInput, out SurfaceData surfaceData, out BuiltinData builtinData)

surfaceData.normalWS = input.worldToTangent[2].xyz;
// Transform the preprocess macro into a material feature (note that silk flag is deduced from the abscence of this one)
#ifdef _MATERIAL_FEATURE_COTTON_WOOL
surfaceData.materialFeatures = MATERIALFEATUREFLAGS_FABRIC_COTTON_WOOL;
#endif
#ifdef _MATERIAL_FEATURE_SUBSURFACE_SCATTERING
surfaceData.materialFeatures |= MATERIALFEATUREFLAGS_FABRIC_SUBSURFACE_SCATTERING;
#endif
#ifdef _MATERIAL_FEATURE_TRANSMISSION
surfaceData.materialFeatures |= MATERIALFEATUREFLAGS_FABRIC_TRANSMISSION;
#endif
// Generate the primary uv coordinates
float2 uvBase = _UVMappingMask.x * input.texCoord0 +
_UVMappingMask.y * input.texCoord1 +
_UVMappingMask.z * input.texCoord2 +
_UVMappingMask.w * input.texCoord3;
// Apply tiling and offset
uvBase = uvBase * _BaseColorMap_ST.xy + _BaseColorMap_ST.zw;
// Generate the detail uv coordinates
float2 uvDetails = _UVMappingMaskDetail.x * input.texCoord0 +
_UVMappingMaskDetail.y * input.texCoord1 +
_UVMappingMaskDetail.z * input.texCoord2 +
_UVMappingMaskDetail.w * input.texCoord3;
// Apply offset and tiling
uvDetails = uvDetails * _DetailMap_ST.xy + _DetailMap_ST.zw;
// We need to start by reading the detail (if any available to override the initial values)
#ifdef _DETAIL_MAP
float4 detailMasks = SAMPLE_TEXTURE2D(_DetailMask, sampler_DetailMask, uvDetails);
float2 detailAOAndSmoothness = SAMPLE_TEXTURE2D(_DetailMap, sampler_DetailMap, uvDetails).rb;
float detailAO = detailAOAndSmoothness.r * 2.0 - 1.0;
float detailSmoothness = detailAOAndSmoothness.g * 2.0 - 1.0;
// Handle the normal detail
float2 detailDerivative = UnpackDerivativeNormalRGorAG(SAMPLE_TEXTURE2D(_DetailMap, sampler_DetailMap, uvDetails), _DetailNormalScale);
float3 detailGradient = SurfaceGradientFromTBN(detailDerivative, input.worldToTangent[0], input.worldToTangent[1]);
#else
float3 detailGradient = float3(0.0, 0.0, 0.0);
float detailMask = 0.0;
#endif
// The base color of the object mixed with the base color texture
surfaceData.baseColor = SAMPLE_TEXTURE2D(_BaseColorMap, sampler_BaseColorMap, uvBase).rgb * _BaseColor.rgb;
#ifdef _DETAIL_MAP
float aoDetailSpeed = saturate(abs(detailAO) * _DetailAOScale);
float3 baseColorOverlay = lerp(sqrt(surfaceData.baseColor), (detailAO < 0.0) ? float3(0.0, 0.0, 0.0) : float3(1.0, 1.0, 1.0), aoDetailSpeed * aoDetailSpeed);
baseColorOverlay *= baseColorOverlay;
surfaceData.baseColor = lerp(surfaceData.baseColor, saturate(baseColorOverlay), detailMask.x);
#endif
// Extract the alpha value (will be useful if we need to trigger the alpha test)
float alpha = SAMPLE_TEXTURE2D(_BaseColorMap, sampler_BaseColorMap, uvBase).a * _BaseColor.a;
#ifdef _NORMALMAP
float2 derivative = UnpackDerivativeNormalRGorAG(SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, uvBase), _NormalScale);
float3 gradient = SurfaceGradientFromTBN(derivative, input.worldToTangent[0], input.worldToTangent[1]) + detailGradient * detailMasks.x;
surfaceData.normalWS = SurfaceGradientResolveNormal(input.worldToTangent[2], gradient);
#else
surfaceData.normalWS = input.worldToTangent[2];
#endif
// Make the tagent match the normal
surfaceData.tangentWS = Orthonormalize(input.worldToTangent[0], surfaceData.normalWS);
#ifdef _MASKMAP
float4 maskSample = SAMPLE_TEXTURE2D(_MaskMap, sampler_MaskMap, uvBase);
#endif
#ifdef _MASKMAP
surfaceData.ambientOcclusion = lerp(_AORemapMin, _AORemapMax, maskSample.y);
surfaceData.perceptualSmoothness = lerp(_SmoothnessRemapMin, _SmoothnessRemapMax, maskSample.w);
surfaceData.specularOcclusion = GetSpecularOcclusionFromAmbientOcclusion(ClampNdotV(dot(surfaceData.normalWS, V)), surfaceData.ambientOcclusion, PerceptualSmoothnessToRoughness(surfaceData.perceptualSmoothness));
#else
surfaceData.perceptualSmoothness = _Smoothness;
surfaceData.ambientOcclusion = 1.0;
surfaceData.specularOcclusion = 1.0;
#endif
// If a detail map was provided, modify the matching smoothness
#ifdef _DETAIL_MAP
float smoothnessDetailSpeed = saturate(abs(detailSmoothness) * _DetailSmoothnessScale);
float smoothnessOverlay = lerp(surfaceData.perceptualSmoothness, (detailSmoothness < 0.0) ? 0.0 : 1.0, smoothnessDetailSpeed);
surfaceData.perceptualSmoothness = lerp(surfaceData.perceptualSmoothness, saturate(smoothnessOverlay), detailMask.x);
#endif
float2 baseColorMapUv = TRANSFORM_TEX(input.texCoord0, _BaseColorMap);
surfaceData.baseColor = SAMPLE_TEXTURE2D(_BaseColorMap, sampler_BaseColorMap, baseColorMapUv).rgb * _BaseColor.rgb;
float alpha = SAMPLE_TEXTURE2D(_BaseColorMap, sampler_BaseColorMap, baseColorMapUv).a * _BaseColor.a;
// Propagate the fuzz tint
surfaceData.fuzzTint = _FuzzTint.xyz;
#ifdef _MATERIAL_FEATURE_SUBSURFACE_SCATTERING
surfaceData.diffusionProfile = _DiffusionProfile;
#ifdef _SUBSURFACEMASK
float4 subSurfaceMaskSample = SAMPLE_TEXTURE2D(_SubsurfaceMaskMap, sampler_SubsurfaceMaskMap, uvBase);
surfaceData.subsurfaceMask = subSurfaceMaskSample.x;
#else
surfaceData.subsurfaceMask = _SubsurfaceMask;
#endif
#endif
#ifdef _MATERIALFEATUREFLAGS_FABRIC_TRANSMISSION
float4 subSurfaceMaskSample = SAMPLE_TEXTURE2D(_ThicknessMap, sampler_ThicknessMap, uvBase);
surfaceData.thickness = dot(SAMPLE_TEXTURE2D_SCALE_BIAS(_ThicknessMap), _ThicknessMapChannelMask);
surfaceData.thickness = lerp(_ThicknessMapRange.x, _ThicknessMapRange.y, surfaceData.thickness);
surfaceData.thickness = lerp(_Thickness, surfaceData.thickness, _ThicknessUseMap);
surfaceData.thickness = _ThicknessRemap.x + surfaceData.thickness * _ThicknessRemap.y;
#else
surfaceData.thickness = _Thickness;
#endif
#ifdef _ANISOTROPYMAP
surfaceData.anisotropy = SAMPLE_TEXTURE2D(_AnisotropyMap, sample_AnisotropyMap, uvBase).x;
#else
surfaceData.anisotropy = _Anisotropy;
#endif
#ifdef _ALPHATEST_ON
DoAlphaTest(alpha, _AlphaCutoff);

if (_DebugMipMapMode != DEBUGMIPMAPMODE_NONE)
{
surfaceData.baseColor = GetTextureDataDebug(_DebugMipMapMode, baseColorMapUv, _BaseColorMap, _BaseColorMap_TexelSize, _BaseColorMap_MipInfo, surfaceData.baseColor);
surfaceData.baseColor = GetTextureDataDebug(_DebugMipMapMode, uvBase, _BaseColorMap, _BaseColorMap_TexelSize, _BaseColorMap_MipInfo, surfaceData.baseColor);
}
#endif

// For back lighting we use the oposite vertex normal
InitBuiltinData(alpha, surfaceData.normalWS, -input.worldToTangent[2], input.positionRWS, input.texCoord1, input.texCoord2, builtinData);
// Support the emissive color and map
builtinData.emissiveColor = _EmissiveColor * lerp(float3(1.0, 1.0, 1.0), surfaceData.baseColor.rgb, _AlbedoAffectEmissive);
#ifdef _EMISSIVE_COLOR_MAP
// Generate the primart uv coordinates
float2 uvEmissive = _UVMappingMaskEmissive.x * input.texCoord0 +
_UVMappingMaskEmissive.y * input.texCoord1 +
_UVMappingMaskEmissive.z * input.texCoord2 +
_UVMappingMaskEmissive.w * input.texCoord3;
uvEmissive = uvEmissive * _EmissiveColorMap_ST.xy + _EmissiveColorMap_ST.zw;
builtinData.emissiveColor *= SAMPLE_TEXTURE2D(_EmissiveColorMap, sampler_EmissiveColorMap, uvEmissive).rgb;
#endif
PostInitBuiltinData(V, posInput, surfaceData, builtinData);
}

17
com.unity.render-pipelines.high-definition/HDRP/Material/Fabric/FabricProperties.hlsl


TEXTURE2D(_MaskMap);
SAMPLER(sampler_MaskMap);
TEXTURE2D(_BentNormalMap); // Reuse sampler from normal map
SAMPLER(sampler_BentNormalMap);
//TEXTURE2D(_BentNormalMap); // Reuse sampler from normal map
//SAMPLER(sampler_BentNormalMap);
TEXTURE2D(_NormalMap);
SAMPLER(sampler_NormalMap);

TEXTURE2D(_ThicknessMap);
SAMPLER(sampler_ThicknessMap);
TEXTURE2D(_EmissiveColorMap);
SAMPLER(sampler_EmissiveColorMap);
float4 _UVMappingMask;
float4 _UVMappingMaskDetail;
float4 _UVMappingMaskEmissive;
float4 _FuzzTint;
float _AlphaCutoff;

float _SubsurfaceMask;
float _Thickness;
float4 _ThicknessRemap;
float4 _EmissiveColorMap_ST;
float4 _EmissiveColor;
float _AlbedoAffectEmissive;
CBUFFER_END

2
com.unity.render-pipelines.high-definition/HDRP/Material/Lit/Lit.hlsl


#define HAS_REFRACTION (defined(_REFRACTION_PLANE) || defined(_REFRACTION_SPHERE)) && (defined(_REFRACTION_SSRAY_PROXY) || defined(_REFRACTION_SSRAY_HIZ))
#define DEFAULT_SPECULAR_VALUE 0.04
// Enum for materialFeatureId (only use for encode/decode GBuffer)
#define GBUFFER_LIT_STANDARD 0
// we have not enough space (3bit) to store mat feature to have SSS and Transmission as bitmask, such why we have all variant

正在加载...
取消
保存