public void ConnectAnchors(AnchorPoint anchor) { if (anchor == null || this.selectedAnchor == null || this.selectedAnchor.GetComponentInParent <RotateByDragging>().Equals(anchor.GetComponentInParent <RotateByDragging>())) { return; } Transform object1 = this.selectedAnchor.transform.parent; Transform object2 = anchor.transform.parent; Transform simpleObject1 = object1; Transform simpleObject2 = object2; bool object1Assembled = false; bool object2Assembled = false; object1.GetComponent <OrigamiObject> ().connectedAnchors.AddLast(anchor); object2.GetComponent <OrigamiObject> ().connectedAnchors.AddLast(this.selectedAnchor); if (object1.GetComponentInParent <AssembledOrigamiObject> ()) { object1 = object1.GetComponentInParent <AssembledOrigamiObject> ().transform; //Debug.Log ("1 is assembled"); object1Assembled = true; } if (object2.GetComponentInParent <AssembledOrigamiObject> ()) { object2 = object2.GetComponentInParent <AssembledOrigamiObject> ().transform; //Debug.Log ("2 is assembled"); object2Assembled = true; } Matrix4x4 transforMatrix1 = Matrix4x4.TRS(Vector3.zero, this.selectedAnchor.transform.rotation, Vector3.one); Matrix4x4 transforMatrix2 = Matrix4x4.TRS(Vector3.zero, anchor.transform.rotation, Vector3.one); Vector3 realNormal1 = transforMatrix1.MultiplyPoint3x4(this.selectedAnchor.normal.normalized); Vector3 realNormal2 = transforMatrix2.MultiplyPoint3x4(anchor.normal.normalized); if (true) { object1.parent.rotation = Quaternion.FromToRotation(realNormal1, -realNormal2) * object1.parent.rotation; } else { object1.parent.rotation = Quaternion.FromToRotation(this.selectedAnchor.normal.normalized, -realNormal2); } transforMatrix1 = Matrix4x4.TRS(Vector3.zero, this.selectedAnchor.transform.rotation, Vector3.one); transforMatrix2 = Matrix4x4.TRS(Vector3.zero, anchor.transform.rotation, Vector3.one); Vector3 realUp1 = transforMatrix1.MultiplyPoint3x4(this.selectedAnchor.directionUp.normalized); Vector3 realUp2 = transforMatrix2.MultiplyPoint3x4(anchor.directionUp.normalized); object1.parent.rotation = Quaternion.FromToRotation(realUp1, realUp2) * object1.parent.rotation; Vector3 realAnchorPos1 = (this.selectedAnchor.transform.position - object1.parent.position); Vector3 realAnchorPos2 = (anchor.transform.position - object2.parent.position); object1.parent.position = object2.parent.position + realAnchorPos2 - realAnchorPos1; Transform oldRotater1 = object1.GetComponentInParent <RotateByDragging>().transform; Transform oldRotater2 = object2.GetComponentInParent <RotateByDragging>().transform; this.selectedAnchor.LinkTo(anchor); OrigamiObject assembled = object1.GetComponent <OrigamiObject> ().Add(object2.GetComponent <OrigamiObject> ()); this.inventory.AssembleReplaceInAssembleArea(object1.GetComponent <OrigamiObject> (), object2.GetComponent <OrigamiObject> (), assembled); AssembledOrigamiObject realAssembled = (AssembledOrigamiObject)assembled; /*if (oldRotater1.childCount <= 0) { * this.assembleObjs.Remove (oldRotater1); * Destroy (oldRotater1.gameObject); * } else if (oldRotater2.GetChild(0) <= 0) { * this.assembleObjs.Remove (oldRotater2); * Destroy (oldRotater2.gameObject); * } else { * Debug.Log ("ERROR ASSEMBLING "+"("+oldRotater1.GetChild(0).childCount+" ; "+oldRotater2.GetChild(0).childCount+")"); * }*/ if (oldRotater1.childCount <= 0) { this.assembleObjs.Remove(oldRotater1); Destroy(oldRotater1.gameObject); } else if (oldRotater1.GetChild(0).childCount == 0) { this.assembleObjs.Remove(oldRotater1); Destroy(oldRotater1.gameObject); } if (oldRotater2.childCount <= 0) { this.assembleObjs.Remove(oldRotater2); Destroy(oldRotater2.gameObject); } else if (oldRotater2.GetChild(0).childCount == 0) { this.assembleObjs.Remove(oldRotater2); Destroy(oldRotater2.gameObject); } this.selectedAnchor.Deselect(); this.selectedAnchor = null; Vector3[] oldPositions = new Vector3[assembled.transform.childCount]; Vector3 barycenter = Vector3.zero; for (int i = 0; i < assembled.transform.childCount; ++i) { oldPositions [i] = assembled.transform.GetChild(i).position; barycenter += assembled.transform.GetChild(i).position / assembled.transform.childCount; } assembled.transform.parent.position = barycenter; assembled.transform.localPosition = Vector3.zero; for (int i = 0; i < assembled.transform.childCount; ++i) { assembled.transform.GetChild(i).position = oldPositions [i]; } realAssembled.ComputeNewBounds(); //Resize the rotater Vector3 origamiBounds = realAssembled.GetBounds().extents; float maxBound = Mathf.Max(origamiBounds.x, origamiBounds.y, origamiBounds.z); maxBound = maxBound == 0 ? undefinedScale : maxBound; float scaleFactor = this.assembleSize / maxBound; assembled.transform.parent.GetComponent <RotateByDragging> ().maxScale = scaleFactor; if (this.inventory.NumberAssembleAreaObjects() == 1) { this.assembleObjScale = scaleFactor; } else if (scaleFactor < this.assembleObjScale) { this.assembleObjScale = scaleFactor; } this.UpdateAssembleObjScale(); }
public void SelectAnchorPoint() { RaycastHit hit; Vector3 startPosRay = Input.mousePosition; if (gameController.VRMode) { startPosRay.x = Screen.width / 2; startPosRay.y = Screen.height / 2; } if (Physics.Raycast(this.inventoryCamera.ScreenPointToRay(startPosRay), out hit)) { if (hit.collider.GetComponent <AnchorPoint> ()) { AnchorPoint pointedAnchor = hit.collider.GetComponent <AnchorPoint> (); if (this.selectedAnchor == null) { this.selectedAnchor = pointedAnchor; pointedAnchor.Select(); return; } if (this.selectedAnchor.Equals(pointedAnchor)) { this.selectedAnchor = null; pointedAnchor.Deselect(); return; } //if (this.selectedAnchor.transform.parent.Equals (pointedAnchor.transform.parent)) { if (this.selectedAnchor.GetComponentInParent <RotateByDragging>().Equals(pointedAnchor.GetComponentInParent <RotateByDragging>())) { this.selectedAnchor.Deselect(); this.selectedAnchor = pointedAnchor; pointedAnchor.Select(); return; } this.ConnectAnchors(pointedAnchor); } } }