Skip to content

Commit 90fcdf7

Browse files
authored
Merge pull request #1901 from Unity-Technologies/gpu-terrain
Enabled per-pixel terrain normal on LW terrain shader.
2 parents eed1665 + 92a7bd1 commit 90fcdf7

File tree

8 files changed

+51
-24
lines changed

8 files changed

+51
-24
lines changed

com.unity.render-pipelines.high-definition/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [3.4.0-preview]
88

9+
### Added
10+
- Added a new TerrainLit shader that supports rendering of Unity terrains.
11+
912
### Fixed
1013
- Fixed an issue where sometimes the deferred shadow texture would not be valid, causing wrong rendering.
1114

com.unity.render-pipelines.lightweight/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1111
- The `RenderingData` struct now holds a reference to `CullResults`.
1212
- When __HDR__ is enabled in the Camera but disabled in the Asset, an information box in the Camera Inspector informs you about it.
1313
- When __MSAA__ is enabled in the Camera but disabled in the Asset, an information box in the Camera Inspector informs you about it.
14+
- Enabled instancing on the terrain shader.
1415
### Changed
1516
- The `RenderingData` struct is now read-only.
1617
- `ScriptableRenderer`always perform a Clear before calling `IRendererSetup::Setup.`

com.unity.render-pipelines.lightweight/LWRP/ShaderLibrary/Terrain/LightweightPassLitTerrain.hlsl

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,25 @@
33

44
#include "LWRP/ShaderLibrary/Lighting.hlsl"
55

6+
#if defined(UNITY_INSTANCING_ENABLED) && defined(_TERRAIN_INSTANCED_PERPIXEL_NORMAL)
7+
#define ENABLE_TERRAIN_PERPIXEL_NORMAL
8+
#endif
9+
10+
#ifdef UNITY_INSTANCING_ENABLED
11+
TEXTURE2D(_TerrainHeightmapTexture);
12+
TEXTURE2D(_TerrainNormalmapTexture);
13+
SAMPLER(sampler_TerrainNormalmapTexture);
14+
float4 _TerrainHeightmapRecipSize; // float4(1.0f/width, 1.0f/height, 1.0f/(width-1), 1.0f/(height-1))
15+
float4 _TerrainHeightmapScale; // float4(hmScale.x, hmScale.y / (float)(kMaxHeight), hmScale.z, 0.0f)
16+
#endif
17+
18+
UNITY_INSTANCING_BUFFER_START(Terrain)
19+
UNITY_DEFINE_INSTANCED_PROP(float4, _TerrainPatchInstanceData) // float4(xBase, yBase, skipScale, ~)
20+
UNITY_INSTANCING_BUFFER_END(Terrain)
21+
622
struct VertexInput
723
{
824
float4 vertex : POSITION;
9-
float4 tangent : TANGENT;
1025
float3 normal : NORMAL;
1126
float2 texcoord : TEXCOORD0;
1227
UNITY_VERTEX_INPUT_INSTANCE_ID
@@ -20,7 +35,7 @@ struct VertexOutput
2035
float4 uvSplat23 : TEXCOORD2; // xy: splat2, zw: splat3
2136
#endif
2237

23-
#if _NORMALMAP
38+
#if defined(_NORMALMAP) && !defined(ENABLE_TERRAIN_PERPIXEL_NORMAL)
2439
half4 normal : TEXCOORD3; // xyz: normal, w: viewDir.x
2540
half4 tangent : TEXCOORD4; // xyz: tangent, w: viewDir.y
2641
half4 binormal : TEXCOORD5; // xyz: binormal, w: viewDir.z
@@ -41,9 +56,15 @@ void InitializeInputData(VertexOutput IN, half3 normalTS, out InputData input)
4156

4257
input.positionWS = IN.positionWS;
4358

44-
#ifdef _NORMALMAP
59+
#if defined(_NORMALMAP) && !defined(ENABLE_TERRAIN_PERPIXEL_NORMAL)
4560
half3 viewDir = half3(IN.normal.w, IN.tangent.w, IN.binormal.w);
4661
input.normalWS = TangentToWorldNormal(normalTS, IN.tangent.xyz, IN.binormal.xyz, IN.normal.xyz);
62+
#elif defined(ENABLE_TERRAIN_PERPIXEL_NORMAL)
63+
half3 viewDir = IN.viewDir;
64+
float2 sampleCoords = (IN.uvMainAndLM.xy / _TerrainHeightmapRecipSize.zw + 0.5f) * _TerrainHeightmapRecipSize.xy;
65+
half3 normalWS = TransformObjectToWorldNormal(normalize(SAMPLE_TEXTURE2D(_TerrainNormalmapTexture, sampler_TerrainNormalmapTexture, sampleCoords).rgb * 2 - 1));
66+
half3 tangentWS = cross(GetObjectToWorldMatrix()._13_23_33, normalWS);
67+
input.normalWS = TangentToWorldNormal(normalTS, tangentWS, cross(normalWS, tangentWS), normalWS);
4768
#else
4869
half3 viewDir = IN.viewDir;
4970
input.normalWS = FragmentNormalWS(IN.normal);
@@ -93,8 +114,6 @@ void SplatmapMix(VertexOutput IN, half4 defaultAlpha, out half4 splatControl, ou
93114
nrm += SAMPLE_TEXTURE2D(_Normal2, sampler_Normal0, IN.uvSplat23.xy) * splatControl.b;
94115
nrm += SAMPLE_TEXTURE2D(_Normal3, sampler_Normal0, IN.uvSplat23.zw) * splatControl.a;
95116
mixedNormal = UnpackNormal(nrm);
96-
#else
97-
mixedNormal = half3(0.0h, 0.0h, 1.0h);
98117
#endif
99118
}
100119

@@ -109,16 +128,6 @@ void SplatmapFinalColor(inout half4 color, half fogCoord)
109128
ApplyFog(color.rgb, fogCoord);
110129
#endif
111130
}
112-
#ifdef UNITY_INSTANCING_ENABLED
113-
TEXTURE2D(_TerrainHeightmapTexture);
114-
TEXTURE2D(_TerrainNormalmapTexture);
115-
float4 _TerrainHeightmapRecipSize; // float4(1.0f/width, 1.0f/height, 1.0f/(width-1), 1.0f/(height-1))
116-
float4 _TerrainHeightmapScale; // float4(hmScale.x, hmScale.y / (float)(kMaxHeight), hmScale.z, 0.0f)
117-
#endif
118-
119-
UNITY_INSTANCING_BUFFER_START(Terrain)
120-
UNITY_DEFINE_INSTANCED_PROP(float4, _TerrainPatchInstanceData) // float4(xBase, yBase, skipScale, ~)
121-
UNITY_INSTANCING_BUFFER_END(Terrain)
122131

123132
void TerrainInstancing(inout float4 vertex, inout float3 normal, inout float2 uv)
124133
{
@@ -132,7 +141,11 @@ void TerrainInstancing(inout float4 vertex, inout float3 normal, inout float2 uv
132141
vertex.xz = sampleCoords * _TerrainHeightmapScale.xz;
133142
vertex.y = height * _TerrainHeightmapScale.y;
134143

135-
normal = _TerrainNormalmapTexture.Load(int3(sampleCoords, 0)).rgb * 2 - 1;
144+
#ifdef ENABLE_TERRAIN_PERPIXEL_NORMAL
145+
normal = float3(0, 1, 0);
146+
#else
147+
normal = _TerrainNormalmapTexture.Load(int3(sampleCoords, 0)).rgb * 2 - 1;
148+
#endif
136149
uv = sampleCoords * _TerrainHeightmapRecipSize.zw;
137150
#endif
138151
}
@@ -169,8 +182,8 @@ VertexOutput SplatmapVert(VertexInput v)
169182

170183
half3 viewDir = VertexViewDirWS(GetCameraPositionWS() - positionWS.xyz);
171184

172-
#ifdef _NORMALMAP
173-
float4 vertexTangent = float4(cross(v.normal, float3(0, 0, 1)), -1.0);
185+
#if defined(_NORMALMAP) && !defined(ENABLE_TERRAIN_PERPIXEL_NORMAL)
186+
float4 vertexTangent = float4(cross(float3(0, 0, 1), v.normal), 1.0);
174187
OutputTangentToWorld(vertexTangent, v.normal, o.tangent.xyz, o.binormal.xyz, o.normal.xyz);
175188

176189
o.normal.w = viewDir.x;
@@ -201,8 +214,8 @@ TEXTURE2D(_MetallicTex); SAMPLER(sampler_MetallicTex);
201214
// Used in Standard Terrain shader
202215
half4 SplatmapFragment(VertexOutput IN) : SV_TARGET
203216
{
217+
half3 normalTS = half3(0.0h, 0.0h, 1.0h);
204218
#ifdef TERRAIN_SPLAT_BASEPASS
205-
half3 normalTS = float3(0, 1, 0);
206219
half3 albedo = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uvMainAndLM.xy).rgb;
207220
half smoothness = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uvMainAndLM.xy).a;
208221
half metallic = SAMPLE_TEXTURE2D(_MetallicTex, sampler_MetallicTex, IN.uvMainAndLM.xy).r;
@@ -212,7 +225,6 @@ half4 SplatmapFragment(VertexOutput IN) : SV_TARGET
212225
half weight;
213226
half4 mixedDiffuse;
214227
half4 defaultSmoothness = half4(_Smoothness0, _Smoothness1, _Smoothness2, _Smoothness3);
215-
half3 normalTS;
216228
SplatmapMix(IN, defaultSmoothness, splatControl, weight, mixedDiffuse, normalTS);
217229

218230
half3 albedo = mixedDiffuse.rgb;

com.unity.render-pipelines.lightweight/LWRP/Shaders/Terrain/LightweightStandardTerrain.shader

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Shader "LightweightPipeline/Terrain/Standard Terrain"
2424
// used in fallback on old cards & base map
2525
[HideInInspector] _MainTex("BaseMap (RGB)", 2D) = "grey" {}
2626
[HideInInspector] _Color("Main Color", Color) = (1,1,1,1)
27+
28+
// TODO: Implement ShaderGUI for the shader and display the checkbox only when instancing is enabled.
29+
[Toggle(_TERRAIN_INSTANCED_PERPIXEL_NORMAL)] _TERRAIN_INSTANCED_PERPIXEL_NORMAL("Enable Instanced Per-pixel Normal", Float) = 0
2730
}
2831

2932
SubShader
@@ -65,6 +68,8 @@ Shader "LightweightPipeline/Terrain/Standard Terrain"
6568
#pragma instancing_options assumeuniformscaling nomatrices nolightprobe nolightmap
6669

6770
#pragma shader_feature _NORMALMAP
71+
// Sample normal in pixel shader when doing instancing
72+
#pragma shader_feature _TERRAIN_INSTANCED_PERPIXEL_NORMAL
6873

6974
#include "LWRP/ShaderLibrary/Terrain/InputSurfaceTerrain.hlsl"
7075
#include "LWRP/ShaderLibrary/Terrain/LightweightPassLitTerrain.hlsl"

com.unity.render-pipelines.lightweight/LWRP/Shaders/Terrain/LightweightStandardTerrainBase.shader

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ Shader "Hidden/LightweightPipeline/Terrain/Standard Terrain Base"
5555
#pragma fragment SplatmapFragment
5656

5757
#pragma shader_feature _NORMALMAP
58+
// Sample normal in pixel shader when doing instancing
59+
#pragma shader_feature _TERRAIN_INSTANCED_PERPIXEL_NORMAL
5860
#define TERRAIN_SPLAT_BASEPASS 1
5961

6062
#include "LWRP/ShaderLibrary/Terrain/InputSurfaceTerrain.hlsl"

com.unity.testing.srp.lightweight/Tests/Scenes/035_Shader_TerrainShaders.unity

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ Terrain:
163163
m_HeightmapMaximumLOD: 0
164164
m_CastShadows: 1
165165
m_DrawHeightmap: 1
166-
m_DrawInstanced: 0
166+
m_DrawInstanced: 1
167167
m_DrawTreesAndFoliage: 1
168168
m_ReflectionProbeUsage: 1
169169
m_MaterialType: 3
@@ -176,6 +176,8 @@ Terrain:
176176
m_PreserveTreePrototypeLayers: 0
177177
m_ScaleInLightmap: 0.0512
178178
m_LightmapParameters: {fileID: 15203, guid: 0000000000000000f000000000000000, type: 0}
179+
m_GroupingID: 0
180+
m_AllowAutoConnect: 0
179181
--- !u!4 &745712513
180182
Transform:
181183
m_ObjectHideFlags: 0
Binary file not shown.

com.unity.testing.srp.lightweight/Tests/Scenes/035_Shader_TerrainShaders/Terrain.mat

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
Material:
55
serializedVersion: 6
66
m_ObjectHideFlags: 0
7-
m_PrefabParentObject: {fileID: 0}
8-
m_PrefabInternal: {fileID: 0}
7+
m_CorrespondingSourceObject: {fileID: 0}
8+
m_PrefabInstance: {fileID: 0}
9+
m_PrefabAsset: {fileID: 0}
910
m_Name: Terrain
1011
m_Shader: {fileID: 4800000, guid: 69c1f799e772cb6438f56c23efccb782, type: 3}
1112
m_ShaderKeywords:
1213
m_LightmapFlags: 4
13-
m_EnableInstancingVariants: 0
14+
m_EnableInstancingVariants: 1
1415
m_DoubleSidedGI: 0
1516
m_CustomRenderQueue: -1
1617
stringTagMap: {}
@@ -117,6 +118,7 @@ Material:
117118
- _SmoothnessTextureChannel: 0
118119
- _SpecularHighlights: 1
119120
- _SrcBlend: 1
121+
- _TERRAIN_INSTANCED_PERPIXEL_NORMAL: 0
120122
- _UVSec: 0
121123
- _WorkflowMode: 1
122124
- _ZWrite: 1

0 commit comments

Comments
 (0)