浏览代码

Added instanced per pixel normalmapping.

/main
Yao Xiaoling 7 年前
当前提交
0208553d
共有 8 个文件被更改,包括 93 次插入9 次删除
  1. 19
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/TerrainLit/TerrainLitUI.cs
  2. 5
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/TerrainLit/TerrainLit.shader
  3. 26
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/TerrainLit/TerrainLitData.hlsl
  4. 6
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/TerrainLit/TerrainLitDataMeshModification.hlsl
  5. 25
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/TerrainLit/TerrainLitData_Basemap.hlsl
  6. 11
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/TerrainLit/TerrainLitSharePass.hlsl
  7. 1
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/TerrainLit/TerrainLit_Basemap.shader
  8. 9
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/RenderPipelineResources/DefaultHDTerrainMaterial.mat

19
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/TerrainLit/TerrainLitUI.cs


public readonly GUIContent layerMapMaskText = new GUIContent("Layer Mask", "Layer mask");
public readonly GUIContent enableHeightBlending = new GUIContent("Enable Height Blending", "Enables layer blending using heightmaps.");
public readonly GUIContent heightTransition = new GUIContent("Height Transition", "Size in world units of the smooth transition between layers.");
public readonly GUIContent enableInstancedPerPixelNormal = new GUIContent("Enable Per-pixel Normal", "Enable per-pixel normal when the terrain uses instanced rendering.");
}
static StylesLayer s_Styles = null;

MaterialProperty heightTransition = null;
const string kHeightTransition = "_HeightTransition";
MaterialProperty enableInstancedPerPixelNormal = null;
const string kEnableInstancedPerPixelNormal = "_EnableInstancedPerPixelNormal";
protected override void FindMaterialProperties(MaterialProperty[] props)
{
enableHeightBlending = FindProperty(kEnableHeightBlending, props, false);

// Density/opacity mode
opacityAsDensity[i] = FindProperty(string.Format("{0}{1}", kOpacityAsDensity, i), props);
}
enableInstancedPerPixelNormal = FindProperty(kEnableInstancedPerPixelNormal, props, false);
}
// We use the user data to save a string that represent the referenced lit material

// TODO: planar/triplannar supprt
//SetupLayersMappingKeywords(material);
bool enableInstancedPerPixelNormal = material.GetFloat(kEnableInstancedPerPixelNormal) > 0.0f;
CoreUtils.SetKeyword(material, "_TERRAIN_INSTANCED_PERPIXEL_NORMAL", enableInstancedPerPixelNormal);
}
public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props)

}
bool layerChanged = DoLayersGUI(materialImporter);
bool enablePerPixelNormalChanged = false;
EditorGUILayout.Space();
EditorGUILayout.LabelField(StylesBaseUnlit.advancedText, EditorStyles.boldLabel);

if (m_MaterialEditor.IsInstancingEnabled())
{
EditorGUI.indentLevel++;
EditorGUI.BeginChangeCheck();
m_MaterialEditor.ShaderProperty(enableInstancedPerPixelNormal, styles.enableInstancedPerPixelNormal);
enablePerPixelNormalChanged = EditorGUI.EndChangeCheck();
EditorGUI.indentLevel--;
}
if (layerChanged || optionsChanged)
if (layerChanged || optionsChanged || enablePerPixelNormalChanged)
{
foreach (var obj in m_MaterialEditor.targets)
{

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


[Enum(Flip, 0, Mirror, 1, None, 2)] _DoubleSidedNormalMode("Double sided normal mode", Float) = 1
[HideInInspector] _DoubleSidedConstants("_DoubleSidedConstants", Vector) = (1, 1, -1, 0)
[ToggleUI] _EnableInstancedPerPixelNormal("Instanced per pixel normal", Float) = 1.0
// Caution: C# code in BaseLitUI.cs call LightmapEmissionFlagsProperty() which assume that there is an existing "_EmissionColor"
// value that exist to identify if the GI emission need to be enabled.
// In our case we don't use such a mechanism but need to keep the code quiet. We declare the value and always enable it.

#pragma shader_feature _TERRAIN_NORMAL_MAP
#pragma shader_feature _TERRAIN_HEIGHT_MAP
// #pragma shader_feature _HEIGHT_BASED_BLEND // _HEIGHT_BASED_BLEND is implied if heightmap is used.
// Sample normal in pixel shader when doing instancing
#pragma shader_feature _TERRAIN_INSTANCED_PERPIXEL_NORMAL
//#pragma shader_feature _MASKMAP0
//#pragma shader_feature _MASKMAP1

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


#include "../Decal/DecalUtilities.hlsl"
#include "TerrainLitSplatCommon.hlsl"
#include "TerrainLitDataMeshModification.hlsl"
void GetSurfaceAndBuiltinData(FragInputs input, float3 V, inout PositionInputs posInput, out SurfaceData surfaceData, out BuiltinData builtinData)
void GetSurfaceAndBuiltinData(inout FragInputs input, float3 V, inout PositionInputs posInput, out SurfaceData surfaceData, out BuiltinData builtinData)
#ifdef ENABLE_TERRAIN_PERPIXEL_NORMAL
{
float3 normalOS = SAMPLE_TEXTURE2D(_TerrainNormalmapTexture, sampler_Control0, (input.texCoord0 + 0.5f) * _TerrainHeightmapRecipSize.xy).rgb * 2 - 1;
float3 normalWS = ((float3x3)GetObjectToWorldMatrix(), normalOS);
float3 tangentWS = cross(GetObjectToWorldMatrix()._13_23_33, normalWS);
float renormFactor = 1.0 / length(normalWS);
// bitangent on the fly option in xnormal to reduce vertex shader outputs.
// this is the mikktspace transformation (must use unnormalized attributes)
float3x3 worldToTangent = CreateWorldToTangent(normalWS, tangentWS.xyz, 1);
// surface gradient based formulation requires a unit length initial normal. We can maintain compliance with mikkts
// by uniformly scaling all 3 vectors since normalization of the perturbed normal will cancel it.
input.worldToTangent[0] = worldToTangent[0] * renormFactor;
input.worldToTangent[1] = worldToTangent[1] * renormFactor;
input.worldToTangent[2] = worldToTangent[2] * renormFactor; // normalizes the interpolated vertex normal
input.texCoord0 *= _TerrainHeightmapRecipSize.zw;
}
#endif
// terrain lightmap uvs are always taken from uv0
input.texCoord1 = input.texCoord2 = input.texCoord0;

GetBuiltinData(input, surfaceData, 1, bentNormalWS, 0, builtinData);
}
#include "TerrainLitDataMeshModification.hlsl"

6
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/TerrainLit/TerrainLitDataMeshModification.hlsl


#endif
#if defined(VARYINGS_NEED_TEXCOORD0) || defined(VARYINGS_DS_NEED_TEXCOORD0)
input.uv0 = sampleCoords * _TerrainHeightmapRecipSize.zw;
#ifdef ENABLE_TERRAIN_PERPIXEL_NORMAL
input.uv0 = sampleCoords;
#else
input.uv0 = sampleCoords * _TerrainHeightmapRecipSize.zw;
#endif
#endif
#endif

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


TEXTURE2D(_MetallicTex);
SAMPLER(sampler_MainTex);
#include "TerrainLitDataMeshModification.hlsl"
#ifdef ENABLE_TERRAIN_PERPIXEL_NORMAL
{
float3 normalOS = SAMPLE_TEXTURE2D(_TerrainNormalmapTexture, sampler_MainTex, (input.texCoord0 + 0.5f) * _TerrainHeightmapRecipSize.xy).rgb * 2 - 1;
float3 normalWS = ((float3x3)GetObjectToWorldMatrix(), normalOS);
float3 tangentWS = cross(GetObjectToWorldMatrix()._13_23_33, normalWS);
float renormFactor = 1.0 / length(normalWS);
// bitangent on the fly option in xnormal to reduce vertex shader outputs.
// this is the mikktspace transformation (must use unnormalized attributes)
float3x3 worldToTangent = CreateWorldToTangent(normalWS, tangentWS.xyz, 1);
// surface gradient based formulation requires a unit length initial normal. We can maintain compliance with mikkts
// by uniformly scaling all 3 vectors since normalization of the perturbed normal will cancel it.
input.worldToTangent[0] = worldToTangent[0] * renormFactor;
input.worldToTangent[1] = worldToTangent[1] * renormFactor;
input.worldToTangent[2] = worldToTangent[2] * renormFactor; // normalizes the interpolated vertex normal
input.texCoord0 *= _TerrainHeightmapRecipSize.zw;
}
#endif
// terrain lightmap uvs are always taken from uv0
input.texCoord1 = input.texCoord2 = input.texCoord0;

GetBuiltinData(input, surfaceData, 1, bentNormalWS, 0, builtinData);
}
#include "TerrainLitDataMeshModification.hlsl"

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


#define VARYINGS_NEED_CULLFACE
#endif
#if defined(UNITY_INSTANCING_ENABLED) && defined(_TERRAIN_INSTANCED_PERPIXEL_NORMAL)
#define ENABLE_TERRAIN_PERPIXEL_NORMAL
#endif
#ifdef ENABLE_TERRAIN_PERPIXEL_NORMAL
// With per-pixel normal enabled, tangent space is created in the pixel shader.
#undef ATTRIBUTES_NEED_NORMAL
#undef ATTRIBUTES_NEED_TANGENT
#undef VARYINGS_NEED_TANGENT_TO_WORLD
#endif
// This include will define the various Attributes/Varyings structure
#include "../../ShaderPass/VaryingMesh.hlsl"

1
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/TerrainLit/TerrainLit_Basemap.shader


#pragma shader_feature _DOUBLESIDED_ON
#pragma shader_feature _DISABLE_DBUFFER
#pragma shader_feature _TERRAIN_INSTANCED_PERPIXEL_NORMAL
//enable GPU instancing support
#pragma multi_compile_instancing

9
ScriptableRenderPipeline/HDRenderPipeline/HDRP/RenderPipelineResources/DefaultHDTerrainMaterial.mat


m_PrefabInternal: {fileID: 0}
m_Name: DefaultHDTerrainMaterial
m_Shader: {fileID: 4800000, guid: 19e129e21de2c4042995eefba8405e9e, type: 3}
m_ShaderKeywords:
m_ShaderKeywords: _TERRAIN_INSTANCED_PERPIXEL_NORMAL
m_LightmapFlags: 4
m_EnableInstancingVariants: 1
m_DoubleSidedGI: 0

m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _Height7:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}

- _DoubleSidedEnable: 0
- _DoubleSidedNormalMode: 1
- _EnableHeightBlending: 1
- _EnableInstancedPerPixelNormal: 1
- _HeightAmplitude0: 0.02
- _HeightAmplitude1: 0.02
- _HeightAmplitude2: 0.02

- _HeightCenter5: 0.5
- _HeightCenter6: 0.5
- _HeightCenter7: 0.5
- _HeightTransition: 0.004
- _HeightTransition: 0
- _Metallic0: 0
- _Metallic1: 0
- _Metallic2: 0

正在加载...
取消
保存