Home Technical Talk

Problem with HLSL in max 2008

pozani2.jpg

Hi I have a problem with this shader ,
seams appear where the texture is mirroring and vertex paint is used
and I don't know why.

Thanks

//
float4x4 wvp : WorldViewProjection < string UIWidget = "None"; >;

texture diff_in:DiffuseMap
<
string UIName = "Diffuse";
int Texcoord = 0;
int MapChannel = 1;
>;

int texcoord1 : Texcoord
<
int Texcoord = 1;
int MapChannel = 0;
>;

int uexcoord2 : Texcoord
<
int Texcoord = 2;
int MapChannel = -2;
>;


sampler diff_in_sampler = sampler_state
{
Texture = <diff_in>;
MipFilter = Anisotropic;
MagFilter = Anisotropic;
MinFilter = Anisotropic;
};

struct app2vertex
{
float4 position : POSITION;
float2 tex_coord : TEXCOORD0;
float3 vertex_paint : TEXCOORD1;
float alfa : TEXCOORD2;
};


struct vertex2pixel
{
float4 position : POSITION;
float2 tex_coord : TEXCOORD0;
float4 vertex_paint : TEXCOORD1;
};




vertex2pixel vertex_mirrorxy(app2vertex In)
{
vertex2pixel Out = (vertex2pixel)0;
Out.position = mul(In.position, wvp); // transform vert position to homogeneous clip space
Out.vertex_paint = float4(In.vertex_paint,In.alfa);
Out.tex_coord = In.tex_coord*2;
return Out;
}

float4 pixel_xy(vertex2pixel In) : COLOR
{
float null= 0;
float2 mir_tex_coord = (0,0);
float modx = int((In.tex_coord.x));
modx = modf((abs(modx)/2),null);
if (modx > 0)
mir_tex_coord.x = -In.tex_coord.x;
else
mir_tex_coord.x = In.tex_coord.x;
//
float mody = int((In.tex_coord.y));
mody = modf((abs(mody)/2),null);
if (mody > 0)
mir_tex_coord.y = -In.tex_coord.y ;
else
mir_tex_coord.y = In.tex_coord.y;

// mir_tex_coord.y += 0.5;
float4 col = tex2D(diff_in_sampler,mir_tex_coord);
return col * In.vertex_paint;
}

technique Mirror_XY
{
pass simple
{
VertexShader = compile vs_1_1 vertex_mirrorxy();
ZEnable = true;
ZWriteEnable = true;
CullMode = cw;
AlphaBlendEnable = true;
DestBlend = InvSrcAlpha;
SrcBlend = SrcAlpha;
PixelShader = compile ps_2_0 pixel_xy();
}

}

Replies

  • Ben Cloward
    Offline / Send Message
    Ben Cloward polycounter lvl 18
    Max 2008 requires that you set addressU and addressV in your texture sampler. Try changing your sampler to this:

    sampler diff_in_sampler = sampler_state
    {
    Texture = <diff_in>;
    MipFilter = Anisotropic;
    MagFilter = Anisotropic;
    MinFilter = Anisotropic;
    AddressU = Wrap;
    AddressV = Wrap;
    };

    If that doesn't work, set them to "Clamp" instead of "Wrap."
  • KikiAlex
    Hi Ben Cloward thanks for the help, but it seams not to work...
    I was wandering where can I find some documentation about the use of HLSL in 3d max,
    I have your second dvd "HLSL SHADER CREATION 2" and it was a good source of information, but I was wandering is there something more in depth on the way you can work with HLSL in 3d Max ?
  • Eric Chadwick
    Some links in this thread, especially check out Ben's last post.
    http://lumonixsoftware.com/yabb/YaBB.pl?num=1166022727
  • CrazyButcher
    Offline / Send Message
    CrazyButcher polycounter lvl 20
    the way you compute the mirroring might create too many inaccurate values, hence the artifacts.
    also you can further optimize the code by doing x and y at the same time

    outcoord.xy = lerp(incoord.xy,-incoord.xy,step(1,fmod(abs(incoord.xy),2)));


    what we do here is first:
    fmod(abs(incoord.xy),2)

    means we get values from 0-2 always
    as you want every "second" to flip sign, this means that values greater 1 should flip
    step(1,fmod...)
    will return 1 when fmod returned values greater 1, else 0

    finally we interpolate from normal incoord (step = 0) to flipped version (step = 1).

    I havent tested this but I think it works.
    However as modulo/frac operations might not yield perfect results, due to precision issues, it could be that artifacts remain.

    anyway always make use of "vector" functions, all operations are done on each component individually, so step might return 1 for x and 0 for y at same time (up to all 4 components). So when you see that you want to do the same for all components, you dont have to split computations and do for each individually, but you can do at same time. "step" basically helps you avoiding "if", as it does an if check (step(a,x) x >= a ? 1:0) for each component.
  • KikiAlex
    Thanks for the reference EricChadwick it was very useful :)
    Thanks CrazyButcher ,I can steal see the seam but it stopped flickering.
  • Ben Cloward
    Offline / Send Message
    Ben Cloward polycounter lvl 18
    Unfortunately, there really isn't very much available on the topic of shaders in 3ds Max. Almost everything is in that thread that Eric posted. I had to learn most of what I know by trial and error and by sending emails directly to Autodesk. The example shaders that ship with Max also help, but overall it's kinda frustrating how little information is available.
  • CrazyButcher
    Offline / Send Message
    CrazyButcher polycounter lvl 20
    I dont think it's 3dsmax to blame, but precision issues in the arithmetic operations.
    If opacity at the edges is important, make sure you give the texture a border of full transparency with a few pixels (2-4).
Sign In or Register to comment.