Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preparation for New Hair Material Type (Marschner) #4475

Merged
merged 6 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ class HairData : HDTargetData
{
public enum MaterialType
{
KajiyaKay
KajiyaKay,
Marschner
}

public enum ScatteringMode
{
Approximate,
DensityVolume
}

[SerializeField]
Expand All @@ -19,6 +26,15 @@ public MaterialType materialType
set => m_MaterialType = value;
}

[SerializeField]
ScatteringMode m_ScatteringMode;

public ScatteringMode scatteringMode
{
get => m_ScatteringMode;
set => m_ScatteringMode = value;
}

[SerializeField]
bool m_UseLightFacingNormal = false;
public bool useLightFacingNormal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,33 @@

namespace UnityEditor.Rendering.HighDefinition.ShaderGraph
{
class HairSurfaceOptionPropertyBlock : SurfaceOptionPropertyBlock
{
class Styles
{
public static GUIContent materialType = new GUIContent("Material Type", "TODO");
}

HairData hairData;

public HairSurfaceOptionPropertyBlock(SurfaceOptionPropertyBlock.Features features, HairData hairData) : base(features)
=> this.hairData = hairData;

protected override void CreatePropertyGUI()
{
// TODO: Un-hide me when Marschner BSDF is available.
// AddProperty(Styles.materialType, () => hairData.materialType, (newValue) => hairData.materialType = newValue);

base.CreatePropertyGUI();
}
}

class HairAdvancedOptionsPropertyBlock : AdvancedOptionsPropertyBlock
{
class Styles
{
public static GUIContent useLightFacingNormal = new GUIContent("Use Light Facing Normal", "TODO");
public static GUIContent scatteringMode = new GUIContent("Scattering Mode", "");
}

HairData hairData;
Expand All @@ -30,6 +52,9 @@ protected override void CreatePropertyGUI()

// Hair specific properties GUI
AddProperty(Styles.useLightFacingNormal, () => hairData.useLightFacingNormal, (newValue) => hairData.useLightFacingNormal = newValue);

if (hairData.materialType == HairData.MaterialType.Marschner)
AddProperty(Styles.scatteringMode, () => hairData.scatteringMode, (newValue) => hairData.scatteringMode = newValue);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ sealed partial class HairSubTarget : LightingSubTarget, ILegacyTarget, IRequires
protected override string raytracingInclude => CoreIncludes.kHairRaytracing;
protected override FieldDescriptor subShaderField => new FieldDescriptor(kSubShader, "Hair SubShader", "");
protected override bool requireSplitLighting => false;
protected override bool supportPathtracing => false;
protected override string pathtracingInclude => CoreIncludes.kHairPathtracing;

HairData m_HairData;

Expand All @@ -48,23 +50,28 @@ public HairData hairData
set => m_HairData = value;
}

public static FieldDescriptor KajiyaKay = new FieldDescriptor(kMaterial, "KajiyaKay", "_MATERIAL_FEATURE_HAIR_KAJIYA_KAY 1");
public static FieldDescriptor KajiyaKay = new FieldDescriptor(kMaterial, "KajiyaKay", "_MATERIAL_FEATURE_HAIR_KAJIYA_KAY 1");
public static FieldDescriptor Marschner = new FieldDescriptor(kMaterial, "Marschner", "_MATERIAL_FEATURE_HAIR_MARSCHNER 1");
public static FieldDescriptor RimTransmissionIntensity = new FieldDescriptor(string.Empty, "RimTransmissionIntensity", "_RIM_TRANSMISSION_INTENSITY 1");
public static FieldDescriptor HairStrandDirection = new FieldDescriptor(string.Empty, "HairStrandDirection", "_HAIR_STRAND_DIRECTION 1");
public static FieldDescriptor UseLightFacingNormal = new FieldDescriptor(string.Empty, "UseLightFacingNormal", "_USE_LIGHT_FACING_NORMAL 1");
public static FieldDescriptor Transmittance = new FieldDescriptor(string.Empty, "Transmittance", "_TRANSMITTANCE 1");
public static FieldDescriptor HairStrandDirection = new FieldDescriptor(string.Empty, "HairStrandDirection", "_HAIR_STRAND_DIRECTION 1");
public static FieldDescriptor UseLightFacingNormal = new FieldDescriptor(string.Empty, "UseLightFacingNormal", "_USE_LIGHT_FACING_NORMAL 1");
public static FieldDescriptor Transmittance = new FieldDescriptor(string.Empty, "Transmittance", "_TRANSMITTANCE 1");
public static FieldDescriptor ScatteringDensityVolume = new FieldDescriptor(string.Empty, "ScatteringDensityVolume", "_USE_DENSITY_VOLUME_SCATTERING 1");

public override void GetFields(ref TargetFieldContext context)
{
base.GetFields(ref context);

var descs = context.blocks.Select(x => x.descriptor);

// Hair specific properties:
context.AddField(KajiyaKay, hairData.materialType == HairData.MaterialType.KajiyaKay);
context.AddField(Marschner, hairData.materialType == HairData.MaterialType.Marschner);
context.AddField(HairStrandDirection, descs.Contains(HDBlockFields.SurfaceDescription.HairStrandDirection) && context.pass.validPixelBlocks.Contains(HDBlockFields.SurfaceDescription.HairStrandDirection));
context.AddField(RimTransmissionIntensity, descs.Contains(HDBlockFields.SurfaceDescription.RimTransmissionIntensity) && context.pass.validPixelBlocks.Contains(HDBlockFields.SurfaceDescription.RimTransmissionIntensity));
context.AddField(UseLightFacingNormal, hairData.useLightFacingNormal);
context.AddField(Transmittance, descs.Contains(HDBlockFields.SurfaceDescription.Transmittance) && context.pass.validPixelBlocks.Contains(HDBlockFields.SurfaceDescription.Transmittance));
context.AddField(ScatteringDensityVolume, hairData.scatteringMode == HairData.ScatteringMode.DensityVolume);

// Misc
context.AddField(SpecularAA, lightingData.specularAA &&
Expand All @@ -77,19 +84,32 @@ public override void GetActiveBlocks(ref TargetActiveBlockContext context)
base.GetActiveBlocks(ref context);

// Hair specific blocks
context.AddBlock(HDBlockFields.SurfaceDescription.Transmittance);
context.AddBlock(HDBlockFields.SurfaceDescription.RimTransmissionIntensity);
context.AddBlock(HDBlockFields.SurfaceDescription.HairStrandDirection);
context.AddBlock(HDBlockFields.SurfaceDescription.SpecularTint);
context.AddBlock(HDBlockFields.SurfaceDescription.SpecularShift);
context.AddBlock(HDBlockFields.SurfaceDescription.SecondarySpecularTint);
context.AddBlock(HDBlockFields.SurfaceDescription.SecondarySmoothness);
context.AddBlock(HDBlockFields.SurfaceDescription.SecondarySpecularShift);
// TODO: Find common parameters between the two material types, if any.
if (hairData.materialType == HairData.MaterialType.KajiyaKay)
{
context.AddBlock(HDBlockFields.SurfaceDescription.Transmittance);
context.AddBlock(HDBlockFields.SurfaceDescription.RimTransmissionIntensity);
context.AddBlock(HDBlockFields.SurfaceDescription.HairStrandDirection);
context.AddBlock(HDBlockFields.SurfaceDescription.SpecularTint);
context.AddBlock(HDBlockFields.SurfaceDescription.SpecularShift);
context.AddBlock(HDBlockFields.SurfaceDescription.SecondarySpecularTint);
context.AddBlock(HDBlockFields.SurfaceDescription.SecondarySmoothness);
context.AddBlock(HDBlockFields.SurfaceDescription.SecondarySpecularShift);
}
else
{
context.AddBlock(HDBlockFields.SurfaceDescription.HairStrandDirection);
context.AddBlock(HDBlockFields.SurfaceDescription.LongitudinalRoughness);
context.AddBlock(HDBlockFields.SurfaceDescription.AzimuthalRoughness);
context.AddBlock(HDBlockFields.SurfaceDescription.PrimaryReflectionRoughness);
context.AddBlock(HDBlockFields.SurfaceDescription.RefractionIndex);
context.AddBlock(HDBlockFields.SurfaceDescription.CuticleAngle);
}
}

protected override void AddInspectorPropertyBlocks(SubTargetPropertiesGUI blockList)
{
blockList.AddPropertyBlock(new SurfaceOptionPropertyBlock(SurfaceOptionPropertyBlock.Features.Lit));
blockList.AddPropertyBlock(new HairSurfaceOptionPropertyBlock(SurfaceOptionPropertyBlock.Features.Lit, hairData));
blockList.AddPropertyBlock(new HairAdvancedOptionsPropertyBlock(hairData));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ void BuildSurfaceData(FragInputs fragInputs, inout SurfaceDescription surfaceDes
$SurfaceDescription.SecondarySpecularTint: surfaceData.secondarySpecularTint = surfaceDescription.SecondarySpecularTint;
$SurfaceDescription.SecondarySpecularShift: surfaceData.secondarySpecularShift = surfaceDescription.SecondarySpecularShift;

// TODO: Adopt smoothness nomenclature + factorization?
$SurfaceDescription.LongitudinalRoughness: surfaceData.roughnessLongitudinal = surfaceDescription.LongitudinalRoughness;
$SurfaceDescription.AzimuthalRoughness: surfaceData.roughnessAzimuthal = surfaceDescription.AzimuthalRoughness;
$SurfaceDescription.PrimaryReflectionRoughness: surfaceData.roughnessPrimaryReflection = surfaceDescription.PrimaryReflectionRoughness;
$SurfaceDescription.RefractionIndex: surfaceData.ior = surfaceDescription.RefractionIndex;
$SurfaceDescription.CuticleAngle: surfaceData.cuticleAngle = surfaceDescription.CuticleAngle;

// These static material feature allow compile time optimization
surfaceData.materialFeatures = 0;

Expand All @@ -49,6 +56,10 @@ void BuildSurfaceData(FragInputs fragInputs, inout SurfaceDescription surfaceDes
surfaceData.materialFeatures |= MATERIALFEATUREFLAGS_HAIR_KAJIYA_KAY;
#endif

#ifdef _MATERIAL_FEATURE_HAIR_MARSCHNER
surfaceData.materialFeatures |= MATERIALFEATUREFLAGS_HAIR_MARSCHNER;
#endif

#ifdef _DOUBLESIDED_ON
float3 doubleSidedConstants = _DoubleSidedConstants.xyz;
#else
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
$Material.KajiyaKay: #define _MATERIAL_FEATURE_HAIR_KAJIYA_KAY 1
$Material.Marschner: #define _MATERIAL_FEATURE_HAIR_MARSCHNER 1
$UseLightFacingNormal: #define _USE_LIGHT_FACING_NORMAL 1
$AmbientOcclusion: #define _AMBIENT_OCCLUSION 1
$SpecularOcclusionFromAO: #define _SPECULAR_OCCLUSION_FROM_AO 1
$SpecularOcclusionFromAOBentNormal: #define _SPECULAR_OCCLUSION_FROM_AO_BENT_NORMAL 1
$SpecularOcclusionCustom: #define _SPECULAR_OCCLUSION_CUSTOM 1
$Specular.AA: #define _ENABLE_GEOMETRIC_SPECULAR_AA 1
$ScatteringDensityVolume: #define _USE_DENSITY_VOLUME_SCATTERING 1
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ public struct SurfaceDescription
new FloatControl(0.5f), ShaderStage.Fragment);
public static BlockFieldDescriptor SecondarySpecularShift = new BlockFieldDescriptor(SurfaceDescription.name, "SecondarySpecularShift", "Secondary Specular Shift", "SURFACEDESCRIPTION_SECONDARYSPECULARSHIFT",
new FloatControl(-0.1f), ShaderStage.Fragment);
public static BlockFieldDescriptor LongitudinalRoughness = new BlockFieldDescriptor(SurfaceDescription.name, "LongitudinalRoughness", "Longitudinal Roughness", "SURFACEDESCRIPTION_LONGITUDINALROUGHNESS",
new FloatControl(0.5f), ShaderStage.Fragment);
public static BlockFieldDescriptor AzimuthalRoughness = new BlockFieldDescriptor(SurfaceDescription.name, "AzimuthalRoughness", "Azimuthal Roughness", "SURFACEDESCRIPTION_AZIMUTHALROUGHNESS",
new FloatControl(0.5f), ShaderStage.Fragment);
public static BlockFieldDescriptor PrimaryReflectionRoughness = new BlockFieldDescriptor(SurfaceDescription.name, "PrimaryReflectionRoughness", "Primary Reflection Roughness", "SURFACEDESCRIPTION_PRIMARYREFLECTIONROUGHNESS",
new FloatControl(1f), ShaderStage.Fragment);
public static BlockFieldDescriptor CuticleAngle = new BlockFieldDescriptor(SurfaceDescription.name, "CuticleAngle", "Cuticle Angle", "SURFACEDESCRIPTION_CUTICLEANGLE",
new FloatControl(1.55f), ShaderStage.Fragment);

// --------------------------------------------------
// StackLit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ static class CoreIncludes
public const string kEyeRaytracing = "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Eye/EyeRaytracing.hlsl";
public const string kStackLitRaytracing = "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/StackLit/StackLitRaytracing.hlsl";
public const string kHairRaytracing = "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Hair/HairRaytracing.hlsl";
public const string kHairPathtracing = "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Hair/HairPathTracing.hlsl";
public const string kRaytracingLightLoop = "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RaytracingLightLoop.hlsl";
public const string kRaytracingCommon = "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RaytracingCommon.hlsl";
public const string kNormalBuffer = "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/NormalBuffer.hlsl";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ partial class Hair : RenderPipelineMaterial
public enum MaterialFeatureFlags
{
HairKajiyaKay = 1 << 0,
HairMarschner = 1 << 1
};

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -70,6 +71,18 @@ public struct SurfaceData

[SurfaceDataAttributes("Secondary Specular Shift")]
public float secondarySpecularShift;

// Marschner
[SurfaceDataAttributes("Longitudinal Roughness")]
public float roughnessLongitudinal;
[SurfaceDataAttributes("Azimuthal Roughness")]
public float roughnessAzimuthal;
[SurfaceDataAttributes("Primary Reflection Roughness")]
public float roughnessPrimaryReflection;
[SurfaceDataAttributes("Refraction Index")]
public float ior;
[SurfaceDataAttributes("Cuticle Angle")]
public float cuticleAngle;
};

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -113,6 +126,9 @@ public struct BSDFData
public float secondarySpecularExponent;
public float specularShift;
public float secondarySpecularShift;

// Marschner
// TODO
};


Expand All @@ -126,23 +142,27 @@ public override void Build(HDRenderPipelineAsset hdAsset, HDRenderPipelineRuntim
{
PreIntegratedFGD.instance.Build(PreIntegratedFGD.FGDIndex.FGD_GGXAndDisneyDiffuse);
LTCAreaLight.instance.Build();
PreIntegratedAzimuthalScattering.instance.Build();
}

public override void Cleanup()
{
PreIntegratedFGD.instance.Cleanup(PreIntegratedFGD.FGDIndex.FGD_GGXAndDisneyDiffuse);
LTCAreaLight.instance.Cleanup();
PreIntegratedAzimuthalScattering.instance.Cleanup();
}

public override void RenderInit(CommandBuffer cmd)
{
PreIntegratedFGD.instance.RenderInit(PreIntegratedFGD.FGDIndex.FGD_GGXAndDisneyDiffuse, cmd);
PreIntegratedAzimuthalScattering.instance.RenderInit(cmd);
}

public override void Bind(CommandBuffer cmd)
{
PreIntegratedFGD.instance.Bind(cmd, PreIntegratedFGD.FGDIndex.FGD_GGXAndDisneyDiffuse);
LTCAreaLight.instance.Bind(cmd);
PreIntegratedAzimuthalScattering.instance.Bind(cmd);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// UnityEngine.Rendering.HighDefinition.Hair+MaterialFeatureFlags: static fields
//
#define MATERIALFEATUREFLAGS_HAIR_KAJIYA_KAY (1)
#define MATERIALFEATUREFLAGS_HAIR_MARSCHNER (2)

//
// UnityEngine.Rendering.HighDefinition.Hair+SurfaceData: static fields
Expand All @@ -29,6 +30,11 @@
#define DEBUGVIEW_HAIR_SURFACEDATA_SECONDARY_SPECULAR_TINT (1414)
#define DEBUGVIEW_HAIR_SURFACEDATA_SPECULAR_SHIFT (1415)
#define DEBUGVIEW_HAIR_SURFACEDATA_SECONDARY_SPECULAR_SHIFT (1416)
#define DEBUGVIEW_HAIR_SURFACEDATA_LONGITUDINAL_ROUGHNESS (1417)
#define DEBUGVIEW_HAIR_SURFACEDATA_AZIMUTHAL_ROUGHNESS (1418)
#define DEBUGVIEW_HAIR_SURFACEDATA_PRIMARY_REFLECTION_ROUGHNESS (1419)
#define DEBUGVIEW_HAIR_SURFACEDATA_REFRACTION_INDEX (1420)
#define DEBUGVIEW_HAIR_SURFACEDATA_CUTICLE_ANGLE (1421)

//
// UnityEngine.Rendering.HighDefinition.Hair+BSDFData: static fields
Expand Down Expand Up @@ -74,6 +80,11 @@ struct SurfaceData
float3 secondarySpecularTint;
float specularShift;
float secondarySpecularShift;
float roughnessLongitudinal;
float roughnessAzimuthal;
float roughnessPrimaryReflection;
float ior;
float cuticleAngle;
};

// Generated from UnityEngine.Rendering.HighDefinition.Hair+BSDFData
Expand Down Expand Up @@ -162,6 +173,21 @@ void GetGeneratedSurfaceDataDebug(uint paramId, SurfaceData surfacedata, inout f
case DEBUGVIEW_HAIR_SURFACEDATA_SECONDARY_SPECULAR_SHIFT:
result = surfacedata.secondarySpecularShift.xxx;
break;
case DEBUGVIEW_HAIR_SURFACEDATA_LONGITUDINAL_ROUGHNESS:
result = surfacedata.roughnessLongitudinal.xxx;
break;
case DEBUGVIEW_HAIR_SURFACEDATA_AZIMUTHAL_ROUGHNESS:
result = surfacedata.roughnessAzimuthal.xxx;
break;
case DEBUGVIEW_HAIR_SURFACEDATA_PRIMARY_REFLECTION_ROUGHNESS:
result = surfacedata.roughnessPrimaryReflection.xxx;
break;
case DEBUGVIEW_HAIR_SURFACEDATA_REFRACTION_INDEX:
result = surfacedata.ior.xxx;
break;
case DEBUGVIEW_HAIR_SURFACEDATA_CUTICLE_ANGLE:
result = surfacedata.cuticleAngle.xxx;
break;
}
}

Expand Down
Loading