Matrix
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 float, float2, float3, float4, half, half2, half3, half4, fixed, fixed2, fixed3, and fixed4, which are denoted as TYPE:
There are more built-in functions, which also work component-wise but are less useful for vectors, e.g., abs, sign, floor, ceil, round, frac, fmod, step, smoothstep, sqrt, pow, exp, exp2, log, log2, radians (converts degrees to radians), degrees (converts radians to degrees), sin, cos, tan, asin, acos, atan, atan2.
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:
Furthermore, the mul function can be used for matrix-vector products of the corresponding dimension, e.g.:
And in Cg:
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:
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:
Geometric Functions[edit | edit source]
The following functions are particularly useful for vector operations. TYPE is any of: float, float2, float3, float4, half, half2, half3, half4, fixed, fixed2, fixed3, and fixed4 (only one of them per line).
Functions for Physics[edit | edit source]
The function
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:
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.
댓글
댓글 쓰기