Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Skewing or Shearing a Mesh/GameObject

Discussion in 'Scripting' started by Zel, Jan 13, 2010.

  1. Zel

    Zel

    Joined:
    Jan 3, 2009
    Posts:
    6
    Hello everyone,

    I'd like to skew/shear a mesh in a parallel direction (any axis). I'v tried it several ways, but none of the methods worked. I've also played around with the matrix4x4 but I could not figure out how to apply it to a mesh. So I turned to manually scaling and rotationg the mesh to get the desired effect.

    Could someone please help me with this. I've spent weeks trying to figure it out and had little luck.

    Thank you all for any and all help.
     
  2. Zel

    Zel

    Joined:
    Jan 3, 2009
    Posts:
    6
    Seriously? No one knows how to parallel skew a Gameobject or Mesh in Unity?
     
  3. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    There isn't a direct way to do this with the tranform properties, but you can apply shearing to the mesh using the Mesh API.

    Shearing basically means offsetting the coordinates along one or two axes based on the distance along the remaining axis. Say you wanted to displace vertices in the XZ plane depending on the Y coordinate. You would define a vector with only X and Z components:-
    Code (csharp):
    1. var shear: Vector3 = new Vector3(1.0, 0, 1.0);
    Then, you would loop through all the vertices, getting the Y coordinate, multiplying that by the shear value and then adding that offset back to the original vertex:-
    Code (csharp):
    1. for (i = 0; i < verts.Length; i++) {
    2.     verts[i] = verts[i] + shear * verts[i].y;
    3. }
    You can also do this and many other transformations using matrices (basically set up the matrix and then multiply it by each vertex in turn).
     
  4. JonnyHilly

    JonnyHilly

    Joined:
    Sep 4, 2009
    Posts:
    707
    resurrecting this ancient thread...
    I too would like to do this, and it seems like a standard standard matrix operation that would be pretty useful.
    anyone else managed to do this legit ?

    I think I may have somehow accidentally gotten the same effect a year or so ago, by scaling objects non-uniformly plus rotating them, then unity got confused, and ended up giving me some deformed geometry that looked a lot like it was sheared. Had to re-import models to fix it.

    anyone ?
     
  5. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,471
    You basically get a direction vector and alter the vertex positions according to this direction vector while preserving certain ones in place to get a skewing effect.
    I cant paste any code because i just submitted a new pack to the assetstore which includes this functionality as a commercial product. But i think you get the idea.
     
  6. JonnyHilly

    JonnyHilly

    Joined:
    Sep 4, 2009
    Posts:
    707
    Thanks,
    yeah I wrote code already to do this one vertex at a time. but I was trying to do it just using a matrix (almost for free), but its not possible to write to transform.matrix4x4, as its read only.

    However someone on another thread mentioned it could maybe de also done with a vertex shader.
     
  7. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    503
    I know I'm here a bit late, but how would I change the code to make the skew vector changeable at runtime? Obviously, the skew will keep adding to itself as is. Here's my current code:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [RequireComponent(typeof(MeshFilter))]
    6. [AddComponentMenu("Unity Tools/Skew")]
    7. public class Skew : MonoBehaviour
    8. {
    9.     //--Variables--
    10.         //-Public-
    11.             //Parameters
    12.     public Vector3 skewDirection;
    13.     public Vector3 skewPosition;
    14.         //-Private-
    15.             //References
    16.     Mesh mesh;
    17.     Vector3[] startVertices;
    18.     Vector3[] vertices;
    19.  
    20.     //--Methods--
    21.     void Awake()
    22.     {
    23.         mesh = GetComponent<MeshFilter>().mesh;
    24.         startVertices = mesh.vertices;
    25.         vertices = startVertices;
    26.     }
    27.     void Update()
    28.     {
    29.         mesh.vertices = startVertices;
    30.         for(int i = 0; i < vertices.Length; i++)
    31.         {
    32.             vertices[i] = startVertices[i] + (skewDirection * startVertices[i].y);
    33.         }
    34.         mesh.vertices = vertices;
    35.         mesh.RecalculateBounds();
    36.         mesh.RecalculateNormals();
    37.         vertices = startVertices;
    38.     }
    39. }
     
    benzsuankularb likes this.
  8. Andrey-Postelzhuk

    Andrey-Postelzhuk

    Joined:
    Nov 26, 2013
    Posts:
    65
  9. mediajolt

    mediajolt

    Joined:
    Nov 1, 2012
    Posts:
    8
    Did you ever release this product on the Asset Store? If so, what is it called? Thanks!