浏览代码

Update cloth shader with CharlieD BSDF

/main
Anis Benyoub 6 年前
当前提交
833ae5fe
共有 4 个文件被更改,包括 293 次插入31 次删除
  1. 130
      com.unity.render-pipelines.high-definition/HDRP/Material/Cloth/Cloth.cs
  2. 165
      com.unity.render-pipelines.high-definition/HDRP/Material/Cloth/Cloth.cs.hlsl
  3. 26
      com.unity.render-pipelines.high-definition/HDRP/Material/Cloth/Cloth.hlsl
  4. 3
      com.unity.render-pipelines.high-definition/HDRP/Material/Cloth/ClothData.hlsl

130
com.unity.render-pipelines.high-definition/HDRP/Material/Cloth/Cloth.cs


using UnityEngine;
using UnityEngine.Rendering;
//-----------------------------------------------------------------------------
// structure definition

public class ShaderName : RenderPipelineMaterial
public class Cloth : RenderPipelineMaterial
// If change, be sure it match what is done in Lit.hlsl: MaterialFeatureFlagsFromGBuffer
// Material bit mask must match the size define LightDefinitions.s_MaterialFeatureMaskFlags value
[GenerateHLSL(PackingRules.Exact)]
public enum MaterialFeatureFlags
{
ClothCottonWool = 1 << 0,
ClothSilk = 1 << 1,
ClothSubsurfaceScattering = 1 << 2,
CothTransmission = 1 << 3
};
// The number '10000' must be a unique value across all material. It give an id to each of the parameters
// so debug material mode can retrieve it and display it. With 10000, it mean that debug values
// are sign from 10000 with incremental order.
[GenerateHLSL(PackingRules.Exact, false, true, 10000)]
[GenerateHLSL(PackingRules.Exact, false, true, 1300)]
[SurfaceDataAttributes("MaterialFeatures")]
public uint materialFeatures;
[SurfaceDataAttributes("Normal", true)]
[SurfaceDataAttributes("Specular Occlusion")]
public float specularOcclusion;
[SurfaceDataAttributes(new string[] { "Normal", "Normal View Space" }, true)]
[SurfaceDataAttributes("Smoothness")]
public float perceptualSmoothness;
[SurfaceDataAttributes("Ambient Occlusion")]
public float ambientOcclusion;
// Fuzz Tint
[SurfaceDataAttributes("Fuzz Tint", false, true)]
public Vector3 fuzzTint;
// MaterialFeature dependent attribute
// SSS
[SurfaceDataAttributes("Diffusion Profile")]
public uint diffusionProfile;
[SurfaceDataAttributes("Subsurface Mask")]
public float subsurfaceMask;
// Transmission
// + Diffusion Profile
[SurfaceDataAttributes("Thickness")]
public float thickness;
// Anisotropic
[SurfaceDataAttributes("Tangent", true)]
public Vector3 tangentWS;
[SurfaceDataAttributes("Anisotropy")]
public float anisotropy; // anisotropic ratio(0->no isotropic; 1->full anisotropy in tangent direction, -1->full anisotropy in bitangent direction)
};
//-----------------------------------------------------------------------------

[GenerateHLSL(PackingRules.Exact, false, true, 10100)]
[GenerateHLSL(PackingRules.Exact, false, true, 1350)]
public uint materialFeatures;
[SurfaceDataAttributes("Normal", true)]
public Vector3 fresnel0;
public float specularOcclusion;
[SurfaceDataAttributes(new string[] { "Normal WS", "Normal View Space" }, true)]
public float perceptualRoughness;
// MaterialFeature dependent attribute
// SSS
public uint diffusionProfile;
public float subsurfaceMask;
// Transmission
// + Diffusion Profile
public float thickness;
public bool useThickObjectMode; // Read from the diffusion profile
public Vector3 transmittance; // Precomputation of transmittance
// Anisotropic
[SurfaceDataAttributes("", true)]
public Vector3 tangentWS;
[SurfaceDataAttributes("", true)]
public Vector3 bitangentWS;
public float roughnessT;
public float roughnessB;
public float anisotropy;
//-----------------------------------------------------------------------------
// Init precomputed textures
//-----------------------------------------------------------------------------
bool m_isInit;
public Cloth() { }
public override void Build(HDRenderPipelineAsset hdAsset)
{
PreIntegratedFGD.instance.Build(PreIntegratedFGD.FGDIndex.FGD_CharlieAndClothLambert);
//LTCAreaLight.instance.Build();
m_isInit = false;
}
public override void Cleanup()
{
PreIntegratedFGD.instance.Cleanup(PreIntegratedFGD.FGDIndex.FGD_CharlieAndClothLambert);
//LTCAreaLight.instance.Cleanup();
m_isInit = false;
}
public override void RenderInit(CommandBuffer cmd)
{
if (m_isInit)
return;
PreIntegratedFGD.instance.RenderInit(PreIntegratedFGD.FGDIndex.FGD_CharlieAndClothLambert, cmd);
m_isInit = true;
}
public override void Bind()
{
PreIntegratedFGD.instance.Bind(PreIntegratedFGD.FGDIndex.FGD_CharlieAndClothLambert);
//LTCAreaLight.instance.Bind();
}
}
}

165
com.unity.render-pipelines.high-definition/HDRP/Material/Cloth/Cloth.cs.hlsl


// This file was automatically generated. Please don't edit by hand.
//
#ifndef SHADERNAME_CS_HLSL
#define SHADERNAME_CS_HLSL
#ifndef CLOTH_CS_HLSL
#define CLOTH_CS_HLSL
// UnityEngine.Experimental.Rendering.HDPipeline.ShaderName+SurfaceData: static fields
// UnityEngine.Experimental.Rendering.HDPipeline.Cloth+MaterialFeatureFlags: static fields
#define DEBUGVIEW_SHADERNAME_SURFACEDATA_BASE_COLOR (10000)
#define DEBUGVIEW_SHADERNAME_SURFACEDATA_NORMAL (10001)
#define MATERIALFEATUREFLAGS_CLOTH_COTTON_WOOL (1)
#define MATERIALFEATUREFLAGS_CLOTH_SILK (2)
#define MATERIALFEATUREFLAGS_CLOTH_SUBSURFACE_SCATTERING (4)
#define MATERIALFEATUREFLAGS_COTH_TRANSMISSION (8)
// UnityEngine.Experimental.Rendering.HDPipeline.ShaderName+BSDFData: static fields
// UnityEngine.Experimental.Rendering.HDPipeline.Cloth+SurfaceData: static fields
#define DEBUGVIEW_SHADERNAME_BSDFDATA_DIFFUSE_COLOR (10100)
#define DEBUGVIEW_SHADERNAME_BSDFDATA_NORMAL (10101)
#define DEBUGVIEW_CLOTH_SURFACEDATA_MATERIAL_FEATURES (1300)
#define DEBUGVIEW_CLOTH_SURFACEDATA_BASE_COLOR (1301)
#define DEBUGVIEW_CLOTH_SURFACEDATA_SPECULAR_OCCLUSION (1302)
#define DEBUGVIEW_CLOTH_SURFACEDATA_NORMAL (1303)
#define DEBUGVIEW_CLOTH_SURFACEDATA_NORMAL_VIEW_SPACE (1304)
#define DEBUGVIEW_CLOTH_SURFACEDATA_SMOOTHNESS (1305)
#define DEBUGVIEW_CLOTH_SURFACEDATA_AMBIENT_OCCLUSION (1306)
#define DEBUGVIEW_CLOTH_SURFACEDATA_FUZZ_TINT (1307)
#define DEBUGVIEW_CLOTH_SURFACEDATA_DIFFUSION_PROFILE (1308)
#define DEBUGVIEW_CLOTH_SURFACEDATA_SUBSURFACE_MASK (1309)
#define DEBUGVIEW_CLOTH_SURFACEDATA_THICKNESS (1310)
#define DEBUGVIEW_CLOTH_SURFACEDATA_TANGENT (1311)
#define DEBUGVIEW_CLOTH_SURFACEDATA_ANISOTROPY (1312)
//
// UnityEngine.Experimental.Rendering.HDPipeline.Cloth+BSDFData: static fields
//
#define DEBUGVIEW_CLOTH_BSDFDATA_MATERIAL_FEATURES (1350)
#define DEBUGVIEW_CLOTH_BSDFDATA_DIFFUSE_COLOR (1351)
#define DEBUGVIEW_CLOTH_BSDFDATA_FRESNEL0 (1352)
#define DEBUGVIEW_CLOTH_BSDFDATA_SPECULAR_OCCLUSION (1353)
#define DEBUGVIEW_CLOTH_BSDFDATA_NORMAL_WS (1354)
#define DEBUGVIEW_CLOTH_BSDFDATA_NORMAL_VIEW_SPACE (1355)
#define DEBUGVIEW_CLOTH_BSDFDATA_PERCEPTUAL_ROUGHNESS (1356)
#define DEBUGVIEW_CLOTH_BSDFDATA_DIFFUSION_PROFILE (1357)
#define DEBUGVIEW_CLOTH_BSDFDATA_SUBSURFACE_MASK (1358)
#define DEBUGVIEW_CLOTH_BSDFDATA_THICKNESS (1359)
#define DEBUGVIEW_CLOTH_BSDFDATA_USE_THICK_OBJECT_MODE (1360)
#define DEBUGVIEW_CLOTH_BSDFDATA_TRANSMITTANCE (1361)
#define DEBUGVIEW_CLOTH_BSDFDATA_TANGENT_WS (1362)
#define DEBUGVIEW_CLOTH_BSDFDATA_BITANGENT_WS (1363)
#define DEBUGVIEW_CLOTH_BSDFDATA_ROUGHNESS_T (1364)
#define DEBUGVIEW_CLOTH_BSDFDATA_ROUGHNESS_B (1365)
#define DEBUGVIEW_CLOTH_BSDFDATA_ANISOTROPY (1366)
// Generated from UnityEngine.Experimental.Rendering.HDPipeline.ShaderName+SurfaceData
// Generated from UnityEngine.Experimental.Rendering.HDPipeline.Cloth+SurfaceData
uint materialFeatures;
float specularOcclusion;
float perceptualSmoothness;
float ambientOcclusion;
float3 fuzzTint;
uint diffusionProfile;
float subsurfaceMask;
float thickness;
float3 tangentWS;
float anisotropy;
// Generated from UnityEngine.Experimental.Rendering.HDPipeline.ShaderName+BSDFData
// Generated from UnityEngine.Experimental.Rendering.HDPipeline.Cloth+BSDFData
uint materialFeatures;
float3 fresnel0;
float specularOcclusion;
float perceptualRoughness;
uint diffusionProfile;
float subsurfaceMask;
float thickness;
bool useThickObjectMode;
float3 transmittance;
float3 tangentWS;
float3 bitangentWS;
float roughnessT;
float roughnessB;
float anisotropy;
};
//

{
switch (paramId)
{
case DEBUGVIEW_SHADERNAME_SURFACEDATA_BASE_COLOR:
case DEBUGVIEW_CLOTH_SURFACEDATA_MATERIAL_FEATURES:
result = GetIndexColor(surfacedata.materialFeatures);
break;
case DEBUGVIEW_CLOTH_SURFACEDATA_BASE_COLOR:
case DEBUGVIEW_SHADERNAME_SURFACEDATA_NORMAL:
case DEBUGVIEW_CLOTH_SURFACEDATA_SPECULAR_OCCLUSION:
result = surfacedata.specularOcclusion.xxx;
break;
case DEBUGVIEW_CLOTH_SURFACEDATA_NORMAL:
result = surfacedata.normalWS * 0.5 + 0.5;
break;
case DEBUGVIEW_CLOTH_SURFACEDATA_NORMAL_VIEW_SPACE:
case DEBUGVIEW_CLOTH_SURFACEDATA_SMOOTHNESS:
result = surfacedata.perceptualSmoothness.xxx;
break;
case DEBUGVIEW_CLOTH_SURFACEDATA_AMBIENT_OCCLUSION:
result = surfacedata.ambientOcclusion.xxx;
break;
case DEBUGVIEW_CLOTH_SURFACEDATA_FUZZ_TINT:
result = surfacedata.fuzzTint;
needLinearToSRGB = true;
break;
case DEBUGVIEW_CLOTH_SURFACEDATA_DIFFUSION_PROFILE:
result = GetIndexColor(surfacedata.diffusionProfile);
break;
case DEBUGVIEW_CLOTH_SURFACEDATA_SUBSURFACE_MASK:
result = surfacedata.subsurfaceMask.xxx;
break;
case DEBUGVIEW_CLOTH_SURFACEDATA_THICKNESS:
result = surfacedata.thickness.xxx;
break;
case DEBUGVIEW_CLOTH_SURFACEDATA_TANGENT:
result = surfacedata.tangentWS * 0.5 + 0.5;
break;
case DEBUGVIEW_CLOTH_SURFACEDATA_ANISOTROPY:
result = surfacedata.anisotropy.xxx;
break;
}
}

{
switch (paramId)
{
case DEBUGVIEW_SHADERNAME_BSDFDATA_DIFFUSE_COLOR:
case DEBUGVIEW_CLOTH_BSDFDATA_MATERIAL_FEATURES:
result = GetIndexColor(bsdfdata.materialFeatures);
break;
case DEBUGVIEW_CLOTH_BSDFDATA_DIFFUSE_COLOR:
case DEBUGVIEW_SHADERNAME_BSDFDATA_NORMAL:
case DEBUGVIEW_CLOTH_BSDFDATA_FRESNEL0:
result = bsdfdata.fresnel0;
break;
case DEBUGVIEW_CLOTH_BSDFDATA_SPECULAR_OCCLUSION:
result = bsdfdata.specularOcclusion.xxx;
break;
case DEBUGVIEW_CLOTH_BSDFDATA_NORMAL_WS:
break;
case DEBUGVIEW_CLOTH_BSDFDATA_NORMAL_VIEW_SPACE:
result = bsdfdata.normalWS * 0.5 + 0.5;
break;
case DEBUGVIEW_CLOTH_BSDFDATA_PERCEPTUAL_ROUGHNESS:
result = bsdfdata.perceptualRoughness.xxx;
break;
case DEBUGVIEW_CLOTH_BSDFDATA_DIFFUSION_PROFILE:
result = GetIndexColor(bsdfdata.diffusionProfile);
break;
case DEBUGVIEW_CLOTH_BSDFDATA_SUBSURFACE_MASK:
result = bsdfdata.subsurfaceMask.xxx;
break;
case DEBUGVIEW_CLOTH_BSDFDATA_THICKNESS:
result = bsdfdata.thickness.xxx;
break;
case DEBUGVIEW_CLOTH_BSDFDATA_USE_THICK_OBJECT_MODE:
result = (bsdfdata.useThickObjectMode) ? float3(1.0, 1.0, 1.0) : float3(0.0, 0.0, 0.0);
break;
case DEBUGVIEW_CLOTH_BSDFDATA_TRANSMITTANCE:
result = bsdfdata.transmittance;
break;
case DEBUGVIEW_CLOTH_BSDFDATA_TANGENT_WS:
result = bsdfdata.tangentWS * 0.5 + 0.5;
break;
case DEBUGVIEW_CLOTH_BSDFDATA_BITANGENT_WS:
result = bsdfdata.bitangentWS * 0.5 + 0.5;
break;
case DEBUGVIEW_CLOTH_BSDFDATA_ROUGHNESS_T:
result = bsdfdata.roughnessT.xxx;
break;
case DEBUGVIEW_CLOTH_BSDFDATA_ROUGHNESS_B:
result = bsdfdata.roughnessB.xxx;
break;
case DEBUGVIEW_CLOTH_BSDFDATA_ANISOTROPY:
result = bsdfdata.anisotropy.xxx;
break;
}
}

26
com.unity.render-pipelines.high-definition/HDRP/Material/Cloth/Cloth.hlsl


// BSDF share between directional light, punctual light and area light (reference)
//-----------------------------------------------------------------------------
// Ref: https://www.slideshare.net/jalnaga/custom-fabric-shader-for-unreal-engine-4
// For cloth we have two type of BRDF
// Non-Metal: Cotton, deim, flax and common fabrics
// Cotton: Roughness of 1.0 (unless wet) - Fuzz rim - specular color is white but is looked like desaturated.
// Metal: Silk, satin, velvet, nylon and polyester
// Silk: Roughness 0.3 - 0.7 - anisotropic - varying specular color
float diffuseTerm = Lambert();
float LdotV, NdotH, LdotH, NdotV, invLenLV;
GetBSDFAngle(V, L, NdotL, preLightData.NdotV, LdotV, NdotH, LdotH, NdotV, invLenLV);
// Cloth are dieletric but we simulate forward scattering effect with colored specular (fuzz tint term)
float3 F = F_Schlick(bsdfData.fresnel0, LdotH);
float D = D_Charlie(NdotH, bsdfData.roughnessT);
// V_Charlie is expensive, use approx with V_Ashikhmin instead
// float Vis = V_Charlie(NdotL, NdotV, bsdfData.roughness);
float Vis = V_Ashikhmin(NdotL, NdotV);
specularLighting = F * Vis * D;
// We don't multiply by 'bsdfData.diffuseColor' here. It's done only once in PostEvaluateBSDF().
diffuseLighting = diffuseTerm;
specularLighting = float3(0.0, 0.0, 0.0);
// Note: diffuseLighting is multiply by color in PostEvaluateBSDF
diffuseLighting = ClothLambert(bsdfData.roughnessT);
}
//-----------------------------------------------------------------------------

float intensity = max(0, attenuation * NdotL); // Warning: attenuation can be greater than 1 due to the inverse square attenuation (when position is close to light)
// Note: We use NdotL here to early out, but in case of clear coat this is not correct. But we are ok with this
UNITY_BRANCH if (intensity > 0.0)
{
BSDF(V, L, NdotL, posInput.positionWS, preLightData, bsdfData, lighting.diffuse, lighting.specular);

float intensity = max(0, attenuation * NdotL); // Warning: attenuation can be greater than 1 due to the inverse square attenuation (when position is close to light)
// Note: We use NdotL here to early out, but in case of clear coat this is not correct. But we are ok with this
UNITY_BRANCH if (intensity > 0.0)
{
// Shader implementer is free to use minRoughness paramter or not(But better if it is done)

3
com.unity.render-pipelines.high-definition/HDRP/Material/Cloth/ClothData.hlsl


void GetSurfaceAndBuiltinData(FragInputs input, float3 V, inout PositionInputs posInput, out SurfaceData surfaceData, out BuiltinData builtinData)
{
// TODO: Remove this zero initialize once we have written all the code
ZERO_INITIALIZE(SurfaceData, surfaceData);
surfaceData.normalWS = input.worldToTangent[2].xyz;
float2 baseColorMapUv = TRANSFORM_TEX(input.texCoord0, _BaseColorMap);

正在加载...
取消
保存