Home > other >  The mouse to drag the uv, joined the amount of rotation calculation
The mouse to drag the uv, joined the amount of rotation calculation

Time:09-19

If uv rotary , how to calculate?
If the object also rotate , how to calculate?

That is to say, need to compute the uv and the rotation of the object,

 using System. Collections. Generic; 
Using UnityEngine;
Using UnityEngine. EventSystems;

Public class NewBehaviourScript: MonoBehaviour IBeginDragHandler, IDragHandler
{
Private static int moveId;

[SerializeField]
Private float rotateSpeed=5;

Private Material Material;
Private Vector3 oldWorld;
Private Vector2 size;
Private Vector2 scale;
Private Vector3 newWorld;
Private Vector3 offset=Vector3. Zero;
Private Vector2 center=new Vector2 (0.5 f to 0.5 f);
Private MeshRenderer MeshRenderer;
Private Mesh Mesh;

Private void Awake ()
{
Mesh=GetComponent (). The mesh;
MeshRenderer=GetComponent (a);
Material=meshRenderer. Material;
Var boundsSize=meshRenderer. Bounds. Size;
Size=new Vector2 (boundsSize. X, boundsSize. Z);//0
//size=new Vector2 (boundsSize. X, boundsSize. Y);//x rotation - 90
//size=new Vector2 (boundsSize. Y, boundsSize. Z);//z rotation - 90
//size=new Vector2 (boundsSize. Y, boundsSize. Z);//z 90
//size=new Vector2 (boundsSize. X, boundsSize. Z);//x rotation - 180
RotateUV (rotateSpeed);
}

Void IBeginDragHandler. OnBeginDrag (PointerEventData eventData)
{
OldWorld=eventData. PointerCurrentRaycast. WorldPosition;
Scale=material. GetTextureScale (" _MainTex ");
MoveId=GetInstanceID ();
}
Void IDragHandler. OnDrag (PointerEventData eventData)
{
If (moveId!=GetInstanceID return ());
If (eventData pointerEnter==null) return;
NewWorld=eventData. PointerCurrentRaycast. WorldPosition;
Offset +=newWorld - oldWorld;

Material. SetTextureOffset (" _MainTex ", new Vector2 (offset.. * scale in x x/size x, offset.. z * scale in y./size, y));
//material. SetTextureOffset (" _MainTex ", new Vector2 (offset) x * scale) x/size x, offset.. * y scale. The y/size. The y));
//material. SetTextureOffset (" _MainTex ", new Vector2 (- offset. Y * scale. X/size x, offset.. z * scale in y./size, y));
//material. SetTextureOffset (" _MainTex ", new Vector2 (offset. Y * scale. X/size x, offset.. z * scale in y./size, y));
//material. SetTextureOffset (" _MainTex ", new Vector2 (offset) x * scale) x/size x, - offset.. z * scale in y./size, y));

OldWorld=newWorld;
}

///& lt; Summary>
///rotating uv
///& lt;/summary>
///& lt; Param name="Angle" & gt;
Public void RotateUV (float Angle)
{
Angle Angle of=* Mathf. Deg2Rad;
List
for (int i=0; i {
Vector2 uv=uvs [I] - center;
Uv=new Vector2 (uv) x * Mathf) Cos (Angle) - uv. Y * Mathf Sin (Angle),
Uv. X * Mathf. Sin (Angle) + uv. Y * Mathf Cos (Angle));
Uv +=center;
Uvs [I]=uv;
}
Mesh. SetUVs (0, uvs);
}
}
  • Related