Advertisement
remo9211

L1_1 BIRP Full

May 20th, 2024
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.13 KB | None | 0 0
  1. Shader "L01_URP/Grass"{
  2. Properties{
  3. [Header(Shading)]
  4. _TopColor("Top Color", Color) = (1,1,1,1)
  5. _BottomColor("Bottom Color", Color) = (1,1,1,1)
  6. _TranslucentGain("Translucent Gain", Range(0,1)) = 0.5
  7.  
  8. _BendRotationRandom("Bend Rotation Random", Range(0, 1)) = 0.2
  9.  
  10. _BladeWidth("Blade Width", Float) = 0.05
  11. _BladeWidthRandom("Blade Width Random", Float) = 0.02
  12. _BladeHeight("Blade Height", Float) = 0.5
  13. _BladeHeightRandom("Blade Height Random", Float) = 0.3
  14.  
  15. _TessellationUniform("Tessellation Uniform", Range(1, 64)) = 1
  16.  
  17. _WindDistortionMap("Wind Distortion Map", 2D) = "white" {}
  18. _WindFrequency("Wind Frequency", Vector) = (0.05, 0.05, 0, 0)
  19.  
  20. _WindStrength("Wind Strength", Float) = 1
  21.  
  22. _BladeForward("Blade Forward Amount", Float) = 0.38
  23. _BladeCurve("Blade Curvature Amount", Range(1, 4)) = 2
  24. }
  25.  
  26. CGINCLUDE
  27. struct vertexInput
  28. {
  29. float4 vertex : POSITION;
  30. float3 normal : NORMAL;
  31. float4 tangent : TANGENT;
  32. };
  33.  
  34. struct vertexOutput
  35. {
  36. float4 vertex : SV_POSITION;
  37. float3 normal : NORMAL;
  38. float4 tangent : TANGENT;
  39. };
  40.  
  41. struct TessellationFactors
  42. {
  43. float edge[3] : SV_TessFactor;
  44. float inside : SV_InsideTessFactor;
  45. };
  46.  
  47. vertexInput vert(vertexInput v)
  48. {
  49. return v;
  50. }
  51.  
  52. vertexOutput tessVert(vertexInput v)
  53. {
  54. vertexOutput o;
  55. // Note that the vertex is NOT transformed to clip
  56. // space here; this is done in the grass geometry shader.
  57. o.vertex = v.vertex;
  58. o.normal = v.normal;
  59. o.tangent = v.tangent;
  60. return o;
  61. }
  62.  
  63. float _TessellationUniform;
  64.  
  65. TessellationFactors patchConstantFunction (InputPatch<vertexInput, 3> patch)
  66. {
  67. TessellationFactors f;
  68. f.edge[0] = _TessellationUniform;
  69. f.edge[1] = _TessellationUniform;
  70. f.edge[2] = _TessellationUniform;
  71. f.inside = _TessellationUniform;
  72. return f;
  73. }
  74.  
  75. [UNITY_domain("tri")]
  76. [UNITY_outputcontrolpoints(3)]
  77. [UNITY_outputtopology("triangle_cw")]
  78. [UNITY_partitioning("integer")]
  79. [UNITY_patchconstantfunc("patchConstantFunction")]
  80. vertexInput hull (InputPatch<vertexInput, 3> patch, uint id : SV_OutputControlPointID)
  81. {
  82. return patch[id];
  83. }
  84.  
  85. [UNITY_domain("tri")]
  86. vertexOutput domain(TessellationFactors factors, OutputPatch<vertexInput, 3> patch, float3 barycentricCoordinates : SV_DomainLocation)
  87. {
  88. vertexInput v;
  89. #define MY_DOMAIN_PROGRAM_INTERPOLATE(fieldName) v.fieldName = \
  90. patch[0].fieldName * barycentricCoordinates.x + \
  91. patch[1].fieldName * barycentricCoordinates.y + \
  92. patch[2].fieldName * barycentricCoordinates.z;
  93. MY_DOMAIN_PROGRAM_INTERPOLATE(vertex)
  94. MY_DOMAIN_PROGRAM_INTERPOLATE(normal)
  95. MY_DOMAIN_PROGRAM_INTERPOLATE(tangent)
  96. return tessVert(v);
  97. }
  98. ENDCG
  99.  
  100. CGINCLUDE
  101. #include "UnityCG.cginc"
  102. #include "Autolight.cginc"
  103.  
  104. #define BLADE_SEGMENTS 3
  105.  
  106. float _BendRotationRandom;
  107.  
  108. float _BladeHeight;
  109. float _BladeHeightRandom;
  110. float _BladeWidth;
  111. float _BladeWidthRandom;
  112.  
  113. sampler2D _WindDistortionMap;
  114. float4 _WindDistortionMap_ST;
  115.  
  116. float2 _WindFrequency;
  117. float _WindStrength;
  118.  
  119. float _BladeForward;
  120. float _BladeCurve;
  121.  
  122. struct geometryOutput{
  123. float4 pos : SV_POSITION;
  124. float2 uv : TEXCOORD0;
  125. unityShadowCoord4 _ShadowCoord : TEXCOORD1;
  126. float3 normal : NORMAL;
  127. };
  128. float rand(float3 co){
  129. return frac(sin(dot(co.xyz, float3(12.9898, 78.233, 53.539))) * 43758.5453);
  130. }
  131. float3x3 AngleAxis3x3(float angle, float3 axis){
  132. float c, s;
  133. sincos(angle, s, c);
  134.  
  135. float t = 1 - c;
  136. float x = axis.x;
  137. float y = axis.y;
  138. float z = axis.z;
  139.  
  140. return float3x3(
  141. t * x * x + c, t * x * y - s * z, t * x * z + s * y,
  142. t * x * y + s * z, t * y * y + c, t * y * z - s * x,
  143. t * x * z - s * y, t * y * z + s * x, t * z * z + c
  144. );
  145. }
  146. geometryOutput VertexOutput(float3 pos, float2 uv, float3 normal){
  147. geometryOutput o;
  148. o.pos = UnityObjectToClipPos(pos);
  149.  
  150. #if UNITY_PASS_SHADOWCASTER
  151. o.pos = UnityApplyLinearShadowBias(o.pos);
  152. #endif
  153.  
  154. o.uv = uv;
  155. o._ShadowCoord = ComputeScreenPos(o.pos);
  156. o.normal = UnityObjectToWorldNormal(normal);
  157. return o;
  158. }
  159.  
  160. geometryOutput GenerateGrassVertex(float3 vertexPosition, float width, float height, float forward, float2 uv, float3x3 transformMatrix){
  161. float3 tangentPoint = float3(width, forward, height);
  162.  
  163. float3 localPosition = vertexPosition + mul(transformMatrix, tangentPoint);
  164.  
  165. float3 tangentNormal = normalize(float3(0, -1, forward));
  166. float3 localNormal = mul(transformMatrix, tangentNormal);
  167.  
  168.  
  169. return VertexOutput(localPosition, uv, localNormal);
  170. }
  171.  
  172. [maxvertexcount(BLADE_SEGMENTS * 2 + 1)]
  173. void geo(triangle vertexOutput IN[3], inout TriangleStream<geometryOutput> triStream){
  174. float3 vNormal = IN[0].normal;
  175. float4 vTangent = IN[0].tangent;
  176. float3 vBinormal = cross(vNormal, vTangent) * vTangent.w;
  177.  
  178. float3x3 tangentToLocal = float3x3(
  179. vTangent.x, vBinormal.x, vNormal.x,
  180. vTangent.y, vBinormal.y, vNormal.y,
  181. vTangent.z, vBinormal.z, vNormal.z
  182. );
  183. float3 pos = IN[0].vertex;
  184.  
  185. float3x3 facingRotationMatrix = AngleAxis3x3(rand(pos) * UNITY_TWO_PI, float3(0, 0, 1));
  186. float3x3 bendRotationMatrix = AngleAxis3x3(rand(pos.zzx) * _BendRotationRandom * UNITY_PI * 0.5, float3(-1, 0, 0));
  187.  
  188. float2 uv = pos.xz * _WindDistortionMap_ST.xy + _WindDistortionMap_ST.zw + _WindFrequency * _Time.y;
  189.  
  190. float2 windSample = (tex2Dlod(_WindDistortionMap, float4(uv, 0, 0)).xy * 2 - 1) * _WindStrength;
  191.  
  192. float3 wind = normalize(float3(windSample.x, windSample.y, 0));
  193.  
  194. float3x3 windRotation = AngleAxis3x3(UNITY_PI * windSample, wind);
  195.  
  196. float3x3 transformationMatrix = mul(mul(mul(tangentToLocal, windRotation), facingRotationMatrix), bendRotationMatrix);
  197.  
  198. float height = (rand(pos.zyx) * 2 - 1) * _BladeHeightRandom + _BladeHeight;
  199. float width = (rand(pos.xzy) * 2 - 1) * _BladeWidthRandom + _BladeWidth;
  200.  
  201. float forward = rand(pos.yyz) * _BladeForward;
  202.  
  203. float3x3 transformationMatrixFacing = mul(tangentToLocal, facingRotationMatrix);
  204.  
  205. for (int i = 0; i < BLADE_SEGMENTS; i++){
  206. float t = i / (float)BLADE_SEGMENTS;
  207. float segmentHeight = height * t;
  208. float segmentWidth = width * (1 - t);
  209. float segmentForward = pow(t, _BladeCurve) * forward;
  210. float3x3 transformMatrix = i == 0 ? transformationMatrixFacing : transformationMatrix;
  211. triStream.Append(GenerateGrassVertex(pos, segmentWidth, segmentHeight, segmentForward, float2(0, t), transformMatrix));
  212. triStream.Append(GenerateGrassVertex(pos, -segmentWidth, segmentHeight, segmentForward, float2(1, t), transformMatrix));
  213. }
  214.  
  215. triStream.Append(GenerateGrassVertex(pos, 0, height, forward, float2(0.5, 1), transformationMatrix));
  216. }
  217. ENDCG
  218. SubShader{
  219. Cull Off
  220. Pass{
  221. Tags{
  222. "RenderType" = "Opaque"
  223. "LightMode" = "ForwardBase"
  224. }
  225.  
  226. CGPROGRAM
  227. #pragma vertex vert
  228. #pragma fragment frag
  229. #pragma hull hull
  230. #pragma domain domain
  231. #pragma geometry geo
  232. #pragma target 4.6
  233. #pragma multi_compile_fwdbase
  234.  
  235. #include "Lighting.cginc"
  236.  
  237. float4 _TopColor;
  238. float4 _BottomColor;
  239. float _TranslucentGain;
  240.  
  241.  
  242. float4 frag (geometryOutput IN, fixed facing : VFACE) : SV_Target{
  243. float3 normal = facing > 0 ? IN.normal : -IN.normal;
  244.  
  245.  
  246. float shadow = SHADOW_ATTENUATION(IN);
  247. float NdotL = saturate(saturate(dot(normal, _WorldSpaceLightPos0)) + _TranslucentGain) * shadow;
  248.  
  249. float3 ambient = ShadeSH9(float4(normal, 1));
  250. float4 lightIntensity = NdotL * _LightColor0 + float4(ambient, 1);
  251. float4 col = lerp(_BottomColor, _TopColor * lightIntensity, IN.uv.y);
  252.  
  253. return col;
  254.  
  255. // return float4(normal * 0.5 + 0.5, 1);
  256. // return lerp(_BottomColor, _TopColor, IN.uv.y) * SHADOW_ATTENUATION(IN);
  257. // return lerp(_BottomColor, _TopColor, IN.uv.y);
  258. // return float4(1, 1, 1, 1);
  259. }
  260. ENDCG
  261. }
  262.  
  263. Pass{
  264. Tags{
  265. "LightMode" = "ShadowCaster"
  266. }
  267. CGPROGRAM
  268. #pragma vertex vert
  269. #pragma geometry geo
  270. #pragma fragment frag
  271. #pragma hull hull
  272. #pragma domain domain
  273. #pragma target 4.6
  274. #pragma multi_compile_shadowcaster
  275.  
  276. float4 frag(geometryOutput IN) : SV_Target{
  277. SHADOW_CASTER_FRAGMENT(IN)
  278. }
  279. ENDCG
  280. }
  281. }
  282. }
Tags: grass BIRP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement