//based on the function above, this function cacluates parameteres of a new global transfrom from a moved target static public NcTransform GetNewGlobalTransformData(NcTransform trModel, NcTransform trTarget, NcTransform trMovedTarget) { ////// position calculation //the local positions of the model in both original and moved target transfrom are the same. Vector3 localPos_in_originalTarget = TranslateGlobalPosToLocal(trModel, trTarget); //print("translatedLocal : " + localPos_in_originalTarget.ToString("F6")); // now we translate the calculated local position in the moved target space to global // This is equivalent to " trMovedTarget.transform.TransformPoint ( localPosInTarget )" // ?? operator is needed because local scale of the moved target may not have a local scale if the NcTransform instance was not initiated from Transfrom class. // However, it will have a value because the we will make the instance from a transform of the moved target. Vector3 newGlobalPos = trMovedTarget.rotation * Vector3.Scale(localPos_in_originalTarget, trMovedTarget.localScale ?? default) + trMovedTarget.position; //print("newGlobalPos : " + newGlobalPos.ToString("F6")); ////// rotation calculation //this translates the global rotation of the model to the local rotation in the target space. Quaternion localRot_in_originalTarget = Quaternion.Inverse(trTarget.rotation) * trModel.rotation; //this translates the local rotation of model in the *MOVED* target space to a global rotation. Quaternion newGlobalRot = trMovedTarget.rotation * localRot_in_originalTarget; ////// scale calculation //This calcualtes the local scale of the model in the original target space Vector3 newLocalScale = new Vector3(trTarget.lossyScale.x / trModel.lossyScale.x, trTarget.lossyScale.y / trModel.lossyScale.y, trTarget.lossyScale.z / trModel.lossyScale.z); //this caclulates the global scale of the model in the moved target space Vector3 newGlobalScale = new Vector3(trMovedTarget.lossyScale.x / newLocalScale.x, trMovedTarget.lossyScale.y / newLocalScale.y, trMovedTarget.lossyScale.z / newLocalScale.z); return(new NcTransform(newGlobalPos, newGlobalRot, newGlobalScale)); }
public static void TestMovetransform() { EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene()); foreach (GameObject go in Selection.gameObjects) { //Debug.Log(Selection.gameObjects.Length + ":" + go.name); } modeltr = GetModelByNameFromSelection("model"); Transform targettr = GetModelByNameFromSelection("target"); Transform movedTargettr = GetModelByNameFromSelection("movedTarget"); NcTransform newGlobalTransform = GetNewGlobalTransformData2(modeltr, targettr, movedTargettr); Undo.RecordObject(modeltr.gameObject.transform, "-moved"); modeltr.position = newGlobalTransform.position; EditorUtility.SetDirty(modeltr.gameObject.transform); Undo.RecordObject(modeltr.gameObject.transform, "-rotated"); modeltr.rotation = newGlobalTransform.rotation; EditorUtility.SetDirty(modeltr.gameObject.transform); Undo.RecordObject(modeltr.gameObject.transform, "-scaled"); modeltr.localScale = newGlobalTransform.lossyScale; EditorUtility.SetDirty(modeltr.gameObject.transform); //Undo.RecordObject(modeltr.gameObject, "4"); //modeltr.SetParent(movedTargettr); //EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene()); //GetNewGlobalTransformData2(modeltr, targettr, movedTargettr); }
public NcTransform(NcTransform tr) { name = tr.name; position = tr.position; rotation = tr.rotation; lossyScale = tr.lossyScale; //lossyScale = Vector3.one; localPosition = tr.localPosition; localRotation = tr.localRotation; localScale = tr.localScale; //localScale = Vector3.one; }
//this function translate the global position of a model to a local position in the target space. Technically equivalent to InverseTransfromPoint. //reference:: https://answers.unity.com/questions/186252/multiply-quaternion-by-vector.html //reference:: https://answers.unity.com/questions/601062/what-inversetransformpoint-does-need-explanation-p.html static public Vector3 TranslateGlobalPosToLocal(NcTransform model, NcTransform target) { //this part is inverse of local to global (inverse function of TransformPoint) Quaternion target_rot = target.rotation; Vector3 target_scale = target.lossyScale; Vector3 target_pos = target.position; Vector3 model_pos = model.position; var diference = (model_pos - target_pos); //var finalPos = Quaternion.Inverse(target_rot) * new Vector3(diference.x / target_scale.x, diference.y / target_scale.y, diference.z / target_scale.z); // This is wrong var finalPos = Vector3.Scale(new Vector3(1 / target_scale.x, 1 / target_scale.y, 1 / target_scale.z), Quaternion.Inverse(target_rot) * diference); return(finalPos); }
public NcTransform GetSwappedYZTransform() { Vector3 originalScale = this.lossyScale; Vector3 originalLocalScale = this.localScale ?? default(Vector3); Vector3 newScale = originalScale; Vector3 newLocalScale = originalLocalScale; newScale.y = originalScale.z; newScale.z = originalScale.y; newLocalScale.y = originalLocalScale.z; newLocalScale.z = originalLocalScale.y; NcTransform res = new NcTransform(this); res.lossyScale = newScale; res.localScale = newLocalScale; return(res); }
// Start is called before the first frame update private void Awake() { OriginalTransformData = new NcTransform(transform); InitialParent = transform.parent; }