Question

For a HLSL shader I'm working on (for practice) I'm trying to execute a part of the code if the texture coordinates (on a model) are above half the respective size (that is x > width / 2 or y > height / 2). I'm familiar with C/C++ and know the basics of HLSL (the very basics). If no other solution is possible, I will set the texture size manually with XNA (in which I'm using the shader, as a matter of fact). Is there a better solution? I'm trying to remain within Shader Model 2.0 if possible.

Was it helpful?

Solution

The default texture coordinate space is normalized to 0..1 so x > width / 2 should simply be texcoord.x > 0.5.

OTHER TIPS

Be careful here. tex2d() and other texture calls should NOT be within if()/else clauses. So if you have a pixel shader input "IN.UV" and your aiming at "OUT.color," you need to do it this way:

float4 aboveCol = tex2d(mySampler,some_texcoords);
float4 belowCol = tex2d(mySampler,some_other_texcoords);
if (UV.x >= 0.5) {
   OUT.color = /* some function of... */ aboveCol;
} else {
   OUT.color = /* some function of... */ belowCol;
}

rather than putting teh tex() calls inside the if() blocks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top