浏览代码

Reconfigured the mask texture channels. Added NormalScale. Removed DensityScale. AO doesn't seem to work...

/main
Yao Xiaoling 6 年前
当前提交
32c8a168
共有 5 个文件被更改,包括 74 次插入159 次删除
  1. 124
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/TerrainLit/TerrainLit.shader
  2. 11
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/TerrainLit/TerrainLitData.hlsl
  3. 7
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/TerrainLit/TerrainLitData_Basemap.hlsl
  4. 79
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/TerrainLit/TerrainLitSplatCommon.hlsl
  5. 12
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/TerrainLit/TerrainLit_Basemap_Gen.shader

124
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/TerrainLit/TerrainLit.shader


{
Properties
{
[HideInInspector] _Control0("Control 0 (RGBA)", 2D) = "" {}
[HideInInspector] _Control1("Control 1 (RGBA)", 2D) = "" {}
[HideInInspector] _Splat0("Layer 0", 2D) = "white" {}
[HideInInspector] _Splat1("Layer 1", 2D) = "white" {}
[HideInInspector] _Splat2("Layer 2", 2D) = "white" {}
[HideInInspector] _Splat3("Layer 3", 2D) = "white" {}
[HideInInspector] _Splat4("Layer 4", 2D) = "white" {}
[HideInInspector] _Splat5("Layer 5", 2D) = "white" {}
[HideInInspector] _Splat6("Layer 6", 2D) = "white" {}
[HideInInspector] _Splat7("Layer 7", 2D) = "white" {}
[HideInInspector] _Normal0("Normal 0", 2D) = "bump" {}
[HideInInspector] _Normal1("Normal 1", 2D) = "bump" {}
[HideInInspector] _Normal2("Normal 2", 2D) = "bump" {}
[HideInInspector] _Normal3("Normal 3", 2D) = "bump" {}
[HideInInspector] _Normal4("Normal 4", 2D) = "bump" {}
[HideInInspector] _Normal5("Normal 5", 2D) = "bump" {}
[HideInInspector] _Normal6("Normal 6", 2D) = "bump" {}
[HideInInspector] _Normal7("Normal 7", 2D) = "bump" {}
// Since we don't use a mask texture for getting the mask, we'll need the metallic property to be serialized as in sRGB space.
[HideInInspector] [Gamma] _Metallic0("Metallic 0", Range(0.0, 1.0)) = 0.0
[HideInInspector] [Gamma] _Metallic1("Metallic 1", Range(0.0, 1.0)) = 0.0
[HideInInspector] [Gamma] _Metallic2("Metallic 2", Range(0.0, 1.0)) = 0.0
[HideInInspector] [Gamma] _Metallic3("Metallic 3", Range(0.0, 1.0)) = 0.0
[HideInInspector] [Gamma] _Metallic4("Metallic 4", Range(0.0, 1.0)) = 0.0
[HideInInspector] [Gamma] _Metallic5("Metallic 5", Range(0.0, 1.0)) = 0.0
[HideInInspector] [Gamma] _Metallic6("Metallic 6", Range(0.0, 1.0)) = 0.0
[HideInInspector] [Gamma] _Metallic7("Metallic 7", Range(0.0, 1.0)) = 0.0
[HideInInspector] _Smoothness0("Smoothness 0", Range(0.0, 1.0)) = 0.0
[HideInInspector] _Smoothness1("Smoothness 1", Range(0.0, 1.0)) = 0.0
[HideInInspector] _Smoothness2("Smoothness 2", Range(0.0, 1.0)) = 0.0
[HideInInspector] _Smoothness3("Smoothness 3", Range(0.0, 1.0)) = 0.0
[HideInInspector] _Smoothness4("Smoothness 4", Range(0.0, 1.0)) = 0.0
[HideInInspector] _Smoothness5("Smoothness 5", Range(0.0, 1.0)) = 0.0
[HideInInspector] _Smoothness6("Smoothness 6", Range(0.0, 1.0)) = 0.0
[HideInInspector] _Smoothness7("Smoothness 7", Range(0.0, 1.0)) = 0.0
[HideInInspector] _Mask0("Mask 0", 2D) = "black" {}
[HideInInspector] _Mask1("Mask 1", 2D) = "black" {}
[HideInInspector] _Mask2("Mask 2", 2D) = "black" {}
[HideInInspector] _Mask3("Mask 3", 2D) = "black" {}
[HideInInspector] _Mask4("Mask 4", 2D) = "black" {}
[HideInInspector] _Mask5("Mask 5", 2D) = "black" {}
[HideInInspector] _Mask6("Mask 6", 2D) = "black" {}
[HideInInspector] _Mask7("Mask 7", 2D) = "black" {}
[HideInInspector] _Density0("_Density0", Float) = 1.0
[HideInInspector] _Density1("_Density1", Float) = 1.0
[HideInInspector] _Density2("_Density2", Float) = 1.0
[HideInInspector] _Density3("_Density3", Float) = 1.0
[HideInInspector] _Density4("_Density4", Float) = 1.0
[HideInInspector] _Density5("_Density5", Float) = 1.0
[HideInInspector] _Density6("_Density6", Float) = 1.0
[HideInInspector] _Density7("_Density7", Float) = 1.0
// Height in mask.r
[HideInInspector] _HeightAmplitude0("Height Scale0", Float) = 0.02
[HideInInspector] _HeightAmplitude1("Height Scale1", Float) = 0.02
[HideInInspector] _HeightAmplitude2("Height Scale2", Float) = 0.02
[HideInInspector] _HeightAmplitude3("Height Scale3", Float) = 0.02
[HideInInspector] _HeightAmplitude4("Height Scale4", Float) = 0.02
[HideInInspector] _HeightAmplitude5("Height Scale5", Float) = 0.02
[HideInInspector] _HeightAmplitude6("Height Scale6", Float) = 0.02
[HideInInspector] _HeightAmplitude7("Height Scale7", Float) = 0.02
[HideInInspector] _HeightCenter0("Height Bias0", Range(0.0, 1.0)) = 0.5
[HideInInspector] _HeightCenter1("Height Bias1", Range(0.0, 1.0)) = 0.5
[HideInInspector] _HeightCenter2("Height Bias2", Range(0.0, 1.0)) = 0.5
[HideInInspector] _HeightCenter3("Height Bias3", Range(0.0, 1.0)) = 0.5
[HideInInspector] _HeightCenter4("Height Bias4", Range(0.0, 1.0)) = 0.5
[HideInInspector] _HeightCenter5("Height Bias5", Range(0.0, 1.0)) = 0.5
[HideInInspector] _HeightCenter6("Height Bias6", Range(0.0, 1.0)) = 0.5
[HideInInspector] _HeightCenter7("Height Bias7", Range(0.0, 1.0)) = 0.5
/*
[HideInInspector] _TexWorldScale0("Tiling", Float) = 1.0
[HideInInspector] _TexWorldScale1("Tiling", Float) = 1.0
[HideInInspector] _TexWorldScale2("Tiling", Float) = 1.0
[HideInInspector] _TexWorldScale3("Tiling", Float) = 1.0
// Following set of parameters represent the parameters node inside the MaterialGraph.
// They are use to fill a SurfaceData. With a MaterialGraph this should not exist.
// All the following properties are filled by the referenced lit shader.
_SmoothnessRemapMin0("SmoothnessRemapMin0", Range(0.0, 1.0)) = 0.0
_SmoothnessRemapMin1("SmoothnessRemapMin1", Range(0.0, 1.0)) = 0.0
_SmoothnessRemapMin2("SmoothnessRemapMin2", Range(0.0, 1.0)) = 0.0
_SmoothnessRemapMin3("SmoothnessRemapMin3", Range(0.0, 1.0)) = 0.0
_SmoothnessRemapMax0("SmoothnessRemapMax0", Range(0.0, 1.0)) = 1.0
_SmoothnessRemapMax1("SmoothnessRemapMax1", Range(0.0, 1.0)) = 1.0
_SmoothnessRemapMax2("SmoothnessRemapMax2", Range(0.0, 1.0)) = 1.0
_SmoothnessRemapMax3("SmoothnessRemapMax3", Range(0.0, 1.0)) = 1.0
_AORemapMin0("AORemapMin0", Range(0.0, 1.0)) = 0.0
_AORemapMin1("AORemapMin1", Range(0.0, 1.0)) = 0.0
_AORemapMin2("AORemapMin2", Range(0.0, 1.0)) = 0.0
_AORemapMin3("AORemapMin3", Range(0.0, 1.0)) = 0.0
_AORemapMax0("AORemapMax0", Range(0.0, 1.0)) = 1.0
_AORemapMax1("AORemapMax1", Range(0.0, 1.0)) = 1.0
_AORemapMax2("AORemapMax2", Range(0.0, 1.0)) = 1.0
_AORemapMax3("AORemapMax3", Range(0.0, 1.0)) = 1.0
*/
// All the following properties exist only in layered lit material
//[HideInInspector] _TexWorldScale0("Tiling", Float) = 1.0
//[HideInInspector] _TexWorldScale1("Tiling", Float) = 1.0
//[HideInInspector] _TexWorldScale2("Tiling", Float) = 1.0
//[HideInInspector] _TexWorldScale3("Tiling", Float) = 1.0
// Following are builtin properties

SubShader
{
// This tags allow to use the shader replacement features
Tags{ "RenderPipeline" = "HDRenderPipeline" "RenderType" = "Opaque" "SplatCount"="8" }
Tags
{
"RenderPipeline" = "HDRenderPipeline"
"RenderType" = "Opaque"
"SplatCount" = "8"
"MaskMapR" = "Metallic"
"MaskMapG" = "AO"
"MaskMapB" = "Height"
"MaskMapA" = "Smoothness"
"DiffuseA" = "Smoothness (becomes Density when Mask map is assigned)" // when MaskMap is disabled
"DiffuseA_MaskMapUsed" = "Density" // when MaskMap is enabled
}
// Caution: The outline selection in the editor use the vertex shader/hull/domain shader of the first pass declare. So it should not bethe meta pass.
Pass

11
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/TerrainLit/TerrainLitData.hlsl


float3 normalTS;
TerrainSplatBlend(input.texCoord0, input.worldToTangent[0], input.worldToTangent[1],
surfaceData.baseColor, normalTS, surfaceData.perceptualSmoothness, surfaceData.metallic);
surfaceData.baseColor, normalTS, surfaceData.perceptualSmoothness, surfaceData.metallic, surfaceData.ambientOcclusion);
surfaceData.ambientOcclusion = 1;
surfaceData.tangentWS = normalize(input.worldToTangent[0].xyz); // The tangent is not normalize in worldToTangent for mikkt. Tag: SURFACE_GRADIENT
surfaceData.subsurfaceMask = 0;
surfaceData.thickness = 1;

float3 bentNormalWS = surfaceData.normalWS;
// By default we use the ambient occlusion with Tri-ace trick (apply outside) for specular occlusion.
//#if defined(_MASKMAP0) || defined(_MASKMAP1) || defined(_MASKMAP2) || defined(_MASKMAP3)
// surfaceData.specularOcclusion = GetSpecularOcclusionFromAmbientOcclusion(dot(surfaceData.normalWS, V), surfaceData.ambientOcclusion, PerceptualSmoothnessToRoughness(surfaceData.perceptualSmoothness));
//#else
#ifdef _MASKMAP
surfaceData.specularOcclusion = GetSpecularOcclusionFromAmbientOcclusion(dot(surfaceData.normalWS, V), surfaceData.ambientOcclusion, PerceptualSmoothnessToRoughness(surfaceData.perceptualSmoothness));
#else
//#endif
#endif
#ifndef _DISABLE_DBUFFER
float alpha = 1;

7
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/TerrainLit/TerrainLitData_Basemap.hlsl


surfaceData.baseColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.texCoord0).rgb;
surfaceData.perceptualSmoothness = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.texCoord0).a;
surfaceData.ambientOcclusion = 1;
surfaceData.ambientOcclusion = SAMPLE_TEXTURE2D(_MetallicTex, sampler_MainTex, input.texCoord0).g;
surfaceData.metallic = SAMPLE_TEXTURE2D(_MetallicTex, sampler_MainTex, input.texCoord0).r;
surfaceData.tangentWS = normalize(input.worldToTangent[0].xyz); // The tangent is not normalize in worldToTangent for mikkt. Tag: SURFACE_GRADIENT
surfaceData.subsurfaceMask = 0;

GetNormalWS(input, V, normalTS, surfaceData.normalWS);
float3 bentNormalWS = surfaceData.normalWS;
surfaceData.specularOcclusion = 1.0;
if (surfaceData.ambientOcclusion != 1.0f)
surfaceData.specularOcclusion = GetSpecularOcclusionFromAmbientOcclusion(dot(surfaceData.normalWS, V), surfaceData.ambientOcclusion, PerceptualSmoothnessToRoughness(surfaceData.perceptualSmoothness));
else
surfaceData.specularOcclusion = 1.0f;
#ifndef _DISABLE_DBUFFER
float alpha = 1;

79
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/TerrainLit/TerrainLitSplatCommon.hlsl


float4 _Splat##n##_ST; \
float _Metallic##n; \
float _Smoothness##n; \
float _Density##n; \
float _HeightCenter##n; \
float _HeightAmplitude##n
float _NormalScale##n; \
float4 _MaskRemapOffset##n; \
float4 _MaskRemapScale##n
DECLARE_TERRAIN_LAYER(0);
DECLARE_TERRAIN_LAYER(1);

return sumHeight;
}
float3 SampleNormalGrad(TEXTURE2D_ARGS(textureName, samplerName), float2 uv, float2 dxuv, float2 dyuv, float3 tangentWS, float3 bitangentWS)
float3 SampleNormalGrad(TEXTURE2D_ARGS(textureName, samplerName), float2 uv, float2 dxuv, float2 dyuv, float scale, float3 tangentWS, float3 bitangentWS)
real2 deriv = UnpackDerivativeNormalRGB(nrm, 1);
real2 deriv = UnpackDerivativeNormalRGB(nrm, scale);
real2 deriv = UnpackDerivativeNormalRGorAG(nrm, 1);
real2 deriv = UnpackDerivativeNormalRGorAG(nrm, scale);
return UnpackNormalRGB(nrm, 1);
return UnpackNormalRGB(nrm, scale);
return UnpackNormalmapRGorAG(nrm, 1);
return UnpackNormalmapRGorAG(nrm, scale);
float4 RemapMasks(float4 masks, float blendMask, float heightCenter, float heightAmplitude, float smoothness, float metallic)
float4 RemapMasks(float4 masks, float blendMask, float metallic, float smoothness, float4 remapOffset, float4 remapScale)
return float4(
(masks.r * blendMask - heightCenter) * heightAmplitude,
masks.g * smoothness,
masks.b * metallic,
0);
float4 ret = masks;
ret.r *= metallic;
ret.b *= blendMask; // height needs to be weighted before remapping
ret.a *= smoothness;
ret = ret * remapScale + remapOffset;
return ret;
}
#ifdef OVERRIDE_SAMPLER_NAME

void TerrainSplatBlend(float2 uv, float3 tangentWS, float3 bitangentWS,
out float3 outAlbedo, out float3 outNormalTS, out float outSmoothness, out float outMetallic)
out float3 outAlbedo, out float3 outNormalTS, out float outSmoothness, out float outMetallic, out float outAO)
{
// TODO: triplanar and SURFACE_GRADIENT?
// TODO: POM

float4 masks[_LAYER_COUNT];
#ifdef _NORMALMAP
#define SampleNormal(i) SampleNormalGrad(_Normal##i, sampler_Splat0, splatuv, splatdxuv, splatdyuv, tangentWS, bitangentWS)
#define SampleNormal(i) SampleNormalGrad(_Normal##i, sampler_Splat0, splatuv, splatdxuv, splatdyuv, _NormalScale##i, tangentWS, bitangentWS)
#define SampleMasks(i, blendMask) RemapMasks(SAMPLE_TEXTURE2D_GRAD(_Mask##i, sampler_Splat0, splatuv, splatdxuv, splatdyuv), blendMask, _HeightCenter##i, _HeightAmplitude##i, _Smoothness##i, _Metallic##i)
#define SampleMasks(i, blendMask) RemapMasks(SAMPLE_TEXTURE2D_GRAD(_Mask##i, sampler_Splat0, splatuv, splatdxuv, splatdyuv), blendMask, _Metallic##i, _Smoothness##i, _MaskRemapOffset##i, _MaskRemapScale##i)
#define NullMask(i) float4(0, 0, _MaskRemapOffset##i.z, 0) // only height matters when weight is zero.
#define SampleMasks(i, blendMask) float4(0, albedo[i].a * _Smoothness##i, _Metallic##i, 0)
#define SampleMasks(i, blendMask) float4(_Metallic##i, 0, 0, albedo[i].a * _Smoothness##i)
#define NullMask(i) float4(0, 0, 0, 0)
#endif
#define SampleResults(i, mask) \

{ \
albedo[i] = float4(0, 0, 0, 0); \
normal[i] = float3(0, 0, 0); \
masks[i] = float4(-1, 0, 0, 0); \
masks[i] = NullMask(i); \
}
float2 dxuv = ddx(uv);

#if defined(_TERRAIN_BLEND_HEIGHT) && defined(_MASKMAP)
// Modify blendMask to take into account the height of the layer. Higher height should be more visible.
float maxHeight = masks[0].x;
maxHeight = max(maxHeight, masks[1].x);
maxHeight = max(maxHeight, masks[2].x);
maxHeight = max(maxHeight, masks[3].x);
float maxHeight = masks[0].z;
maxHeight = max(maxHeight, masks[1].z);
maxHeight = max(maxHeight, masks[2].z);
maxHeight = max(maxHeight, masks[3].z);
maxHeight = max(maxHeight, masks[4].x);
maxHeight = max(maxHeight, masks[5].x);
maxHeight = max(maxHeight, masks[6].x);
maxHeight = max(maxHeight, masks[7].x);
maxHeight = max(maxHeight, masks[4].z);
maxHeight = max(maxHeight, masks[5].z);
maxHeight = max(maxHeight, masks[6].z);
maxHeight = max(maxHeight, masks[7].z);
#endif
// Make sure that transition is not zero otherwise the next computation will be wrong.

// The goal here is to have all but the highest layer at negative heights, then we add the transition so that if the next highest layer is near transition it will have a positive value.
// Then we clamp this to zero and normalize everything so that highest layer has a value of 1.
float4 weightedHeights0 = { masks[0].x, masks[1].x, masks[2].x, masks[3].x };
float4 weightedHeights0 = { masks[0].z, masks[1].z, masks[2].z, masks[3].z };
float4 weightedHeights1 = { masks[4].x, masks[5].x, masks[6].x, masks[7].x };
float4 weightedHeights1 = { masks[4].z, masks[5].z, masks[6].z, masks[7].z };
weightedHeights1 = weightedHeights1 - maxHeight.xxxx;
weightedHeights1 = (max(0, weightedHeights1 + transition) + 1e-6) * blendMasks1;
#else

#elif defined(_TERRAIN_BLEND_DENSITY) && defined(_MASKMAP)
// Denser layers are more visible.
float4 opacityAsDensity0 = saturate((float4(albedo[0].a, albedo[1].a, albedo[2].a, albedo[3].a) - (float4(1.0, 1.0, 1.0, 1.0) - blendMasks0)) * 20.0); // 20.0 is the number of steps in inputAlphaMask (Density mask. We decided 20 empirically)
float4 useOpacityAsDensityParam0 = { _Density0, _Density1, _Density2, _Density3 };
float4 useOpacityAsDensityParam0 = { 1, 1, 1, 1 };
float4 useOpacityAsDensityParam1 = { _Density4, _Density5, _Density6, _Density7 };
float4 useOpacityAsDensityParam1 = { 1, 1, 1, 1 };
blendMasks1 = lerp(blendMasks1, opacityAsDensity1, useOpacityAsDensityParam1);
#endif
#endif

// If a top layer doesn't use the full weight, the remaining can be use by the following layer.
float weightsSum = 0.0;
UNITY_UNROLL
for (int i = _LAYER_COUNT - 1; i >= 0; --i)
UNITY_UNROLL for (int i = _LAYER_COUNT - 1; i >= 0; --i)
{
weights[i] = min(weights[i], (1.0 - weightsSum));
weightsSum = saturate(weightsSum + weights[i]);

outAlbedo = 0;
outNormalTS = 0;
float2 outMasks = 0;
float3 outMasks = 0;
outNormalTS += normal[i].rgb * weights[i];
outMasks += masks[i].yz * weights[i];
outNormalTS += normal[i].rgb * weights[i]; // no need to normalize
outMasks += masks[i].xyw * weights[i];
}
#ifndef _NORMALMAP
#ifdef SURFACE_GRADIENT

#endif
#endif
outSmoothness = outMasks.x;
outMetallic = outMasks.y;
outSmoothness = outMasks.z;
outMetallic = outMasks.x;
outAO = saturate(1 - outMasks.y);
}

12
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/TerrainLit/TerrainLit_Basemap_Gen.shader


float4 albedo;
float3 normalTS;
float metallic;
float ao;
albedo.xyz, normalTS, albedo.w, metallic);
albedo.xyz, normalTS, albedo.w, metallic, ao);
return albedo;
}

Tags
{
"Name" = "_MetallicTex"
"Format" = "R8"
"Format" = "RG16"
"Size" = "1/4"
}

return o;
}
float4 frag(v2f i) : SV_Target
float2 frag(v2f i) : SV_Target
float ao;
albedo.xyz, normalTS, albedo.w, metallic);
albedo.xyz, normalTS, albedo.w, metallic, ao);
return metallic;
return float2(metallic, ao);
}
ENDHLSL

正在加载...
取消
保存