Exemplo n.º 1
0
        /// <summary>
        /// 把连续9个float值按照列优先的顺序转换为mat3
        /// </summary>
        /// <param name="values"></param>
        /// <param name="startIndex"></param>
        /// <returns></returns>
        public static mat3 ToMat3(this float[] values, int startIndex = 0)
        {
            mat3 result = new mat3(
                values.ToVec3(startIndex + 0), values.ToVec3(startIndex + 3), values.ToVec3(startIndex + 6));

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Build a look at view matrix.
        /// transform object's coordinate from world's space to camera's space.
        /// </summary>
        /// <param name="eye">The eye.</param>
        /// <param name="center">The center.</param>
        /// <param name="up">Up.</param>
        /// <returns></returns>
        public static mat3 lookAt(vec2 eye, vec2 center, bool up)
        {
            // camera's back in world space coordinate system
            vec2 back = (eye - center).normalize();
            // camera's right in world space coordinate system
            vec2 right = up.cross(back).normalize();

            if (!up)
            {
                right = -right;
            }

            mat3 viewMatrix = new mat3(1);

            viewMatrix.col0.x = right.x;
            viewMatrix.col1.x = right.y;
            viewMatrix.col0.y = back.x;
            viewMatrix.col1.y = back.y;

            // Translation in world space coordinate system
            viewMatrix.col3.x = -eye.dot(right);
            viewMatrix.col3.y = -eye.dot(back);

            return(viewMatrix);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Applies a translation transformation to matrix <paramref name="m"/> by vector <paramref name="v"/>.
        /// </summary>
        /// <param name="m">The matrix to transform.</param>
        /// <param name="v">The vector to translate by.</param>
        /// <returns><paramref name="m"/> translated by <paramref name="v"/>.</returns>
        public static mat3 translate(mat3 m, vec2 v)
        {
            mat3 result = m;

            result.col2 = m.col0 * v.x + m.col1 * v.y + m.col2;
            return(result);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Applies a scale transformation to matrix <paramref name="m"/> by vector <paramref name="v"/>.
        /// </summary>
        /// <param name="m">The matrix to transform.</param>
        /// <param name="v">The vector to scale by.</param>
        /// <returns><paramref name="m"/> scaled by <paramref name="v"/>.</returns>
        public static mat3 scale(mat3 m, vec2 v)
        {
            mat3 result = m;

            result.col0 = m.col0 * v.x;
            result.col1 = m.col1 * v.y;
            result.col2 = m.col2;

            return(result);
        }
Exemplo n.º 5
0
        /// Builds a rotation 3 * 3 matrix created from an angle.
        /// </summary>
        /// <param name="m">The m.</param>
        /// <param name="angleDegree">ANgle in Degree.</param>
        /// <returns></returns>
        public static mat3 rotate(mat3 m, float angleDegree)
        {
            float c = (float)Math.Cos(angleDegree * Math.PI / 180.0);
            float s = (float)Math.Sin(angleDegree * Math.PI / 180.0);

            mat3 rotate = mat3.identity();

            rotate.col0 = new vec2(c, s);
            rotate.col1 = new vec2(-s, c);

            mat3 result = rotate * m;

            return(result);
        }
Exemplo n.º 6
0
        internal override bool SetValue(ValueType value)
        {
            if (value.GetType() != typeof(mat3))
            {
                throw new ArgumentException(string.Format("[{0}] not match [{1}]'s value.",
                                                          value.GetType().Name, this.GetType().Name));
            }

            var v = (mat3)value;

            if (v != this.value)
            {
                this.value   = v;
                this.Updated = true;
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Multiplies the <paramref name="lhs"/> matrix by the <paramref name="rhs"/> matrix.
        /// </summary>
        /// <param name="lhs">The LHS matrix.</param>
        /// <param name="rhs">The RHS matrix.</param>
        /// <returns>The product of <paramref name="lhs"/> and <paramref name="rhs"/>.</returns>
        public static mat3 operator *(mat3 lhs, mat3 rhs)
        {
            mat3 result = new mat3(
                new vec3(
                    lhs[0][0] * rhs[0][0] + lhs[1][0] * rhs[0][1] + lhs[2][0] * rhs[0][2],
                    lhs[0][1] * rhs[0][0] + lhs[1][1] * rhs[0][1] + lhs[2][1] * rhs[0][2],
                    lhs[0][2] * rhs[0][0] + lhs[1][2] * rhs[0][1] + lhs[2][2] * rhs[0][2]
                    ),
                new vec3(
                    lhs[0][0] * rhs[1][0] + lhs[1][0] * rhs[1][1] + lhs[2][0] * rhs[1][2],
                    lhs[0][1] * rhs[1][0] + lhs[1][1] * rhs[1][1] + lhs[2][1] * rhs[1][2],
                    lhs[0][2] * rhs[1][0] + lhs[1][2] * rhs[1][1] + lhs[2][2] * rhs[1][2]
                    ),
                new vec3(
                    lhs[0][0] * rhs[2][0] + lhs[1][0] * rhs[2][1] + lhs[2][0] * rhs[2][2],
                    lhs[0][1] * rhs[2][0] + lhs[1][1] * rhs[2][1] + lhs[2][1] * rhs[2][2],
                    lhs[0][2] * rhs[2][0] + lhs[1][2] * rhs[2][1] + lhs[2][2] * rhs[2][2]
                    )
                );

            return(result);
        }