Pixel shader Texture problem

I try to access the texture coordinate in my Pixel Shader

My Input Structure read the Texture without problem

so this In.TextureUV is a correct float2 value

But I want to get the U and V coordinate so I can draw my own output color

Is there a way to access this U coordinate And how...

//--------------------------------------------------------------------------------------

// Pixel shader output structure

//--------------------------------------------------------------------------------------

struct PS_OUTPUT

{

float4 RGBColor : COLOR0; // Pixel color

}

;

//--------------------------------------------------------------------------------------

// This shader outputs the pixel's color by modulating the texture's

// color with diffuse material color

//--------------------------------------------------------------------------------------

PS_OUTPUT RenderScenePS( VS_OUTPUT In,

uniform bool bTexture )

{

PS_OUTPUT Output;

float Value1=0.5;

// THIS LINE DOES NOT WORK...
Value1=In.TextureUV.x;

Output.RGBColor = tex2D(MeshTextureSampler, In.TextureUV)*Value1;

return Output;

}



Answer this question

Pixel shader Texture problem

  • CodieMorgan

    Which pixel shader target are you compiling for

    For ps_1_1, ps_1_2, and ps_1_3, you can't use the same texture coordinate to sample a texture and as a value in the same pixel shader. For example:

    // This will work with ps1.1 to ps1.3
    PS_OUTPUT RenderScenePS( VS_OUTPUT In )
    {
    PS_OUTPUT Output;

    // texture coordinate used as a vector input
    Output.RGBColor = In.TextureUV.xxxx;

    return Output;
    }

    // This will work with ps1.1 to ps1.3
    PS_OUTPUT RenderScenePS( VS_OUTPUT In )
    {
    PS_OUTPUT Output;

    // texture coordinate used to sample a texture
    Output.RGBColor = tex2D(Sampler, In.TextureUV);

    return Output;
    }


    // This will NOT work with ps1.1 to ps1.3
    PS_OUTPUT RenderScenePS( VS_OUTPUT In )
    {
    PS_OUTPUT Output;

    // texture coordinate used to sample a texture AND as a vector value
    Output.RGBColor = tex2D(Sampler, In.TextureUV);
    Output.RGBColor = Output.RGBColor * In.TextureUV.xxxx;

    return Output;
    }

    If you need to use a texture coordinate to sample a texture and as a vector value in the same ps1.1-ps1.3 shader, you'll need to pass the texture coordinate in twice, i.e.:

    struct VS_OUTPUT
    {
    float4 Pos : POSITION;
    float2 TextureUV_UsedForSampling : TEXCOORD0;
    float3 TextureUV_UsedAsValue : TEXCOORD1;
    };

    Then write the same value to both texture coordinates in the vertex shader and use as appropriate in the pixel shader.

    Something else to bear in mind is that for ps1.1 to ps1.3 shaders, if you use a texture coordinate as a vector value, each of the elements (x,y,z,w) are clamped into the 0.0 to 1.0 range so if you need anything outside of that range you'll need to bias/scale as appropriate.



  • F0PS

    Thank you so much for your answer I will try your solution

    Currently I can draw an horizontal line or a vertical line with my code (on a Quad texture)

    But I have a hard time plotting a function like sin...

    In Render Monkey their is a effect that draw Electric effect on a Quad using Perlin Noise
    That's what I try to draw on my quad...eventually ;-)

    PS_OUTPUT RenderScenePS( VS_OUTPUT In,

    uniform bool bTexture )

    {

    PS_OUTPUT Output;

    PS_OUTPUT OutputX;

    PS_OUTPUT OutputY;

    PS_OUTPUT Output3;

    //Output.RGBColor=(float4)tex2D(MeshTextureSampler, In.TextureUV);

    //Output.RGBColor=float4(In.TextureUV.x,1.0f,1.0f,0);

    Output3.RGBColor=float4(In.TextureUV.x,0,0,0);

    float ValeurY=0.5f;

    OutputX.RGBColor=float4(In.TextureUV.x,0,0,0);

    OutputY.RGBColor=float4(In.TextureUV.y,0,0,0);

    if (OutputY.RGBColor[0]>=ValeurY){

    if (OutputY.RGBColor[0]<=(ValeurY+0.01f)){

    Output3.RGBColor[0]=1.0f;

    Output3.RGBColor[1]=0.0f;

    Output3.RGBColor[2]=1.0f;

    }

    }

    return Output3;

    }

    //--------------------------------------------------------------------------------------

    // Renders scene to render target

    //--------------------------------------------------------------------------------------

    technique RenderSceneWithTexture1Light

    {

    pass P0

    {

    VertexShader = compile vs_1_1 RenderSceneVS( 1, true, true );

    PixelShader = compile ps_2_0 RenderScenePS( true ); // trivial pixel shader (could use FF instead if desired)

    //PixelShader = compile ps_2_0 ps_main();

    }

    This line in RenderMonkey seems to work but not inside my effect (I will try your solution)

    float4 ps_main( float4 inDiffuse: COLOR0, float2 inTex: TEXCOORD0 ) : COLOR0
    {

    return tex2D( MeshTextureSampler, inTex )*inTex.y;
    //return tex2D( MeshTextureSampler, inTex );

    }

    }


  • GordonBJ

    Thanks to your advice I've made my Pixel Shader Work for multitexture

    I want to draw a sin wave on a quad, to plot v = sin(u)

    But when I try to use variable I cash
    In my pixel shader I only can do one ''if''
    In this shader both loop work alone...but when I try both I crash

    I removed the second if statement and all work OK so it's not what inside the second if that make the first if crash

    Is it because I use too much asm instruction

    Or too many register

    Can Effect compile shader 2_0

    I call compile 2_0 so I presume I'm in 2_0...but maybe not

    Any help is appreciated I can't figure out why I crash

    PS_OUTPUT RenderScenePS( VS_OUTPUT In,

    uniform bool bTexture )

    {

    PS_OUTPUT Output;

    PS_OUTPUT OutputX;

    PS_OUTPUT OutputY;

    PS_OUTPUT Output1;

    PS_OUTPUT Output2;

    PS_OUTPUT Output3;

    //Output.RGBColor=float4(In.TextureUV0.x,In.TextureUV0.y,0,0);

    float4 ColorA=float4(In.TextureUV0.x,In.TextureUV0.y,0,0);

    Output1.RGBColor=tex2D(MeshTextureSampler, In.TextureUV2);

    Output2.RGBColor=tex2D(MeshTextureSamplerPos, In.TextureUV3);

    //Both if statement work alone...but not together

    /*

    if (ColorA[0]>0.2f){

    Output1.RGBColor[0]=Output2.RGBColor[0];

    Output1.RGBColor[1]=Output2.RGBColor[1];

    Output1.RGBColor[2]=Output2.RGBColor[2];

    }

    */

    if (Output1.RGBColor.r>0.9f){

    Output1.RGBColor[0]=Output2.RGBColor[0];

    Output1.RGBColor[1]=Output2.RGBColor[1];

    Output1.RGBColor[2]=Output2.RGBColor[2];

    }

    return Output1;

    }

    VS_OUTPUT RenderSceneVS( float4 vPos : POSITION,

    float3 vNormal : NORMAL,

    float2 vTexCoord0 : TEXCOORD0,

    float2 vTexCoord1 : TEXCOORD1,

    float2 vTexCoord2 : TEXCOORD2,

    float2 vTexCoord3 : TEXCOORD3,

    uniform int nNumLights,

    uniform bool bTexture,

    uniform bool bAnimate )

    {

    VS_OUTPUT Output;

    float3 vNormalWorldSpace;

    float4 vAnimatedPos = vPos;

    // Animation the vertex based on time and the vertex's object space position

    //if( bAnimate )

    // vAnimatedPos += float4(vNormal, 0) * (sin(g_fTime+5.5)+0.5)*5+100.0;

    // Transform the position from object space to homogeneous projection space

    Output.Position = mul(vAnimatedPos, g_mWorldViewProjection);

    // Transform the normal from object space to world space

    vNormalWorldSpace = normalize(mul(vNormal, (float3x3)g_mWorld)); // normal (world space)

    // Compute simple directional lighting equation

    float3 vTotalLightDiffuse = float3(0,0,0);

    for(int i=0; i<nNumLights; i++ )

    vTotalLightDiffuse += g_LightDiffuseIdea * max(0,dot(vNormalWorldSpace, g_LightDirIdea));

    Output.Diffuse.rgb = g_MaterialDiffuseColor * vTotalLightDiffuse +

    g_MaterialAmbientColor * g_LightAmbient;

    Output.Diffuse.a = 1.0f;

    Output.TextureUV0 = vTexCoord0;

    Output.TextureUV1 = vTexCoord0;

    Output.TextureUV2 = vTexCoord0;

    Output.TextureUV3 = vTexCoord0;

    return Output;

    }

    struct VS_OUTPUT

    {

    float4 Position : POSITION; // vertex position

    float4 Diffuse : COLOR0; // vertex diffuse color

    float2 TextureUV0 : TEXCOORD0; // vertex texture coords

    float2 TextureUV1 : TEXCOORD1; // vertex texture coords

    float2 TextureUV2 : TEXCOORD2; // vertex texture coords

    float2 TextureUV3 : TEXCOORD3; // vertex texture coords

    };


  • Pixel shader Texture problem