public virtual void Calculate() { mesh = GetComponent <MeshFilter>().sharedMesh; int[] tris = mesh.triangles; Vector3[] verts = mesh.vertices; Matrix3x3 scaleMatrix = new Matrix3x3(); scaleMatrix.SetRow(0, new Vector3(transform.localScale.x, 0, 0)); scaleMatrix.SetRow(1, new Vector3(0, transform.localScale.y, 0)); scaleMatrix.SetRow(2, new Vector3(0, 0, transform.localScale.z)); for (int i = 0; i < tris.Length; i += 3) { Vector3 center = scaleMatrix.Transform(((verts[tris[i + 0]] + verts[tris[i + 1]] + verts[tris[i + 2]]) / 3)) + transform.position; Vector3 a = scaleMatrix.Transform(verts[tris[i + 0]] - verts[tris[i + 1]]); Vector3 b = scaleMatrix.Transform(verts[tris[i + 0]] - verts[tris[i + 2]]); Vector3 normal = Vector3.Cross(a, b).normalized; if (!localNormals.Contains(normal) && !localNormals.Contains(-normal)) { localNormals.Add(normal); //Debug.DrawLine(center, center + normal * 0.2f, Color.yellow, 3); } } }
// determines if the point p3 lies to the left of the line spanned by p1 and p2. public static bool LeftOf(Vector3 p1, Vector3 p2, Vector3 p3) { Matrix3x3 mat = Matrix3x3.zero; mat.SetRow(0, new Vector3(1, p1.x, p1.y)); mat.SetRow(1, new Vector3(1, p2.x, p2.y)); mat.SetRow(2, new Vector3(1, p3.x, p3.y)); return(mat.determinant > 0); }
public static Matrix3x3 RotationMatrix(float angle) { Matrix3x3 matrix = new Matrix3x3(); matrix.SetRow(0, new Vector3(Mathf.Cos(angle), -Mathf.Sin(angle), 0)); matrix.SetRow(1, new Vector3(Mathf.Sin(angle), Mathf.Cos(angle), 0)); matrix.SetRow(2, Vector3.forward); return(matrix); }
public static Matrix3x3 Scale(float sx, float sy) { Matrix3x3 matrix = new Matrix3x3(); matrix.SetRow(0, new Vector3(sx, 0.0f, 0.0f)); matrix.SetRow(1, new Vector3(0.0f, sy, 0.0f)); matrix.SetRow(2, new Vector3(0.0f, 0.0f, 1.0f)); return(matrix); }
public static Matrix3x3 Rotate(float angle) { Matrix3x3 matrix = new Matrix3x3(); matrix.SetRow(0, new Vector3(Mathf.Cos(angle), -Mathf.Sin(angle), 0.0f)); matrix.SetRow(1, new Vector3(Mathf.Sin(angle), Mathf.Cos(angle), 0.0f)); matrix.SetRow(2, new Vector3(0.0f, 0.0f, 1.0f)); return(matrix); }
// Creates a Scale Matrix. public Matrix3x3 ScaleMatrix(Vector3 scale) { Matrix3x3 m = new Matrix3x3(); m.SetRow(0, new Vector3(scale.x, 0.0f, 0.0f)); m.SetRow(1, new Vector3(0.0f, scale.y, 0.0f)); m.SetRow(2, new Vector3(0.0f, 0.0f, 1.0f)); return(m); }
// Creates a Translational Matrix. public Matrix3x3 TransMatrix(Vector3 offset) { Matrix3x3 m = new Matrix3x3(); m.SetRow(0, new Vector3(1.0f, 0.0f, offset.x)); m.SetRow(1, new Vector3(0.0f, 1.0f, offset.y)); m.SetRow(2, new Vector3(0.0f, 0.0f, 1.0f)); return(m); }
public static Matrix3x3 Scale3x3(Vector3 offset) { Matrix3x3 matrix = new Matrix3x3(); matrix.SetRow(0, new Vector3(offset.x, 0.0f, 0.0f)); matrix.SetRow(1, new Vector3(0.0f, offset.y, 0.0f)); matrix.SetRow(2, new Vector3(0.0f, 0.0f, 1.0f)); return(matrix); }
public static Matrix3x3 TranslationMatrix(Vector3 offset) { Matrix3x3 matrix = new Matrix3x3(); matrix.SetRow(0, new Vector3(1, 0, offset.x)); matrix.SetRow(1, new Vector3(0, 1, offset.y)); matrix.SetRow(2, Vector3.forward); return(matrix); }
public static Matrix3x3 Translate(float dx, float dy) { Matrix3x3 matrix = new Matrix3x3(); matrix.SetRow(0, new Vector3(1.0f, 0.0f, dx)); matrix.SetRow(1, new Vector3(0.0f, 1.0f, dy)); matrix.SetRow(2, new Vector3(0.0f, 0.0f, 1.0f)); return(matrix); }
// Creates a Rotation Matrix. public Matrix3x3 RotMatrix(float angle) { Matrix3x3 m = new Matrix3x3(); m.SetRow(0, new Vector3(Mathf.Cos(angle), -Mathf.Sin(angle), 0.0f)); m.SetRow(1, new Vector3(Mathf.Sin(angle), Mathf.Cos(angle), 0.0f)); m.SetRow(2, new Vector3(0.0f, 0.0f, 1.0f)); return(m); }
//Creates a matrix to scale the mesh Matrix3x3 ScaleMatrix(Vector2 scale) { Matrix3x3 scaleMatrix = new Matrix3x3(); scaleMatrix.SetRow(0, new Vector3(scale.x, 0, 0)); scaleMatrix.SetRow(1, new Vector3(0, scale.y, 0)); scaleMatrix.SetRow(2, new Vector3(0, 0, 1)); return(scaleMatrix); }
public static Matrix3x3 Translate(Vector3 offset) { Matrix3x3 matrix = new Matrix3x3(); matrix.SetRow(0, new Vector3(1.0f, 0.0f, offset.x)); matrix.SetRow(1, new Vector3(0.0f, 1.0f, offset.y)); matrix.SetRow(2, new Vector3(0.0f, 0.0f, 1.0f)); return(matrix); }
//Creates a matrix to rotate the mesh Matrix3x3 RotationMatrix(float angle) { Matrix3x3 angleMatrix = new Matrix3x3(); angleMatrix.SetRow(0, new Vector3(Mathf.Cos(angle * Mathf.PI / 180), -Mathf.Sin(angle * Mathf.PI / 180), 0)); angleMatrix.SetRow(1, new Vector3(Mathf.Sin(angle * Mathf.PI / 180), Mathf.Cos(angle * Mathf.PI / 180), 0)); angleMatrix.SetRow(2, new Vector3(0, 0, 1)); return(angleMatrix); }
//Creates a matrix to translate the mesh Matrix3x3 TranslationMatrix(Vector2 direction) { Matrix3x3 translateMatrix = new Matrix3x3(); translateMatrix.SetRow(0, new Vector3(1, 0, direction.x)); translateMatrix.SetRow(1, new Vector3(0, 1, direction.y)); translateMatrix.SetRow(2, new Vector3(0, 0, 1)); return(translateMatrix); }
/// <summary> /// Calculates the translation matrix to translate to the given point /// </summary> /// <returns>The translation matrix for the given point.</returns> /// <param name="dir">The point in which to translate to.</param> Matrix3x3 TranslationMatrix(Vector2 dir) { Matrix3x3 transM = new Matrix3x3(); // Create translation matrix transM.SetRow(0, new Vector3(1, 0, dir.x)); transM.SetRow(1, new Vector3(0, 1, dir.y)); transM.SetRow(2, new Vector3(0, 0, 1)); return(transM); }
/// <summary> /// Calculates the rotation matrix around the origin by the given angle /// </summary> /// <returns>The rotation matrix around the origin by the given angle.</returns> /// <param name="angle">The angle to rotate by in radians.</param> Matrix3x3 RotationMatrix(float angle) { Matrix3x3 rotM = new Matrix3x3(); // Create the rotation matrix rotM.SetRow(0, new Vector3(Mathf.Cos(angle), -Mathf.Sin(angle), 0)); rotM.SetRow(1, new Vector3(Mathf.Sin(angle), Mathf.Cos(angle), 0)); rotM.SetRow(2, new Vector3(0, 0, 1)); return(rotM); }
Matrix3x3 rotate(float angle) { Matrix3x3 matrix = new Matrix3x3(); // Set the rows of the matrix matrix.SetRow(0, new Vector3(Mathf.Cos(angle), -Mathf.Sin(angle), 0.0f)); matrix.SetRow(1, new Vector3(Mathf.Sin(angle), Mathf.Cos(angle), 0.0f)); matrix.SetRow(2, new Vector3(0.0f, 0.0f, 1.0f)); return(matrix); }
/// <summary> /// Calculates the scale matrix for the given directions /// </summary> /// <returns>The scale matrix for the given directions.</returns> /// <param name="scale">The vector in which to scale in the x, y and z directions.</param> Matrix3x3 ScaleMatrix(Vector3 scale) { Matrix3x3 scaleM = new Matrix3x3(); // Create the scale matrix scaleM.SetRow(0, new Vector3(scale.x, 0, 0)); scaleM.SetRow(1, new Vector3(0, scale.y, 0)); scaleM.SetRow(2, new Vector3(0, 0, scale.z)); return(scaleM); }
public static Matrix3x3 Translate(Vector3 offset) { // Create a new matrix Matrix3x3 matrix = new Matrix3x3(); // Set the rows of the matrix matrix.SetRow(0, new Vector3(1.0f, 0.0f, offset.x)); matrix.SetRow(1, new Vector3(0.0f, 1.0f, offset.y)); matrix.SetRow(2, new Vector3(0.0f, 0.0f, 1.0f)); // Return the matrix return(matrix); }
Matrix3x3 move(float increment) { Matrix3x3 matrix = new Matrix3x3(); matrix.SetRow(0, new Vector3(mesh.vertices[0].x + increment, 1.0f, 0.0f)); matrix.SetRow(1, new Vector3(mesh.vertices[1].x + increment, 1.0f, 0.0f)); matrix.SetRow(2, new Vector3(1.0f, 1.0f, 1.0f)); print(mesh.vertices[0]); print(mesh.vertices[1]); print(mesh.vertices[2]); return(matrix); }
public override void Calculate() { base.Calculate(); type = colliderType.Box; float xx = transform.lossyScale.x * transform.lossyScale.x; float yy = transform.lossyScale.y * transform.lossyScale.y; float zz = transform.lossyScale.z * transform.lossyScale.z; localInertiaTensor = new Matrix3x3(); localInertiaTensor.SetRow(0, new Vector3(mass * yy * zz / 12f, 0, 0)); localInertiaTensor.SetRow(1, new Vector3(0, mass * xx * zz / 12f, 0)); localInertiaTensor.SetRow(2, new Vector3(0, 0, mass * xx * yy / 12f)); }
public static Matrix3x3 Rotate(float angle) { // Create a new matrix Matrix3x3 matrix = new Matrix3x3(); // Set the rows of the matrix matrix.SetRow(0, new Vector3(Mathf.Cos(angle), -Mathf.Sin(angle), 0.0f)); matrix.SetRow(1, new Vector3(Mathf.Sin(angle), Mathf.Cos(angle), 0.0f)); matrix.SetRow(2, new Vector3(0.0f, 0.0f, 1.0f)); // Return the matrix return(matrix); }
public static Vector3[] GenerateSmoothNormals(Mesh _srcMesh, bool _convertToTangentSpace) { Vector3[] verticies = _srcMesh.vertices; var groups = verticies.Select((vertex, index) => new KeyValuePair <Vector3, int>(vertex, index)).GroupBy(pair => pair.Key); Vector3[] normals = RenegerateNormals(_srcMesh.triangles, verticies); Vector3[] smoothNormals = normals.Copy(); foreach (var group in groups) { if (group.Count() == 1) { continue; } Vector3 smoothNormal = Vector3.zero; foreach (var index in group) { smoothNormal += normals[index.Value]; } smoothNormal = smoothNormal.normalized; foreach (var index in group) { smoothNormals[index.Value] = smoothNormal; } } if (_convertToTangentSpace) { Vector4[] tangents = _srcMesh.tangents; for (int i = 0; i < smoothNormals.Length; i++) { Vector3 tangent = tangents[i].ToVector3().normalized; Vector3 normal = normals[i].normalized; Vector3 biNormal = Vector3.Cross(normal, tangent).normalized *tangents[i].w; Matrix3x3 tbnMatrix = Matrix3x3.identity; tbnMatrix.SetRow(0, tangent); tbnMatrix.SetRow(1, biNormal); tbnMatrix.SetRow(2, normal); smoothNormals[i] = tbnMatrix * smoothNormals[i].normalized; } } return(smoothNormals); }
// Operators // Multiply two matrices together public static Matrix3x3 operator*(Matrix3x3 b, Matrix3x3 c) { // New matrix3x3 to store the values Matrix3x3 newMatrix = new Matrix3x3(); // For each row in b, multiply by c and find new row for (int i = 0; i < matrixOrder; i++) { Vector3 r = new Vector3(b[i, 0] * c[0, 0] + b[i, 1] * c[1, 0] + b[i, 2] * c[2, 0], b[i, 0] * c[0, 1] + b[i, 1] * c[1, 1] + b[i, 2] * c[2, 1], b[i, 0] * c[0, 2] + b[i, 1] * c[1, 2] + b[i, 2] * c[2, 2]); newMatrix.SetRow(i, r); } return(newMatrix); }
// Operators // Multiply two matrices together public static Matrix3x3 operator *(Matrix3x3 b, Matrix3x3 c) { Matrix3x3 newMatrix = new Matrix3x3(); for (int i = 0; i < matrixOrder; i++) { Vector3 r = new Vector3( b[i, 0] * c[0, 0] + b[i, 1] * c[1, 0] + b[i, 2] * c[2, 0], b[i, 0] * c[0, 1] + b[i, 1] * c[1, 1] + b[i, 2] * c[2, 1], b[i, 0] * c[0, 2] + b[i, 1] * c[1, 2] + b[i, 2] * c[2, 2] ); newMatrix.SetRow(i, r); } return(newMatrix); }
// Operators // Multiply two matrices together public static Matrix3x3 operator*(Matrix3x3 b, Matrix3x3 c) { // -- Your Code here -- Matrix3x3 d = new Matrix3x3(); for (int i = 0; i < matrixOrder; i++) { Vector3 r = new Vector3( b [i, 0] * c [0, 0] + b [i, 1] * c [1, 0] + b [i, 2] * c [2, 0], b [i, 0] * c [0, 1] + b [i, 1] * c [1, 1] + b [i, 2] * c [2, 1], b [i, 0] * c [0, 2] + b [i, 1] * c [1, 2] + b [i, 2] * c [2, 2] ); d.SetRow(i, r); } return(d); }