Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SpeedTree7CommonPasses.hlsl 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #ifndef UNIVERSAL_SPEEDTREE7COMMON_PASSES_INCLUDED
  2. #define UNIVERSAL_SPEEDTREE7COMMON_PASSES_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  4. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/UnityGBuffer.hlsl"
  5. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl"
  6. #include "SpeedTreeUtility.hlsl"
  7. #if defined(LOD_FADE_CROSSFADE)
  8. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/LODCrossFade.hlsl"
  9. #endif
  10. struct SpeedTreeVertexInput
  11. {
  12. float4 vertex : POSITION;
  13. float3 normal : NORMAL;
  14. float4 tangent : TANGENT;
  15. float4 texcoord : TEXCOORD0;
  16. float4 texcoord1 : TEXCOORD1;
  17. float4 texcoord2 : TEXCOORD2;
  18. float2 texcoord3 : TEXCOORD3;
  19. half4 color : COLOR;
  20. UNITY_VERTEX_INPUT_INSTANCE_ID
  21. };
  22. struct SpeedTreeVertexOutput
  23. {
  24. #ifdef VERTEX_COLOR
  25. half4 color : COLOR;
  26. #endif
  27. half3 uvHueVariation : TEXCOORD0;
  28. #ifdef GEOM_TYPE_BRANCH_DETAIL
  29. half3 detail : TEXCOORD1;
  30. #endif
  31. half4 fogFactorAndVertexLight : TEXCOORD2; // x: fogFactor, yzw: vertex light
  32. #ifdef EFFECT_BUMP
  33. half4 normalWS : TEXCOORD3; // xyz: normal, w: viewDir.x
  34. half4 tangentWS : TEXCOORD4; // xyz: tangent, w: viewDir.y
  35. half4 bitangentWS : TEXCOORD5; // xyz: bitangent, w: viewDir.z
  36. #else
  37. half3 normalWS : TEXCOORD3;
  38. half3 viewDirWS : TEXCOORD4;
  39. #endif
  40. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  41. float4 shadowCoord : TEXCOORD6;
  42. #endif
  43. float3 positionWS : TEXCOORD7;
  44. DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH, 8);
  45. float4 clipPos : SV_POSITION;
  46. UNITY_VERTEX_INPUT_INSTANCE_ID
  47. UNITY_VERTEX_OUTPUT_STEREO
  48. };
  49. struct SpeedTreeVertexDepthOutput
  50. {
  51. half3 uvHueVariation : TEXCOORD0;
  52. half3 viewDirWS : TEXCOORD1;
  53. float4 clipPos : SV_POSITION;
  54. UNITY_VERTEX_INPUT_INSTANCE_ID
  55. UNITY_VERTEX_OUTPUT_STEREO
  56. };
  57. struct SpeedTreeVertexDepthNormalOutput
  58. {
  59. half3 uvHueVariation : TEXCOORD0;
  60. float4 clipPos : SV_POSITION;
  61. #ifdef GEOM_TYPE_BRANCH_DETAIL
  62. half3 detail : TEXCOORD1;
  63. #endif
  64. #ifdef EFFECT_BUMP
  65. half4 normalWS : TEXCOORD2; // xyz: normal, w: viewDir.x
  66. half4 tangentWS : TEXCOORD3; // xyz: tangent, w: viewDir.y
  67. half4 bitangentWS : TEXCOORD4; // xyz: bitangent, w: viewDir.z
  68. #else
  69. half3 normalWS : TEXCOORD2;
  70. half3 viewDirWS : TEXCOORD3;
  71. #endif
  72. UNITY_VERTEX_INPUT_INSTANCE_ID
  73. UNITY_VERTEX_OUTPUT_STEREO
  74. };
  75. void InitializeInputData(SpeedTreeVertexOutput input, half3 normalTS, out InputData inputData)
  76. {
  77. inputData = (InputData)0;
  78. inputData.positionWS = input.positionWS.xyz;
  79. inputData.positionCS = input.clipPos;
  80. #ifdef EFFECT_BUMP
  81. inputData.normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, input.bitangentWS.xyz, input.normalWS.xyz));
  82. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  83. inputData.viewDirectionWS = half3(input.normalWS.w, input.tangentWS.w, input.bitangentWS.w);
  84. #else
  85. inputData.normalWS = NormalizeNormalPerPixel(input.normalWS);
  86. inputData.viewDirectionWS = input.viewDirWS;
  87. #endif
  88. inputData.viewDirectionWS = SafeNormalize(inputData.viewDirectionWS);
  89. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  90. inputData.shadowCoord = input.shadowCoord;
  91. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  92. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  93. #else
  94. inputData.shadowCoord = float4(0, 0, 0, 0);
  95. #endif
  96. inputData.fogCoord = InitializeInputDataFog(float4(input.positionWS, 1.0), input.fogFactorAndVertexLight.x);
  97. inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
  98. // Billboards cannot use lightmaps.
  99. #if !defined(LIGHTMAP_ON) && (defined(PROBE_VOLUMES_L1) || defined(PROBE_VOLUMES_L2))
  100. inputData.bakedGI = SAMPLE_GI(input.vertexSH,
  101. GetAbsolutePositionWS(inputData.positionWS),
  102. inputData.normalWS,
  103. inputData.viewDirectionWS,
  104. inputData.positionCS.xy,
  105. input.probeOcclusion,
  106. inputData.shadowMask);
  107. #else
  108. inputData.bakedGI = SAMPLE_GI(NOT_USED, input.vertexSH, inputData.normalWS);
  109. #endif
  110. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.clipPos);
  111. inputData.shadowMask = half4(1, 1, 1, 1); // No GI currently.
  112. #if defined(DEBUG_DISPLAY) && !defined(LIGHTMAP_ON)
  113. inputData.vertexSH = input.vertexSH;
  114. #endif
  115. #if defined(_NORMALMAP)
  116. inputData.tangentToWorld = half3x3(input.tangentWS.xyz, input.bitangentWS.xyz, input.normalWS.xyz);
  117. #endif
  118. }
  119. #ifdef GBUFFER
  120. FragmentOutput SpeedTree7Frag(SpeedTreeVertexOutput input)
  121. #else
  122. half4 SpeedTree7Frag(SpeedTreeVertexOutput input) : SV_Target
  123. #endif
  124. {
  125. UNITY_SETUP_INSTANCE_ID(input);
  126. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  127. half2 uv = input.uvHueVariation.xy;
  128. half4 diffuse = SampleAlbedoAlpha(uv, TEXTURE2D_ARGS(_MainTex, sampler_MainTex));
  129. diffuse.a *= _Color.a;
  130. #ifdef SPEEDTREE_ALPHATEST
  131. diffuse.a = AlphaDiscard(diffuse.a, _Cutoff);
  132. #endif
  133. #ifdef LOD_FADE_CROSSFADE
  134. LODFadeCrossFade(input.clipPos);
  135. #endif
  136. half3 diffuseColor = diffuse.rgb;
  137. #ifdef GEOM_TYPE_BRANCH_DETAIL
  138. half4 detailColor = tex2D(_DetailTex, input.detail.xy);
  139. diffuseColor.rgb = lerp(diffuseColor.rgb, detailColor.rgb, input.detail.z < 2.0f ? saturate(input.detail.z) : detailColor.a);
  140. #endif
  141. #ifdef EFFECT_HUE_VARIATION
  142. half3 shiftedColor = lerp(diffuseColor.rgb, _HueVariation.rgb, input.uvHueVariation.z);
  143. half maxBase = max(diffuseColor.r, max(diffuseColor.g, diffuseColor.b));
  144. half newMaxBase = max(shiftedColor.r, max(shiftedColor.g, shiftedColor.b));
  145. maxBase /= newMaxBase;
  146. maxBase = maxBase * 0.5f + 0.5f;
  147. // preserve vibrance
  148. shiftedColor.rgb *= maxBase;
  149. diffuseColor.rgb = saturate(shiftedColor);
  150. #endif
  151. #ifdef EFFECT_BUMP
  152. half3 normalTs = SampleNormal(uv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap));
  153. #ifdef GEOM_TYPE_BRANCH_DETAIL
  154. half3 detailNormal = SampleNormal(input.detail.xy, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap));
  155. normalTs = lerp(normalTs, detailNormal, input.detail.z < 2.0f ? saturate(input.detail.z) : detailColor.a);
  156. #endif
  157. #else
  158. half3 normalTs = half3(0, 0, 1);
  159. #endif
  160. InputData inputData;
  161. InitializeInputData(input, normalTs, inputData);
  162. SETUP_DEBUG_TEXTURE_DATA(inputData, input.uvHueVariation.xy);
  163. #ifdef VERTEX_COLOR
  164. diffuseColor.rgb *= input.color.rgb;
  165. #else
  166. diffuseColor.rgb *= _Color.rgb;
  167. #endif
  168. SurfaceData surfaceData;
  169. surfaceData.albedo = diffuseColor.rgb;
  170. surfaceData.alpha = diffuse.a;
  171. surfaceData.emission = half3(0, 0, 0);
  172. surfaceData.metallic = 0;
  173. surfaceData.occlusion = 1;
  174. surfaceData.smoothness = 0;
  175. surfaceData.specular = half3(0, 0, 0);
  176. surfaceData.clearCoatMask = 0;
  177. surfaceData.clearCoatSmoothness = 1;
  178. surfaceData.normalTS = normalTs;
  179. #ifdef GBUFFER
  180. half4 color = half4(inputData.bakedGI * diffuseColor.rgb, diffuse.a);
  181. surfaceData.occlusion = 1.0;
  182. return SurfaceDataToGbuffer(surfaceData, inputData, color.rgb, kLightingSimpleLit);
  183. #else
  184. half4 color = UniversalFragmentBlinnPhong(inputData, surfaceData);
  185. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  186. color.a = OutputAlpha(color.a, _Surface);
  187. return color;
  188. #endif
  189. }
  190. half4 SpeedTree7FragDepth(SpeedTreeVertexDepthOutput input) : SV_Target
  191. {
  192. UNITY_SETUP_INSTANCE_ID(input);
  193. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  194. half2 uv = input.uvHueVariation.xy;
  195. half4 diffuse = SampleAlbedoAlpha(uv, TEXTURE2D_ARGS(_MainTex, sampler_MainTex));
  196. diffuse.a *= _Color.a;
  197. #ifdef SPEEDTREE_ALPHATEST
  198. AlphaDiscard(diffuse.a, _Cutoff);
  199. #endif
  200. #ifdef LOD_FADE_CROSSFADE
  201. LODFadeCrossFade(input.clipPos);
  202. #endif
  203. #if defined(SCENESELECTIONPASS)
  204. // We use depth prepass for scene selection in the editor, this code allow to output the outline correctly
  205. return half4(_ObjectId, _PassValue, 1.0, 1.0);
  206. #else
  207. return half4(input.clipPos.z, 0, 0, 0);
  208. #endif
  209. }
  210. half4 SpeedTree7FragDepthNormal(SpeedTreeVertexDepthNormalOutput input) : SV_Target
  211. {
  212. UNITY_SETUP_INSTANCE_ID(input);
  213. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  214. half2 uv = input.uvHueVariation.xy;
  215. half4 diffuse = SampleAlbedoAlpha(uv, TEXTURE2D_ARGS(_MainTex, sampler_MainTex));
  216. diffuse.a *= _Color.a;
  217. #ifdef SPEEDTREE_ALPHATEST
  218. AlphaDiscard(diffuse.a, _Cutoff);
  219. #endif
  220. #ifdef LOD_FADE_CROSSFADE
  221. LODFadeCrossFade(input.clipPos);
  222. #endif
  223. #if defined(EFFECT_BUMP)
  224. half3 normalTS = SampleNormal(uv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap));
  225. #ifdef GEOM_TYPE_BRANCH_DETAIL
  226. half4 detailColor = tex2D(_DetailTex, input.detail.xy);
  227. half3 detailNormal = SampleNormal(input.detail.xy, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap));
  228. normalTS = lerp(normalTS, detailNormal, input.detail.z < 2.0f ? saturate(input.detail.z) : detailColor.a);
  229. #endif
  230. half3 normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, input.bitangentWS.xyz, input.normalWS.xyz)).xyz;
  231. #else
  232. half3 normalWS = input.normalWS.xyz;
  233. #endif
  234. return half4(NormalizeNormalPerPixel(normalWS), 0.0);
  235. }
  236. #endif