void CalcR(MegaBookPage page, MegaBookAxis axis, float ang) { float len = 0.0f; if (!fromto) { switch (axis) { case MegaBookAxis.X: len = page.bbox.max.x - page.bbox.min.x; break; case MegaBookAxis.Z: len = page.bbox.max.y - page.bbox.min.y; break; case MegaBookAxis.Y: len = page.bbox.max.z - page.bbox.min.z; break; } } else { len = to - from; } if (Mathf.Abs(ang) < 0.0001f) { r = 0.0f; } else { r = len / ang; } }
public void Deform(MegaBookPage page, Vector3[] verts, Vector3[] sverts) { Calc(page); for (int i = 0; i < verts.Length; i++) { if (r == 0.0f && !fromto) { sverts[i] = verts[i]; } else { Vector3 p = tm.MultiplyPoint3x4(verts[i]); // tm may have an offset gizmo etc if (fromto) { if (p.y <= from) { sverts[i] = invtm.MultiplyPoint3x4(tmBelow.MultiplyPoint3x4(p)); continue; } else { if (p.y >= to) { sverts[i] = invtm.MultiplyPoint3x4(tmAbove.MultiplyPoint3x4(p)); continue; } } } if (r == 0.0f) { sverts[i] = invtm.MultiplyPoint3x4(p); } else { float x = p.x; float y = p.y; float yr = y / r; float c = Mathf.Cos(Mathf.PI - yr); float s = Mathf.Sin(Mathf.PI - yr); float px = r * c + r - x * c; p.x = px; float pz = r * s - x * s; p.y = pz; sverts[i] = invtm.MultiplyPoint3x4(p); } } } }
void OnSceneGUI() { MegaBookBuilder mod = (MegaBookBuilder)target; MegaBookPage pg = mod.pages[mod.editpage]; switch (Tools.current) { case Tool.Move: for (int i = 0; i < pg.objects.Count; i++) { #if UNITY_3_5 if (pg.objects[i].obj && pg.objects[i].obj.active) #else if (pg.objects[i].obj && pg.objects[i].obj.activeInHierarchy) #endif { Transform trans = pg.objects[i].obj.transform; Vector3 pos = Handles.PositionHandle(trans.position, trans.rotation); //Quaternion.identity); if (pos != trans.position) { Vector3 delta = trans.position - pos; //float off = delta.y; delta.y = 0.0f; MegaBookPageParams parms = mod.pageparams[mod.editpage]; //.objects[i]]; MegaBookPageObject pobj = parms.objects[i]; pobj.pos -= delta; pobj.pos.x = Mathf.Clamp(pobj.pos.x, 0.0f, 99.99f); pobj.pos.z = Mathf.Clamp(pobj.pos.z, 0.0f, 99.99f); //if ( Mathf.Abs(delta.x) < 0.01f && Mathf.Abs(delta.z) < 0.01f ) //pobj.offset += off; mod.UpdateAttached(); EditorUtility.SetDirty(target); } } } break; } }
public void AttachItUV() { if (book) { attached = true; MegaBookPage pobj = book.pages[page]; Bounds bounds = pobj.bbox; // Calc local pos from pos in bounds Vector3 pp = pos * 0.01f; pp.x = Mathf.Clamp01(pp.x); pp.y = Mathf.Clamp01(pp.y); pp.z = Mathf.Clamp01(pp.z); Vector3 lpos = bounds.min + (Vector3.Scale(pp, bounds.size)); Vector3 objSpacePt = lpos; Vector3[] verts = pobj.verts; int[] tris = pobj.obj.GetComponent <MeshFilter>().sharedMesh.triangles; int index = -1; MegaBookNearest.NearestPointOnMesh1(objSpacePt, verts, tris, ref index, ref BaryCoord); if (index >= 0) { BaryVerts[0] = tris[index]; BaryVerts[1] = tris[index + 1]; BaryVerts[2] = tris[index + 2]; } MegaBookNearest.NearestPointOnMesh1(objSpacePt + attachforward, verts, tris, ref index, ref BaryCoord1); if (index >= 0) { BaryVerts1[0] = tris[index]; BaryVerts1[1] = tris[index + 1]; BaryVerts1[2] = tris[index + 2]; } } }
void LateUpdate() { if (book) { if (!attached) { AttachItUV(); } MegaBookPage pg = book.pages[page]; if (visibleObj) { #if UNITY_3_5 if (pg.showobjects) { if (visibleObj.active) { visibleObj.SetActiveRecursively(false); } return; } else { if (!visibleObj.active) { visibleObj.SetActiveRecursively(true); } } #else if (pg.showobjects) { if (visibleObj.activeInHierarchy) { visibleObj.SetActive(false); } return; } else { if (!visibleObj.activeInHierarchy) { visibleObj.SetActive(true); } } #endif } GameObject target = pg.obj; Vector3 v0 = pg.sverts[BaryVerts[0]]; Vector3 v1 = pg.sverts[BaryVerts[1]]; Vector3 v2 = pg.sverts[BaryVerts[2]]; Vector3 pos = target.transform.localToWorldMatrix.MultiplyPoint(GetCoordMine(v0, v1, v2, BaryCoord)); // Rotation Vector3 va = v1 - v0; Vector3 vb = v2 - v1; Vector3 norm = Vector3.Cross(va, vb); v0 = pg.sverts[BaryVerts1[0]]; v1 = pg.sverts[BaryVerts1[1]]; v2 = pg.sverts[BaryVerts1[2]]; Vector3 fwd = target.transform.localToWorldMatrix.MultiplyPoint(GetCoordMine(v0, v1, v2, BaryCoord1)) - pos; Quaternion erot = Quaternion.Euler(AxisRot); Quaternion rot = Quaternion.LookRotation(fwd, norm) * erot; transform.position = pos + (offset * norm.normalized); transform.rotation = rot; } }
void Calc(MegaBookPage page) { SetTM(); if (from > to) { from = to; } if (to < from) { to = from; } mat = Matrix4x4.identity; switch (axis) { case MegaBookAxis.X: MegaBookMatrix.RotateZ(ref mat, Mathf.PI * 0.5f); break; case MegaBookAxis.Y: MegaBookMatrix.RotateX(ref mat, -Mathf.PI * 0.5f); break; case MegaBookAxis.Z: break; } MegaBookMatrix.RotateY(ref mat, Mathf.Deg2Rad * dir); SetAxis(mat); CalcR(page, axis, Mathf.Deg2Rad * angle); if (fromto) { fromto = false; float len = to - from; float rat1, rat2; if (len == 0.0f) { rat1 = rat2 = 1.0f; } else { rat1 = to / len; rat2 = from / len; } Vector3 pt; tmAbove = Matrix4x4.identity; MegaBookMatrix.Translate(ref tmAbove, 0.0f, -to, 0.0f); MegaBookMatrix.RotateZ(ref tmAbove, -Mathf.Deg2Rad * angle * rat1); MegaBookMatrix.Translate(ref tmAbove, 0.0f, to, 0.0f); pt = new Vector3(0.0f, to, 0.0f); MegaBookMatrix.Translate(ref tmAbove, tm.MultiplyPoint3x4(Map(0, invtm.MultiplyPoint3x4(pt))) - pt); tmBelow = Matrix4x4.identity; MegaBookMatrix.Translate(ref tmBelow, 0.0f, -from, 0.0f); MegaBookMatrix.RotateZ(ref tmBelow, Mathf.Deg2Rad * angle * rat2); MegaBookMatrix.Translate(ref tmBelow, 0.0f, from, 0.0f); pt = new Vector3(0.0f, from, 0.0f); MegaBookMatrix.Translate(ref tmBelow, tm.MultiplyPoint3x4(Map(0, invtm.MultiplyPoint3x4(pt))) - pt); fromto = true; } }