Advertisement
Guest User

Untitled

a guest
May 9th, 2025
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. Vertex shader:
  2. void main(in float4 InPos : POSITION,
  3. in float3 InNormal : NORMAL,
  4. in float3 InTangent : TANGENT,
  5. in float3 InBiNormal : BINORMAL,
  6. in float4 InColor : COLOR,
  7. in float2 InTexCoord_Diff : TEXCOORD0,
  8. in float2 InTexCoord_LiMa : TEXCOORD1,
  9. out float4 OutPos : POSITION,
  10. out float4 OutColor : COLOR,
  11. out float2 OutTexCoord_Diff : TEXCOORD0,
  12. out float2 OutTexCoord_LiMa : TEXCOORD1,
  13. out float3 OutEyeDir_TS : TEXCOORD2,
  14. uniform float3 EyePos_OS)
  15. {
  16. float3 objN = normalize(InNormal);
  17. float3 objT = normalize(InTangent);
  18. float3 objB = normalize(InBiNormal);
  19.  
  20. float3x3 TBN = float3x3(objT, objB, objN);
  21. TBN[0] = normalize(TBN[0]);
  22. TBN[1] = normalize(TBN[1]);
  23. TBN[2] = normalize(TBN[2]);
  24.  
  25. float3x3 TBN_T = transpose(TBN);
  26.  
  27. float3 viewDir_ObjectSpace = normalize(EyePos_OS - InPos.xyz);
  28.  
  29. OutEyeDir_TS = normalize(mul(TBN_T, viewDir_ObjectSpace));
  30.  
  31. OutPos = mul(glstate.matrix.mvp, InPos);
  32. OutColor = InColor;
  33. OutTexCoord_Diff= InTexCoord_Diff;
  34. OutTexCoord_LiMa= InTexCoord_LiMa;
  35. }
  36.  
  37.  
  38. fragment:
  39. half2 ParallaxOcclusionMapping(half2 texCoords, half3 viewDirTS, half parallaxHeightScale, sampler2D heightMap)
  40. {
  41. const float numLayers = 10.0h;
  42. float layerDepth = 1.0h / numLayers;
  43. float currentLayerDepth = 0.0h;
  44.  
  45. half2 P = (viewDirTS.xy / max(abs(viewDirTS.z), 0.001h)) * parallaxHeightScale;
  46. half2 deltaTexCoords = P / numLayers;
  47.  
  48. half2 currentTexCoords = texCoords;
  49. float currentDepthMapValue = 1.0h - tex2D(heightMap, currentTexCoords).r;
  50.  
  51. for (int i=0; i < int(numLayers); ++i)
  52. {
  53. if (currentLayerDepth >= currentDepthMapValue)
  54. break;
  55.  
  56. currentTexCoords -= deltaTexCoords;
  57. currentDepthMapValue = 1.0h - tex2D(heightMap, currentTexCoords).r;
  58. currentLayerDepth += layerDepth;
  59. }
  60.  
  61. half2 prevTexCoords = currentTexCoords + deltaTexCoords;
  62.  
  63. float depthAfterCollision = currentDepthMapValue - currentLayerDepth;
  64. float depthBeforeCollision = (1.0h - tex2D(heightMap, prevTexCoords).r) - currentLayerDepth + layerDepth;
  65.  
  66. float weight = depthAfterCollision / (depthAfterCollision - depthBeforeCollision + 0.0001h);
  67. half2 finalTexCoords = prevTexCoords * weight + currentTexCoords * (1.0h - weight);
  68.  
  69. return finalTexCoords;
  70. }
  71.  
  72. void main(in half4 InColor : COLOR,
  73. in half2 InTexCoord_Diff : TEXCOORD0,
  74. in half2 InTexCoord_LiMa : TEXCOORD1,
  75. in half3 EyeDir_TS : TEXCOORD2,
  76. out half4 OutColor : COLOR,
  77. uniform sampler2D DiffuseMapSampler : TEXUNIT0,
  78. uniform sampler2D LightMapSampler : TEXUNIT1,
  79. uniform sampler2D LightDirMapSampler : TEXUNIT2,
  80. uniform sampler2D NormalMapSampler : TEXUNIT3,
  81. uniform sampler2D SpecMapSampler : TEXUNIT4,
  82. uniform sampler2D HeightMapSampler : TEXUNIT5,
  83. uniform half ParallaxScale)
  84. {
  85. half3 normViewDirTS = normalize(EyeDir_TS);
  86. half2 parallaxCoords = ParallaxOcclusionMapping(InTexCoord_Diff, normViewDirTS, ParallaxScale, HeightMapSampler);
  87.  
  88. const half4 DiffuseC = tex2D(DiffuseMapSampler, parallaxCoords);
  89. const half4 SpecularC = tex2D(SpecMapSampler, parallaxCoords);
  90. const half3 NormalTS =2.0*(tex2D(NormalMapSampler, parallaxCoords).xyz-0.5);
  91.  
  92. const half4 LightMapC = tex2D(LightMapSampler, InTexCoord_LiMa);
  93. const half4 LightMapDirC= tex2D(LightDirMapSampler, InTexCoord_LiMa);
  94. const half3 LightDir_TS =2.0*(LightMapDirC.xyz-0.5);
  95.  
  96. const half3 HalfwayDir_TS =normalize(normViewDirTS + LightDir_TS);
  97.  
  98. const half4 diff =half4(saturate(dot(LightDir_TS, NormalTS)).xxx, 1.0);
  99. const half4 spec =half4(pow(saturate(dot(HalfwayDir_TS, NormalTS)), 32.0).xxx, 0.0);
  100.  
  101. const half4 LightMapCorrectedC=half4(LightMapC.xyz/LightMapDirC.w, 1.0);
  102.  
  103. OutColor=LightMapCorrectedC*InColor*(diff*DiffuseC + spec*SpecularC);
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement