static void RenderGizmo(MegaShape shape, GizmoType gizmoType) { if ((gizmoType & GizmoType.NotInSelectionHierarchy) != 0) { if ((gizmoType & GizmoType.Active) != 0) { DrawGizmos(shape, new Color(1.0f, 1.0f, 1.0f, 1.0f)); Color col = Color.yellow; col.a = 0.5f; Gizmos.color = col; //Color.yellow; //CursorPos = shape.FindNearestPoint(CursorPos, 5, ref CursorKnot, ref CursorTangent); //CursorSpline = shape.FindNearestPoint(CursorPos, 5, ref CursorKnot, ref CursorTangent); //Gizmos.DrawSphere(shape.transform.TransformPoint(CursorPos), shape.KnotSize); CursorPos = shape.InterpCurve3D(0, CursorPercent * 0.01f, true); Gizmos.DrawSphere(shape.transform.TransformPoint(CursorPos), shape.KnotSize * 0.01f); Handles.color = Color.white; Handles.Label(shape.transform.TransformPoint(CursorPos), "Cursor " + CursorPercent.ToString("0.00") + "% - " + CursorPos); } else { DrawGizmos(shape, new Color(1.0f, 1.0f, 1.0f, 0.25f)); } } Gizmos.DrawIcon(shape.transform.position, "MegaSpherify icon.png"); Handles.Label(shape.transform.position, " " + shape.name); }
void DisplaySpline(MegaShape shape, MegaSpline spline) { bool closed = EditorGUILayout.Toggle("Closed", spline.closed); if (closed != spline.closed) { spline.closed = closed; shape.CalcLength(10); EditorUtility.SetDirty(target); //shape.BuildMesh(); } EditorGUILayout.LabelField("Length ", spline.length.ToString("0.000")); showknots = EditorGUILayout.Foldout(showknots, "Knots"); if (showknots) { for (int i = 0; i < spline.knots.Count; i++) { DisplayKnot(shape, spline, spline.knots[i]); //EditorGUILayout.Separator(); } } }
Vector3 Deform(MegaShape path, float alpha, float off, Vector3 scale, float removeDof, Vector3 locoff, out Matrix4x4 omat, Vector3 sploff) { float tw1 = 0.0f; Vector3 ps = pathtm.MultiplyPoint(path.InterpCurve3D(curve, alpha, path.normalizedInterp, ref tw1) + sploff); Vector3 ps1 = pathtm.MultiplyPoint(path.InterpCurve3D(curve, alpha + (tangent * 0.001f), path.normalizedInterp) + sploff); if (path.splines[curve].closed) { alpha = Mathf.Repeat(alpha, 1.0f); } else { alpha = Mathf.Clamp01(alpha); } if (useTwist) { tw = meshrot * Quaternion.AngleAxis((twist * twistCrv.Evaluate(alpha)) + tw1, Vector3.forward); } Vector3 relativePos = ps1 - ps; relativePos.y *= removeDof; Quaternion rotation = Quaternion.LookRotation(relativePos) * tw; wtm.SetTRS(ps, rotation, scale); omat = mat * wtm; return(ps); }
bool AnimParse(BinaryReader br, string cid) { MegaShape ms = (MegaShape)target; switch (cid) { case "V": int v = br.ReadInt32(); ma = new MegaKnotAnim(); int s = ms.GetSpline(v, ref ma); //.s, ref ma.p, ref ma.t); if (ms.splines[s].animations == null) { ms.splines[s].animations = new List <MegaKnotAnim>(); } ms.splines[s].animations.Add(ma); break; case "Anim": //ma.con = MegaBezVector3KeyControl.LoadBezVector3KeyControl(br); ma.con = MegaParseBezVector3Control.LoadBezVector3KeyControl(br); break; } return(true); }
bool ParseShape(BinaryReader br, string cid) { MegaShape ms = (MegaShape)target; switch (cid) { case "Num": int count = br.ReadInt32(); ms.splines = new List <MegaSpline>(count); //id = 0; break; case "Spline": MegaSpline spl = new MegaSpline(); ms.splines.Add(spl); //MegaMorphEditor.Parse(br, SplineParse); MegaParse.Parse(br, SplineParse); break; case "Anim": //Debug.Log("Anim info"); //MegaMorphEditor.Parse(br, AnimParse); MegaParse.Parse(br, AnimParse); break; } return(true); }
static void CreateShape(string type, System.Type classtype) { Vector3 pos = Vector3.zero; if (UnityEditor.SceneView.lastActiveSceneView != null) { pos = UnityEditor.SceneView.lastActiveSceneView.pivot; } GameObject go = new GameObject(type + " Shape"); MegaShape ms = (MegaShape)go.AddComponent(classtype); go.transform.position = pos; Selection.activeObject = go; if (ms != null) { ms.axis = axis; ms.drawHandles = drawhandles; ms.drawKnots = drawknots; ms.col1 = col1; ms.col2 = col2; ms.KnotSize = knotsize; ms.stepdist = stepdist; ms.makeMesh = makemesh; ms.handleType = MegaHandleType.Free; } }
Vector3 PosHandlesSnap(MegaShape shape, Vector3 pos, Quaternion q) { switch ( shape.handleType ) { case MegaHandleType.Position: pos = Handles.PositionHandle(pos, q); break; case MegaHandleType.Free: pos = Handles.FreeMoveHandle(pos, q, shape.KnotSize * 0.01f, Vector3.zero, Handles.CircleCap); break; } if ( shape.usesnap ) { if ( shape.snap.x != 0.0f ) pos.x = (int)(pos.x / shape.snap.x) * shape.snap.x; if ( shape.snap.y != 0.0f ) pos.y = (int)(pos.y / shape.snap.y) * shape.snap.y; if ( shape.snap.z != 0.0f ) pos.z = (int)(pos.z / shape.snap.z) * shape.snap.z; } return pos; }
void InitCurves(MegaShape shape) { totalbounds.size = Vector3.zero; totalarea = 0.0f; if (shape != null) { curves.Clear(); for (int i = 0; i < shape.splines.Count; i++) { if (shape.splines[i].closed && usespline[i]) { MegaCurveList curve = new MegaCurveList(); curve.curve = i; totalarea += shape.splines[i].Area(); curve.area = Mathf.Abs(shape.splines[i].Area()); curve.box = new Bounds(shape.splines[i].knots[0].p, Vector3.zero); for (int k = 1; k < shape.splines[i].knots.Count; k++) { curve.box.Encapsulate(shape.splines[i].knots[k].p); } curve.min = curve.box.min; curve.size = curve.box.size; curves.Add(curve); totalbounds.Encapsulate(curve.box); } } } }
void LoadSXL(float scale) { MegaShape ms = (MegaShape)target; string filename = EditorUtility.OpenFilePanel("SXL File", lastpath, "sxl"); if ( filename == null || filename.Length < 1 ) return; lastpath = filename; bool opt = true; if ( ms.splines != null && ms.splines.Count > 0 ) opt = EditorUtility.DisplayDialog("Spline Import Option", "Splines already present, do you want to 'Add' or 'Replace' splines with this file?", "Add", "Replace"); int startspline = 0; if ( opt ) startspline = ms.splines.Count; StreamReader streamReader = new StreamReader(filename); string text = streamReader.ReadToEnd(); streamReader.Close(); MegaShapeSXL sxl = new MegaShapeSXL(); sxl.importData(text, ms, scale, opt, startspline); //.splines[0]); ms.imported = true; }
public void ParseShape(MegaXMLNode node, MegaShape shape) { for ( int i = 0; i < node.values.Count; i++ ) { MegaXMLValue val = node.values[i]; //Debug.Log("Shape val " + val.name); switch ( val.name ) { case "name": break; case "p": break; case "r": break; case "s": break; } } foreach ( MegaXMLNode n in node.children ) { //Debug.Log("Shape tagName " + n.tagName); switch ( n.tagName ) { case "Spline": ParseSpline(n, shape); break; } } }
public void ParseSpline(MegaXMLNode node, MegaShape shape) { MegaSpline spline = new MegaSpline(); for ( int i = 0; i < node.values.Count; i++ ) { MegaXMLValue val = node.values[i]; //Debug.Log("Spline val " + val.name); switch ( val.name ) { case "flags": break; case "closed": spline.closed = int.Parse(val.value) > 0 ? true : false; break; } } foreach ( MegaXMLNode n in node.children ) { //Debug.Log("Spline tagName " + n.tagName); switch ( n.tagName ) { case "K": ParseKnot(n, shape, spline); break; } } //Debug.Log("************** Add Spline"); shape.splines.Add(spline); }
public void ParseSpline(MegaXMLNode node, MegaShape shape) { MegaSpline spline = new MegaSpline(); for (int i = 0; i < node.values.Count; i++) { MegaXMLValue val = node.values[i]; //Debug.Log("Spline val " + val.name); switch (val.name) { case "flags": break; case "closed": spline.closed = int.Parse(val.value) > 0 ? true : false; break; } } foreach (MegaXMLNode n in node.children) { //Debug.Log("Spline tagName " + n.tagName); switch (n.tagName) { case "K": ParseKnot(n, shape, spline); break; } } //Debug.Log("************** Add Spline"); shape.splines.Add(spline); }
void LoadKML(float scale) { MegaShape ms = (MegaShape)target; string filename = EditorUtility.OpenFilePanel("KML File", lastpath, "kml"); if ( filename == null || filename.Length < 1 ) return; lastpath = filename; //bool opt = true; //if ( ms.splines != null && ms.splines.Count > 0 ) // opt = EditorUtility.DisplayDialog("Spline Import Option", "Splines already present, do you want to 'Add' or 'Replace' splines with this file?", "Add", "Replace"); //int startspline = 0; //if ( opt ) // startspline = ms.splines.Count; MegaKML kml = new MegaKML(); kml.KMLDecode(filename); Vector3[] points = kml.GetPoints(ImportScale); ms.BuildSpline(ms.selcurve, points, true); }
public void ParseShape(MegaXMLNode node, MegaShape shape) { for (int i = 0; i < node.values.Count; i++) { MegaXMLValue val = node.values[i]; //Debug.Log("Shape val " + val.name); switch (val.name) { case "name": break; case "p": break; case "r": break; case "s": break; } } foreach (MegaXMLNode n in node.children) { //Debug.Log("Shape tagName " + n.tagName); switch (n.tagName) { case "Spline": ParseSpline(n, shape); break; } } }
public void ParseKnot(MegaXMLNode node, MegaShape shape, MegaSpline spline) { Vector3 p = Vector3.zero; Vector3 invec = Vector3.zero; Vector3 outvec = Vector3.zero; for (int i = 0; i < node.values.Count; i++) { MegaXMLValue val = node.values[i]; //Debug.Log("Knot val " + val.name); switch (val.name) { case "p": p = ParseV3Split(val.value, 0); break; case "i": invec = ParseV3Split(val.value, 0); break; case "o": outvec = ParseV3Split(val.value, 0); break; case "l": break; } } spline.AddKnot(p, invec, outvec); }
void ClearAnim(MegaShape shape) { MegaSpline spline = shape.splines[shape.selcurve]; if ( spline.splineanim != null ) { spline.splineanim.Init(spline); } }
static public string Export(MegaShape shape, int x, int y, float strokewidth, Color col) { string file = ""; Color32 c = col; file += "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; file += "<!-- MegaShapes SVG Exporter v1.0 -->\n"; file += "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n"; file += "<svg version=\"1.1\" id=\"" + shape.name + "\" x=\"0px\" y=\"0px\" width=\"640.0px\" height=\"480.0px\">\n"; for (int i = 0; i < shape.splines.Count; i++) { MegaSpline spline = shape.splines[i]; file += "<path d=\""; MegaKnot k1; MegaKnot k = spline.knots[0]; k1 = k; file += "M" + k.p[x] + "," + -k.p[y]; //Vector3 cp = k.p; for (int j = 1; j < spline.knots.Count; j++) { k = spline.knots[j]; Vector3 po = k1.outvec; // - cp; // - k1.p; Vector3 pi = k.invec; // - cp; // - k.p; Vector3 kp = k.p; // - cp; kp[y] = -kp[y]; po[y] = -po[y]; pi[y] = -pi[y]; file += "C" + po[x] + "," + po[y]; file += " " + pi[x] + "," + pi[y]; file += " " + kp[x] + "," + kp[y]; k1 = k; } if (spline.closed) { file += "z\""; } file += " fill=\"none\""; file += " stroke=\"#" + c.r.ToString("x") + c.g.ToString("x") + c.b.ToString("x") + "\""; file += " stroke-width=\"" + strokewidth + "\""; file += "/>\n"; } file += "</svg>\n"; return(file); }
public static string Export(MegaShape shape, int x, int y, float strokewidth, Color col) { string file = ""; Color32 c = col; file += "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; file += "<!-- MegaShapes SVG Exporter v1.0 -->\n"; file += "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n"; file += "<svg version=\"1.1\" id=\"" + shape.name + "\" x=\"0px\" y=\"0px\" width=\"640.0px\" height=\"480.0px\">\n"; for ( int i = 0; i < shape.splines.Count; i++ ) { MegaSpline spline = shape.splines[i]; file += "<path d=\""; MegaKnot k1; MegaKnot k = spline.knots[0]; k1 = k; file += "M" + k.p[x] + "," + -k.p[y]; //Vector3 cp = k.p; for ( int j = 1; j < spline.knots.Count; j++ ) { k = spline.knots[j]; Vector3 po = k1.outvec; // - cp; // - k1.p; Vector3 pi = k.invec; // - cp; // - k.p; Vector3 kp = k.p; // - cp; kp[y] = -kp[y]; po[y] = -po[y]; pi[y] = -pi[y]; file += "C" + po[x] + "," + po[y]; file += " " + pi[x] + "," + pi[y]; file += " " + kp[x] + "," + kp[y]; k1 = k; } if ( spline.closed ) { file += "z\""; } file += " fill=\"none\""; file += " stroke=\"#" + c.r.ToString("x") + c.g.ToString("x") + c.b.ToString("x") + "\""; file += " stroke-width=\"" + strokewidth + "\""; file += "/>\n"; } file += "</svg>\n"; return file; }
void BuildObjectLinks(MegaShape path) { float len = path.splines[curve].length; if ( LinkSize < 0.1f ) LinkSize = 0.1f; // Assume z axis for now float linklen = (linkOff1.y - linkOff.y) * linkScale.x * LinkSize; int lc = (int)(len / linklen); if ( lc != linkcount ) InitLinkObjects(path); Quaternion linkrot1 = Quaternion.identity; linkrot1 = Quaternion.Euler(rotate); float spos = start * 0.01f; Vector3 poff = linkPivot * linkScale.x * LinkSize; float lastalpha = spos; Vector3 pos = Vector3.zero; Matrix4x4 pmat = Matrix4x4.TRS(poff, linkrot1, Vector3.one); Vector3 lrot = Vector3.zero; Quaternion frot = Quaternion.identity; Random.seed = seed; for ( int i = 0; i < linkcount; i++ ) { float alpha = ((float)(i + 1) / (float)linkcount) + spos; Quaternion lq = GetLinkQuat(alpha, lastalpha, out pos, path); lastalpha = alpha; Quaternion lr = Quaternion.Euler(lrot); frot = lq * linkrot1 * lr; if ( linkobjs[i] ) { Matrix4x4 lmat = Matrix4x4.TRS(pos, lq, Vector3.one) * pmat; linkobjs[i].localPosition = lmat.GetColumn(3); linkobjs[i].localRotation = frot; linkobjs[i].localScale = linkScale * LinkSize; } if ( randRot ) { float r = Random.Range(0.0f, 1.0f); lrot = (int)(r * (int)(360.0f / MegaUtils.LargestValue1(linkRot))) * linkRot; } else lrot += linkRot; } }
public override void OnInspectorGUI() { base.OnInspectorGUI(); triggerAnimation = target as TriggerAnimation; MegaShape shape = triggerAnimation.layer.layerPath; triggerAnimation.Update(ref triggerAnimation.curve, "Curve", 0, shape.splines.Count - 1); }
public void LoadXML(string svgdata, MegaShape shape) { MegaXMLReader xml = new MegaXMLReader(); MegaXMLNode node = xml.read(svgdata); shape.splines.Clear(); shape.selcurve = 0; splineindex = 0; ParseXML(node, shape); }
static void RenderGizmo(MegaShape shape, GizmoType gizmoType) { if ( (gizmoType & GizmoType.Active) != 0 && Selection.activeObject == shape.gameObject ) { if ( shape.splines == null || shape.splines.Count == 0 ) return; DrawGizmos(shape, new Color(1.0f, 1.0f, 1.0f, 1.0f)); if ( shape.splines[shape.selcurve].knots.Count > 1 ) { Color col = Color.yellow; col.a = 0.5f; Gizmos.color = col; //Color.yellow; CursorPos = shape.InterpCurve3D(shape.selcurve, shape.CursorPercent * 0.01f, true); Gizmos.DrawSphere(shape.transform.TransformPoint(CursorPos), shape.KnotSize * 0.01f); Handles.color = Color.white; if ( shape.handleType == MegaHandleType.Free && editmode ) { int s = shape.selcurve; { for ( int p = 0; p < shape.splines[s].knots.Count; p++ ) { if ( shape.drawKnots ) //&& s == shape.selcurve ) { Gizmos.color = Color.green; Gizmos.DrawSphere(shape.transform.TransformPoint(shape.splines[s].knots[p].p), shape.KnotSize * 0.01f); } if ( shape.drawHandles ) { Gizmos.color = Color.red; Gizmos.DrawSphere(shape.transform.TransformPoint(shape.splines[s].knots[p].invec), shape.KnotSize * 0.01f); Gizmos.DrawSphere(shape.transform.TransformPoint(shape.splines[s].knots[p].outvec), shape.KnotSize * 0.01f); } } } } } } else DrawGizmos(shape, new Color(1.0f, 1.0f, 1.0f, 0.25f)); if ( Camera.current ) { Vector3 vis = Camera.current.WorldToScreenPoint(shape.transform.position); if ( vis.z > 0.0f ) { Gizmos.DrawIcon(shape.transform.position, "MegaSpherify icon.png", false); Handles.Label(shape.transform.position, " " + shape.name); } } }
void Export(MegaShape shape) { string filename = EditorUtility.SaveFilePanel("Export Shape to SVG", "", shape.name, ".svg"); if ( filename.Length > 0 ) { string data = MegaShapeSVG.Export(shape, (int)xaxis, (int)yaxis, strokewidth, strokecol); System.IO.File.WriteAllText(filename, data); } }
void DisplayKnot(MegaShape shape, MegaSpline spline, MegaKnot knot) { bool recalc = false; Vector3 p = EditorGUILayout.Vector3Field("Pos", knot.p); #if false Vector3 invec = EditorGUILayout.Vector3Field("In", knot.invec); //Vector3 outvec = EditorGUILayout.Vector3Field("Out", knot.outvec); if (invec != knot.invec) { if (shape.lockhandles) { Vector3 d = invec - knot.invec; knot.outvec -= d; } knot.invec = invec; recalc = true; } Vector3 outvec = EditorGUILayout.Vector3Field("Out", knot.outvec); if (outvec != knot.outvec) { if (shape.lockhandles) { Vector3 d = outvec - knot.outvec; knot.invec -= d; } knot.outvec = outvec; recalc = true; } //Vector3 p = EditorGUILayout.Vector3Field("Pos", knot.p); #endif delta = p - knot.p; knot.invec += delta; knot.outvec += delta; if (knot.p != p) { recalc = true; knot.p = p; } if (recalc) { shape.CalcLength(10); } }
public override void SplineNotify(MegaShape shape, int reason) { for (int i = 0; i < Layers.Length; i++) { if (Layers[i].SplineNotify(shape, reason)) { rebuild = true; break; } } }
public void ParseXML(MegaXMLNode node, MegaShape shape) { foreach ( MegaXMLNode n in node.children ) { switch ( n.tagName ) { case "Shape": ParseShape(n, shape); break; } ParseXML(n, shape); } }
public void LoadXML(string svgdata, MegaShape shape, bool clear, int start) { MegaXMLReader xml = new MegaXMLReader(); MegaXMLNode node = xml.read(svgdata); if ( !clear ) shape.splines.Clear(); shape.selcurve = start; splineindex = start; ParseXML(node, shape); }
public void ParseXML(MegaXMLNode node, MegaShape shape) { foreach (MegaXMLNode n in node.children) { switch (n.tagName) { case "Shape": ParseShape(n, shape); break; } ParseXML(n, shape); } }
static void RenderGizmo(MegaFlowCreateFromSplines flow, GizmoType gizmoType) { if (!flow.shownotsel && Selection.activeGameObject != flow.gameObject) { return; } flow.DrawGizmo(); float rng = 1.0f / (flow.maxvel - flow.minvel); for (int i = 0; i < flow.splines.Count; i++) { if (flow.splines[i].include) { MegaShape shape = flow.splines[i].shape; if (shape) { Handles.matrix = shape.transform.localToWorldMatrix; Color col = Color.green; for (int s = 0; s < shape.splines.Count; s++) { float step = 1.0f / (float)flow.splines[i].visrings; for (float j = 0.0f; j < 1.0f; j += step) { Vector3 p = shape.InterpCurve3D(s, j, true); Vector3 pt = shape.InterpCurve3D(s, j + 0.01f, true); float dist = flow.splines[i].distcrv.Evaluate(j) * flow.splines[i].falloffdist; float va = flow.splines[i].velcrv.Evaluate(j); col = flow.GetCol(va * flow.splines[i].velocity * rng); //flow.splines[i].velcrv.Evaluate(j)); col.a = flow.splines[i].ringalpha; //float va = flow.splines[i].velcrv.Evaluate(j); Handles.color = col; Vector3 dir = (pt - p).normalized; Handles.DrawWireDisc(p, dir, dist); Quaternion rot = Quaternion.LookRotation(dir); MegaFlowHandles.ArrowCap(0, p, rot, flow.splines[i].velocity * flow.vellen * va); } } } } } Handles.matrix = Matrix4x4.identity; }
public void SetShape(MegaShape newshape) { shape = newshape; usespline.Clear(); if (shape) { for (int i = 0; i < shape.splines.Count; i++) { usespline.Add(true); } } }
Quaternion GetLinkQuat(float alpha, float last, out Vector3 ps, MegaShape path) { int k = 0; ps = path.splines[curve].InterpCurve3D(last, shape.normalizedInterp, ref k); Vector3 ps1 = path.splines[curve].InterpCurve3D(alpha, shape.normalizedInterp, ref k); Vector3 relativePos = ps1 - ps; Quaternion rotation = Quaternion.LookRotation(relativePos, trackup); return(rotation); }
void BuildObjectLinks(MegaShape path) { float len = path.splines[curve].length; if (LinkSize < 0.1f) { LinkSize = 0.1f; } // Assume z axis for now float linklen = (linkOff1.y - linkOff.y) * linkScale.x * LinkSize; int lc = (int)(len / linklen); if (lc != linkcount) { InitLinkObjects(path); } Quaternion linkrot1 = Quaternion.identity; linkrot1 = Quaternion.Euler(rotate); float spos = start * 0.01f; Vector3 poff = linkPivot * linkScale.x * LinkSize; float lastalpha = spos; Vector3 pos = Vector3.zero; Matrix4x4 pmat = Matrix4x4.TRS(poff, linkrot1, Vector3.one); Quaternion frot = Quaternion.identity; for (int i = 0; i < linkcount; i++) { float alpha = ((float)(i + 1) / (float)linkcount) + spos; Quaternion lq = GetLinkQuat(alpha, lastalpha, out pos, path); lastalpha = alpha; frot = lq * linkrot1; if (linkobjs[i]) { Matrix4x4 lmat = Matrix4x4.TRS(pos, lq, Vector3.one) * pmat; linkobjs[i].localPosition = lmat.GetColumn(3); linkobjs[i].localRotation = frot; linkobjs[i].localScale = linkScale * LinkSize; } } }
public MegaPathTarget AddTarget(MegaShape shape, int curve, float weight) { MegaPathTarget target = new MegaPathTarget(); target.shape = shape; target.Weight = weight; target.curve = curve; target.modifier = 1.0f; target.offset = 0.0f; Targets.Add(target); return target; }
public virtual bool SplineNotify(MegaShape shape, int reason) { if (shape == layerPath) { return(true); } if (shape == layerSection) { return(true); } return(false); }
public MegaPathTarget AddTarget(MegaShape shape, int curve, float weight) { MegaPathTarget target = new MegaPathTarget(); target.shape = shape; target.Weight = weight; target.curve = curve; target.modifier = 1.0f; target.offset = 0.0f; Targets.Add(target); return(target); }
public MegaSpline NewSpline(MegaShape shape) { if ( shape.splines.Count == 0 ) { MegaSpline newspline = new MegaSpline(); shape.splines.Add(newspline); } MegaSpline spline = shape.splines[0]; spline.knots.Clear(); spline.closed = false; return spline; }
public void LoadXML(string svgdata, MegaShape shape, bool clear, int start) { MegaXMLReader xml = new MegaXMLReader(); MegaXMLNode node = xml.read(svgdata); if (!clear) { shape.splines.Clear(); } shape.selcurve = start; splineindex = start; ParseXML(node, shape); }
public MegaSpline NewSpline(MegaShape shape) { if (shape.splines.Count == 0) { MegaSpline newspline = new MegaSpline(); shape.splines.Add(newspline); } MegaSpline spline = shape.splines[0]; spline.knots.Clear(); spline.closed = false; return(spline); }
MegaSpline GetSpline(MegaShape shape) { MegaSpline spline; if ( splineindex < shape.splines.Count ) spline = shape.splines[splineindex]; else { spline = new MegaSpline(); shape.splines.Add(spline); } splineindex++; return spline; }
public void importData(string svgdata, MegaShape shape, float scale, bool clear, int start) { LoadXML(svgdata, shape, clear, start); for ( int i = start; i < splineindex; i++ ) { float area = shape.splines[i].Area(); if ( area < 0.0f ) shape.splines[i].reverse = false; else shape.splines[i].reverse = true; } //shape.Centre(0.01f, new Vector3(-1.0f, 1.0f, 1.0f)); shape.Centre(scale, new Vector3(-1.0f, 1.0f, 1.0f), start); shape.CalcLength(); //10); }
public void ParseXML(MegaXMLNode node, MegaShape shape) { foreach ( MegaXMLNode n in node.children ) { switch ( n.tagName ) { case "circle": ParseCircle(n, shape); break; case "path": ParsePath(n, shape); break; case "ellipse": ParseEllipse(n, shape); break; case "rect": ParseRect(n, shape); break; case "polygon": ParsePolygon(n, shape); break; default: break; } ParseXML(n, shape); } }
void BuildObjectLinks(MegaShape path) { float len = path.splines[curve].length; if ( LinkSize < 0.1f ) LinkSize = 0.1f; // Assume z axis for now float linklen = (linkOff1.y - linkOff.y) * linkScale.x * LinkSize; int lc = (int)(len / linklen); if ( lc != linkcount ) InitLinkObjects(path); Quaternion linkrot1 = Quaternion.identity; linkrot1 = Quaternion.Euler(rotate); float spos = start * 0.01f; Vector3 poff = linkPivot * linkScale.x * LinkSize; float lastalpha = spos; Vector3 pos = Vector3.zero; Matrix4x4 pmat = Matrix4x4.TRS(poff, linkrot1, Vector3.one); Quaternion frot = Quaternion.identity; for ( int i = 0; i < linkcount; i++ ) { float alpha = ((float)(i + 1) / (float)linkcount) + spos; Quaternion lq = GetLinkQuat(alpha, lastalpha, out pos, path); lastalpha = alpha; frot = lq * linkrot1; if ( linkobjs[i] ) { Matrix4x4 lmat = Matrix4x4.TRS(pos, lq, Vector3.one) * pmat; linkobjs[i].localPosition = lmat.GetColumn(3); linkobjs[i].localRotation = frot; linkobjs[i].localScale = linkScale * LinkSize; } } }
public void ParseKnot(MegaXMLNode node, MegaShape shape, MegaSpline spline) { Vector3 p = Vector3.zero; Vector3 invec = Vector3.zero; Vector3 outvec = Vector3.zero; for ( int i = 0; i < node.values.Count; i++ ) { MegaXMLValue val = node.values[i]; //Debug.Log("Knot val " + val.name); switch ( val.name ) { case "p": p = ParseV3Split(val.value, 0); break; case "i": invec = ParseV3Split(val.value, 0); break; case "o": outvec = ParseV3Split(val.value, 0); break; case "l": break; } } spline.AddKnot(p, invec, outvec); }
void DisplaySpline(MegaShape shape, MegaSpline spline) { bool closed = EditorGUILayout.Toggle("Closed", spline.closed); if ( closed != spline.closed ) { spline.closed = closed; shape.CalcLength(); //10); EditorUtility.SetDirty(target); //shape.BuildMesh(); } spline.reverse = EditorGUILayout.Toggle("Reverse", spline.reverse); EditorGUILayout.LabelField("Length ", spline.length.ToString("0.000")); spline.twistmode = (MegaShapeEase)EditorGUILayout.EnumPopup("Twist Mode", spline.twistmode); showknots = EditorGUILayout.Foldout(showknots, "Knots"); if ( showknots ) { for ( int i = 0; i < spline.knots.Count; i++ ) { DisplayKnot(shape, spline, spline.knots[i], i); //EditorGUILayout.Separator(); } } }
void ParsePolygon(MegaXMLNode node, MegaShape shape) { MegaSpline spline = GetSpline(shape); spline.knots.Clear(); spline.closed = true; char[] charSeparators = new char[] { ' ' }; for ( int i = 0; i < node.values.Count; i++ ) { MegaXMLValue val = node.values[i]; switch ( val.name ) { case "points": string[] coordinates = val.value.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries); for ( int j = 0; j < coordinates.Length; j++ ) { Vector3 p = ParseV2Split(coordinates[j], 0); MegaKnot k = new MegaKnot(); k.p = SwapAxis(new Vector3(p.x, 0.0f, p.y), shape.axis); k.invec = k.p; k.outvec = k.p; spline.knots.Add(k); } break; } } if ( spline.closed ) { Vector3 delta1 = spline.knots[0].outvec - spline.knots[0].p; spline.knots[0].invec = spline.knots[0].p - delta1; } }
void Export(MegaShape shape) { string filename = EditorUtility.SaveFilePanel("Export Shape to SVG", "", shape.name, ".svg"); if ( filename.Length > 0 ) { string data = MegaShapeSVG.Export(shape, (int)xaxis, (int)yaxis, strokewidth, strokecol); System.IO.File.WriteAllText(filename, data); } }
void DisplayKnot(MegaShape shape, MegaSpline spline, MegaKnot knot, int i) { bool recalc = false; Vector3 p = EditorGUILayout.Vector3Field("Knot [" + i + "] Pos", knot.p); delta = p - knot.p; knot.invec += delta; knot.outvec += delta; if ( knot.p != p ) { recalc = true; knot.p = p; } if ( recalc ) { shape.CalcLength(); //10); } knot.twist = EditorGUILayout.FloatField("Twist", knot.twist); knot.id = EditorGUILayout.IntField("ID", knot.id); }
void ClearAnim(MegaShape shape) { MegaSpline spline = shape.splines[shape.selcurve]; if ( spline.splineanim != null ) { spline.splineanim.Init(spline); } }
void AddKeyFrame(MegaShape shape, float t) { MegaSpline spline = shape.splines[shape.selcurve]; //if ( spline.splineanim == null ) //{ //Debug.Log("1"); //MegaSplineAnim sa = new MegaSplineAnim(); //sa.Init(spline); //spline.splineanim = sa; //} //else //{ spline.splineanim.AddState(spline, t); //} }
// Dont want this in here, want in editor // If we go over a knot then should draw to the knot static void DrawGizmos(MegaShape shape, Color modcol1) { if ( ((1 << shape.gameObject.layer) & Camera.current.cullingMask) == 0 ) return; if ( !shape.drawspline ) return; Matrix4x4 tm = shape.transform.localToWorldMatrix; for ( int s = 0; s < shape.splines.Count; s++ ) { float ldist = shape.stepdist * 0.1f; if ( ldist < 0.01f ) ldist = 0.01f; Color modcol = modcol1; if ( s != shape.selcurve && modcol1.a == 1.0f ) modcol.a *= 0.5f; if ( shape.splines[s].length / ldist > 500.0f ) ldist = shape.splines[s].length / 500.0f; float ds = shape.splines[s].length / (shape.splines[s].length / ldist); if ( ds > shape.splines[s].length ) ds = shape.splines[s].length; int c = 0; int k = -1; int lk = -1; Vector3 first = shape.splines[s].Interpolate(0.0f, shape.normalizedInterp, ref lk); for ( float dist = ds; dist < shape.splines[s].length; dist += ds ) { float alpha = dist / shape.splines[s].length; Vector3 pos = shape.splines[s].Interpolate(alpha, shape.normalizedInterp, ref k); if ( (c & 1) == 1 ) Gizmos.color = shape.col1 * modcol; else Gizmos.color = shape.col2 * modcol; if ( k != lk ) { for ( lk = lk + 1; lk <= k; lk++ ) { Gizmos.DrawLine(tm.MultiplyPoint(first), tm.MultiplyPoint(shape.splines[s].knots[lk].p)); first = shape.splines[s].knots[lk].p; } } lk = k; Gizmos.DrawLine(tm.MultiplyPoint(first), tm.MultiplyPoint(pos)); c++; first = pos; } if ( (c & 1) == 1 ) Gizmos.color = shape.col1 * modcol; else Gizmos.color = shape.col2 * modcol; Vector3 lastpos; if ( shape.splines[s].closed ) lastpos = shape.splines[s].Interpolate(0.0f, shape.normalizedInterp, ref k); else lastpos = shape.splines[s].Interpolate(1.0f, shape.normalizedInterp, ref k); Gizmos.DrawLine(tm.MultiplyPoint(first), tm.MultiplyPoint(lastpos)); } }
// Animation keyframe stuff // Need system to grab state of curve void AnimationKeyFrames(MegaShape shape) { MegaSpline spline = shape.splines[shape.selcurve]; shape.showanimations = EditorGUILayout.Foldout(shape.showanimations, "Animations"); if ( shape.showanimations ) { shape.keytime = EditorGUILayout.FloatField("Key Time", shape.keytime); if ( shape.keytime < 0.0f ) shape.keytime = 0.0f; spline.splineanim.Enabled = EditorGUILayout.BeginToggleGroup("Enabled", spline.splineanim.Enabled); EditorGUILayout.BeginHorizontal(); //if ( spline.splineanim == null ) //{ //} //else { //if ( GUILayout.Button("Create") ) //{ //spline.splineanim = new MegaSplineAnim(); //spline.splineanim.Init(spline); //} if ( GUILayout.Button("Add Key") ) { AddKeyFrame(shape, shape.keytime); } if ( GUILayout.Button("Clear") ) { ClearAnim(shape); } //if ( GUILayout.Button("Delete") ) //{ // spline.splineanim = null; //} } EditorGUILayout.EndHorizontal(); //if ( spline.splineanim == null ) // return; // Need to show each keyframe if ( spline.splineanim != null ) { //EditorGUILayout.LabelField("Frames " + spline.splineanim.knots[0].) int nk = spline.splineanim.NumKeys(); float mt = 0.0f; for ( int i = 0; i < nk; i++ ) { EditorGUILayout.BeginHorizontal(); mt = spline.splineanim.GetKeyTime(i); EditorGUILayout.LabelField("" + i, GUILayout.MaxWidth(20)); //" + " Time: " + mt); float t = EditorGUILayout.FloatField("", mt, GUILayout.MaxWidth(100)); if ( t != mt ) spline.splineanim.SetKeyTime(spline, i, t); if ( GUILayout.Button("Delete", GUILayout.MaxWidth(50)) ) spline.splineanim.RemoveKey(i); if ( GUILayout.Button("Update", GUILayout.MaxWidth(50)) ) spline.splineanim.UpdateKey(spline, i); if ( GUILayout.Button("Get", GUILayout.MaxWidth(50)) ) { spline.splineanim.GetKey(spline, i); EditorUtility.SetDirty(target); } EditorGUILayout.EndHorizontal(); } shape.MaxTime = mt; float at = EditorGUILayout.Slider("T", shape.testtime, 0.0f, mt); if ( at != shape.testtime ) { shape.testtime = at; if ( !shape.animate ) { for ( int s = 0; s < shape.splines.Count; s++ ) { if ( shape.splines[s].splineanim != null && shape.splines[s].splineanim.Enabled ) { shape.splines[s].splineanim.GetState1(shape.splines[s], at); shape.splines[s].CalcLength(); //(10); // could use less here } } } } } EditorGUILayout.EndToggleGroup(); } }
static void RenderGizmo(MegaShape shape, GizmoType gizmoType) { if ( (gizmoType & GizmoType.Active) != 0 && Selection.activeObject == shape.gameObject ) { if ( shape.splines == null || shape.splines.Count == 0 ) return; DrawGizmos(shape, new Color(1.0f, 1.0f, 1.0f, 1.0f)); Color col = Color.yellow; col.a = 0.5f; Gizmos.color = col; //Color.yellow; CursorPos = shape.InterpCurve3D(shape.selcurve, shape.CursorPercent * 0.01f, true); Gizmos.DrawSphere(shape.transform.TransformPoint(CursorPos), shape.KnotSize * 0.01f); Handles.color = Color.white; if ( shape.handleType == MegaHandleType.Free ) { int s = shape.selcurve; { for ( int p = 0; p < shape.splines[s].knots.Count; p++ ) { if ( shape.drawKnots ) //&& s == shape.selcurve ) { Gizmos.color = Color.green; Gizmos.DrawSphere(shape.transform.TransformPoint(shape.splines[s].knots[p].p), shape.KnotSize * 0.01f); } if ( shape.drawHandles ) { Gizmos.color = Color.red; Gizmos.DrawSphere(shape.transform.TransformPoint(shape.splines[s].knots[p].invec), shape.KnotSize * 0.01f); Gizmos.DrawSphere(shape.transform.TransformPoint(shape.splines[s].knots[p].outvec), shape.KnotSize * 0.01f); } } } } } else DrawGizmos(shape, new Color(1.0f, 1.0f, 1.0f, 0.25f)); if ( Camera.current ) { Vector3 vis = Camera.current.WorldToScreenPoint(shape.transform.position); if ( vis.z > 0.0f ) { Gizmos.DrawIcon(shape.transform.position, "MegaSpherify icon.png", false); Handles.Label(shape.transform.position, " " + shape.name); } } }
void ParseRect(MegaXMLNode node, MegaShape shape) { MegaSpline spline = GetSpline(shape); Vector3[] ppoints = new Vector3[4]; float w = 0.0f; float h = 0.0f; float x = 0.0f; float y = 0.0f; for ( int i = 0; i < node.values.Count; i++ ) { MegaXMLValue val = node.values[i]; switch ( val.name ) { case "x": x = float.Parse(val.value); break; case "y": y = float.Parse(val.value); break; case "width": w = float.Parse(val.value); break; case "height": h = float.Parse(val.value); break; case "transform": Debug.Log("SVG Transform not implemented yet"); break; } } ppoints[0] = new Vector3(x, 0.0f, y); ppoints[1] = new Vector3(x, 0.0f, y + h); ppoints[2] = new Vector3(x + w, 0.0f, y + h); ppoints[3] = new Vector3(x + w, 0.0f, y); spline.closed = true; spline.knots.Clear(); //spline.AddKnot(ppoints[0], ppoints[0], ppoints[0]); //spline.AddKnot(ppoints[1], ppoints[1], ppoints[1]); //spline.AddKnot(ppoints[2], ppoints[2], ppoints[2]); //spline.AddKnot(ppoints[3], ppoints[3], ppoints[3]); AddKnot(spline, ppoints[0], ppoints[0], ppoints[0], shape.axis); AddKnot(spline, ppoints[1], ppoints[1], ppoints[1], shape.axis); AddKnot(spline, ppoints[2], ppoints[2], ppoints[2], shape.axis); AddKnot(spline, ppoints[3], ppoints[3], ppoints[3], shape.axis); }
void ParseCircle(MegaXMLNode node, MegaShape shape) { MegaSpline spline = GetSpline(shape); float cx = 0.0f; float cy = 0.0f; float r = 0.0f; for ( int i = 0; i < node.values.Count; i++ ) { MegaXMLValue val = node.values[i]; switch ( val.name ) { case "cx": cx = float.Parse(val.value); break; case "cy": cy = float.Parse(val.value); break; case "r": r = float.Parse(val.value); break; } } float vector = CIRCLE_VECTOR_LENGTH * r; spline.knots.Clear(); for ( int ix = 0; ix < 4; ++ix ) { float angle = (Mathf.PI * 2.0f) * (float)ix / (float)4; float sinfac = Mathf.Sin(angle); float cosfac = Mathf.Cos(angle); Vector3 p = new Vector3((cosfac * r) + cx, 0.0f, (sinfac * r) + cy); Vector3 rotvec = new Vector3(sinfac * vector, 0.0f, -cosfac * vector); //spline.AddKnot(p, p + rotvec, p - rotvec); AddKnot(spline, p, p + rotvec, p - rotvec, shape.axis); } spline.closed = true; }
void ParsePath(MegaXMLNode node, MegaShape shape) { Vector3 cp = Vector3.zero; Vector2 cP1; Vector2 cP2; char[] charSeparators = new char[] { ',', ' ' }; MegaSpline spline = null; MegaKnot k; string[] coord; for ( int i = 0; i < node.values.Count; i++ ) { MegaXMLValue val = node.values[i]; switch ( val.name ) { case "d": #if UNITY_FLASH string[] coordinates = null; //string.Split(val.value, @"(?=[MmLlCcSsZzHhVv])"); #else string[] coordinates = Regex.Split(val.value, @"(?=[MmLlCcSsZzHhVv])"); #endif for ( int j = 0; j < coordinates.Length; j++ ) { if ( coordinates[j].Length > 0 ) { string v = coordinates[j].Substring(1); if ( v != null && v.Length > 0 ) { v = v.Replace("-", ",-"); while ( v.Length > 0 && (v[0] == ',' || v[0] == ' ') ) v = v.Substring(1); } switch ( coordinates[j][0] ) { case 'Z': case 'z': if ( spline != null ) { spline.closed = true; #if false Vector3 delta1 = spline.knots[0].outvec - spline.knots[0].p; spline.knots[0].invec = spline.knots[0].p - delta1; if ( spline.knots[0].p == spline.knots[spline.knots.Count - 1].p ) spline.knots.Remove(spline.knots[spline.knots.Count - 1]); #else int kc = spline.knots.Count - 1; spline.knots[0].invec = spline.knots[kc].invec; spline.knots.Remove(spline.knots[kc]); #endif } break; case 'M': spline = GetSpline(shape); spline.knots.Clear(); cp = ParseV2Split(v, 0); k = new MegaKnot(); k.p = SwapAxis(new Vector3(cp.x, 0.0f, cp.y), shape.axis); k.invec = k.p; k.outvec = k.p; spline.knots.Add(k); break; case 'm': spline = GetSpline(shape); spline.knots.Clear(); Vector3 cp1 = ParseV2Split(v, 0); cp.x += cp1.x; cp.y += cp1.y; k = new MegaKnot(); k.p = SwapAxis(new Vector3(cp.x, 0.0f, cp.y), shape.axis); k.invec = k.p; k.outvec = k.p; spline.knots.Add(k); break; case 'l': coord = v.Split(","[0]); for ( int k0 = 0; k0 < coord.Length; k0 = k0 + 2 ) cp += ParseV2(coord, k0); spline.knots[spline.knots.Count - 1].outvec = SwapAxis(new Vector3(cp.x, 0.0f, cp.y), shape.axis); k = new MegaKnot(); k.p = SwapAxis(new Vector3(cp.x, 0.0f, cp.y), shape.axis); k.invec = SwapAxis(new Vector3(cp.x, 0.0f, cp.y), shape.axis); k.outvec = k.p - (k.invec - k.p); spline.knots.Add(k); break; case 'c': coord = v.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries); for ( int k2 = 0; k2 < coord.Length; k2 += 6 ) { cP1 = cp + ParseV2(coord, k2); cP2 = cp + ParseV2(coord, k2 + 2); cp += ParseV2(coord, k2 + 4); spline.knots[spline.knots.Count - 1].outvec = SwapAxis(new Vector3(cP1.x, 0.0f, cP1.y), shape.axis); k = new MegaKnot(); k.p = SwapAxis(new Vector3(cp.x, 0.0f, cp.y), shape.axis); k.invec = SwapAxis(new Vector3(cP2.x, 0.0f, cP2.y), shape.axis); k.outvec = k.p - (k.invec - k.p); spline.knots.Add(k); } break; case 'L': coord = v.Split(","[0]); for ( int k3 = 0; k3 < coord.Length; k3 = k3 + 2 ) cp = ParseV2(coord, k3); spline.knots[spline.knots.Count - 1].outvec = SwapAxis(new Vector3(cp.x, 0.0f, cp.y), shape.axis); k = new MegaKnot(); k.p = SwapAxis(new Vector3(cp.x, 0.0f, cp.y), shape.axis); k.invec = SwapAxis(new Vector3(cp.x, 0.0f, cp.y), shape.axis); k.outvec = k.p - (k.invec - k.p); spline.knots.Add(k); break; case 'v': //Debug.Log("v: " + v); coord = v.Split(","[0]); for ( int k4 = 0; k4 < coord.Length; k4++ ) cp.y += float.Parse(coord[k4]); spline.knots[spline.knots.Count - 1].outvec = SwapAxis(new Vector3(cp.x, 0.0f, cp.y), shape.axis); k = new MegaKnot(); k.p = SwapAxis(new Vector3(cp.x, 0.0f, cp.y), shape.axis); k.invec = SwapAxis(new Vector3(cp.x, 0.0f, cp.y), shape.axis); k.outvec = k.p - (k.invec - k.p); spline.knots.Add(k); break; case 'V': coord = v.Split(","[0]); for ( int k9 = 0; k9 < coord.Length; k9++ ) cp.y = float.Parse(coord[k9]); spline.knots[spline.knots.Count - 1].outvec = SwapAxis(new Vector3(cp.x, 0.0f, cp.y), shape.axis); k = new MegaKnot(); k.p = SwapAxis(new Vector3(cp.x, 0.0f, cp.y), shape.axis); k.invec = SwapAxis(new Vector3(cp.x, 0.0f, cp.y), shape.axis); k.outvec = k.p - (k.invec - k.p); spline.knots.Add(k); break; case 'h': coord = v.Split(","[0]); for ( int k5 = 0; k5 < coord.Length; k5++ ) cp.x += float.Parse(coord[k5]); spline.knots[spline.knots.Count - 1].outvec = SwapAxis(new Vector3(cp.x, 0.0f, cp.y), shape.axis); k = new MegaKnot(); k.p = SwapAxis(new Vector3(cp.x, 0.0f, cp.y), shape.axis); k.invec = SwapAxis(new Vector3(cp.x, 0.0f, cp.y), shape.axis); k.outvec = k.p - (k.invec - k.p); spline.knots.Add(k); break; case 'H': coord = v.Split(","[0]); for ( int k6 = 0; k6 < coord.Length; k6++ ) cp.x = float.Parse(coord[k6]); spline.knots[spline.knots.Count - 1].outvec = SwapAxis(new Vector3(cp.x, 0.0f, cp.y), shape.axis); k = new MegaKnot(); k.p = SwapAxis(new Vector3(cp.x, 0.0f, cp.y), shape.axis); k.invec = SwapAxis(new Vector3(cp.x, 0.0f, cp.y), shape.axis); k.outvec = k.p - (k.invec - k.p); spline.knots.Add(k); break; case 'S': coord = v.Split(","[0]); for ( int k7 = 0; k7 < coord.Length; k7 = k7 + 4 ) { cp = ParseV2(coord, k7 + 2); cP1 = ParseV2(coord, k7); k = new MegaKnot(); k.p = SwapAxis(new Vector3(cp.x, 0.0f, cp.y), shape.axis); k.invec = SwapAxis(new Vector3(cP1.x, 0.0f, cP1.y), shape.axis); k.outvec = k.p - (k.invec - k.p); spline.knots.Add(k); } break; case 's': coord = v.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries); for ( int k7 = 0; k7 < coord.Length; k7 = k7 + 4 ) { cP1 = cp + ParseV2(coord, k7); cp += ParseV2(coord, k7 + 2); k = new MegaKnot(); k.p = SwapAxis(new Vector3(cp.x, 0.0f, cp.y), shape.axis); k.invec = SwapAxis(new Vector3(cP1.x, 0.0f, cP1.y), shape.axis); k.outvec = k.p - (k.invec - k.p); spline.knots.Add(k); } break; case 'C': coord = v.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries); for ( int k2 = 0; k2 < coord.Length; k2 += 6 ) { cP1 = ParseV2(coord, k2); cP2 = ParseV2(coord, k2 + 2); cp = ParseV2(coord, k2 + 4); spline.knots[spline.knots.Count - 1].outvec = SwapAxis(new Vector3(cP1.x, 0.0f, cP1.y), shape.axis); k = new MegaKnot(); k.p = SwapAxis(new Vector3(cp.x, 0.0f, cp.y), shape.axis); k.invec = SwapAxis(new Vector3(cP2.x, 0.0f, cP2.y), shape.axis); k.outvec = k.p - (k.invec - k.p); spline.knots.Add(k); } break; default: break; } } } break; } } }
// Taken from chain mesher void InitLinkObjects(MegaShape path) { if ( LinkObj == null ) return; float len = path.splines[curve].length; // Assume z axis for now float linklen = (linkOff1.y - linkOff.y) * linkScale.x * LinkSize; linkcount = (int)(len / linklen); for ( int i = linkcount; i < gameObject.transform.childCount; i++ ) { GameObject go = gameObject.transform.GetChild(i).gameObject; if ( Application.isEditor ) DestroyImmediate(go); else Destroy(go); } linkobjs = new Transform[linkcount]; if ( linkcount > gameObject.transform.childCount ) { for ( int i = 0; i < gameObject.transform.childCount; i++ ) { GameObject go = gameObject.transform.GetChild(i).gameObject; #if UNITY_3_5 go.SetActiveRecursively(true); #else go.SetActive(true); #endif linkobjs[i] = go.transform; } int index = gameObject.transform.childCount; for ( int i = index; i < linkcount; i++ ) { GameObject go = new GameObject(); go.name = "Link"; GameObject obj = LinkObj; if ( obj ) { MeshRenderer mr = (MeshRenderer)obj.GetComponent<MeshRenderer>(); Mesh ms = MegaUtils.GetSharedMesh(obj); MeshRenderer mr1 = (MeshRenderer)go.AddComponent<MeshRenderer>(); MeshFilter mf1 = (MeshFilter)go.AddComponent<MeshFilter>(); mf1.sharedMesh = ms; mr1.sharedMaterial = mr.sharedMaterial; go.transform.parent = gameObject.transform; linkobjs[i] = go.transform; } } } else { for ( int i = 0; i < linkcount; i++ ) { GameObject go = gameObject.transform.GetChild(i).gameObject; #if UNITY_3_5 go.SetActiveRecursively(true); #else go.SetActive(true); #endif linkobjs[i] = go.transform; } } Random.seed = 0; for ( int i = 0; i < linkcount; i++ ) { GameObject obj = LinkObj; //1[oi]; GameObject go = gameObject.transform.GetChild(i).gameObject; MeshRenderer mr = (MeshRenderer)obj.GetComponent<MeshRenderer>(); Mesh ms = MegaUtils.GetSharedMesh(obj); MeshRenderer mr1 = (MeshRenderer)go.GetComponent<MeshRenderer>(); MeshFilter mf1 = (MeshFilter)go.GetComponent<MeshFilter>(); mf1.sharedMesh = ms; mr1.sharedMaterials = mr.sharedMaterials; } }
Vector3 PosHandlesSnap(MegaShape shape, Vector3 pos, Quaternion q) { switch ( shape.handleType ) { case MegaHandleType.Position: pos = Handles.PositionHandle(pos, q); break; case MegaHandleType.Free: pos = Handles.FreeMoveHandle(pos, q, shape.KnotSize * 0.01f, Vector3.zero, Handles.CircleCap); break; } if ( shape.usesnap ) { if ( shape.snap.x != 0.0f ) pos.x = (int)(pos.x / shape.snap.x) * shape.snap.x; if ( shape.snap.y != 0.0f ) pos.y = (int)(pos.y / shape.snap.y) * shape.snap.y; if ( shape.snap.z != 0.0f ) pos.z = (int)(pos.z / shape.snap.z) * shape.snap.z; } return pos; }
void ParseEllipse(MegaXMLNode node, MegaShape shape) { MegaSpline spline = GetSpline(shape); float cx = 0.0f; float cy = 0.0f; float rx = 0.0f; float ry = 0.0f; for ( int i = 0; i < node.values.Count; i++ ) { MegaXMLValue val = node.values[i]; switch ( val.name ) { case "cx": cx = float.Parse(val.value); break; case "cy": cy = float.Parse(val.value); break; case "rx": rx = float.Parse(val.value); break; case "ry": ry = float.Parse(val.value); break; } } ry = Mathf.Clamp(ry, 0.0f, float.MaxValue); rx = Mathf.Clamp(rx, 0.0f, float.MaxValue); float radius, xmult, ymult; if ( ry < rx ) { radius = rx; xmult = 1.0f; ymult = ry / rx; } else { if ( rx < ry ) { radius = ry; xmult = rx / ry; ymult = 1.0f; } else { radius = ry; xmult = ymult = 1.0f; } } float vector = CIRCLE_VECTOR_LENGTH * radius; Vector3 mult = new Vector3(xmult, ymult, 1.0f); for ( int ix = 0; ix < 4; ++ix ) { float angle = 6.2831853f * (float)ix / 4.0f; float sinfac = Mathf.Sin(angle); float cosfac = Mathf.Cos(angle); Vector3 p = new Vector3(cosfac * radius + cx, 0.0f, sinfac * radius + cy); Vector3 rotvec = new Vector3(sinfac * vector, 0.0f, -cosfac * vector); //spline.AddKnot(Vector3.Scale(p, mult), Vector3.Scale((p + rotvec), mult), Vector3.Scale((p - rotvec), mult)); //, tm); AddKnot(spline, Vector3.Scale(p, mult), Vector3.Scale((p + rotvec), mult), Vector3.Scale((p - rotvec), mult), shape.axis); //, tm); } spline.closed = true; }
Vector3 PosHandles(MegaShape shape, Vector3 pos, Quaternion q) { switch ( shape.handleType ) { case MegaHandleType.Position: pos = Handles.PositionHandle(pos, q); break; case MegaHandleType.Free: pos = Handles.FreeMoveHandle(pos, q, shape.KnotSize * 0.01f, Vector3.zero, Handles.CircleCap); break; } return pos; }
//public MegaTriangulator(MegaKnot[] points) //{ // m_points = new List<Vector2>(); //points); // //} static public List<int> Triangulate(MegaShape shape, MegaSpline spline, float dist, ref List<Vector3> verts, ref List<Vector2> uvs, ref List<int> indices, Vector3 pivot) { // Find m_points.Clear(); List<MegaKnot> knots = spline.knots; Vector3 min = knots[0].p; Vector3 max = knots[0].p; for ( int i = 1; i < knots.Count; i++ ) { Vector3 p1 = knots[i].p; if ( p1.x < min.x ) min.x = p1.x; if ( p1.y < min.y ) min.y = p1.y; if ( p1.z < min.z ) min.z = p1.z; if ( p1.x > max.x ) max.x = p1.x; if ( p1.y > max.y ) max.y = p1.y; if ( p1.z > max.z ) max.z = p1.z; } Vector3 size = max - min; int removeaxis = 0; if ( Mathf.Abs(size.x) < Mathf.Abs(size.y) ) { if ( Mathf.Abs(size.x) < Mathf.Abs(size.z) ) removeaxis = 0; else removeaxis = 2; } else { if ( Mathf.Abs(size.y) < Mathf.Abs(size.z) ) removeaxis = 1; else removeaxis = 2; } Vector3 tp = Vector3.zero; #if false for ( int i = 0; i < knots.Count; i++ ) { for ( int a = 0; a < steps; a ++ ) { float alpha = (float)a / (float)steps; Vector3 p = spline.knots[i].Interpolate(alpha, spline.knots[i]); switch ( removeaxis ) { case 0: tp.x = p.y; tp.y = p.z; break; case 1: tp.x = p.x; tp.y = p.z; break; case 2: tp.x = p.x; tp.y = p.y; break; } verts.Add(p); m_points.Add(tp); } } #endif float ds = spline.length / (spline.length / dist); if ( ds > spline.length ) ds = spline.length; //int c = 0; int k = -1; //int lk = -1; //Vector3 first = spline.Interpolate(0.0f, shape.normalizedInterp, ref lk); Vector3 p = Vector3.zero; for ( float dst = 0.0f; dst < spline.length; dst += ds ) { float alpha = dst / spline.length; p = spline.Interpolate(alpha, shape.normalizedInterp, ref k) + pivot; switch ( removeaxis ) { case 0: tp.x = p.y; tp.y = p.z; break; case 1: tp.x = p.x; tp.y = p.z; break; case 2: tp.x = p.x; tp.y = p.y; break; } tp.z = dst; verts.Add(p); m_points.Add(tp); // Dont need this here as can do in post step //tp.x = (tp.x - min.x) / size.x; //tp.y = (tp.y - min.z) / size.z; tp.x = (tp.x - min.x); // / size.x; tp.y = (tp.y - min.z); // / size.z; uvs.Add(tp); } //if ( spline.closed ) // p = spline.Interpolate(0.0f, shape.normalizedInterp, ref k); //else // p = spline.Interpolate(1.0f, shape.normalizedInterp, ref k); //switch ( removeaxis ) //{ // case 0: tp.x = p.y; tp.y = p.z; break; // case 1: tp.x = p.x; tp.y = p.z; break; // case 2: tp.x = p.x; tp.y = p.y; break; //} //verts.Add(p); //m_points.Add(tp); return Triangulate(indices); }