Lux URP MeshTerrain.shader 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. Shader "Lux URP/Terrain/Mesh Terrain"
  2. {
  3. Properties
  4. {
  5. [HeaderHelpLuxURP_URL(v7hplahjb13)]
  6. [Header(Surface Options)]
  7. [Space(8)]
  8. [ToggleOff(_RECEIVE_SHADOWS_OFF)]
  9. _ReceiveShadows ("Receive Shadows", Float) = 1.0
  10. [Header(Surface Inputs)]
  11. [Space(8)]
  12. [Toggle(_NORMALMAP)]
  13. _ApplyNormal ("Enable Normal Maps", Float) = 1.0
  14. [Toggle(_TOPDOWNPROJECTION)]
  15. _ApplyTopDownProjection ("Enable Top Down Projection", Float) = 0.0
  16. _TopDownTiling (" Tiling in World Space", Float) = 1.0
  17. [Space(5)]
  18. [NoScaleOffset] _DetailA0 ("Detail 0 Albedo (RGB) Smoothness (A)", 2D) = "gray" {}
  19. [NoScaleOffset] _Normal0 (" Normal 0", 2D) = "bump" {}
  20. [NoScaleOffset] _DetailA1 ("Detail 1 Albedo (RGB) Smoothness (A)", 2D) = "gray" {}
  21. [NoScaleOffset] _Normal1 (" Normal 1", 2D) = "bump" {}
  22. [NoScaleOffset] _DetailA2 ("Detail 2 Albedo (RGB) Smoothness (A)", 2D) = "gray" {}
  23. [NoScaleOffset] _Normal2 (" Normal 2", 2D) = "bump" {}
  24. [NoScaleOffset] _DetailA3 ("Detail 3 Albedo (RGB) Smoothness (A)", 2D) = "gray" {}
  25. [NoScaleOffset] _Normal3 (" Normal 3", 2D) = "bump" {}
  26. [Space(5)]
  27. [Toggle(_USEVERTEXCOLORS)]
  28. _VertexColors ("Use Vertex Colors", Float) = 0.0
  29. [NoScaleOffset] _SplatMap ("Splat Map (RGB)", 2D) = "red" {}
  30. [Space(5)]
  31. [LuxURPVectorTwoDrawer] _SplatTiling("Detail Tiling (UV)", Vector) = (1,1,0,0)
  32. _Specular("Specular", Color) = (0.2,0.2,0.2,0)
  33. _Occlusion("Occlusion", Range(0, 1)) = 0
  34. [Header(Advanced)]
  35. [Space(8)]
  36. [ToggleOff]
  37. _SpecularHighlights ("Enable Specular Highlights", Float) = 1.0
  38. [ToggleOff]
  39. _EnvironmentReflections ("Environment Reflections", Float) = 1.0
  40. }
  41. // HLSLINCLUDE
  42. // #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  43. // ENDHLSL
  44. SubShader
  45. {
  46. Tags
  47. {
  48. "RenderPipeline" = "UniversalPipeline"
  49. "RenderType"="Opaque"
  50. "Queue"="Geometry-100"
  51. }
  52. Pass
  53. {
  54. Name "ForwardLit"
  55. Tags{"LightMode" = "UniversalForward"}
  56. Blend One Zero
  57. Cull Back
  58. ZTest LEqual
  59. ZWrite On
  60. HLSLPROGRAM
  61. // Required to compile gles 2.0 with standard srp library
  62. #pragma prefer_hlslcc gles
  63. #pragma exclude_renderers d3d11_9x
  64. #pragma target 2.0
  65. // Tell Polybrush that this shader supports 4 texture channels
  66. #define Z_TEXTURE_CHANNELS 4
  67. #define Z_MESH_ATTRIBUTES COLOR
  68. // -------------------------------------
  69. // Material Keywords
  70. #define _SPECULAR_SETUP 1
  71. #pragma shader_feature_local _NORMALMAP
  72. #pragma shader_feature_local_fragment _TOPDOWNPROJECTION
  73. #pragma shader_feature_local _USEVERTEXCOLORS
  74. #pragma shader_feature_local_fragment _SPECULARHIGHLIGHTS_OFF
  75. #pragma shader_feature_local_fragment _ENVIRONMENTREFLECTIONS_OFF
  76. #pragma shader_feature_local _RECEIVE_SHADOWS_OFF
  77. // -------------------------------------
  78. // Universal Pipeline keywords
  79. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
  80. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
  81. #pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
  82. #pragma multi_compile_fragment _ _ADDITIONAL_LIGHT_SHADOWS
  83. #pragma multi_compile_fragment _ _SHADOWS_SOFT
  84. #pragma multi_compile_fragment _ _SCREEN_SPACE_OCCLUSION
  85. #pragma multi_compile _ LIGHTMAP_SHADOW_MIXING
  86. #pragma multi_compile _ SHADOWS_SHADOWMASK
  87. // -------------------------------------
  88. // Unity defined keywords
  89. #pragma multi_compile _ DIRLIGHTMAP_COMBINED
  90. #pragma multi_compile _ LIGHTMAP_ON
  91. #pragma multi_compile_fog
  92. //--------------------------------------
  93. // GPU Instancing
  94. // Does this make sense here? Well: maybe
  95. #pragma multi_compile_instancing
  96. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  97. #pragma vertex vert
  98. #pragma fragment frag
  99. //#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  100. // defines SurfaceData, textures and the functions Alpha, SampleAlbedoAlpha, SampleNormal, SampleEmission
  101. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl"
  102. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  103. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
  104. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"
  105. CBUFFER_START(UnityPerMaterial)
  106. float2 _SplatTiling;
  107. float4 _Specular;
  108. float _Occlusion;
  109. half _TopDownTiling;
  110. CBUFFER_END
  111. TEXTURE2D(_DetailA0); SAMPLER(sampler_DetailA0); float4 _DetailA0_TexelSize;
  112. TEXTURE2D(_Normal0); SAMPLER(sampler_Normal0); float4 _Normal0_TexelSize;
  113. TEXTURE2D(_DetailA1); float4 _DetailA1_TexelSize;
  114. TEXTURE2D(_Normal1); float4 _Normal1_TexelSize;
  115. TEXTURE2D(_DetailA2); float4 _DetailA2_TexelSize;
  116. TEXTURE2D(_Normal2); float4 _Normal2_TexelSize;
  117. TEXTURE2D(_DetailA3); float4 _DetailA3_TexelSize;
  118. TEXTURE2D(_Normal3); float4 _Normal3_TexelSize;
  119. TEXTURE2D(_SplatMap); SAMPLER(sampler_SplatMap); float4 _SplatMap_TexelSize;
  120. struct VertexInput
  121. {
  122. float4 positionOS : POSITION;
  123. float3 normalOS : NORMAL;
  124. float4 tangentOS : TANGENT;
  125. float2 texcoord : TEXCOORD0;
  126. float2 lightmapUV : TEXCOORD1;
  127. #if defined(_USEVERTEXCOLORS)
  128. half4 color : COLOR;
  129. #endif
  130. UNITY_VERTEX_INPUT_INSTANCE_ID
  131. };
  132. struct VertexOutput
  133. {
  134. float4 positionCS : SV_POSITION;
  135. DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH, 0);
  136. half4 fogFactorAndVertexLight : TEXCOORD1; // x: fogFactor, yzw: vertex light
  137. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  138. float4 shadowCoord : TEXCOORD2;
  139. #endif
  140. float3 normalWS : TEXCOORD3;
  141. float3 viewDirWS : TEXCOORD4;
  142. #ifdef _NORMALMAP
  143. float4 tangentWS : TEXCOORD5;
  144. #endif
  145. float3 positionWS : TEXCOORD6;
  146. float2 uv0 : TEXCOORD7;
  147. #if defined(_USEVERTEXCOLORS)
  148. half4 color : COLOR;
  149. #endif
  150. UNITY_VERTEX_INPUT_INSTANCE_ID
  151. UNITY_VERTEX_OUTPUT_STEREO
  152. };
  153. VertexOutput vert (VertexInput input)
  154. {
  155. VertexOutput output = (VertexOutput)0;
  156. UNITY_SETUP_INSTANCE_ID(input);
  157. UNITY_TRANSFER_INSTANCE_ID(input, output);
  158. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  159. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  160. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  161. float3 viewDirWS = GetCameraPositionWS() - vertexInput.positionWS;
  162. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  163. half fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  164. output.uv0 = input.texcoord; //TRANSFORM_TEX(input.texcoord, _BaseMap);
  165. output.positionWS = vertexInput.positionWS;
  166. // Already normalized from normal transform to WS.
  167. output.normalWS = normalInput.normalWS;
  168. output.viewDirWS = viewDirWS;
  169. #ifdef _NORMALMAP
  170. float sign = input.tangentOS.w * GetOddNegativeScale();
  171. output.tangentWS = float4(normalInput.tangentWS.xyz, sign);
  172. #endif
  173. OUTPUT_LIGHTMAP_UV(input.lightmapUV, unity_LightmapST, output.lightmapUV);
  174. OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
  175. output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
  176. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  177. output.shadowCoord = GetShadowCoord(vertexInput);
  178. #endif
  179. output.positionCS = vertexInput.positionCS;
  180. #if defined(_USEVERTEXCOLORS)
  181. output.color = input.color;
  182. #endif
  183. return output;
  184. }
  185. // Surface function which has full access to all vertex interpolators
  186. inline void InitializeStandardLitSurfaceData(VertexOutput input, out SurfaceData outSurfaceData, out half3 topdownNormal)
  187. {
  188. topdownNormal = 0;
  189. float2 detailUV = input.uv0 * _SplatTiling;
  190. half4 splatControl = 0;
  191. #if defined(_USEVERTEXCOLORS)
  192. splatControl = input.color;
  193. #else
  194. splatControl.rgb = SAMPLE_TEXTURE2D(_SplatMap, sampler_SplatMap, input.uv0.xy).rgb;
  195. #endif
  196. splatControl.a = 1.0h - splatControl.r - splatControl.g - splatControl.b;
  197. #if defined(_TOPDOWNPROJECTION)
  198. float2 uvWS = input.positionWS.xz * _TopDownTiling;
  199. half4 albedoAlpha = SAMPLE_TEXTURE2D(_DetailA0, sampler_DetailA0, uvWS) * splatControl.r;
  200. #else
  201. half4 albedoAlpha = SAMPLE_TEXTURE2D(_DetailA0, sampler_DetailA0, detailUV) * splatControl.r;
  202. #endif
  203. albedoAlpha += SAMPLE_TEXTURE2D(_DetailA1, sampler_DetailA0, detailUV) * splatControl.g;
  204. albedoAlpha += SAMPLE_TEXTURE2D(_DetailA2, sampler_DetailA0, detailUV) * splatControl.b;
  205. albedoAlpha += SAMPLE_TEXTURE2D(_DetailA3, sampler_DetailA0, detailUV) * splatControl.a;
  206. half3 normalTS = 0;
  207. #if defined(_NORMALMAP)
  208. half4 nrm = 0.0h;
  209. #if defined(_TOPDOWNPROJECTION)
  210. topdownNormal = UnpackNormal (SAMPLE_TEXTURE2D(_Normal0, sampler_Normal0, uvWS));
  211. // Without safe normalization of gba normals are off withing the blend range.
  212. splatControl.gba /= dot( max(splatControl.gba, half3(0.0001h, 0.0001h, 0.0001h)), half3(1.0h,1.0h,1.0h));
  213. #else
  214. nrm = SAMPLE_TEXTURE2D(_Normal0, sampler_Normal0, detailUV) * splatControl.r;
  215. #endif
  216. nrm += SAMPLE_TEXTURE2D(_Normal1, sampler_Normal0, detailUV) * splatControl.g;
  217. nrm += SAMPLE_TEXTURE2D(_Normal2, sampler_Normal0, detailUV) * splatControl.b;
  218. nrm += SAMPLE_TEXTURE2D(_Normal3, sampler_Normal0, detailUV) * splatControl.a;
  219. normalTS = UnpackNormal(nrm);
  220. #endif
  221. #if defined(_TOPDOWNPROJECTION) && defined(_NORMALMAP)
  222. float sgn = input.tangentWS.w; // should be either +1 or -1
  223. float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  224. normalTS = normalize(TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, bitangent, input.normalWS.xyz)));
  225. // We use Reoriented Normal Mapping to bring the the top down normal into world space
  226. half3 n1 = /*normalize*/(input.normalWS.xzy);
  227. half3 n2 = topdownNormal.xyz;
  228. n1.z += 1.0h;
  229. n2.xy *= -1.0h;
  230. half3 topDownNormalWS = n1 * dot(n1, n2) / n1.z - n2;
  231. topDownNormalWS = topDownNormalWS.xzy;
  232. // Finally we blend both normals in world space
  233. normalTS = normalize(normalTS * (1.0h - splatControl.r) + topDownNormalWS * splatControl.r);
  234. #endif
  235. outSurfaceData.albedo = albedoAlpha.rgb;
  236. outSurfaceData.smoothness = albedoAlpha.a;
  237. outSurfaceData.normalTS = normalTS;
  238. outSurfaceData.emission = 0;
  239. outSurfaceData.metallic = 0;
  240. outSurfaceData.specular = _Specular.rgb;
  241. outSurfaceData.occlusion = 1.0h - _Occlusion;
  242. outSurfaceData.alpha = 1;
  243. outSurfaceData.clearCoatMask = 0;
  244. outSurfaceData.clearCoatSmoothness = 0;
  245. }
  246. half4 frag (VertexOutput input ) : SV_Target
  247. {
  248. UNITY_SETUP_INSTANCE_ID(input);
  249. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  250. SurfaceData surfaceData;
  251. half3 topdownNormal;
  252. half topnormalstrength;
  253. // Get the surface description
  254. InitializeStandardLitSurfaceData(input, surfaceData, topdownNormal);
  255. // Transfer all to world space
  256. InputData inputData;
  257. inputData.positionWS = input.positionWS;
  258. half3 viewDirWS = SafeNormalize(input.viewDirWS);
  259. #ifdef _NORMALMAP
  260. #if !defined(_TOPDOWNPROJECTION)
  261. float sgn = input.tangentWS.w; // should be either +1 or -1
  262. float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  263. inputData.normalWS = TransformTangentToWorld(surfaceData.normalTS, half3x3(input.tangentWS.xyz, bitangent.xyz, input.normalWS.xyz));
  264. #else
  265. inputData.normalWS = surfaceData.normalTS;
  266. #endif
  267. #else
  268. inputData.normalWS = input.normalWS;
  269. #endif
  270. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  271. inputData.viewDirectionWS = viewDirWS;
  272. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  273. inputData.shadowCoord = input.shadowCoord;
  274. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  275. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  276. #else
  277. inputData.shadowCoord = float4(0, 0, 0, 0);
  278. #endif
  279. inputData.fogCoord = input.fogFactorAndVertexLight.x;
  280. inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
  281. inputData.bakedGI = SAMPLE_GI(input.lightmapUV, input.vertexSH, inputData.normalWS);
  282. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  283. inputData.shadowMask = SAMPLE_SHADOWMASK(input.lightmapUV);
  284. half4 color = UniversalFragmentPBR(
  285. inputData,
  286. surfaceData.albedo,
  287. surfaceData.metallic,
  288. surfaceData.specular,
  289. surfaceData.smoothness,
  290. surfaceData.occlusion,
  291. surfaceData.emission,
  292. surfaceData.alpha);
  293. // Computes fog factor per-vertex
  294. color.rgb = MixFog(color.rgb, input.fogFactorAndVertexLight.x);
  295. return color;
  296. }
  297. ENDHLSL
  298. }
  299. Pass
  300. {
  301. Name "ShadowCaster"
  302. Tags{"LightMode" = "ShadowCaster"}
  303. ZWrite On
  304. ZTest LEqual
  305. ColorMask 0
  306. Cull Back
  307. HLSLPROGRAM
  308. // Required to compile gles 2.0 with standard srp library
  309. #pragma prefer_hlslcc gles
  310. #pragma exclude_renderers d3d11_9x
  311. #pragma target 2.0
  312. //--------------------------------------
  313. // GPU Instancing
  314. #pragma multi_compile_instancing
  315. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  316. #pragma vertex ShadowPassVertex
  317. #pragma fragment ShadowPassFragment
  318. //#define _NORMALMAP 1
  319. #define _SPECULAR_SETUP 1
  320. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  321. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
  322. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl"
  323. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
  324. CBUFFER_START(UnityPerMaterial)
  325. float2 _SplatTiling;
  326. float4 _Specular;
  327. float _Occlusion;
  328. half _TopDownTiling;
  329. CBUFFER_END
  330. struct VertexInput
  331. {
  332. float4 positionOS : POSITION;
  333. float3 normalOS : NORMAL;
  334. UNITY_VERTEX_INPUT_INSTANCE_ID
  335. };
  336. struct VertexOutput
  337. {
  338. float4 positionCS : SV_POSITION;
  339. UNITY_VERTEX_INPUT_INSTANCE_ID
  340. };
  341. float3 _LightDirection;
  342. VertexOutput ShadowPassVertex(VertexInput input)
  343. {
  344. VertexOutput output;
  345. UNITY_SETUP_INSTANCE_ID(input);
  346. UNITY_TRANSFER_INSTANCE_ID(input, output);
  347. float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
  348. float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
  349. // Needs Shadows.hlsl
  350. float4 positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, _LightDirection));
  351. #if UNITY_REVERSED_Z
  352. positionCS.z = min(positionCS.z, positionCS.w * UNITY_NEAR_CLIP_VALUE);
  353. #else
  354. positionCS.z = max(positionCS.z, positionCS.w * UNITY_NEAR_CLIP_VALUE);
  355. #endif
  356. output.positionCS = positionCS;
  357. return output;
  358. }
  359. half4 ShadowPassFragment(VertexOutput input ) : SV_TARGET
  360. {
  361. UNITY_SETUP_INSTANCE_ID(input);
  362. return 0;
  363. }
  364. ENDHLSL
  365. }
  366. Pass
  367. {
  368. Name "DepthOnly"
  369. Tags{"LightMode" = "DepthOnly"}
  370. ZWrite On
  371. ColorMask 0
  372. Cull Back
  373. HLSLPROGRAM
  374. // Required to compile gles 2.0 with standard srp library
  375. #pragma prefer_hlslcc gles
  376. #pragma exclude_renderers d3d11_9x
  377. #pragma target 2.0
  378. //--------------------------------------
  379. // GPU Instancing
  380. #pragma multi_compile_instancing
  381. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  382. #pragma vertex vert
  383. #pragma fragment frag
  384. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  385. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  386. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
  387. CBUFFER_START(UnityPerMaterial)
  388. float2 _SplatTiling;
  389. float4 _Specular;
  390. float _Occlusion;
  391. half _TopDownTiling;
  392. CBUFFER_END
  393. struct VertexInput
  394. {
  395. float4 positionOS : POSITION;
  396. UNITY_VERTEX_INPUT_INSTANCE_ID
  397. };
  398. struct VertexOutput
  399. {
  400. float4 positionCS : SV_POSITION;
  401. UNITY_VERTEX_INPUT_INSTANCE_ID
  402. UNITY_VERTEX_OUTPUT_STEREO
  403. };
  404. VertexOutput vert(VertexInput input)
  405. {
  406. VertexOutput output = (VertexOutput)0;
  407. UNITY_SETUP_INSTANCE_ID(input);
  408. UNITY_TRANSFER_INSTANCE_ID(input, output);
  409. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  410. output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
  411. return output;
  412. }
  413. half4 frag(VertexOutput input ) : SV_TARGET
  414. {
  415. UNITY_SETUP_INSTANCE_ID(input);
  416. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  417. return 0;
  418. }
  419. ENDHLSL
  420. }
  421. // Depth Normal ---------------------------------------------
  422. // This pass is used when drawing to a _CameraNormalsTexture texture
  423. Pass
  424. {
  425. Name "DepthNormals"
  426. Tags{"LightMode" = "DepthNormals"}
  427. ZWrite On
  428. Cull Back
  429. HLSLPROGRAM
  430. // Required to compile gles 2.0 with standard srp library
  431. #pragma prefer_hlslcc gles
  432. #pragma exclude_renderers d3d11_9x
  433. #pragma target 2.0
  434. #pragma vertex DepthNormalsVertex
  435. #pragma fragment DepthNormalsFragment
  436. // -------------------------------------
  437. // Material Keywords
  438. #pragma shader_feature_local _NORMALMAP
  439. //--------------------------------------
  440. // GPU Instancing
  441. #pragma multi_compile_instancing
  442. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  443. //#include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
  444. //#include "Packages/com.unity.render-pipelines.universal/Shaders/DepthNormalsPass.hlsl"
  445. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  446. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  447. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
  448. CBUFFER_START(UnityPerMaterial)
  449. float2 _SplatTiling;
  450. float4 _Specular;
  451. float _Occlusion;
  452. half _TopDownTiling;
  453. CBUFFER_END
  454. struct VertexInput
  455. {
  456. float4 positionOS : POSITION;
  457. float3 normal : NORMAL;
  458. UNITY_VERTEX_INPUT_INSTANCE_ID
  459. };
  460. struct VertexOutput
  461. {
  462. float4 positionCS : SV_POSITION;
  463. float3 normalWS : TEXCOORD1;
  464. UNITY_VERTEX_INPUT_INSTANCE_ID
  465. UNITY_VERTEX_OUTPUT_STEREO
  466. };
  467. VertexOutput DepthNormalsVertex(VertexInput input)
  468. {
  469. VertexOutput output = (VertexOutput)0;
  470. UNITY_SETUP_INSTANCE_ID(input);
  471. UNITY_TRANSFER_INSTANCE_ID(input, output);
  472. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  473. output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
  474. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normal, float4(1,1,1,1));
  475. output.normalWS = NormalizeNormalPerVertex(normalInput.normalWS);
  476. return output;
  477. }
  478. half4 DepthNormalsFragment(VertexOutput input ) : SV_TARGET
  479. {
  480. UNITY_SETUP_INSTANCE_ID(input);
  481. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  482. return float4(PackNormalOctRectEncode(TransformWorldToViewDir(input.normalWS, true)), 0.0, 0.0);
  483. }
  484. ENDHLSL
  485. }
  486. // Meta -----------------------------------------------------
  487. // This pass it not used during regular rendering, only for lightmap baking.
  488. Pass
  489. {
  490. Name "Meta"
  491. Tags{"LightMode" = "Meta"}
  492. Cull Off
  493. HLSLPROGRAM
  494. // Tell Polybrush that this shader supports 4 texture channels
  495. #define Z_TEXTURE_CHANNELS 4
  496. #define Z_MESH_ATTRIBUTES COLOR
  497. // Required to compile gles 2.0 with standard srp library
  498. #pragma prefer_hlslcc gles
  499. #pragma exclude_renderers d3d11_9x
  500. #pragma target 2.0
  501. #pragma vertex vert
  502. #pragma fragment frag
  503. //#define _NORMALMAP 1
  504. #define _SPECULAR_SETUP 1
  505. #pragma shader_feature_local_fragment _TOPDOWNPROJECTION
  506. #pragma shader_feature_local_fragment _USEVERTEXCOLORS
  507. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  508. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/MetaInput.hlsl"
  509. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl"
  510. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
  511. CBUFFER_START(UnityPerMaterial)
  512. float2 _SplatTiling;
  513. float4 _Specular;
  514. float _Occlusion;
  515. half _TopDownTiling;
  516. CBUFFER_END
  517. TEXTURE2D(_DetailA0); SAMPLER(sampler_DetailA0); float4 _DetailA0_TexelSize;
  518. TEXTURE2D(_DetailA1); float4 _DetailA1_TexelSize;
  519. TEXTURE2D(_DetailA2); float4 _DetailA2_TexelSize;
  520. TEXTURE2D(_DetailA3); float4 _DetailA3_TexelSize;
  521. TEXTURE2D(_SplatMap); SAMPLER(sampler_SplatMap); float4 _SplatMap_TexelSize;
  522. struct VertexInput
  523. {
  524. float4 positionOS : POSITION;
  525. float3 normalOS : NORMAL;
  526. float4 tangentOS : TANGENT;
  527. float2 texcoord : TEXCOORD0;
  528. float2 lightmapUV : TEXCOORD1;
  529. #if defined(_USEVERTEXCOLORS)
  530. half4 color : COLOR;
  531. #endif
  532. UNITY_VERTEX_INPUT_INSTANCE_ID
  533. };
  534. struct VertexOutput
  535. {
  536. float4 positionCS : SV_POSITION;
  537. float2 uv0 : TEXCOORD0;
  538. float2 uv1 : TEXCOORD1;
  539. float3 positionWS : TEXCOORD2;
  540. #if defined(_USEVERTEXCOLORS)
  541. half4 color : COLOR;
  542. #endif
  543. UNITY_VERTEX_INPUT_INSTANCE_ID
  544. UNITY_VERTEX_OUTPUT_STEREO
  545. };
  546. VertexOutput vert(VertexInput input)
  547. {
  548. VertexOutput output = (VertexOutput)0;
  549. UNITY_SETUP_INSTANCE_ID(input);
  550. UNITY_TRANSFER_INSTANCE_ID(input, output);
  551. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  552. output.positionWS = mul(UNITY_MATRIX_M, input.positionOS).xyz;
  553. output.uv0 = input.texcoord;
  554. output.uv1 = input.lightmapUV;
  555. output.positionCS = MetaVertexPosition(input.positionOS, input.lightmapUV, input.lightmapUV, unity_LightmapST, unity_DynamicLightmapST);
  556. #if defined(_USEVERTEXCOLORS)
  557. output.color = input.color;
  558. #endif
  559. return output;
  560. }
  561. half4 frag(VertexOutput input ) : SV_TARGET
  562. {
  563. UNITY_SETUP_INSTANCE_ID(input);
  564. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  565. float2 detailUV = input.uv0 * _SplatTiling;
  566. half4 splatControl = 0;
  567. #if defined(_USEVERTEXCOLORS)
  568. splatControl = input.color;
  569. splatControl.a = 1.0h - splatControl.r - splatControl.g - splatControl.b;
  570. #else
  571. splatControl.rgb = SAMPLE_TEXTURE2D(_SplatMap, sampler_SplatMap, input.uv0).rgb;
  572. splatControl.a = 1.0h - splatControl.r - splatControl.g - splatControl.b;
  573. #endif
  574. #if defined(_TOPDOWNPROJECTION)
  575. float2 uvWS = input.positionWS.xz * _TopDownTiling;
  576. half4 albedoAlpha = SAMPLE_TEXTURE2D(_DetailA0, sampler_DetailA0, uvWS) * splatControl.r;
  577. #else
  578. half4 albedoAlpha = SAMPLE_TEXTURE2D(_DetailA0, sampler_DetailA0, detailUV) * splatControl.r;
  579. #endif
  580. albedoAlpha += SAMPLE_TEXTURE2D(_DetailA1, sampler_DetailA0, detailUV) * splatControl.g;
  581. albedoAlpha += SAMPLE_TEXTURE2D(_DetailA2, sampler_DetailA0, detailUV) * splatControl.b;
  582. albedoAlpha += SAMPLE_TEXTURE2D(_DetailA3, sampler_DetailA0, detailUV) * splatControl.a;
  583. #if _AlphaClip
  584. // clip(Alpha - AlphaClipThreshold);
  585. #endif
  586. MetaInput metaInput = (MetaInput)0;
  587. metaInput.Albedo = albedoAlpha.rgb;
  588. metaInput.Emission = 0;
  589. return MetaFragment(metaInput);
  590. }
  591. ENDHLSL
  592. }
  593. }
  594. FallBack "Hidden/InternalErrorShader"
  595. }