struct VIn { float4 p : POSITION; float3 n : NORMAL; float3 t : TANGENT; float2 uv : TEXCOORD0; }; struct V2In { float4 p : POSITION; float3 n : NORMAL; float3 t : TANGENT; float2 uv1 : TEXCOORD0; float2 uv2 : TEXCOORD1; }; struct VOut { float4 p : POSITION; float2 uv : TEXCOORD0; float4 wp : TEXCOORD1; float3 n : TEXCOORD2; float3 t : TEXCOORD3; float3 b : TEXCOORD4; float3 sdir : TEXCOORD5; }; struct PIn { float2 uv : TEXCOORD0; float4 wp : TEXCOORD1; float3 n : TEXCOORD2; float3 t : TEXCOORD3; float3 b : TEXCOORD4; float3 sdir : TEXCOORD5; }; struct VRefOut { float4 p : POSITION; float2 uv : TEXCOORD0; float4 wp : TEXCOORD1; float3 n : TEXCOORD2; float3 t : TEXCOORD3; float3 b : TEXCOORD4; float3 sdir : TEXCOORD5; float3 texProj : TEXCOORD6; }; struct PRefIn { float2 uv : TEXCOORD0; float4 wp : TEXCOORD1; float3 n : TEXCOORD2; float3 t : TEXCOORD3; float3 b : TEXCOORD4; float3 sdir : TEXCOORD5; float3 texProj : TEXCOORD6; }; void ambient_vs(VIn IN, uniform float4x4 wvpMat, out float4 oPos : POSITION, out float2 oUV : TEXCOORD0) { oPos = mul(wvpMat, IN.p); oUV = IN.uv; } float4 ambient_ps(in float2 uv : TEXCOORD0, uniform float3 ambient, uniform float4 matDif, uniform sampler2D dMap, uniform sampler2D aoMap): COLOR0 { return tex2D(dMap, uv) * tex2D(aoMap, uv) * float4(ambient, 1) * float4(matDif.rgb, 1); } void ambient_uv2_vs(V2In IN, uniform float4x4 wvpMat, out float4 oPos : POSITION, out float2 dUV : TEXCOORD0, out float2 oUV : TEXCOORD1) { oPos = mul(wvpMat, IN.p); dUV = IN.uv1; oUV = IN.uv2; } float4 ambient_uv2_ps(in float2 uv1 : TEXCOORD0, in float2 uv2 : TEXCOORD1, uniform float3 ambient, uniform float4 matDif, uniform sampler2D dMap, uniform sampler2D aoMap): COLOR0 { return tex2D(dMap, uv1) * tex2D(aoMap, uv2) * float4(ambient, 1) * float4(matDif.rgb, 1); } VOut diffuse_vs(VIn IN, uniform float4x4 wMat, uniform float4x4 wvpMat, uniform float4 spotlightDir ) { VOut OUT; OUT.wp = mul(wMat, IN.p); OUT.p = mul(wvpMat, IN.p); OUT.uv = IN.uv; OUT.n = IN.n; OUT.t = IN.t; OUT.b = cross(IN.t, IN.n); OUT.sdir = mul(wMat, spotlightDir).xyz; // spotlight dir in world space return OUT; } float4 diffuse_ps( PIn IN, uniform float3 lightDif0, uniform float4 lightPos0, uniform float4 lightAtt0, uniform float3 lightSpec0, uniform float4 matDif, uniform float4 matSpec, uniform float matShininess, uniform float3 camPos, uniform float4 invSMSize, uniform float4 spotlightParams, uniform float4x4 iTWMat, uniform float reflection, uniform sampler2D diffuseMap : register(s0), uniform sampler2D specMap : register(s1), uniform sampler2D normalMap : register(s2)): COLOR0 { // direction float3 ld0 = normalize(lightPos0.xyz - (lightPos0.w * IN.wp.xyz)); half lightDist = length(lightPos0.xyz - IN.wp.xyz) / lightAtt0.r; // attenuation half ila = lightDist * lightDist; // quadratic falloff half la = 1.0 - ila; float4 normalTex = tex2D(normalMap, IN.uv); float3x3 tbn = float3x3(IN.t, IN.b, IN.n); float3 normal = mul(transpose(tbn), normalTex.xyz * 2 - 1); // to object space normal = normalize(mul((float3x3)iTWMat, normal)); float3 diffuse = max(dot(ld0, normal), 0); // calculate the spotlight effect float spot = (spotlightParams.x == 1 && spotlightParams.y == 0 && spotlightParams.z == 0 && spotlightParams.w == 1 ? 1 : // if so, then it's not a spot light saturate( (dot(ld0, normalize(-IN.sdir)) - spotlightParams.y) / (spotlightParams.x - spotlightParams.y))); float3 camDir = normalize(camPos - IN.wp.xyz); float3 halfVec = normalize(ld0 + camDir); float3 specular = pow(max(dot(normal, halfVec), 0), matShininess); float4 diffuseTex = tex2D(diffuseMap, IN.uv); float4 specTex = tex2D(specMap, IN.uv); float3 diffuseContrib = (diffuse * lightDif0 * diffuseTex.rgb * matDif.rgb); float3 specularContrib = (specular * lightSpec0 * specTex.rgb * matSpec.rgb); float3 light0C = (diffuseContrib + specularContrib) * la * spot; return float4(light0C, diffuseTex.a); } VRefOut diffuse_reflect_vs(VIn IN, uniform float4x4 wMat, uniform float4x4 wvpMat, uniform float4 spotlightDir, uniform float3 eyePositionW ) { VRefOut OUT; OUT.wp = mul(wMat, IN.p); OUT.p = mul(wvpMat, IN.p); OUT.uv = IN.uv; OUT.n = IN.n; OUT.t = IN.t; OUT.b = cross(IN.t, IN.n); OUT.sdir = mul(wMat, spotlightDir).xyz; // spotlight dir in world space float3 posW = mul(wMat, IN.p).xyz; float3 Normal = normalize(mul((float3x3)wMat, IN.n)); float3 eyePos = posW - eyePositionW; OUT.texProj = reflect(eyePos, Normal); OUT.texProj.z = - OUT.texProj.z; return OUT; } float4 diffuse_reflect_ps( PRefIn IN, uniform float3 lightDif0, uniform float4 lightPos0, uniform float4 lightAtt0, uniform float3 lightSpec0, uniform float4 matDif, uniform float4 matSpec, uniform float matShininess, uniform float3 camPos, uniform float4 invSMSize, uniform float4 spotlightParams, uniform float4x4 iTWMat, uniform float reflectivity, uniform sampler2D diffuseMap : register(s0), uniform sampler2D specMap : register(s1), uniform sampler2D normalMap : register(s2), uniform samplerCUBE reflectMap : register(s3)): COLOR0 { // direction float3 ld0 = normalize(lightPos0.xyz - (lightPos0.w * IN.wp.xyz)); half lightDist = length(lightPos0.xyz - IN.wp.xyz) / lightAtt0.r; // attenuation half ila = lightDist * lightDist; // quadratic falloff half la = 1.0 - ila; float4 normalTex = tex2D(normalMap, IN.uv); float3x3 tbn = float3x3(IN.t, IN.b, IN.n); float3 normal = mul(transpose(tbn), normalTex.xyz * 2 - 1); // to object space normal = normalize(mul((float3x3)iTWMat, normal)); float3 diffuse = max(dot(ld0, normal), 0); // calculate the spotlight effect float spot = (spotlightParams.x == 1 && spotlightParams.y == 0 && spotlightParams.z == 0 && spotlightParams.w == 1 ? 1 : // if so, then it's not a spot light saturate( (dot(ld0, normalize(-IN.sdir)) - spotlightParams.y) / (spotlightParams.x - spotlightParams.y))); float3 camDir = normalize(camPos - IN.wp.xyz); float3 halfVec = normalize(ld0 + camDir); float3 specular = pow(max(dot(normal, halfVec), 0), matShininess); float4 diffuseTex = tex2D(diffuseMap, IN.uv); float4 specTex = tex2D(specMap, IN.uv); float4 reflecTex = texCUBE(reflectMap, IN.texProj); float3 diffuseContrib = (diffuse * lightDif0 * diffuseTex.rgb * matDif.rgb); float3 specularContrib = (specular * lightSpec0 * specTex.rgb * matSpec.rgb); float3 light0C = (diffuseContrib + specularContrib) * la * spot; float3 reflectColor = (reflecTex.rgb * diffuseContrib * reflectivity) + (reflecTex.rbg * specularContrib * reflectivity); return float4(light0C + reflectColor, diffuseTex.a); }