I am trying to implement a cg shader that use only 1 sampler2D map (all 6 faces of the cube map are combined into this map in a certain order). The purpose is to use render to target technique. Set all 6 different size of viewports of the 6 directions of the camera to create such sampler2D map.
Code: Select all
void main_v(float4 Position: POSITION,
float3 Normal: NORMAL,
float2 Uv: TEXCOORD0,
float3 Tangent: TEXCOORD1,
float3 Binormal: TEXCOORD2,
out float4 oPosition: POSITION,
out float2 oUv: TEXCOORD0,
out float3 oLightDir: TEXCOORD1,
out float3 oEyeDir: TEXCOORD2,
out float3 oHalfAngle: TEXCOORD3,
out float3 R : TEXCOORD4,
uniform float4 lightPosition,
uniform float3 eyePosition,
uniform float4x4 WorldViewProj,
uniform float4x4 modelToWorld,
uniform float4x4 mInvWorld)
{
oPosition = mul(WorldViewProj, Position);
oUv = Uv;
float3 LightDir = normalize(lightPosition.xyz - (Position * lightPosition.w));
float3 EyeDir = eyePosition - Position.xyz;
float3x3 TBN = float3x3(Tangent, Binormal, Normal);
// Transformacja do Tangent Space
LightDir = normalize(mul(TBN, LightDir));
EyeDir = normalize(mul(TBN, EyeDir));
oLightDir = LightDir;
oEyeDir = EyeDir;
oHalfAngle = normalize(EyeDir + LightDir);
float3 N = Normal;
N = normalize(N);
// Compute the incident and reflected vectors
float4 positionW = mul(modelToWorld, Position);
positionW/=positionW.w;
float3 I = positionW.xyz - eyePosition;
R = I - 2.0 * N * dot(N, I);
R = reflect(I, N);
}
void main_f(float2 Uv: TEXCOORD0,
float3 LightDir : TEXCOORD1,
float3 EyeDir : TEXCOORD2,
float3 HalfAngle : TEXCOORD3,
float3 R : TEXCOORD4,
uniform float bumpness,
uniform float specularPow,
uniform float cubePow,
uniform float3 LightDiffuse,
uniform float3 LightAmbient,
uniform sampler2D DecalMap : register(s0),
uniform sampler2D NormalMap : register(s1),
uniform sampler2D SpecularMap : register(s2),
uniform sampler2D CubeMap : register(s3),
out float4 oColor : COLOR)
{
float3 smooth = { 0.5f, 0.5f, 1.0f };
float3 Normal = tex2D (NormalMap, Uv).rgb;
Normal = lerp( smooth, Normal, bumpness );
Normal = normalize (Normal*2.0 -1.0);
float3 Diffuse = max (dot (LightDir, Normal), 0.0) * LightDiffuse;
float3 DecalColor = tex2D (DecalMap, Uv).rgb;
float3 AmbientColor = tex2D(DecalMap, Uv).rgb * LightAmbient;
float3 SpecularColor = tex2D(SpecularMap, Uv).rgb;
float Specular = max (dot (HalfAngle, Normal), 0.0);
Specular = pow (Specular, specularPow);
float3 texCoord = R;
float3 vec = normalize(texCoord);
float2 texCoord2DTemp = {0.0f, 0.0f};
float4 ltt = {0.0f, 0.0f, 0.0f, 1.0f};
float3 stf;
float3 absVec = abs(vec);
float3 sgn = sign(vec);
float3 sgnVec = -sign(vec) / 2 + 0.5; // maps + to 0 and - to 1
float absMa = max( max(absVec.x, absVec.y), absVec.z);
float3 faceBaseVec = float3( 0, 2, 4);
float3 faceVec = faceBaseVec + sgnVec;
if ( abs(vec.x) == absMa ) { stf = float3(-vec.z * sgn.x, -vec.y, faceVec.x); }
else if ( abs(vec.y) == absMa ) { stf = float3(+vec.x, vec.z * sgn.z, faceVec.y); }
else if ( abs(vec.z) == absMa ) { stf = float3(vec.x * sgn.x, -vec.y, faceVec.z); }
float2 st = (stf.xy/absMa + 1) / 2;
int face = int(stf.z);
texCoord2DTemp = st;
if(face == 0)
{
texCoord2DTemp.x = (1.0/6.0)*texCoord2DTemp.x;
ltt = tex2D(CubeMap,texCoord2DTemp);
}
else if(face == 1)
{
texCoord2DTemp.x = (1.0/6.0) + (1.0/6.0)*texCoord2DTemp.x;
ltt = tex2D(CubeMap,texCoord2DTemp);
}
else if(face == 2)
{
texCoord2DTemp.x = (2.0/6.0) + (1.0/6.0)*texCoord2DTemp.x;
ltt = tex2D(CubeMap,texCoord2DTemp);
}
else if(face == 3)
{
texCoord2DTemp.x = (3.0/6.0) + (1.0/6.0)*texCoord2DTemp.x;
ltt = tex2D(CubeMap,texCoord2DTemp);
}
else if(face == 4)
{
texCoord2DTemp.x = (4.0/6.0) + (1.0/6.0)*texCoord2DTemp.x;
ltt = tex2D(CubeMap,texCoord2DTemp);
}
else if(face == 5)
{
texCoord2DTemp.x = (5.0/6.0) + (1.0/6.0)*texCoord2DTemp.x;
ltt = tex2D(CubeMap,texCoord2DTemp);
}
float4 fColor = float4 (DecalColor * Diffuse + Specular * SpecularColor + AmbientColor, 1.0);//
oColor = lerp(fColor, ltt, 1);
}
I cannot find anything wrong by justing looking at the shader code.
But the problem is it wouldn't compile.
When I leave only 1 face loopup in the pixel shader, and comment all the rest 5 faces. It works totally fine and the reflection is correct for that face.
I tried to replace the 'if{}else if{}' by some thing like 'if(){}else{if(){...}}'. Also with no luck.
Has anybody got this kind of stuff work or is it the end of using only 1 texture instead of a DDS file?
What are other alternatives for the dynamic cube mapping?
Thanks in advance!!
[/code]