• Create Account

# Unit vector in an effect file

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

9 replies to this topic

### #1Hseptic  Members   -  Reputation: 146

Like
0Likes
Like

Posted 25 May 2012 - 07:47 PM

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?

### #2iedoc  Members   -  Reputation: 2033

Like
0Likes
Like

Posted 26 May 2012 - 04:47 AM

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

Edited by iedoc, 26 May 2012 - 04:48 AM.

Braynzar Soft - DirectX Lessons & Game Programming Resources!

### #3Hseptic  Members   -  Reputation: 146

Like
0Likes
Like

Posted 26 May 2012 - 05:49 AM

How do I perform dot products in HLSL?

### #4Bacterius  Crossbones+   -  Reputation: 13031

Like
0Likes
Like

Posted 26 May 2012 - 06:19 AM

It's just dot(vector1, vector2);

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

### #5Hseptic  Members   -  Reputation: 146

Like
0Likes
Like

Posted 26 May 2012 - 07:15 AM

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?

### #6Bacterius  Crossbones+   -  Reputation: 13031

Like
0Likes
Like

Posted 26 May 2012 - 07:25 AM

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.”

### #7Hseptic  Members   -  Reputation: 146

Like
0Likes
Like

Posted 26 May 2012 - 08:07 AM

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};
{
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[i]*sin(k[i]*my_x - gTime*w[i] + p[i]);

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;

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
{
FillMode = WireFrame;

}
}


Edited by Hseptic, 26 May 2012 - 08:09 AM.

### #8JohnnyCode  Members   -  Reputation: 1017

Like
0Likes
Like

Posted 26 May 2012 - 09:21 AM

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};

### #9Hseptic  Members   -  Reputation: 146

Like
0Likes
Like

Posted 26 May 2012 - 09:40 AM

I tried to put it in the SumOfRadialSineWaves function, but it still wouldn't work.

### #10Bacterius  Crossbones+   -  Reputation: 13031

Like
0Likes
Like

Posted 26 May 2012 - 07:06 PM

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.”

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS