浏览代码

Add ambient occlusion properties on Stacklit

/main
sebastienlagarde 7 年前
当前提交
bac63c51
共有 11 个文件被更改,包括 90 次插入36 次删除
  1. 2
      ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/CommonMaterial.hlsl
  2. 5
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/StackLit/StackLitUI.cs
  3. 16
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/LightEvaluation.hlsl
  4. 4
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.hlsl
  5. 11
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/StackLit/StackLit.cs
  6. 10
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/StackLit/StackLit.cs.hlsl
  7. 29
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/StackLit/StackLit.hlsl
  8. 12
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/StackLit/StackLit.shader
  9. 16
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/StackLit/StackLitData.hlsl
  10. 19
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/StackLit/StackLitProperties.hlsl
  11. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderVariablesFunctions.hlsl

2
ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/CommonMaterial.hlsl


void ConvertRoughnessToAnisotropy(real roughnessT, real roughnessB, out real anisotropy)
{
return ((roughnessT - roughnessB) / (roughnessT + roughnessB + 0.0001));
anisotropy = ((roughnessT - roughnessB) / (roughnessT + roughnessB + 0.0001));
}
// Same as ConvertAnisotropyToRoughness but

5
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/StackLit/StackLitUI.cs


protected const string k_NormalMapUV = "_NormalMapUV";
protected const string k_NormalScale = "_NormalScale";
protected const string k_AmbientOcclusion = "_AmbientOcclusion";
protected const string k_AmbientOcclusionMap = "_AmbientOcclusionMap";
// Emissive
protected const string k_EmissiveColor = "_EmissiveColor";
protected const string k_EmissiveColorMap = "_EmissiveColorMap";

new TextureProperty(this, k_NormalMap, k_NormalScale, "Normal", "Normal Map", false, false, true),
//new TextureProperty(this, k_BaseColorMap, k_BaseColor, "Dielectric IoR", "Index of Refraction for Dielectric", false),
new TextureProperty(this, k_AmbientOcclusionMap, k_AmbientOcclusion, "AmbientOcclusion", "AmbientOcclusion", false, false),
}),
new GroupProperty(this, "_Emissive", "Emissive", new BaseProperty[]

16
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/LightEvaluation.hlsl


// This files include various function uses to evaluate lights
// To use deferred directional shadow with cascaded shadow map,
// To use deferred directional shadow with cascaded shadow map,
// it is required to define USE_DEFERRED_DIRECTIONAL_SHADOWS before including this files
//-----------------------------------------------------------------------------

float3 indirectSpecularOcclusion;
};
void GetScreenSpaceAmbientOcclusion(float2 positionSS, float NdotV, float perceptualRoughness, float specularOcclusionFromData, out AmbientOcclusionFactor aoFactor)
void GetScreenSpaceAmbientOcclusion(float2 positionSS, float NdotV, float perceptualRoughness, float ambientOcclusionFromData, float specularOcclusionFromData, out AmbientOcclusionFactor aoFactor)
{
// Note: When we ImageLoad outside of texture size, the value returned by Load is 0 (Note: On Metal maybe it clamp to value of texture which is also fine)
// We use this property to have a neutral value for AO that doesn't consume a sampler and work also with compute shader (i.e use ImageLoad)

float roughness = PerceptualRoughnessToRoughness(perceptualRoughness);
float specularOcclusion = GetSpecularOcclusionFromAmbientOcclusion(ClampNdotV(NdotV), indirectAmbientOcclusion, roughness);
aoFactor.indirectAmbientOcclusion = lerp(_AmbientOcclusionParam.rgb, float3(1.0, 1.0, 1.0), indirectAmbientOcclusion);
aoFactor.indirectSpecularOcclusion = lerp(_AmbientOcclusionParam.rgb, float3(1.0, 1.0, 1.0), min(specularOcclusionFromData, specularOcclusion));
aoFactor.indirectAmbientOcclusion = lerp(_AmbientOcclusionParam.rgb, float3(1.0, 1.0, 1.0), min(ambientOcclusionFromData, indirectAmbientOcclusion));
aoFactor.indirectSpecularOcclusion = lerp(_AmbientOcclusionParam.rgb, float3(1.0, 1.0, 1.0), min(specularOcclusionFromData, specularOcclusion));
void GetScreenSpaceAmbientOcclusionMultibounce(float2 positionSS, float NdotV, float perceptualRoughness, float specularOcclusionFromData, float3 diffuseColor, float3 fresnel0, out AmbientOcclusionFactor aoFactor)
void GetScreenSpaceAmbientOcclusionMultibounce(float2 positionSS, float NdotV, float perceptualRoughness, float ambientOcclusionFromData, float specularOcclusionFromData, float3 diffuseColor, float3 fresnel0, out AmbientOcclusionFactor aoFactor)
{
// Use GTAOMultiBounce approximation for ambient occlusion (allow to get a tint from the diffuseColor)
// Note: When we ImageLoad outside of texture size, the value returned by Load is 0 (Note: On Metal maybe it clamp to value of texture which is also fine)

float roughness = PerceptualRoughnessToRoughness(perceptualRoughness);
float specularOcclusion = GetSpecularOcclusionFromAmbientOcclusion(ClampNdotV(NdotV), indirectAmbientOcclusion, roughness);
aoFactor.indirectAmbientOcclusion = GTAOMultiBounce(indirectAmbientOcclusion, diffuseColor);
aoFactor.directAmbientOcclusion = GTAOMultiBounce(min(specularOcclusionFromData, specularOcclusion), fresnel0);
aoFactor.indirectSpecularOcclusion = GTAOMultiBounce(directAmbientOcclusion, diffuseColor);
aoFactor.indirectSpecularOcclusion = GTAOMultiBounce(min(specularOcclusionFromData, specularOcclusion), fresnel0);
aoFactor.indirectAmbientOcclusion = GTAOMultiBounce(min(ambientOcclusionFromData, indirectAmbientOcclusion), diffuseColor);
aoFactor.directAmbientOcclusion = GTAOMultiBounce(directAmbientOcclusion, diffuseColor);
}

4
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.hlsl


AmbientOcclusionFactor aoFactor;
// Use GTAOMultiBounce approximation for ambient occlusion (allow to get a tint from the baseColor)
#if 0
GetScreenSpaceAmbientOcclusion(posInput.positionSS, preLightData.NdotV, bsdfData.perceptualRoughness, bsdfData.specularOcclusion, aoFactor);
GetScreenSpaceAmbientOcclusion(posInput.positionSS, preLightData.NdotV, bsdfData.perceptualRoughness, 1.0, bsdfData.specularOcclusion, aoFactor);
GetScreenSpaceAmbientOcclusionMultibounce(posInput.positionSS, preLightData.NdotV, bsdfData.perceptualRoughness, bsdfData.specularOcclusion, bsdfData.diffuseColor, bsdfData.fresnel0, aoFactor);
GetScreenSpaceAmbientOcclusionMultibounce(posInput.positionSS, preLightData.NdotV, bsdfData.perceptualRoughness, 1.0, bsdfData.specularOcclusion, bsdfData.diffuseColor, bsdfData.fresnel0, aoFactor);
#endif
// Add indirect diffuse + emissive (if any) - Ambient occlusion is multiply by emissive which is wrong but not a big deal

11
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/StackLit/StackLit.cs


[SurfaceDataAttributes("Coat Extinction Coefficient")]
public Vector3 coatExtinction;
[SurfaceDataAttributes("Ambient Occlusion")]
public float ambientOcclusion;
};
//-----------------------------------------------------------------------------

public float coatThickness;
public Vector3 coatExtinction;
//public fixed float test[2];
//could use something like that:
//[MarshalAs(UnmanagedType.ByValArray, SizeConst=5)]
//public float[] test;
public float ambientOcclusion;
//-----------------------------------------------------------------------------
// Init precomputed textures
//-----------------------------------------------------------------------------

10
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/StackLit/StackLit.cs.hlsl


#define DEBUGVIEW_STACKLIT_SURFACEDATA_COAT_IOR (1311)
#define DEBUGVIEW_STACKLIT_SURFACEDATA_COAT_THICKNESS (1312)
#define DEBUGVIEW_STACKLIT_SURFACEDATA_COAT_EXTINCTION_COEFFICIENT (1313)
#define DEBUGVIEW_STACKLIT_SURFACEDATA_AMBIENT_OCCLUSION (1314)
//
// UnityEngine.Experimental.Rendering.HDPipeline.StackLit+BSDFData: static fields

#define DEBUGVIEW_STACKLIT_BSDFDATA_COAT_IOR (1417)
#define DEBUGVIEW_STACKLIT_BSDFDATA_COAT_THICKNESS (1418)
#define DEBUGVIEW_STACKLIT_BSDFDATA_COAT_EXTINCTION (1419)
#define DEBUGVIEW_STACKLIT_BSDFDATA_AMBIENT_OCCLUSION (1420)
// Generated from UnityEngine.Experimental.Rendering.HDPipeline.StackLit+SurfaceData
// PackingRules = Exact

float coatIor;
float coatThickness;
float3 coatExtinction;
float ambientOcclusion;
};
// Generated from UnityEngine.Experimental.Rendering.HDPipeline.StackLit+BSDFData

float coatIor;
float coatThickness;
float3 coatExtinction;
float ambientOcclusion;
};
//

case DEBUGVIEW_STACKLIT_SURFACEDATA_COAT_EXTINCTION_COEFFICIENT:
result = surfacedata.coatExtinction;
break;
case DEBUGVIEW_STACKLIT_SURFACEDATA_AMBIENT_OCCLUSION:
result = surfacedata.ambientOcclusion.xxx;
break;
}
}

break;
case DEBUGVIEW_STACKLIT_BSDFDATA_COAT_EXTINCTION:
result = bsdfdata.coatExtinction;
break;
case DEBUGVIEW_STACKLIT_BSDFDATA_AMBIENT_OCCLUSION:
result = bsdfdata.ambientOcclusion.xxx;
break;
}
}

29
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/StackLit/StackLit.hlsl


ConvertAnisotropyToClampRoughness(bsdfData.perceptualRoughnessB, bsdfData.anisotropy, bsdfData.roughnessBT, bsdfData.roughnessBB);
}
bsdfData.ambientOcclusion = surfaceData.ambientOcclusion;
ApplyDebugToBSDFData(bsdfData);
return bsdfData;
}

// This function require the 3 structure surfaceData, builtinData, bsdfData because it may require both the engine side data, and data that will not be store inside the gbuffer.
float3 GetBakedDiffuseLighting(SurfaceData surfaceData, BuiltinData builtinData, BSDFData bsdfData, PreLightData preLightData)
{
//NEWLITTODO
// TODO: Handle SSS
#ifdef DEBUG_DISPLAY
if (_DebugLightingMode == DEBUGLIGHTINGMODE_LUX_METER)

}
#endif
//NEWLITTODO
// Premultiply bake diffuse lighting information with DisneyDiffuse pre-integration
//return builtinData.bakeDiffuseLighting * preLightData.diffuseFGD * surfaceData.ambientOcclusion * bsdfData.diffuseColor + builtinData.emissiveColor;
return builtinData.bakeDiffuseLighting * bsdfData.diffuseColor; //...todo, just to return something for now, .bakeDiffuseLighting is bogus for now anyway.
// Premultiply bake diffuse lighting information
return builtinData.bakeDiffuseLighting * surfaceData.ambientOcclusion * bsdfData.diffuseColor + builtinData.emissiveColor;
}

PreLightData preLightData, BSDFData bsdfData, BakeLightingData bakeLightingData, AggregateLighting lighting,
out float3 diffuseLighting, out float3 specularLighting)
{
// TODO: AO, SO, SSS, Refraction
float3 bakeDiffuseLighting = bakeLightingData.bakeDiffuseLighting;
AmbientOcclusionFactor aoFactor;
// Use GTAOMultiBounce approximation for ambient occlusion (allow to get a tint from the baseColor)
GetScreenSpaceAmbientOcclusionMultibounce(posInput.positionSS, preLightData.NdotV, lerp(bsdfData.perceptualRoughnessA, bsdfData.perceptualRoughnessB, bsdfData.lobeMix), bsdfData.ambientOcclusion, 1.0, bsdfData.diffuseColor, bsdfData.fresnel0, aoFactor);
// Add indirect diffuse + emissive (if any) - Ambient occlusion is multiply by emissive which is wrong but not a big deal
bakeDiffuseLighting *= aoFactor.indirectAmbientOcclusion;
lighting.direct.diffuse *= aoFactor.directAmbientOcclusion;
diffuseLighting = bsdfData.diffuseColor * lighting.direct.diffuse + bakeLightingData.bakeDiffuseLighting;
diffuseLighting = bsdfData.diffuseColor * lighting.direct.diffuse + bakeDiffuseLighting;
if( !IsVLayeredEnabled(bsdfData) )
if (!IsVLayeredEnabled(bsdfData))
//specularLighting = ApplyEnergyCompensationToSpecularLighting(specularLighting, bsdfData.fresnel0, preLightData.energyCompensation[0]);
#ifdef DEBUG_DISPLAY

break;
case DEBUGLIGHTINGMODE_INDIRECT_DIFFUSE_OCCLUSION:
//diffuseLighting = aoFactor.indirectAmbientOcclusion;
diffuseLighting = aoFactor.indirectAmbientOcclusion;
break;
case DEBUGLIGHTINGMODE_INDIRECT_SPECULAR_OCCLUSION:

12
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/StackLit/StackLit.shader


_MetallicMapChannel("Metallic Map Channel", Float) = 0.0
_MetallicMapChannelMask("Metallic Map Channel Mask", Vector) = (1, 0, 0, 0)
_MetallicRemap("Metallic Remap", Vector) = (0, 1, 0, 0)
[ToggleUI] _MetallicRemapInverted("Invert Metallic Remap", Float) = 0.0
[HideInInspector] _MetallicRange("Metallic Range", Vector) = (0, 1, 0, 0)
[HideInInspector] _SmoothnessAMapShow("SmoothnessA Map Show", Float) = 0

_NormalMapUVLocal("NormalMapUV Local", Float) = 0.0
_NormalMapObjSpace("NormalMapUV Local", Float) = 0.0
_NormalScale("Normal Scale", Range(0.0, 2.0)) = 1
[HideInInspector] _AmbientOcclusionMapShow("AmbientOcclusion Map Show", Float) = 0
_AmbientOcclusion("AmbientOcclusion", Range(0.0, 1.0)) = 0
_AmbientOcclusionMap("AmbientOcclusion Map", 2D) = "black" {}
_AmbientOcclusionUseMap("AmbientOcclusion Use Map", Float) = 0
_AmbientOcclusionMapUV("AmbientOcclusion Map UV", Float) = 0.0
_AmbientOcclusionMapUVLocal("AmbientOcclusion Map UV Local", Float) = 0.0
_AmbientOcclusionMapChannel("AmbientOcclusion Map Channel", Float) = 0.0
_AmbientOcclusionMapChannelMask("AmbientOcclusion Map Channel Mask", Vector) = (1, 0, 0, 0)
_AmbientOcclusionRemap("AmbientOcclusion Remap", Vector) = (0, 1, 0, 0)
[HideInInspector] _AmbientOcclusionRange("AmbientOcclusion Range", Vector) = (0, 1, 0, 0)
[HideInInspector] _EmissiveColorMapShow("Emissive Color Map Show", Float) = 0.0
_EmissiveColor("Emissive Color", Color) = (1, 1, 1)

16
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/StackLit/StackLitData.hlsl


surfaceData.metallic = lerp(_MetallicRange.x, _MetallicRange.y, surfaceData.metallic);
surfaceData.metallic = lerp(_Metallic, surfaceData.metallic, _MetallicUseMap);
surfaceData.ambientOcclusion = dot(SAMPLE_TEXTURE2D_SCALE_BIAS(_AmbientOcclusionMap), _AmbientOcclusionMapChannelMask);
surfaceData.ambientOcclusion = lerp(_AmbientOcclusionRange.x, _AmbientOcclusionRange.y, surfaceData.ambientOcclusion);
surfaceData.ambientOcclusion = lerp(_AmbientOcclusion, surfaceData.ambientOcclusion, _AmbientOcclusionUseMap);
// These static material feature allow compile time optimization
// TODO: As we add features, or-set the flags eg MATERIALFEATUREFLAGS_STACK_LIT_* with #ifdef

builtinData.opacity = alpha;
// TODO:
builtinData.bakeDiffuseLighting = float3(0.0, 0.0, 0.0);
builtinData.bakeDiffuseLighting = SampleBakedGI(input.positionWS, surfaceData.normalWS, input.texCoord1, input.texCoord2);
// Emissive Intensity is only use here, but is part of BuiltinData to enforce UI parameters as we want the users to fill one color and one intensity
builtinData.emissiveIntensity = _EmissiveIntensity; // We still store intensity here so we can reuse it with debug code

// TODO:
builtinData.velocity = float2(0.0, 0.0);
//NEWLITTODO: shader feature SHADOWS_SHADOWMASK not there yet.
#ifdef SHADOWS_SHADOWMASK
float4 shadowMask = SampleShadowMask(input.positionWS, input.texCoord1);
builtinData.shadowMask0 = shadowMask.x;
builtinData.shadowMask1 = shadowMask.y;
builtinData.shadowMask2 = shadowMask.z;
builtinData.shadowMask3 = shadowMask.w;
#else
#endif
#if (SHADERPASS == SHADERPASS_DISTORTION) || defined(DEBUG_DISPLAY)
float3 distortion = SAMPLE_TEXTURE2D(_DistortionVectorMap, sampler_DistortionVectorMap, input.texCoord0).rgb;

19
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/StackLit/StackLitProperties.hlsl


TEXTURE2D(_SmoothnessBMap);
SAMPLER(sampler_SmoothnessBMap);
TEXTURE2D(_NormalMap);
SAMPLER(sampler_NormalMap);
TEXTURE2D(_AmbientOcclusionMap);
SAMPLER(sampler_AmbientOcclusionMap);
TEXTURE2D(_SubsurfaceMaskMap);
SAMPLER(sampler_SubsurfaceMaskMap);

TEXTURE2D(_EmissiveColorMap);
SAMPLER(sampler_EmissiveColorMap);
TEXTURE2D(_NormalMap);
SAMPLER(sampler_NormalMap);
CBUFFER_START(UnityPerMaterial)

float4 _NormalMap_ST;
float4 _NormalMap_TexelSize;
float4 _NormalMap_MipInfo;
float _AmbientOcclusion;
float _AmbientOcclusionUseMap;
float _AmbientOcclusionMapUV;
float _AmbientOcclusionMapUVLocal;
float4 _AmbientOcclusionMap_ST;
float4 _AmbientOcclusionMap_TexelSize;
float4 _AmbientOcclusionMap_MipInfo;
float4 _AmbientOcclusionMapChannelMask;
float4 _AmbientOcclusionRange;
float3 _EmissiveColor;
float4 _EmissiveColorMap_ST;

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderVariablesFunctions.hlsl


return mul((float3x3)GetViewToHClipMatrix(), directionVS);
}
// This function always return the absolute position in WS either the CameraRelative mode is enabled or not
float3 GetAbsolutePositionWS(float3 positionWS)
{
#if (SHADEROPTIONS_CAMERA_RELATIVE_RENDERING != 0)

}
// This function always return the camera relative position in WS either the CameraRelative mode is enabled or not
float3 GetCameraRelativePositionWS(float3 positionWS)
{
#if (SHADEROPTIONS_CAMERA_RELATIVE_RENDERING != 0)

正在加载...
取消
保存