/// <summary> /// Calculates the UVs of a <see cref="Vertex"/> from the data in <paramref name="texInfo"/>, <paramref name="matrix"/> /// and <paramref name="texture"/> and returns a <see cref="Vertex"/> with the UVs set. /// </summary> /// <param name="vertex">The <see cref="Vertex"/> to calculate UVs for.</param> /// <param name="texInfo">The <see cref="TextureInfo"/> defining the UV axes for this <see cref="Vertex"/>.</param> /// <param name="dims">The width and height of the texture applied on this <see cref="Vertex"/>'s surface.</param> /// <returns>The passed <see cref="Vertex"/> with UVs calculated.</returns> public static Vertex CalcUV(this Vertex vertex, TextureInfo texInfo, Vector2 dims) { Matrix4x4 matrix = texInfo.BuildTexMatrix().inverse; Vector3 textureCoord = matrix.MultiplyPoint3x4(vertex.position); vertex.uv0 = new Vector2((texInfo.UAxis.sqrMagnitude * textureCoord.x + texInfo.Translation.x) / dims.x, (texInfo.VAxis.sqrMagnitude * textureCoord.y + texInfo.Translation.y) / dims.y); return(vertex); }
/// <summary> /// Calculates the UVs on all vertices in this <see cref="Mesh"/> given a <see cref="TextureInfo"/> /// containing U and V axis projections. /// </summary> /// <param name="mesh">This <see cref="Mesh"/>.</param> /// <param name="textureInfo"> /// A <see cref="TextureInfo"/> containing axes for projecting the texture onto the <see cref="Face"/> this /// <see cref="Mesh"/> was created from. /// </param> /// <param name="dims"> /// The width and height of the <see cref="Texture2D"/> bring projected onto the <see cref="Face"/> this /// <see cref="Mesh"/> was created from. /// </param> public static void CalculateUVs(this Mesh mesh, TextureInfo textureInfo, Vector2 dims) { Vector2[] uv = new Vector2[mesh.vertices.Length]; Matrix4x4 textureMatrixInverse = textureInfo.BuildTexMatrix().inverse; for (int i = 0; i < uv.Length; ++i) { Vector3 transformVertex = textureMatrixInverse.MultiplyPoint3x4(mesh.vertices[i]); Vector2 uv0 = textureInfo.CalculateUV(transformVertex, dims); uv[i] = uv0; } mesh.uv = uv; }