1 registered members (AndrewAMD),
1,248
guests, and 6
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Texture coords in shaders, are not linearly proportional?
#327121
06/04/10 18:10
06/04/10 18:10
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
OP
Serious User
|
OP
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
Hi friends, I have been playing with 3dgs terraintex3 shader. I thought that i could read texture colors from only one texture using each quadrant for each texture with a texture like this: I modified terraintex3.fx file to this:
#define SHADOWMAP
#include <define>
#include <transform>
#include <sun>
#include <lights>
#include <fog>
#include <normal>
Texture entSkin1; // Red/green/blue for blending
Texture entSkin2; // MultiTexture file
Texture entSkin3; // ShadowMap
//Texture entSkin4; // NormalMap
float4 vecSkill41;
float fAmbient;
sampler sMaskTex = sampler_state { Texture = <entSkin1>; MipFilter = Linear; };
sampler sQuadTex = sampler_state { Texture = <entSkin2>; MipFilter = Linear; };
sampler sShadowTex = sampler_state { Texture = <entSkin3>; MipFilter = Linear; };
//sampler sNormalTex = sampler_state { Texture = <entSkin4>; MipFilter = Linear; };
//////////////////////////////////////////////////////////////////////
struct out_QuadTex // Output to the pixelshader fragment
{
float4 Pos : POSITION;
float4 Color : COLOR0;
float Fog : FOG;
float2 MaskCoord : TEXCOORD0;
float2 NWCoord : TEXCOORD1;
float2 NECoord : TEXCOORD2;
float2 SWCoord : TEXCOORD3;
float2 SECoord : TEXCOORD4;
float2 ShadowCoord : TEXCOORD5;
};
out_QuadTex vs_QuadTex (
float4 inPos : POSITION,
float3 inNormal : NORMAL,
float2 inTexCoord0 : TEXCOORD0 )
{
out_QuadTex Out;
Out.Pos = DoTransform(inPos); // transform to screen coordinates
// rotate and normalize the normal
float3 N = DoNormal(inNormal);
float3 P = mul(inPos,matWorld);
Out.Color = fAmbient; // Add ambient and sun light
for (int i=0; i<8; i++) // Add 8 dynamic lights
Out.Color += DoLight(P,N,i);
Out.Fog = DoFog(inPos); // Add fog
Out.MaskCoord = inTexCoord0.xy;
Out.ShadowCoord = inTexCoord0.xy;
// scale the texture coordinates for the masked textures
Out.NWCoord = (inTexCoord0.xy * vecSkill41.w)%0.5;
Out.NECoord.x = ((inTexCoord0.x * vecSkill41.w)%0.5)+0.5;
Out.NECoord.y = (inTexCoord0.y * vecSkill41.w)%0.5;
Out.SWCoord.x = (inTexCoord0.x * vecSkill41.w)%0.5;
Out.SWCoord.y = ((inTexCoord0.y * vecSkill41.w)%0.5)+0.5;
Out.SECoord = ((inTexCoord0.xy * vecSkill41.w)%0.5)+0.5;/**/
/*Out.NWCoord = inTexCoord0.xy * vecSkill41.w;
Out.NECoord = inTexCoord0.xy * vecSkill41.w;
Out.SWCoord = inTexCoord0.xy * vecSkill41.w;
Out.SECoord = inTexCoord0.xy * vecSkill41.w;/**/
return Out;
}
float4 ps_QuadTex(out_QuadTex In): COLOR
{
// retrieve the pixels for the textures and the masks
float4 MaskColor = tex2D(sMaskTex,In.MaskCoord);
float4 NWColor = tex2D(sQuadTex,In.NWCoord);
float4 NEColor = tex2D(sQuadTex,In.NECoord);
float4 SWColor = tex2D(sQuadTex,In.SWCoord);
float4 SEColor = tex2D(sQuadTex,In.SECoord);
float4 ShadowColor = tex2D(sShadowTex,In.ShadowCoord);
// blend the blue, red and green textures over the base texture
float4 NEMixed = lerp(NWColor,NEColor,MaskColor.b);
float4 SWMixed = lerp(NEMixed,SWColor,MaskColor.r);
float4 FinalColor = lerp(SWMixed,SEColor,MaskColor.g);
// Add the vertex light and shadow map
#ifdef SHADOWMAP
FinalColor *= ShadowColor;
#else
FinalColor *= In.Color;
#endif
FinalColor.a = 1.0f; // prevent transparency
return FinalColor;
}
technique QuadTex_13
{
pass one
{
sampler[0] = (sMaskTex);
sampler[1] = (sQuadTex);
sampler[2] = (sShadowTex);
VertexShader = compile vs_2_0 vs_QuadTex();
PixelShader = compile ps_2_0 ps_QuadTex();
}
}
// fallback if nothing works
technique fallback { pass one { } }
My main problem is in this blocks:
// scale the texture coordinates for the masked textures
Out.NWCoord = (inTexCoord0.xy * vecSkill41.w)%0.5;
Out.NECoord.x = ((inTexCoord0.x * vecSkill41.w)%0.5)+0.5;
Out.NECoord.y = (inTexCoord0.y * vecSkill41.w)%0.5;
Out.SWCoord.x = (inTexCoord0.x * vecSkill41.w)%0.5;
Out.SWCoord.y = ((inTexCoord0.y * vecSkill41.w)%0.5)+0.5;
Out.SECoord = ((inTexCoord0.xy * vecSkill41.w)%0.5)+0.5;/**/
/*Out.NWCoord = inTexCoord0.xy * vecSkill41.w;
Out.NECoord = inTexCoord0.xy * vecSkill41.w;
Out.SWCoord = inTexCoord0.xy * vecSkill41.w;
Out.SECoord = inTexCoord0.xy * vecSkill41.w;/**/
As you can see in pixelshader code, i tried to calculate four different coordinates to be used in the mixture with reference to the mask map. If the mask is black uses NW quadrant, if it is blue uses NE quadrant, and so... My surprise comes when I run the shader and got this result: As you can see, the mixed texture loses his proportions and it is drawn one bigsize and one small in a clear pattern while the colors are perfectly in place. Could anyone clarify why this happens? Texture coords in shaders, are not linearly proportional? How can i resolve it? I'm not a good codder and i'm touching the roof of my limited understanding of these things, so any response will be appreciated. thx in advance, txes. Salud!
|
|
|
Re: Texture coords in shaders, are not linearly proportional?
[Re: txesmi]
#327125
06/04/10 18:44
06/04/10 18:44
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Im only a noob to shaders myself, so I havent tried decoding your code, it is more complex than I have gotten yet...
But on the off-chance it helps you, I recently discovered, the hard way, that IF a texture you are reading from is not 'square', then the individual pixels themselves are not square either...
So maybe the width and height distortions you are getting is due to your entSkin1 not being square...
I hope this helps you out some...
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Texture coords in shaders, are not linearly proportional?
[Re: EvilSOB]
#327137
06/04/10 19:37
06/04/10 19:37
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
OP
Serious User
|
OP
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
@EvilSOB: Thank you for your reply, but it is not the problem. Every texture is squared and power of two. Anyway, great to been helped!
|
|
|
Re: Texture coords in shaders, are not linearly proportional?
[Re: txesmi]
#327154
06/04/10 20:25
06/04/10 20:25
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Dude, download THIS. I got it from the Wiki, very useful...
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Texture coords in shaders, are not linearly proportional?
[Re: txesmi]
#327161
06/04/10 20:42
06/04/10 20:42
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
OP
Serious User
|
OP
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
Absolutly great!! code:
#define SHADOWMAP
#include <define>
#include <transform>
#include <sun>
#include <lights>
#include <fog>
#include <normal>
Texture entSkin1; // Red/green/blue for blending
Texture entSkin2; // MultiTexture
Texture entSkin3; // ShadowMap
//Texture entSkin4; // NormalMap
float4 vecSkill41;
float fAmbient;
sampler sMaskTex = sampler_state { Texture = <entSkin1>; MipFilter = Linear; };
sampler sQuadTex = sampler_state { Texture = <entSkin2>; MipFilter = Linear; };
sampler sShadowTex = sampler_state { Texture = <entSkin3>; MipFilter = Linear; };
//sampler sNormalTex = sampler_state { Texture = <entSkin4>; MipFilter = Linear; };
//////////////////////////////////////////////////////////////////////
struct out_QuadTex // Output to the pixelshader fragment
{
float4 Pos : POSITION;
float4 Color : COLOR0;
float Fog : FOG;
float2 MaskCoord : TEXCOORD0;
float2 NWCoord : TEXCOORD1;
float2 NECoord : TEXCOORD2;
float2 SWCoord : TEXCOORD3;
float2 SECoord : TEXCOORD4;
float2 ShadowCoord: TEXCOORD5;
};
out_QuadTex vs_QuadTex (
float4 inPos : POSITION,
float3 inNormal : NORMAL,
float2 inTexCoord0 : TEXCOORD0 )
{
out_QuadTex Out;
Out.Pos = DoTransform(inPos); // transform to screen coordinates
// rotate and normalize the normal
float3 N = DoNormal(inNormal);
float3 P = mul(inPos,matWorld);
Out.Color = fAmbient; // Add ambient and sun light
for (int i=0; i<8; i++) // Add 8 dynamic lights
Out.Color += DoLight(P,N,i);
Out.Fog = DoFog(inPos); // Add fog
Out.MaskCoord = inTexCoord0.xy;
Out.ShadowCoord = inTexCoord0.xy;
// scale the texture coordinates for the masked textures
Out.NWCoord = inTexCoord0.xy * vecSkill41.w;
Out.NECoord = inTexCoord0.xy * vecSkill41.w;
Out.SWCoord = inTexCoord0.xy * vecSkill41.w;
Out.SECoord = inTexCoord0.xy * vecSkill41.w;
return Out;
}
float4 ps_QuadTex(out_QuadTex In): COLOR
{
float HalfSize = 0.5;
In.NWCoord = fmod(In.NWCoord, HalfSize);
In.NECoord.x = fmod(In.NECoord.x, HalfSize)+0.5;
In.NECoord.y = fmod(In.NECoord.y, HalfSize);
In.SWCoord.x = fmod(In.SWCoord.x, HalfSize);
In.SWCoord.y = fmod(In.SWCoord.y, HalfSize)+0.5;
In.SECoord = fmod(In.SECoord, HalfSize);
// retrieve the pixels for the textures and the masks
float4 MaskColor = tex2D(sMaskTex,In.MaskCoord);
float4 NWColor = tex2D(sQuadTex,In.NWCoord);
float4 NEColor = tex2D(sQuadTex,In.NECoord);
float4 SWColor = tex2D(sQuadTex,In.SWCoord);
float4 SEColor = tex2D(sQuadTex,In.SECoord);
float4 ShadowColor = tex2D(sShadowTex,In.ShadowCoord);
// blend the blue, red and green textures over the base texture
float4 NEMixed = lerp(NWColor,NEColor,MaskColor.b);
float4 SWMixed = lerp(NEMixed,SWColor,MaskColor.r);
float4 FinalColor = lerp(SWMixed,SEColor,MaskColor.g);
// Add the vertex light and shadow map
#ifdef SHADOWMAP
FinalColor *= ShadowColor;
#else
FinalColor *= In.Color;
#endif
FinalColor.a = 1.0f; // prevent transparency
return FinalColor;
}
technique QuadTex_13
{
pass one
{
sampler[0] = (sMaskTex);
sampler[1] = (sQuadTex);
sampler[2] = (sShadowTex);
VertexShader = compile vs_2_0 vs_QuadTex();
PixelShader = compile ps_2_0 ps_QuadTex();
}
}
// fallback if nothing works
technique fallback { pass one { } }
@ChrisB How many times can I thank you without sounding like a bore? Sincerelly, thank you very much! @EvilSOB I will take good care of that! With my indispensable Collins. Uploaded to my webspace. Maybe i translate it for the spanish community. Thank you!
|
|
|
Re: Texture coords in shaders, are not linearly proportional?
[Re: txesmi]
#327165
06/04/10 20:56
06/04/10 20:56
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
No problem dude.
Bcause your new terrain shader looks easier to understand than most others I have seen.
Its given me ideas for a similar shader for very large models. I can now see a way that the different "quadrants" can be re-scaled "differently" onto the surface of an object...
Thanks dude!
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
|