Unit vector in an effect file

Started by
8 comments, last by Bacterius 11 years, 10 months ago
Is there any way to insert a unit vector into an effect file either by declaring it within the effect file or inputting it there somehow?
Advertisement
you can use the "normalize()" function in hlsl to make a vector unit length, or you can create the vector in your app and send it to the shaders using a constant buffer.

in hlsl here are a couple unit vectors for you

float3 unitVector1 = float3(1,0,0); // unit vector

float3 unitVector2 = float3(10, 2, 7); // Not unit vector yet

unitVector = normalize(unitVector); // Now it's a unit length vector
How do I perform dot products in HLSL?
It's just dot(vector1, vector2);

The MSDN has more information.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”


you can use the "normalize()" function in hlsl to make a vector unit length, or you can create the vector in your app and send it to the shaders using a constant buffer.

in hlsl here are a couple unit vectors for you

float3 unitVector1 = float3(1,0,0); // unit vector

float3 unitVector2 = float3(10, 2, 7); // Not unit vector yet

unitVector = normalize(unitVector); // Now it's a unit length vector


Unfortunately, when I try to normalize my vector in the effect file like so...


float3 udir = float3 (1, 0, 1);
udir = normalize(udir);


... it says "unrecognized identifier udir". Why won't it recognize my vector?
Can you show your entire shader? As far as I can tell these two lines are valid (out of context, anyway). There must be something else in the shader code that's preventing this to work.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”


Can you show your entire shader? As far as I can tell these two lines are valid (out of context, anyway). There must be something else in the shader code that's preventing this to work.



uniform extern float4x4 gWVP;
uniform extern float gTime;
struct OutputVS
{
float4 posH : POSITION0;
float4 color : COLOR0;
};
float3 udir = float3 (1, 0, 1);
udir = normalize(udir);
static float a[2] = {0.8f, 0.2f};
static float k[2] = {1.0, 8.0f};

static float w[2] = {1.0f, 8.0f};

static float p[2] = {0.0f, 1.0f};
float SumOfRadialSineWaves(float x, float z)
{
float3 p = float3(x, 0, z);
float my_x = dot(udir, p);

float sum = 0.0f;
for(int i = 0; i < 2; ++i)
sum += a*sin(k*my_x - gTime*w + p);

return sum;
}
float4 GetColorFromHeight(float y)
{
if( abs(y) <= 0.2f ) // black
return float4(0.0f, 0.0f, 0.0f, 1.0f);
else if(abs(y) <= 0.5f ) // blue
return float4(0.0f, 0.0f, 1.0f, 1.0f);
else if(abs(y) <= 0.8f ) // green
return float4(0.0f, 1.0f, 0.0f, 1.0f);
else if(abs(y) <= 1.0f ) // red
return float4(1.0f, 0.0f, 0.0f, 1.0f);
else // yellow
return float4(1.0f, 1.0f, 0.0f, 1.0f);
}
OutputVS ColorVS(float3 posL : POSITION0)
{
OutputVS outVS = (OutputVS)0;
posL.y = SumOfRadialSineWaves(posL.x, posL.z);

outVS.color = GetColorFromHeight(posL.y);

outVS.posH = mul(float4(posL, 1.0f), gWVP);

return outVS;
}
float4 ColorPS(float4 c : COLOR0) : COLOR
{
return c;
}
technique HeightColorTech
{
pass P0
{
vertexShader = compile vs_2_0 ColorVS();
pixelShader = compile ps_2_0 ColorPS();
FillMode = WireFrame;

}
}
you cannot perform instruction operations in declaration section of shader:

float3 udir = float3 (1, 0, 1);
udir = normalize(udir);
static float a[2] = {0.8f, 0.2f};

[/quote]
I tried to put it in the SumOfRadialSineWaves function, but it still wouldn't work.
Does it still say the same thing? Can you post the new shader with the udir code inside the SumOfRadianSineWaves function?

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

This topic is closed to new replies.

Advertisement