i try convert matrix to array before setvalue, but i got an index out of bound error. May i know what is the different by using matrix and float array?
var data = Matrix.ToFloatArray(worldMatrix);
_effect.Parameters[“World”].SetValue(data);
When setting float4x4 you can just send as a matrix, no need to convert to float array.
Also I’m not sure but I think if you declare it as float4x4 and don’t use all of it some of it may be optimized out… because I did an experiment and the following:
output.Position = mul(position, MatrixTransform);
is ok, but if I remove that line and only do:
output.Position.x = MatrixTransform[0];
I’m getting out of bound error too (on batch end).
And if I don’t use the matrix at all I get Object reference not set to an instance of an object., which is normal.
PS. you can also declare it like this matrix World;
Yes, you can see that error if any columns of your matrix have been optimized out.
For example I changed Normal = normalize(mul(input.Normal, World)); to the following because I never use Normal.w and so the 4th column of World was optimized away.
Normal = normalize(float3(dot(input.Normal, WorldCol1.xyz),
dot(input.Normal, WorldCol2.xyz),
dot(input.Normal, WorldCol3.xyz)));
To compensate, I now interact with the parameter like this:
public Matrix World
{
get
{
return Matrix.Transpose(
new Matrix(
worldCol1Param.GetValueVector4(),
worldCol2Param.GetValueVector4(),
worldCol3Param.GetValueVector4(),
Vector4.Zero));
}
set
{
worldCol1Param.SetValue(new Vector4(value.M11, value.M21, value.M31, value.M41));
worldCol2Param.SetValue(new Vector4(value.M12, value.M22, value.M32, value.M42));
worldCol3Param.SetValue(new Vector4(value.M13, value.M23, value.M33, value.M43));
}
}
Note there’s some transposing because of the way MG and HLSL treat matrices as row major and column major, respectively.