示例#1
0
        /// <summary>
        /// Build a rotation matrix from the specified axis/angle rotation.
        /// </summary>
        /// <param name="axis">The axis to rotate about.</param>
        /// <param name="angle">Angle in radians to rotate counter-clockwise (looking in the direction of the given axis).</param>
        /// <param name="result">A matrix instance.</param>
        public static void CreateFromAxisAngle(SCNVector3 axis, pfloat angle, out SCNMatrix4 result)
        {
            pfloat cos = (float)System.Math.Cos(-angle);
            pfloat sin = (float)System.Math.Sin(-angle);
            pfloat t   = 1.0f - cos;

            axis.Normalize();

            result = new SCNMatrix4(t * axis.X * axis.X + cos, t * axis.X * axis.Y - sin * axis.Z, t * axis.X * axis.Z + sin * axis.Y, 0.0f,
                                    t * axis.X * axis.Y + sin * axis.Z, t * axis.Y * axis.Y + cos, t * axis.Y * axis.Z - sin * axis.X, 0.0f,
                                    t * axis.X * axis.Z - sin * axis.Y, t * axis.Y * axis.Z + sin * axis.X, t * axis.Z * axis.Z + cos, 0.0f,
                                    0, 0, 0, 1);
        }
示例#2
0
        /// <summary>
        /// Build a world space to camera space matrix
        /// </summary>
        /// <param name="eye">Eye (camera) position in world space</param>
        /// <param name="target">Target position in world space</param>
        /// <param name="up">Up vector in world space (should not be parallel to the camera direction, that is target - eye)</param>
        /// <returns>A SCNMatrix4 that transforms world space to camera space</returns>
        public static SCNMatrix4 LookAt(SCNVector3 eye, SCNVector3 target, SCNVector3 up)
        {
            SCNVector3 z = SCNVector3.Normalize(eye - target);
            SCNVector3 x = SCNVector3.Normalize(SCNVector3.Cross(up, z));
            SCNVector3 y = SCNVector3.Normalize(SCNVector3.Cross(z, x));

            SCNMatrix4 rot = new SCNMatrix4(new SCNVector4(x.X, y.X, z.X, 0.0f),
                                            new SCNVector4(x.Y, y.Y, z.Y, 0.0f),
                                            new SCNVector4(x.Z, y.Z, z.Z, 0.0f),
                                            SCNVector4.UnitW);

            SCNMatrix4 trans = SCNMatrix4.CreateTranslation(-eye);

            return(trans * rot);
        }
示例#3
0
        public static SCNQuaternion FromAxisAngle(SCNVector3 axis, float angle)
        {
            if (axis.LengthSquared == 0.0f)
            {
                return(Identity);
            }

            SCNQuaternion result = Identity;

            angle *= 0.5f;
            axis.Normalize();
            result.Xyz = axis * (float)System.Math.Sin(angle);
            result.W   = (float)System.Math.Cos(angle);

            return(Normalize(result));
        }