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+
622struct 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
123132void 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
202215half4 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;
0 commit comments