Experiment: A Reference Extractor for UV Tools



I made this fast experiment two months ago. At the time I was working on the first version of Curved Poly UV Operator (part of Curved Poly- Shape Control) and UV Unwrapping Tools (for now part of the alpha version of Curved Poly- Maker). I wanted to have an easy instrument to use to generate a set of reference images showing texture unwrapping. I wanted to use them to draw the textures with some painting tool (I'm not exactly an artist, but I have a pretty old version of Photoshop and I like Gimp). At the time I was still pretty much struggling with the bugged code of Curved Poly- Maker to create a good model for my dear Cody and Proofy and I wanted to experiment with their textures.

About MushroomsLabs Experiments

Mushrooms Labs experiments are fast development activities, made in 4-10 hours with a clear objective, usually involving the exploration of new technologies or of new tools for Curved Poly or for the Shadow Framework. I usually go straight forward to the goal, and, in the end, try to figure out what I did wrong. I think that if I can learn from my errors, it's worth to share them so that everybody can learn as well as me.

A simple Script to Map UV Coordinates on a Target Model

This was the most fast and simple solution I could think (I was pretty on a rush for this). Basically I wrote a script which maps uv coordinates from of a mesh to vertices coordinates on another mesh. It also maps normals on the color channel of the new mesh. After that: I can change material, with different shaders to get different properties on screen. In this way, to get what I was looking for, I only needed to do some screenshots and a few cuts on the images. Here is the script:


  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEditor;

  public class UVReferenceExtractor : MonoBehaviour
  {
      /*The input mesh with the uv-coordinates you want to map */
      public MeshFilter meshfilter;
      /*The output mesh which will receive new uv coordinates */
      public MeshFilter outputfilter;
  }

  /*This Custom Editor will add an Export button to the Defaul Inspector.*/
  [CustomEditor(typeof(UVReferenceExtractor))]
  public class UVReferenceExtractorEditor : Editor{

      /*This method convert a list of vertices in a list of colors. This is used
      to map to map colors as a new normals color. */
      private List VerticesToColors(List vertices) {
          List colors = new List();
          for (int i = 0; i < vertices.Count; i++) {
              float r = Mathf.Abs(vertices[i].x);
              float g = Mathf.Abs(vertices[i].y);
              float b = Mathf.Abs(vertices[i].z);
              colors.Add( new Color(r,g,b));
          }
          return colors;
      }

      public override void OnInspectorGUI()
      {
          DrawDefaultInspector();
          UVReferenceExtractor target = (UVReferenceExtractor)this.target;

          if (GUILayout.Button("Export")) {
              /*Take the input mesh*/
              Mesh mesh = target.meshfilter.mesh;

              /*Create the new mesh*/
              Mesh newMesh = new Mesh();

              /*Assign input mesh uv on newMesh vertices*/
              List uvs = new List();
              mesh.GetUVs(0, uvs);
              newMesh.SetVertices(uvs);

              /*Assign input mesh uv also on newMesh uvs*/
              newMesh.SetUVs(0,uvs);

              /*Assign input mesh normals on newMesh normals*/
              List normals = new List();
              mesh.GetNormals(normals);
              newMesh.SetNormals(normals);

              /*Assign input mesh normals on newMesh colors,
              after converting each normal into a color with the VerticesToColors method*/
              List vertices = new List();
              mesh.GetVertices(vertices);
              newMesh.SetColors(VerticesToColors(vertices));

              /*Rebuild all submeshes on newMesh. The input mesh
              may have more than one submesh because Curved Poly - Maker
              allows to split a model into more geometries which map to
              different submeshes*/
              for (int i = 0; i < mesh.subMeshCount; i++) {

                  List triangles = new List();
                  mesh.GetTriangles(triangles, i);
                  newMesh.SetTriangles(triangles, i);
              }

              /*Assign newMesh to the outputMeshFilter*/
              target.outputfilter.sharedMesh = newMesh;

          }
      }
  }

  

To apply it on a real case, it's neccessary to have two GameObjects. For the first, I used a Curved Poly, for the second the simplest thing to do is to create a new Sphere or Cube in the scene (the mesh is going to be overwritten). Here a little screenshot :

UV Reference Extractor Setup

The input Curved Poly is an unwrapped Cylinder. You can see the unwrap on the UV Panel, which is on the left of the model. On the right you can see the sphere. Then, it's necessary to setup an instance of the script with the two GameObjects and press the Export button:

UV Reference Extractor Execute

As you can see the sphere model has been replaced by a mesh which has the same shape as the unwrap in UV Panel.

In the end

In the end I combined some screenshots directly from the UV Panel, and added the ones from the exported mesh on different layers to have a complete reference. After some experiments with textures (did I told you I'm not exactly an artist?), I decided to completely change approach: as I do with Deep into the Details, I applied to the mushrooms the most simple textures possible, and I modelled every detail the best I could. So now you probably don't think this article was worth reading. If that is the case, here is a sample image of Cody, taken during the development of Curved Poly UV Tools: consider it an award for your patience.

Award for your patience.