Matrix

 https://github.com/Unity-Technologies/Unity.Mathematics/blob/master/src/Unity.Mathematics/float2x2.gen.cs


https://en.wikibooks.org/wiki/Cg_Programming/Vector_and_Matrix_Operations

float3 RotateAroundYInDegrees(float3 vertex, float degrees)

{

float alpha = degrees * UNITY_PI / 180.0;

float sina, cosa;

sincos(alpha, sina, cosa);

float2x2 m = float2x2(cosa, -sina, sina, cosa);

return float3(mul(m, vertex.xz), vertex.y).xzy;

}

Component-Wise Functions[edit | edit source]

The following functions work component-wise for variables of type floatfloat2float3float4halfhalf2half3half4fixedfixed2fixed3, and fixed4, which are denoted as TYPE:

TYPE min(TYPE a, TYPE b) // returns a if a < b, b otherwise
TYPE max(TYPE a, TYPE b) // returns a if a > b, b otherwise
TYPE clamp(TYPE a, TYPE minVal, TYPE maxVal) 
   // = min(max(a, minVal), maxVal) 
TYPE lerp(TYPE a, TYPE b, TYPE wb) // = a * (TYPE(1.0, 1.0, ...) - wb) + b * wb
TYPE lerp(TYPE a, TYPE b, float wb) // = a * TYPE(1.0 - wb, 1.0 - wb, ...) + b * TYPE(wb, wb, ...)

There are more built-in functions, which also work component-wise but are less useful for vectors, e.g., abssignfloorceilroundfracfmodstepsmoothstepsqrtpowexpexp2loglog2radians (converts degrees to radians), degrees (converts radians to degrees), sincostanasinacosatanatan2.

Matrix Functions[edit | edit source]

For the usual matrix-matrix product in linear algebra, the mul function should be used. It computes components of the resulting matrix by multiplying a row (of the first matrix) with a column (of the second matrix), e.g.:

   

And in Cg:

float2x2 a = float2x2(1., 2.,  3., 4.);
float2x2 b = float2x2(10., 20.,  30., 40.);
float2x2 c = mul(a, b); // = float2x2(
   // 1. * 10. + 2. * 30., 1. * 20. + 2. * 40., 
   // 3. * 10. + 4. * 30., 3. * 20. + 4. * 40.)

Furthermore, the mul function can be used for matrix-vector products of the corresponding dimension, e.g.:

   

And in Cg:

float2 v = float2(10., 20.);
float2x2 m = float2x2(1., 2.,  3., 4.);
float2 w = mul(m, v); // = float2x2(1. * 10. + 2. * 20., 3. * 10. + 4. * 20.)

Note that the vector has to be the second argument to be multiplied to the matrix from the right.

If a vector is specified as first argument, it is multiplied to a matrix from the left:

float2 v = float2(10., 20.);
float2x2 m = float2x2(1., 2.,  3., 4.);
float2 w = mul(v, m); // = float(10. * 1. + 20. * 3., 10. * 2. + 20. * 4.)

Multiplying a row vector from the left to a matrix corresponds to multiplying a column vector to the transposed matrix from the right:

In components:

         

Thus, multiplying a vector from the left to a matrix corresponds to multiplying it from the right to the transposed matrix without explicitly computing the transposed matrix.

There is also a function transpose to compute the transposed matrix:

float2x2 m = float2x2(1., 2.,  3., 4.);
float2x2 n = transpose(m); // = float2x2(1., 3., 2., 4)

Geometric Functions[edit | edit source]

The following functions are particularly useful for vector operations. TYPE is any of: float, float2, float3float4halfhalf2half3half4fixedfixed2fixed3, and fixed4 (only one of them per line).

float3 cross(float3 a, float3 b) 
   // = float3(a[1] * b[2] - a[2] * b[1], 
   // a[2] * b[0] - a[0] * b[2], 
   // a[0] * b[1] - a[1] * b[0]) 
float dot(TYPE a, TYPE b) // = a[0] * b[0] + a[1] * b[1] + ... 
float length(TYPE a) // = sqrt(dot(a, a))
float distance(TYPE a, TYPE b) // = length(a - b)
TYPE normalize(TYPE a) // = a / length(a)
TYPE faceforward(TYPE n, TYPE i, TYPE nRef) 
   // returns n if dot(nRef, i) < 0, -n otherwise
TYPE reflect(TYPE i, TYPE n) // = i - 2. * dot(n, i) * n  
   // this computes the reflection of vector 'i' 
   // at a plane of normalized(!) normal vector 'n'

Functions for Physics[edit | edit source]

The function

TYPE refract(TYPE i, TYPE n, float r)

computes the direction of a refracted ray if i specifies the normalized(!) direction of the incoming ray and n specifies the normalized(!) normal vector of the interface of two optical media (e.g. air and water). The vector n should point to the side from where i is coming, i.e. the dot product of n and i should be negative. The floating-point number r is the ratio of the refractive index of the medium from where the ray comes to the refractive index of the medium on the other side of the surface. Thus, if a ray comes from air (refractive index about 1.0) and hits the surface of water (refractive index 1.33), then the ratio r is 1.0 / 1.33 = 0.75. The computation of the function is:

float d = 1.0 - r * r * (1.0 - dot(n, i) * dot(n, i));
if (d < 0.0) return TYPE(0.0, 0.0, ...); // total internal reflection
return r * i - (r * dot(n, i) + sqrt(d)) * n;

As the code shows, the function returns a vector of length 0 in the case of total internal reflection (see the entry in Wikipedia), i.e. if the ray does not pass the interface between the two materials.

댓글

이 블로그의 인기 게시물

About AActor!!! "UObject" has no member "BeginPlay"

UNREAL Android build information

Shader informations nice blog ~ ~