Shader question: Get Tex Coords of onscreen object

Started by
3 comments, last by danromeo 12 years, 1 month ago
shader 3.0

In my vertex shader I pass a Vector3 position transformed by View*Projection:

Output.ObjectPosition=mul(ObjectPosition, camViewProjection);


In my pixel shader, how can I get the texture coordinates of the passed Vector3 ObjectPosition??

So that I can determine if the pixel being drawn is within a distance range of ObjectPosition.
Advertisement
Your vector3 position is part of a vertex description, which I am going to assume you also include a UV coordinate to texture your objects(which you pass to your pixel shader as an "out" parameter of your vertex shader). Could you clarify a little bit exactly what you are having an issue with?
Thanks for your reply. I'm trying to project a moon image onto a skydome at the proper location. So the shader is drawing the dome geometry with texcoords for the dome as part of the vertex declaration, and MoonPosition is being passed to the shader from the program, not in the vertex declaration.

In the pixel shader I want to do: If texcoord is within a range of MoonPosition, draw the moon.

I'm not a wiz at this stuff, still trying to wrap my head around it.
It sounds like you may be confusing the term "texcoord", which is a UV coordinate, with a vertex position. If this is correct, and I am understanding correctly, all you need to do to achieve what you want, is pass the vector position from the vShader into the pShader using the TEXCOORDn semantic, then simply do a distance check(using your formula of choice). root(pow(x2-x1,2) + pow(y2-y1, 2) + pow(z2-z1, 2))
I know the diff between a UV and a vertex position.

The problem (I THINK) is mapping the texcoords for the moon image. The distance equation will tell me if the pixel being drawn is near the moon position, but how do I map the texcoords for the moon image into that space?

What if I use this for starter code, somebody elses, not mine: This puts the moon image in the middle of my skydome. How do I align the output with my MoonPosition? If I could get the texcoord that the MoonPosition vector is falling at it would be easy. THANKS for your help.

if (PSIn.TexCoords.x > 0.49f && PSIn.TexCoords.x < 0.51f)
{
if (PSIn.TexCoords.y > 0.49f && PSIn.TexCoords.y < 0.51f)
{
float leftSpan = 0.51f - 0.49f;
float rightSpan = 0.51f - 0.49f;

float XvalueScaled = (PSIn.TexCoords.x - 0.49f) / leftSpan;
float YvalueScaled = (PSIn.TexCoords.y - 0.49f) / leftSpan;

float2 MoonCoords = float2(XvalueScaled, YvalueScaled);

Output.Color = tex2D(MoonSampler, MoonCoords);
}
}

This topic is closed to new replies.

Advertisement