uniform float textureRepeat; uniform sampler2D ColorMapSampler : register(s0); uniform sampler2D DetailMapSampler : register(s1); uniform sampler2D BumpMapSampler : register(s2); #define PARALLAX_ITERATION 4 uniform float HEIGHT_SCALE; uniform float HEIGHT_BIAS; float3x3 TransfModelToTangent( in float3 Tangent, in float3 Binormal, in float3 Normal ) { float T2 = dot(Tangent, Tangent); float B2 = dot(Binormal, Binormal); float N2 = dot(Normal, Normal); float BT = dot(Binormal, Tangent); float det = B2 * T2 - BT * BT; return float3x3( (B2 * Tangent - BT * Binormal)/det, (T2 * Binormal - BT * Tangent)/det, Normal/N2 ); // simplified solution //return float3x3(Tangent/T2, Binormal/B2, Normal/N2); // more simplified solution //return float3x3(Tangent, Binormal, Normal); } float4 Illumination(float3 Light, float3 Normal, float3 View, float2 TexCoord, float2 TexCoord2) { // Blinn lighting float3 Half = normalize(Light + View); float Diffuse = dot(Normal, Light); float Specular = dot(Normal, Half); float4 Lighting = lit(Diffuse, Specular, 16); float4 Color= tex2D(ColorMapSampler, TexCoord) * tex2D(DetailMapSampler, TexCoord2); return (Lighting.y + 0.1) * Color; } struct VS_OUTPUT { float4 hPosition : POSITION; // point in normalized device space before homogeneous division float2 TexCoord : TEXCOORD0; // texture coordinates float2 TexCoord2 : TEXCOORD6; // texture coordinates float3 mView : TEXCOORD1; // model space view vector float3 mLight : TEXCOORD2; // model space light vector float3 wTangent : TEXCOORD3; // model space tangent vector float3 wBinormal : TEXCOORD4; // model space binormal vector float3 wNormal : TEXCOORD5; // model space triangle normal vector }; void DISCARD_BY_TEX(float2 Tex) { if (Tex.x > textureRepeat || Tex.x < 0 || Tex.y > textureRepeat || Tex.y < 0) discard; } float2 PARALLAX_MAPPING_OFFSET_LIMIT(sampler2D heightMap, float2 TexCoord, float3 View) { float4 Normal = tex2D(heightMap, TexCoord); float h = Normal.a * HEIGHT_SCALE + HEIGHT_BIAS; return TexCoord + h * View.xy; } float2 PARALLAX_MAPPING_ITER(sampler2D heightMap, float2 TexCoord, float3 View) { for(int i = 0; i < PARALLAX_ITERATION; i++) { float4 Normal = tex2D(heightMap, TexCoord); float h = Normal.a * HEIGHT_SCALE + HEIGHT_BIAS; TexCoord += h * Normal.z * View; } return TexCoord.xy; } //------------------------------------------------------------------------------------ // // ParallaxSlopeIterationPS: Iterative Parallax Mapping with Slope pixel shader // //------------------------------------------------------------------------------------ float4 ParallaxSlopeIterationPS(VS_OUTPUT IN) : COLOR { //return tex2D(BumpMapSampler,IN.TexCoord).a; IN.wTangent = normalize(IN.wTangent); IN.wBinormal = normalize(IN.wBinormal); IN.wNormal = normalize(IN.wNormal); // get model space normal vector float3x3 ModelToTangent = TransfModelToTangent(IN.wTangent, IN.wBinormal, IN.wNormal ); // needs normalization because of linear interpolation float3 mLight = normalize( IN.mLight ); float3 mView = normalize( IN.mView ); float3 tView = normalize( mul(ModelToTangent, IN.mView ) ); // performing parallax mapping float2 ParallaxTex = PARALLAX_MAPPING_ITER(BumpMapSampler, IN.TexCoord, tView); DISCARD_BY_TEX(ParallaxTex); // get model space normal vector float3 tNormal = tex2D(BumpMapSampler, ParallaxTex).rgb; // Normal vector should be transformed with the inverse transpose of TangentToModel // which is the transpose of ModelToTangent float3 mNormal = normalize( mul( tNormal, ModelToTangent ) ); //mNormal = normalize(IN.wNormal); // illumination calculation return Illumination(mLight, mNormal, mView, ParallaxTex, IN.TexCoord2); }