public static Vector2[] PlanarProject(Vector3[] verts, Vector3 planeNormal) { if (verts.Length < 3) { Debug.LogWarning("Attempting to project UVs on a face with < 3 vertices. This is most often caused by removing or creating Geometry and Undo-ing without selecting a new face. Try deselecting this object then performing your edits."); return(new Vector2[verts.Length]); } Vector2[] uvs = new Vector2[verts.Length]; Vector3 vec = Vector3.zero; pb_UV.ProjectionAxis project = pb_Math.GetProjectionAxis(planeNormal); switch (project) { case pb_UV.ProjectionAxis.Planar_X: vec = Vector3.up; break; case pb_UV.ProjectionAxis.Planar_Y: vec = Vector3.forward; break; case pb_UV.ProjectionAxis.Planar_Y_Negative: vec = -Vector3.forward; break; case pb_UV.ProjectionAxis.Planar_Z: vec = Vector3.up; break; default: vec = Vector3.forward; break; } /** * Assign vertices to UV coordinates */ for (int i = 0; i < verts.Length; i++) { float u, v; Vector3 uAxis, vAxis; // get U axis uAxis = Vector3.Cross(planeNormal, vec); uAxis.Normalize(); // calculate V axis relative to U vAxis = Vector3.Cross(uAxis, planeNormal); vAxis.Normalize(); u = Vector3.Dot(uAxis, verts[i]); v = Vector3.Dot(vAxis, verts[i]); uvs[i] = new Vector2(u, v); } return(uvs); }
private void SetProjectionAxis(pb_UV.ProjectionAxis projectionAxis, pb_Object[] sel) { for (int i = 0; i < sel.Length; i++) { foreach (pb_Face q in sel[i].SelectedFaces) { q.uv.projectionAxis = projectionAxis; } sel[i].RefreshUV(SelectedFacesInEditZone[i]); } }
public static Vector3 ToVector3(this pb_UV.ProjectionAxis pa) { // Planar_X, // projects on x axis // Planar_Y, // projects on y axis // Planar_Z, // projects on z axis // Planar_Y_Negative switch (pa) { case pb_UV.ProjectionAxis.Planar_X: return(Vector3.right); case pb_UV.ProjectionAxis.Planar_Z: return(Vector3.forward); case pb_UV.ProjectionAxis.Planar_Y_Negative: return(-Vector3.up); default: // case Planar_Y: return(Vector3.up); } }