Unity Object Scale Rotation Drag Control Frame (VR supported)
Draw borders and control points
1. Obtain eight vertex coordinates
/// <summary> ///Return the corner coordinates in clockwise order from top to bottom /// </summary> public static void Get8CornerPositions(this Bounds bounds,Transform transform,ref Vector3[] allPoints) { if(allPoints == null || allPoints. Length < 8) { allPoints = new Vector3[8]; } Vector3 center = bounds.center; Vector3 extents = bounds.extents; float cxx = center.x; float cyy = center.y; float czz = center.z; float xx = extents.x; float yy = extents.y; float zz = extents.z; Float lead=cxx - xx;//The X coordinate of the left line Float redge=cxx+xx;//X coordinate of the right line float backedge = czz + zz; float frontedge = czz - zz; float topedge = cyy + yy; float bottomedge = cyy - yy; allPoints[0] = transform. TransformPoint(ledge, topedge, backedge); allPoints[1] = transform. TransformPoint(redge, topedge, backedge); allPoints[2] = transform. TransformPoint(redge, topedge, frontedge); allPoints[3] = transform. TransformPoint(ledge, topedge, frontedge); allPoints[4] = transform. TransformPoint(ledge, bottomedge, backedge); allPoints[5] = transform. TransformPoint(redge, bottomedge, backedge); allPoints[6] = transform. TransformPoint(redge, bottomedge, frontedge); allPoints[7] = transform. TransformPoint(ledge, bottomedge, frontedge); }
Bounds tt = new Bounds(m_collider.center, m_collider.size); tt.Get8CornerPositions(this.transform, ref allCornerPoints);
2. Obtain the coordinates of the rotation control point
3. Get the edge coordinates
4. Generate control points and lines
Control rotation
//Direction vector from handle to control frame center point Vector3 direct = Pos(a) - Pos(o); //Direct projection on the rotation plane Vector3 worldCtrlNormal = this.transform.TransformDirection(Normal(plane)); Vector3 k = Vector3.ProjectOnPlane(direct,worldCtrlNormal); //Find the angle (with direction) of two vectors on this rotation plane float angle = Vector3.SignedAngle(Pos(k) - Pos(o), k,worldCtrlNormal); //Rotate the specified angle if (Mathf. Abs(angle) > 0.01) { this.transform.rotation *= Quaternion.Euler(angle * Normal(plane)); }
Control zoom
Vector3 scaleDirect = A.position -B.position;
float v2 = Vector3.Dot(Pos(a) - A.position, Vector3.Normalize(scaleDirect));// Projection of handle on this axis Float scaleNumber=v2/Vector3. Distance (A.pos, B.pos);//The multiple that needs to be scaled again on the current basis
this.transform.localScale *= scaleNumber;
this.rigRoot.transform.localScale /= scaleNumber;
Compatible with VR or mouse