Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Probuilder Scripting, Autounwrap several faces at one

Discussion in 'World Building' started by nachocgi, Sep 25, 2019.

  1. nachocgi

    nachocgi

    Joined:
    Mar 13, 2018
    Posts:
    18
    hi again guys,
    instead of apply the AutoUnwrapSetting face by face
    Code (CSharp):
    1. foreach ( var face in mesh.faces )...
    could it be possible to select a group of faces and AutoWrap it at once? (it would look like a planar projection...or simply Unwrap that part of the mesh as one UV island)

    this would be very useful in simple modifications,
    for example, an object that enlarges in one Axis (like an Scale in one Axis but moving vertex instead), with the texture set on Tiled, so the texture would not deform, but would keep the correct aspect

    is this possible in Probuilder API?
    thanks!!!!
     
    Last edited: Sep 25, 2019
  2. nachocgi

    nachocgi

    Joined:
    Mar 13, 2018
    Posts:
    18
    to clarify what Im triyng to achieve, I made this montage:


    the AutoUnwrap per faces is ok on Objects like Planes or Boxes, but how can be this achieve in a mesh like the picture?

    Dont know if this is possible in Unity without Probuilder, or if is only avaible via Probuilder, Im open to any advice because simply Im searching a way to do it, no matter what.

    thanks guys!!
     
  3. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    552
  4. nachocgi

    nachocgi

    Joined:
    Mar 13, 2018
    Posts:
    18
  5. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    552
    Try this out. I haven't tested it, but this is the gist of what you'll need to do.

    Code (CSharp):
    1. void ProjectFaces()
    2. {
    3.     var mesh = MeshSelection.activeMesh;
    4.     var faces = mesh.GetSelectedFaces();
    5.     List<int> indices = new List<int>();
    6.     // Faces can reference the same vertex more than once. Use this function to get a distinct list of vertex
    7.     // indices to work with.
    8.     Face.GetDistinctIndices(faces, indices);
    9.     // Project only the vertex positions that we care about by passing the indices parameter. The third argument
    10.     // determines from what plane positions will be projected.
    11.     var projected = Projection.PlanarProject(mesh.positions, indices, -Vector3.forward);
    12.     // Get a copy of the textures array, assign the new projected values, then re-apply to the mesh.
    13.     var uvs = mesh.textures;
    14.     for (int i = 0, c = indices.Count; i < c; ++i)
    15.         uvs[indices[i]] = projected[i];
    16.     mesh.textures = uvs;
    17.     // If you want your changes to UV array to "stick", the faces need to be flagged as manually unwrapped.
    18.     // Otherwise the face UVs will be recalculated the next time `ProBuilderMesh.Refresh` is invoked.
    19.     foreach (var face in faces)
    20.         face.manualUV = true;
    21. }
     
  6. nachocgi

    nachocgi

    Joined:
    Mar 13, 2018
    Posts:
    18
    Many many thanks kaarrrlll, you are great!
     
  7. Lahiter

    Lahiter

    Joined:
    Mar 1, 2018
    Posts:
    14
    Hi, I'm having a problem with probuilder and I think is related to the topic of this thread.

    I'm building a mesh with multiple materials. The problem is I can't get rid of the seams in the corners of this frame.

    Untitled-2.png
    Something similar happens in the back of the image.

    Untitled-3.png

    I sort of fixed it by making the fill of the uvs of type Tile, but I was wondering if its possible to get rid of the seams using the fill type of Fit.

    Thank you.