Advertisement
senis_kenis

Untitled

Apr 12th, 2023
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. #ifndef FUR_SHELL_LIT_HLSL
  2. #define FUR_SHELL_LIT_HLSL
  3.  
  4. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  5. #include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
  6. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl"
  7. #include "./Param.hlsl"
  8. #include "../Common/Common.hlsl"
  9.  
  10. struct Attributes
  11. {
  12. float4 positionOS : POSITION;
  13. float3 normalOS : NORMAL;
  14. float4 tangentOS : TANGENT;
  15. float2 texcoord : TEXCOORD0;
  16. float2 lightmapUV : TEXCOORD1;
  17. UNITY_VERTEX_INPUT_INSTANCE_ID
  18.  
  19. };
  20.  
  21. struct Varyings
  22. {
  23. float4 positionCS : SV_POSITION;
  24. float3 positionWS : TEXCOORD0;
  25. float3 normalWS : TEXCOORD1;
  26. float3 tangentWS : TEXCOORD2;
  27. float2 uv : TEXCOORD4;
  28. float4 lightmapUVOrVertexSH : TEXCOORD5;
  29. // DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH, 5);
  30. half4 fogFactorAndVertexLight : TEXCOORD6; // x: fogFactor, yzw: vertex light
  31. float layer : TEXCOORD7;
  32.  
  33. UNITY_VERTEX_INPUT_INSTANCE_ID
  34. UNITY_VERTEX_OUTPUT_STEREO
  35. };
  36.  
  37. Attributes vert(Attributes input)
  38. {
  39. return input;
  40. }
  41.  
  42. void AppendShellVertex(inout TriangleStream<Varyings> stream, Attributes input, int index)
  43. {
  44. Varyings output = (Varyings)0;
  45.  
  46. UNITY_SETUP_INSTANCE_ID(input);
  47. ZERO_INITIALIZE(Varyings, output); //Insert
  48. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  49.  
  50. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  51. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  52.  
  53. float moveFactor = pow(abs((float)index / _ShellAmount), _BaseMove.w);
  54. float3 posOS = input.positionOS.xyz;
  55. float3 windAngle = _Time.w * _WindFreq.xyz;
  56. float3 windMove = moveFactor * _WindMove.xyz * sin(windAngle + posOS * _WindMove.w);
  57. float3 move = moveFactor * _BaseMove.xyz;
  58. float3 shellDir = SafeNormalize(normalInput.normalWS + move + windMove);
  59. float3 viewDirWS = GetCameraPositionWS() - vertexInput.positionWS;
  60.  
  61. output.positionWS = vertexInput.positionWS + shellDir * (_ShellStep * index);
  62. output.positionCS = TransformWorldToHClip(output.positionWS);
  63. output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
  64. output.normalWS = normalInput.normalWS;
  65. output.tangentWS = normalInput.tangentWS;
  66. output.layer = (float)index / _ShellAmount;
  67.  
  68. float3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  69. float fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  70. output.fogFactorAndVertexLight = float4(fogFactor, vertexLight);
  71.  
  72. OUTPUT_LIGHTMAP_UV( input.texcoord, unity_LightmapST, output.lightmapUVOrVertexSH.xy );
  73. OUTPUT_SH( normalInput.normalWS.xyz, output.lightmapUVOrVertexSH.xyz );
  74.  
  75.  
  76.  
  77. //OUTPUT_LIGHTMAP_UV(input.lightmapUV, unity_LightmapST, output.lightmapUV);
  78. //OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
  79.  
  80. stream.Append(output);
  81. }
  82.  
  83. [maxvertexcount(24)]
  84. void geom(triangle Attributes input[3], inout TriangleStream<Varyings> stream)
  85. {
  86. [loop] for (float i = 0; i < _ShellAmount; ++i)
  87. {
  88. [unroll] for (float j = 0; j < 3; ++j)
  89. {
  90. AppendShellVertex(stream, input[j], i);
  91. }
  92. stream.RestartStrip();
  93. }
  94. }
  95.  
  96. inline float3 TransformHClipToWorld(float4 positionCS)
  97. {
  98. return mul(UNITY_MATRIX_I_VP, positionCS).xyz;
  99. }
  100.  
  101. float4 frag(Varyings input) : SV_Target
  102. {
  103. UNITY_SETUP_INSTANCE_ID(input);
  104. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  105.  
  106. float2 furUv = input.uv / _BaseMap_ST.xy * _FurScale;
  107. float4 furColor = SAMPLE_TEXTURE2D(_FurMap, sampler_FurMap, furUv);
  108. float alpha = furColor.r * (1.0 - input.layer);
  109. if (input.layer > 0.0 && alpha < _AlphaCutout) discard;
  110.  
  111. float3 viewDirWS = SafeNormalize(GetCameraPositionWS() - input.positionWS);
  112. float3 normalTS = UnpackNormalScale(
  113. SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, furUv),
  114. _NormalScale);
  115. float3 bitangent = SafeNormalize(viewDirWS.y * cross(input.normalWS, input.tangentWS));
  116. float3 normalWS = SafeNormalize(TransformTangentToWorld(
  117. normalTS,
  118. float3x3(input.tangentWS, bitangent, input.normalWS)));
  119.  
  120. SurfaceData surfaceData = (SurfaceData)0;
  121. InitializeStandardLitSurfaceData(input.uv, surfaceData);
  122. surfaceData.occlusion = lerp(1.0 - _Occlusion, 1.0, input.layer);
  123. surfaceData.albedo *= surfaceData.occlusion;
  124. surfaceData.emission = 0;
  125.  
  126. InputData inputData = (InputData)0;
  127. inputData.positionWS = input.positionWS;
  128. inputData.normalWS = normalWS;
  129. inputData.viewDirectionWS = viewDirWS;
  130.  
  131.  
  132. #if defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  133. inputData.shadowCoord = TransformWorldToShadowCoord(input.positionWS);
  134. #else
  135. inputData.shadowCoord = float4(0, 0, 0, 0);
  136. #endif
  137. //#if defined(_MAIN_LIGHT_SHADOWS) && !defined(_RECEIVE_SHADOWS_OFF)
  138. // inputData.shadowCoord = TransformWorldToShadowCoord(input.positionWS);
  139. //#else
  140. // inputData.shadowCoord = float4(0, 0, 0, 0);
  141. //#endif
  142.  
  143.  
  144. inputData.fogCoord = input.fogFactorAndVertexLight.x;
  145. inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
  146.  
  147. float3 SH = input.lightmapUVOrVertexSH.xyz;
  148. inputData.bakedGI = SAMPLE_GI( input.lightmapUVOrVertexSH.xy, SH, inputData.normalWS );
  149. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  150. inputData.shadowMask = SAMPLE_SHADOWMASK(input.lightmapUVOrVertexSH.xy);
  151.  
  152. float4 color = UniversalFragmentPBR(inputData, surfaceData);
  153.  
  154. //ApplyRimLight(color.rgb, input.positionWS, viewDirWS, normalWS);
  155. //color.rgb += _AmbientColor;
  156. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  157.  
  158. return color;// half4(SH, 1);// color; //
  159. }
  160.  
  161. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement