Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Vertex shader:
- void main(in float4 InPos : POSITION,
- in float3 InNormal : NORMAL,
- in float3 InTangent : TANGENT,
- in float3 InBiNormal : BINORMAL,
- in float4 InColor : COLOR,
- in float2 InTexCoord_Diff : TEXCOORD0,
- in float2 InTexCoord_LiMa : TEXCOORD1,
- out float4 OutPos : POSITION,
- out float4 OutColor : COLOR,
- out float2 OutTexCoord_Diff : TEXCOORD0,
- out float2 OutTexCoord_LiMa : TEXCOORD1,
- out float3 OutEyeDir_TS : TEXCOORD2,
- uniform float3 EyePos_OS)
- {
- float3 objN = normalize(InNormal);
- float3 objT = normalize(InTangent);
- float3 objB = normalize(InBiNormal);
- float3x3 TBN = float3x3(objT, objB, objN);
- TBN[0] = normalize(TBN[0]);
- TBN[1] = normalize(TBN[1]);
- TBN[2] = normalize(TBN[2]);
- float3x3 TBN_T = transpose(TBN);
- float3 viewDir_ObjectSpace = normalize(EyePos_OS - InPos.xyz);
- OutEyeDir_TS = normalize(mul(TBN_T, viewDir_ObjectSpace));
- OutPos = mul(glstate.matrix.mvp, InPos);
- OutColor = InColor;
- OutTexCoord_Diff= InTexCoord_Diff;
- OutTexCoord_LiMa= InTexCoord_LiMa;
- }
- fragment:
- half2 ParallaxOcclusionMapping(half2 texCoords, half3 viewDirTS, half parallaxHeightScale, sampler2D heightMap)
- {
- const float numLayers = 10.0h;
- float layerDepth = 1.0h / numLayers;
- float currentLayerDepth = 0.0h;
- half2 P = (viewDirTS.xy / max(abs(viewDirTS.z), 0.001h)) * parallaxHeightScale;
- half2 deltaTexCoords = P / numLayers;
- half2 currentTexCoords = texCoords;
- float currentDepthMapValue = 1.0h - tex2D(heightMap, currentTexCoords).r;
- for (int i=0; i < int(numLayers); ++i)
- {
- if (currentLayerDepth >= currentDepthMapValue)
- break;
- currentTexCoords -= deltaTexCoords;
- currentDepthMapValue = 1.0h - tex2D(heightMap, currentTexCoords).r;
- currentLayerDepth += layerDepth;
- }
- half2 prevTexCoords = currentTexCoords + deltaTexCoords;
- float depthAfterCollision = currentDepthMapValue - currentLayerDepth;
- float depthBeforeCollision = (1.0h - tex2D(heightMap, prevTexCoords).r) - currentLayerDepth + layerDepth;
- float weight = depthAfterCollision / (depthAfterCollision - depthBeforeCollision + 0.0001h);
- half2 finalTexCoords = prevTexCoords * weight + currentTexCoords * (1.0h - weight);
- return finalTexCoords;
- }
- void main(in half4 InColor : COLOR,
- in half2 InTexCoord_Diff : TEXCOORD0,
- in half2 InTexCoord_LiMa : TEXCOORD1,
- in half3 EyeDir_TS : TEXCOORD2,
- out half4 OutColor : COLOR,
- uniform sampler2D DiffuseMapSampler : TEXUNIT0,
- uniform sampler2D LightMapSampler : TEXUNIT1,
- uniform sampler2D LightDirMapSampler : TEXUNIT2,
- uniform sampler2D NormalMapSampler : TEXUNIT3,
- uniform sampler2D SpecMapSampler : TEXUNIT4,
- uniform sampler2D HeightMapSampler : TEXUNIT5,
- uniform half ParallaxScale)
- {
- half3 normViewDirTS = normalize(EyeDir_TS);
- half2 parallaxCoords = ParallaxOcclusionMapping(InTexCoord_Diff, normViewDirTS, ParallaxScale, HeightMapSampler);
- const half4 DiffuseC = tex2D(DiffuseMapSampler, parallaxCoords);
- const half4 SpecularC = tex2D(SpecMapSampler, parallaxCoords);
- const half3 NormalTS =2.0*(tex2D(NormalMapSampler, parallaxCoords).xyz-0.5);
- const half4 LightMapC = tex2D(LightMapSampler, InTexCoord_LiMa);
- const half4 LightMapDirC= tex2D(LightDirMapSampler, InTexCoord_LiMa);
- const half3 LightDir_TS =2.0*(LightMapDirC.xyz-0.5);
- const half3 HalfwayDir_TS =normalize(normViewDirTS + LightDir_TS);
- const half4 diff =half4(saturate(dot(LightDir_TS, NormalTS)).xxx, 1.0);
- const half4 spec =half4(pow(saturate(dot(HalfwayDir_TS, NormalTS)), 32.0).xxx, 0.0);
- const half4 LightMapCorrectedC=half4(LightMapC.xyz/LightMapDirC.w, 1.0);
- OutColor=LightMapCorrectedC*InColor*(diff*DiffuseC + spec*SpecularC);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement