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 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 bool UseDistance = true; // use distance method #endregion Fields #region Methods public Quaternion GetRot(MegaSpline spline, float alpha, bool interp, ref Vector3 lastup) { int k = -1; Vector3 ps; Vector3 ps1; Vector3 ps2; if ( spline.closed ) { alpha = Mathf.Repeat(alpha, 1.0f); ps = spline.Interpolate(alpha, interp, ref k); } else ps = spline.InterpCurve3D(alpha, interp, ref k); alpha += tangentDist / target.GetCurveLength(curve); //0.01f; if ( spline.closed ) { alpha = alpha % 1.0f; ps1 = spline.Interpolate(alpha, interp, ref k); } else ps1 = spline.InterpCurve3D(alpha, interp, ref k); ps1.x = ps2.x = ps1.x - ps.x; ps1.y = ps2.y = ps1.y - ps.y; ps1.z = ps2.z = ps1.z - ps.z; //ps1.x -= ps.x; //ps1.y -= ps.y; // * align; //ps1.z -= ps.z; //Debug.Log("lupin: " + lastup); //wtm.SetTRS(ps, Quaternion.LookRotation(ps1, up), Vector3.one); //MegaMatrix.SetTR(ref wtm, ps, Quaternion.LookRotation(ps1, lastup)); Quaternion rot = Quaternion.LookRotation(ps1, lastup); // calc new up value ps2 = ps2.normalized; Vector3 cross = Vector3.Cross(ps2, lastup); lastup = Vector3.Cross(cross, ps2); //Debug.Log("lupout: " + lastup); return rot; }
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); }
public static MegaSpline Copy(MegaSpline src) { MegaSpline spl = new MegaSpline(); spl.closed = src.closed; spl.offset = src.offset; spl.rotate = src.rotate; spl.scale = src.scale; spl.length = src.length; spl.knots = new List<MegaKnot>(); //src.knots); spl.constantSpeed = src.constantSpeed; spl.subdivs = src.subdivs; for ( int i = 0; i < src.knots.Count; i++ ) { MegaKnot knot = new MegaKnot(); knot.p = src.knots[i].p; knot.invec = src.knots[i].invec; knot.outvec = src.knots[i].outvec; knot.seglength = src.knots[i].seglength; knot.length = src.knots[i].length; knot.notlocked = src.knots[i].notlocked; spl.knots.Add(knot); } spl.animations = new List<MegaKnotAnim>(src.animations); return spl; }
public void AutoCurve(MegaSpline spline, int start, int end) { if ( spline.closed ) { int pk = (start - 1) % spline.knots.Count; Vector3 premid = (spline.knots[pk].p + spline.knots[start].p) * 0.5f; for ( int k = start; k < end; k++ ) { int nk = (k + 1) % spline.knots.Count; Vector3 mid = (spline.knots[nk].p + spline.knots[k].p) * 0.5f; Vector3 mp = (mid + premid) * 0.5f; spline.knots[k].invec = spline.knots[k].p + (premid - mp); spline.knots[k].outvec = spline.knots[k].p + (mid - mp); premid = mid; } } else { int pk = (start - 1) % spline.knots.Count; Vector3 premid = (spline.knots[pk].p + spline.knots[start].p) * 0.5f; for ( int k = start; k < end - 1; k++ ) { Vector3 mid = (spline.knots[k + 1].p + spline.knots[k].p) * 0.5f; Vector3 mp = (mid + premid) * 0.5f; spline.knots[k].invec = spline.knots[k].p + (premid - mp); spline.knots[k].outvec = spline.knots[k].p + (mid - mp); premid = mid; } } spline.CalcLength(); //10); }
// Keep track of up method public Matrix4x4 GetDeformMatNewMethod(MegaSpline spline, float alpha, bool interp, float align, ref Vector3 lastup) { int k = -1; Vector3 ps; Vector3 ps1; Vector3 ps2; if ( spline.closed ) { alpha = Mathf.Repeat(alpha, 1.0f); ps = spline.Interpolate(alpha, interp, ref k); } else ps = spline.InterpCurve3D(alpha, interp, ref k); alpha += tangent; //0.01f; if ( spline.closed ) { alpha = alpha % 1.0f; ps1 = spline.Interpolate(alpha, interp, ref k); } else ps1 = spline.InterpCurve3D(alpha, interp, ref k); ps1.x = ps2.x = ps1.x - ps.x; ps1.y = ps2.y = ps1.y - ps.y; ps1.z = ps2.z = ps1.z - ps.z; //ps1.x -= ps.x; //ps1.y -= ps.y; // * align; //ps1.z -= ps.z; ps1.y *= align; Quaternion rot = lastrot; if ( ps1 != Vector3.zero ) rot = Quaternion.LookRotation(ps1, lastup); //wtm.SetTRS(ps, Quaternion.LookRotation(ps1, up), Vector3.one); //Debug.Log("lupin: " + lastup); MegaMatrix.SetTR(ref wtm, ps, rot); //Quaternion.LookRotation(ps1, lastup)); lastrot = rot; // calc new up value ps2 = ps2.normalized; Vector3 cross = Vector3.Cross(ps2, lastup); lastup = Vector3.Cross(cross, ps2); //Debug.Log("lupout: " + lastup); return wtm; }
public override void OnInspectorGUI() { MegaShape shape = (MegaShape)target; EditorGUILayout.BeginHorizontal(); if ( GUILayout.Button("Add Knot") ) { if ( shape.splines == null || shape.splines.Count == 0 ) { MegaSpline spline = new MegaSpline(); // Have methods for these shape.splines.Add(spline); } //Undo.RegisterUndo(target, "Add Knot"); MegaKnot knot = new MegaKnot(); int sp = 0; if ( selected == -1 || shape.splines[0].knots.Count == 1 ) { shape.splines[0].knots.Add(knot); selected = shape.splines[0].knots.Count - 1; } else { if ( selected < shape.splines[0].knots.Count - 1 ) { sp = selected + 1; knot.p = shape.splines[0].knots[selected].Interpolate(0.5f, shape.splines[0].knots[selected + 1]); Vector3 t = shape.splines[0].knots[selected].Interpolate(0.51f, shape.splines[0].knots[selected + 1]); knot.outvec = (t - knot.p); //.Normalize(); knot.outvec.Normalize(); knot.outvec *= shape.splines[0].knots[selected].seglength * 0.25f; knot.invec = -knot.outvec; knot.invec += knot.p; knot.outvec += knot.p; } else { if ( shape.splines[0].closed ) { sp = selected + 1; knot.p = shape.splines[0].knots[selected].Interpolate(0.5f, shape.splines[0].knots[0]); Vector3 t = shape.splines[0].knots[selected].Interpolate(0.51f, shape.splines[0].knots[0]); knot.outvec = (t - knot.p); //.Normalize(); knot.outvec.Normalize(); knot.outvec *= shape.splines[0].knots[selected].seglength * 0.25f; knot.invec = -knot.outvec; knot.invec += knot.p; knot.outvec += knot.p; } else { sp = selected - 1; //Debug.Log("selected " + selected + " count " + shape.splines[0].knots.Count + " sp " + sp); knot.p = shape.splines[0].knots[sp].Interpolate(0.5f, shape.splines[0].knots[sp + 1]); Vector3 t = shape.splines[0].knots[sp].Interpolate(0.51f, shape.splines[0].knots[sp + 1]); knot.outvec = (t - knot.p); //.Normalize(); knot.outvec.Normalize(); knot.outvec *= shape.splines[0].knots[sp].seglength * 0.25f; knot.invec = -knot.outvec; knot.invec += knot.p; knot.outvec += knot.p; sp++; } } shape.splines[0].knots.Insert(sp, knot); selected = sp; //++; } shape.CalcLength(10); EditorUtility.SetDirty(target); } if ( GUILayout.Button("Delete Knot") ) { if ( selected != -1 ) { //Undo.RegisterUndo(target, "Delete Knot"); shape.splines[0].knots.RemoveAt(selected); selected--; shape.CalcLength(10); } EditorUtility.SetDirty(target); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); if ( GUILayout.Button("Match Handles") ) { if ( selected != -1 ) { //Undo.RegisterUndo(target, "Match Handles"); Vector3 p = shape.splines[0].knots[selected].p; Vector3 d = shape.splines[0].knots[selected].outvec - p; shape.splines[0].knots[selected].invec = p - d; shape.CalcLength(10); } EditorUtility.SetDirty(target); } if ( GUILayout.Button("Load") ) { // Load a spl file from max, so delete everything and replace LoadShape(ImportScale); } EditorGUILayout.EndHorizontal(); showcommon = EditorGUILayout.Foldout(showcommon, "Common Params"); bool rebuild = false; //Params(); if ( showcommon ) { ImportScale = EditorGUILayout.FloatField("Import Scale", ImportScale); MegaAxis av = (MegaAxis)EditorGUILayout.EnumPopup("Axis", shape.axis); if ( av != shape.axis ) { shape.axis = av; rebuild = true; } shape.col1 = EditorGUILayout.ColorField("Col 1", shape.col1); shape.col2 = EditorGUILayout.ColorField("Col 2", shape.col2); shape.KnotCol = EditorGUILayout.ColorField("Knot Col", shape.KnotCol); shape.HandleCol = EditorGUILayout.ColorField("Handle Col", shape.HandleCol); shape.VecCol = EditorGUILayout.ColorField("Vec Col", shape.VecCol); shape.KnotSize = EditorGUILayout.FloatField("Knot Size", shape.KnotSize); shape.stepdist = EditorGUILayout.FloatField("Step Dist", shape.stepdist); if ( shape.stepdist < 0.01f ) shape.stepdist = 0.01f; shape.normalizedInterp = EditorGUILayout.Toggle("Normalized Interp", shape.normalizedInterp); shape.drawHandles = EditorGUILayout.Toggle("Draw Handles", shape.drawHandles); shape.drawKnots = EditorGUILayout.Toggle("Draw Knots", shape.drawKnots); shape.drawspline = EditorGUILayout.Toggle("Draw Spline", shape.drawspline); shape.lockhandles = EditorGUILayout.Toggle("Lock Handles", shape.lockhandles); shape.animate = EditorGUILayout.Toggle("Animate", shape.animate); if ( shape.animate ) { shape.time = EditorGUILayout.FloatField("Time", shape.time); shape.MaxTime = EditorGUILayout.FloatField("Loop Time", shape.MaxTime); shape.speed = EditorGUILayout.FloatField("Speed", shape.speed); shape.LoopMode = (MegaRepeatMode)EditorGUILayout.EnumPopup("Loop Mode", shape.LoopMode); } showsplines = EditorGUILayout.Foldout(showsplines, "Splines"); if ( showsplines ) { for ( int i = 0; i < shape.splines.Count; i++ ) { DisplaySpline(shape, shape.splines[i]); } } } if ( Params() ) { rebuild = true; } if ( GUI.changed ) { EditorUtility.SetDirty(target); //shape.CalcLength(10); } if ( rebuild ) { shape.MakeShape(); EditorUtility.SetDirty(target); } }
public void SetState(MegaSpline spline, float t) { }
// Should be a spline method public Matrix4x4 GetDeformMat(MegaSpline spline, float alpha, bool interp) { int k = -1; Vector3 ps; Vector3 ps1; if ( spline.closed ) { alpha = Mathf.Repeat(alpha, 1.0f); ps = spline.Interpolate(alpha, interp, ref k); } else ps = spline.InterpCurve3D(alpha, interp, ref k); alpha += tangent; //0.01f; if ( spline.closed ) { alpha = alpha % 1.0f; ps1 = spline.Interpolate(alpha, interp, ref k); } else ps1 = spline.InterpCurve3D(alpha, interp, ref k); ps1.x -= ps.x; ps1.y = 0.0f; //ps.y; ps1.z -= ps.z; //wtm.SetTRS(ps, Quaternion.LookRotation(ps1, up), Vector3.one); MegaMatrix.SetTR(ref wtm, ps, Quaternion.LookRotation(ps1, up)); return wtm; }
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")); //if ( GUILayout.Button("Outline") ) //{ // shape.OutlineSpline(shape, shape.selcurve, outline, true); // EditorUtility.SetDirty(target); // buildmesh = true; //} showknots = EditorGUILayout.Foldout(showknots, "Knots"); if ( showknots ) { for ( int i = 0; i < spline.knots.Count; i++ ) { DisplayKnot(shape, spline, spline.knots[i]); //EditorGUILayout.Separator(); } } }
bool ParseShape(BinaryReader br, string cid) { MegaShape ms = (MegaShape)target; switch ( cid ) { case "Num": int count = br.ReadInt32(); ms.splines = new List<MegaSpline>(count); break; case "Spline": MegaSpline spl = new MegaSpline(); ms.splines.Add(spl); MegaParse.Parse(br, SplineParse); break; case "Anim": MegaParse.Parse(br, AnimParse); break; } return true; }
public override void OnInspectorGUI() { //undoManager.CheckUndo(); bool buildmesh = false; MegaShape shape = (MegaShape)target; EditorGUILayout.BeginHorizontal(); int curve = shape.selcurve; if ( GUILayout.Button("Add Knot") ) { if ( shape.splines == null || shape.splines.Count == 0 ) { MegaSpline spline = new MegaSpline(); // Have methods for these shape.splines.Add(spline); } //Undo.RegisterUndo(target, "Add Knot"); MegaKnot knot = new MegaKnot(); // Add a point at CursorPos //sp = selected + 1; //Debug.Log("CursorPos " + CursorPos + " CursorKnot " + CursorKnot); float per = shape.CursorPercent * 0.01f; CursorTangent = shape.splines[curve].Interpolate(per + 0.01f, true, ref CursorKnot); //this.GetPositionOnSpline(i) - p; CursorPos = shape.splines[curve].Interpolate(per, true, ref CursorKnot); //this.GetPositionOnSpline(i) - p; knot.p = CursorPos; //CursorTangent = //Vector3 t = shape.splines[0].knots[selected].Interpolate(0.51f, shape.splines[0].knots[0]); knot.outvec = (CursorTangent - knot.p); knot.outvec.Normalize(); knot.outvec *= shape.splines[curve].knots[CursorKnot].seglength * 0.25f; knot.invec = -knot.outvec; knot.invec += knot.p; knot.outvec += knot.p; shape.splines[curve].knots.Insert(CursorKnot + 1, knot); shape.CalcLength(); //10); EditorUtility.SetDirty(target); buildmesh = true; } if ( GUILayout.Button("Delete Knot") ) { if ( selected != -1 ) { //Undo.RegisterUndo(target, "Delete Knot"); shape.splines[curve].knots.RemoveAt(selected); selected--; shape.CalcLength(); //10); } EditorUtility.SetDirty(target); buildmesh = true; } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); if ( GUILayout.Button("Match Handles") ) { if ( selected != -1 ) { //Undo.RegisterUndo(target, "Match Handles"); Vector3 p = shape.splines[curve].knots[selected].p; Vector3 d = shape.splines[curve].knots[selected].outvec - p; shape.splines[curve].knots[selected].invec = p - d; shape.CalcLength(); //10); } EditorUtility.SetDirty(target); buildmesh = true; } if ( GUILayout.Button("Load") ) { // Load a spl file from max, so delete everything and replace LoadShape(ImportScale); buildmesh = true; } if ( GUILayout.Button("Load SXL") ) { // Load a spl file from max, so delete everything and replace LoadSXL(ImportScale); buildmesh = true; } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); if ( GUILayout.Button("AutoCurve") ) { shape.AutoCurve(); EditorUtility.SetDirty(target); buildmesh = true; } if ( GUILayout.Button("Reverse") ) { shape.Reverse(curve); EditorUtility.SetDirty(target); buildmesh = true; } EditorGUILayout.EndHorizontal(); if ( GUILayout.Button("Apply Scaling") ) { shape.Scale(shape.transform.localScale); EditorUtility.SetDirty(target); shape.transform.localScale = Vector3.one; buildmesh = true; } //outline = EditorGUILayout.FloatField("Outline", outline); //if ( GUILayout.Button("Outline") ) //{ // shape.OutlineSpline(shape, shape.selcurve, outline, true); // EditorUtility.SetDirty(target); // buildmesh = true; //} if ( GUILayout.Button("Import SVG") ) { LoadSVG(ImportScale); buildmesh = true; } showcommon = EditorGUILayout.Foldout(showcommon, "Common Params"); bool rebuild = false; //Params(); if ( showcommon ) { //CursorPos = EditorGUILayout.Vector3Field("Cursor", CursorPos); shape.CursorPercent = EditorGUILayout.FloatField("Cursor", shape.CursorPercent); shape.CursorPercent = Mathf.Repeat(shape.CursorPercent, 100.0f); ImportScale = EditorGUILayout.FloatField("Import Scale", ImportScale); MegaAxis av = (MegaAxis)EditorGUILayout.EnumPopup("Axis", shape.axis); if ( av != shape.axis ) { shape.axis = av; rebuild = true; } if ( shape.splines.Count > 1 ) { //shape.selcurve = EditorGUILayout.IntField("Curve", shape.selcurve); shape.selcurve = EditorGUILayout.IntSlider("Curve", shape.selcurve, 0, shape.splines.Count - 1); } if ( shape.selcurve < 0 ) shape.selcurve = 0; if ( shape.selcurve > shape.splines.Count - 1 ) shape.selcurve = shape.splines.Count - 1; EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Colors"); //shape.col1 = EditorGUILayout.ColorField("Col 1", shape.col1); //shape.col2 = EditorGUILayout.ColorField("Col 2", shape.col2); shape.col1 = EditorGUILayout.ColorField(shape.col1); shape.col2 = EditorGUILayout.ColorField(shape.col2); EditorGUILayout.EndHorizontal(); //shape.KnotCol = EditorGUILayout.ColorField("Knot Col", shape.KnotCol); //shape.HandleCol = EditorGUILayout.ColorField("Handle Col", shape.HandleCol); shape.VecCol = EditorGUILayout.ColorField("Vec Col", shape.VecCol); shape.KnotSize = EditorGUILayout.FloatField("Knot Size", shape.KnotSize); shape.stepdist = EditorGUILayout.FloatField("Step Dist", shape.stepdist); MegaSpline spline = shape.splines[shape.selcurve]; if ( shape.stepdist < 0.01f ) shape.stepdist = 0.01f; shape.dolateupdate = EditorGUILayout.Toggle("Do Late Update", shape.dolateupdate); shape.normalizedInterp = EditorGUILayout.Toggle("Normalized Interp", shape.normalizedInterp); spline.constantSpeed = EditorGUILayout.Toggle("Constant Speed", spline.constantSpeed); int subdivs = EditorGUILayout.IntField("Calc Subdivs", spline.subdivs); if ( subdivs < 2 ) subdivs = 2; if ( subdivs != spline.subdivs ) { //spline.subdivs = subdivs; spline.CalcLength(subdivs); } shape.drawHandles = EditorGUILayout.Toggle("Draw Handles", shape.drawHandles); shape.drawKnots = EditorGUILayout.Toggle("Draw Knots", shape.drawKnots); shape.drawspline = EditorGUILayout.Toggle("Draw Spline", shape.drawspline); shape.lockhandles = EditorGUILayout.Toggle("Lock Handles", shape.lockhandles); shape.autosmooth = EditorGUILayout.Toggle("Auto Smooth", shape.autosmooth); shape.handleType = (MegaHandleType)EditorGUILayout.EnumPopup("Handle Type", shape.handleType); showlabels = EditorGUILayout.Toggle("Labels", showlabels); hidewire = EditorGUILayout.Toggle("Hide Wire", hidewire); EditorUtility.SetSelectedWireframeHidden(shape.renderer, hidewire); shape.animate = EditorGUILayout.Toggle("Animate", shape.animate); if ( shape.animate ) { shape.time = EditorGUILayout.FloatField("Time", shape.time); shape.MaxTime = EditorGUILayout.FloatField("Loop Time", shape.MaxTime); shape.speed = EditorGUILayout.FloatField("Speed", shape.speed); shape.LoopMode = (MegaRepeatMode)EditorGUILayout.EnumPopup("Loop Mode", shape.LoopMode); } if ( shape.splines.Count > 0 ) { //shape.selcurve = EditorGUILayout.IntField("Curve", shape.selcurve); if ( spline.outlineSpline != -1 ) { int outlineSpline = EditorGUILayout.IntSlider("Outline Spl", spline.outlineSpline, 0, shape.splines.Count - 1); float outline = EditorGUILayout.FloatField("Outline", spline.outline); if ( outline != spline.outline || outlineSpline != spline.outlineSpline ) { spline.outlineSpline = outlineSpline; spline.outline = outline; if ( outlineSpline != shape.selcurve ) { shape.OutlineSpline(shape.splines[spline.outlineSpline], spline, spline.outline, true); spline.CalcLength(); //10); EditorUtility.SetDirty(target); buildmesh = true; } } } else { outline = EditorGUILayout.FloatField("Outline", outline); if ( GUILayout.Button("Outline") ) { shape.OutlineSpline(shape, shape.selcurve, outline, true); shape.splines[shape.splines.Count - 1].outline = outline; shape.splines[shape.splines.Count - 1].outlineSpline = shape.selcurve; shape.selcurve = shape.splines.Count - 1; EditorUtility.SetDirty(target); buildmesh = true; } } } // Mesher shape.makeMesh = EditorGUILayout.Toggle("Make Mesh", shape.makeMesh); if ( shape.makeMesh ) { shape.meshType = (MeshShapeType)EditorGUILayout.EnumPopup("Mesh Type", shape.meshType); shape.Pivot = EditorGUILayout.Vector3Field("Pivot", shape.Pivot); shape.CalcTangents = EditorGUILayout.Toggle("Calc Tangents", shape.CalcTangents); shape.GenUV = EditorGUILayout.Toggle("Gen UV", shape.GenUV); EditorGUILayout.BeginVertical("Box"); switch ( shape.meshType ) { case MeshShapeType.Fill: shape.DoubleSided = EditorGUILayout.Toggle("Double Sided", shape.DoubleSided); shape.Height = EditorGUILayout.FloatField("Height", shape.Height); //shape.HeightSegs = EditorGUILayout.IntField("HeightSegs", shape.HeightSegs); shape.UseHeightCurve = EditorGUILayout.Toggle("Use Height Crv", shape.UseHeightCurve); if ( shape.UseHeightCurve ) { shape.heightCrv = EditorGUILayout.CurveField("Height Curve", shape.heightCrv); shape.heightOff = EditorGUILayout.Slider("Height Off", shape.heightOff, -1.0f, 1.0f); } shape.mat1 = (Material)EditorGUILayout.ObjectField("Top Mat", shape.mat1, typeof(Material), true); shape.mat2 = (Material)EditorGUILayout.ObjectField("Bot Mat", shape.mat2, typeof(Material), true); shape.mat3 = (Material)EditorGUILayout.ObjectField("Side Mat", shape.mat3, typeof(Material), true); shape.PhysUV = EditorGUILayout.Toggle("Physical UV", shape.PhysUV); shape.UVOffset = EditorGUILayout.Vector2Field("UV Offset", shape.UVOffset); shape.UVRotate = EditorGUILayout.Vector2Field("UV Rotate", shape.UVRotate); shape.UVScale = EditorGUILayout.Vector2Field("UV Scale", shape.UVScale); shape.UVOffset1 = EditorGUILayout.Vector2Field("UV Offset1", shape.UVOffset1); shape.UVRotate1 = EditorGUILayout.Vector2Field("UV Rotate1", shape.UVRotate1); shape.UVScale1 = EditorGUILayout.Vector2Field("UV Scale1", shape.UVScale1); break; //case MeshShapeType.Line: //shape.DoubleSided = EditorGUILayout.Toggle("Double Sided", shape.DoubleSided); //shape.Height = EditorGUILayout.FloatField("Height", shape.Height); //shape.HeightSegs = EditorGUILayout.IntField("HeightSegs", shape.HeightSegs); //shape.heightCrv = EditorGUILayout.CurveField("Height Curve", shape.heightCrv); //shape.Start = EditorGUILayout.FloatField("Start", shape.Start); //shape.End = EditorGUILayout.FloatField("End", shape.End); //shape.Rotate = EditorGUILayout.FloatField("Rotate", shape.Rotate); //break; case MeshShapeType.Tube: shape.TubeStart = EditorGUILayout.Slider("Start", shape.TubeStart, -1.0f, 2.0f); shape.TubeLength = EditorGUILayout.Slider("Length", shape.TubeLength, 0.0f, 1.0f); shape.rotate = EditorGUILayout.FloatField("Rotate", shape.rotate); shape.tsides = EditorGUILayout.IntField("Sides", shape.tsides); shape.tradius = EditorGUILayout.FloatField("Radius", shape.tradius); shape.offset = EditorGUILayout.FloatField("Offset", shape.offset); //shape.SegsPerUnit = EditorGUILayout.FloatField("Segs", shape.SegsPerUnit); shape.scaleX = EditorGUILayout.CurveField("Scale X", shape.scaleX); shape.unlinkScale = EditorGUILayout.BeginToggleGroup("unlink Scale", shape.unlinkScale); shape.scaleY = EditorGUILayout.CurveField("Scale Y", shape.scaleY); EditorGUILayout.EndToggleGroup(); shape.strands = EditorGUILayout.IntField("Strands", shape.strands); if ( shape.strands > 1 ) { shape.strandRadius = EditorGUILayout.FloatField("Strand Radius", shape.strandRadius); shape.TwistPerUnit = EditorGUILayout.FloatField("Twist", shape.TwistPerUnit); shape.startAng = EditorGUILayout.FloatField("Start Twist", shape.startAng); } shape.UVOffset = EditorGUILayout.Vector2Field("UV Offset", shape.UVOffset); shape.uvtilex = EditorGUILayout.FloatField("UV Tile X", shape.uvtilex); shape.uvtiley = EditorGUILayout.FloatField("UV Tile Y", shape.uvtiley); shape.cap = EditorGUILayout.Toggle("Cap", shape.cap); shape.RopeUp = (MegaAxis)EditorGUILayout.EnumPopup("Up", shape.RopeUp); shape.mat1 = (Material)EditorGUILayout.ObjectField("Mat", shape.mat1, typeof(Material), true); //shape.Sides = EditorGUILayout.IntField("Sides", shape.Sides); //shape.TubeStep = EditorGUILayout.FloatField("TubeStep", shape.TubeStep); //shape.Start = EditorGUILayout.FloatField("Start", shape.Start); //shape.End = EditorGUILayout.FloatField("End", shape.End); break; case MeshShapeType.Ribbon: shape.TubeStart = EditorGUILayout.Slider("Start", shape.TubeStart, -1.0f, 2.0f); shape.TubeLength = EditorGUILayout.Slider("Length", shape.TubeLength, 0.0f, 1.0f); shape.boxwidth = EditorGUILayout.FloatField("Width", shape.boxwidth); shape.raxis = (MegaAxis)EditorGUILayout.EnumPopup("Axis", shape.raxis); shape.rotate = EditorGUILayout.FloatField("Rotate", shape.rotate); shape.ribsegs = EditorGUILayout.IntField("Segs", shape.ribsegs); if ( shape.ribsegs < 1 ) shape.ribsegs = 1; shape.offset = EditorGUILayout.FloatField("Offset", shape.offset); shape.scaleX = EditorGUILayout.CurveField("Scale X", shape.scaleX); shape.strands = EditorGUILayout.IntField("Strands", shape.strands); if ( shape.strands > 1 ) { shape.strandRadius = EditorGUILayout.FloatField("Strand Radius", shape.strandRadius); shape.TwistPerUnit = EditorGUILayout.FloatField("Twist", shape.TwistPerUnit); shape.startAng = EditorGUILayout.FloatField("Start Twist", shape.startAng); //shape.SegsPerUnit = EditorGUILayout.FloatField("Segs", shape.SegsPerUnit); } shape.UVOffset = EditorGUILayout.Vector2Field("UV Offset", shape.UVOffset); shape.uvtilex = EditorGUILayout.FloatField("UV Tile X", shape.uvtilex); shape.uvtiley = EditorGUILayout.FloatField("UV Tile Y", shape.uvtiley); shape.RopeUp = (MegaAxis)EditorGUILayout.EnumPopup("Up", shape.RopeUp); shape.mat1 = (Material)EditorGUILayout.ObjectField("Mat", shape.mat1, typeof(Material), true); break; case MeshShapeType.Box: shape.TubeStart = EditorGUILayout.Slider("Start", shape.TubeStart, -1.0f, 2.0f); shape.TubeLength = EditorGUILayout.Slider("Length", shape.TubeLength, 0.0f, 1.0f); shape.rotate = EditorGUILayout.FloatField("Rotate", shape.rotate); shape.boxwidth = EditorGUILayout.FloatField("Box Width", shape.boxwidth); shape.boxheight = EditorGUILayout.FloatField("Box Height", shape.boxheight); shape.offset = EditorGUILayout.FloatField("Offset", shape.offset); //shape.strandRadius = EditorGUILayout.FloatField("Strand Radius", shape.strandRadius); //shape.strandRadius = EditorGUILayout.FloatField("Strand Radius", shape.strandRadius); //shape.SegsPerUnit = EditorGUILayout.FloatField("Segs", shape.SegsPerUnit); shape.scaleX = EditorGUILayout.CurveField("Scale X", shape.scaleX); shape.unlinkScale = EditorGUILayout.BeginToggleGroup("unlink Scale", shape.unlinkScale); shape.scaleY = EditorGUILayout.CurveField("Scale Y", shape.scaleY); EditorGUILayout.EndToggleGroup(); shape.strands = EditorGUILayout.IntField("Strands", shape.strands); if ( shape.strands > 1 ) { shape.tradius = EditorGUILayout.FloatField("Radius", shape.tradius); shape.TwistPerUnit = EditorGUILayout.FloatField("Twist", shape.TwistPerUnit); shape.startAng = EditorGUILayout.FloatField("Start Twist", shape.startAng); } shape.UVOffset = EditorGUILayout.Vector2Field("UV Offset", shape.UVOffset); shape.uvtilex = EditorGUILayout.FloatField("UV Tile X", shape.uvtilex); shape.uvtiley = EditorGUILayout.FloatField("UV Tile Y", shape.uvtiley); shape.cap = EditorGUILayout.Toggle("Cap", shape.cap); shape.RopeUp = (MegaAxis)EditorGUILayout.EnumPopup("Up", shape.RopeUp); shape.mat1 = (Material)EditorGUILayout.ObjectField("Mat", shape.mat1, typeof(Material), true); //shape.Sides = EditorGUILayout.IntField("Sides", shape.Sides); //shape.TubeStep = EditorGUILayout.FloatField("TubeStep", shape.TubeStep); //shape.Start = EditorGUILayout.FloatField("Start", shape.Start); //shape.End = EditorGUILayout.FloatField("End", shape.End); break; } if ( shape.strands < 1 ) shape.strands = 1; EditorGUILayout.EndVertical(); } else { //shape.shapemesh = null; shape.ClearMesh(); } showsplines = EditorGUILayout.Foldout(showsplines, "Spline Data"); if ( showsplines ) { EditorGUILayout.BeginVertical("Box"); if ( shape.splines != null && shape.splines.Count > 0 ) DisplaySpline(shape, shape.splines[shape.selcurve]); EditorGUILayout.EndVertical(); #if false for ( int i = 0; i < shape.splines.Count; i++ ) { // We should only show the selected curve EditorGUILayout.BeginVertical("Box"); DisplaySpline(shape, shape.splines[i]); EditorGUILayout.EndVertical(); } #endif } EditorGUILayout.BeginHorizontal(); Color col = GUI.backgroundColor; GUI.backgroundColor = Color.green; if ( GUILayout.Button("Add") ) { // Create a new spline in the shape MegaSpline spl = MegaSpline.Copy(shape.splines[shape.selcurve]); shape.splines.Add(spl); shape.selcurve = shape.splines.Count - 1; EditorUtility.SetDirty(shape); buildmesh = true; } if ( shape.splines.Count > 1 ) { GUI.backgroundColor = Color.red; if ( GUILayout.Button("Delete") ) { // Delete current spline shape.splines.RemoveAt(shape.selcurve); for ( int i = 0; i < shape.splines.Count; i++ ) { if ( shape.splines[i].outlineSpline == shape.selcurve ) shape.splines[i].outlineSpline = -1; if ( shape.splines[i].outlineSpline > shape.selcurve ) shape.splines[i].outlineSpline--; } shape.selcurve--; if ( shape.selcurve < 0 ) shape.selcurve = 0; EditorUtility.SetDirty(shape); buildmesh = true; } } GUI.backgroundColor = col; EditorGUILayout.EndHorizontal(); } if ( !shape.imported ) { if ( Params() ) { rebuild = true; } } if ( GUI.changed ) { EditorUtility.SetDirty(target); //shape.CalcLength(10); buildmesh = true; } if ( rebuild ) { shape.MakeShape(); EditorUtility.SetDirty(target); buildmesh = true; } if ( buildmesh ) { if ( shape.makeMesh ) { shape.SetMats(); shape.BuildMesh(); } } //undoManager.CheckDirty(); }
void DisplayKnot(MegaShape shape, MegaSpline spline, MegaKnot knot) { bool recalc = false; Vector3 p = EditorGUILayout.Vector3Field("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); } }
public void OnSceneGUI() { MegaLoftLayerComplex layer = (MegaLoftLayerComplex)target; MegaShapeLoft loft = layer.gameObject.GetComponent <MegaShapeLoft>(); if (loft == null) { return; } if (layer.layerPath == null) { return; } if (!layer.showsections) { return; } MegaSpline pathspline = layer.layerPath.splines[layer.curve]; Matrix4x4 pathtm = Matrix4x4.identity; if (layer.SnapToPath) { pathtm = layer.layerPath.transform.localToWorldMatrix; } Matrix4x4 twisttm = Matrix4x4.identity; Matrix4x4 tm; float offx = 0.0f; float offy = 0.0f; float offz = 0.0f; Vector3 lastup = locup; for (int i = 1; i < layer.sections.Count - 1; i++) { MegaLoftSection section = layer.sections[i]; float alpha = section.alpha; if (layer.useOffsetX) { offx = layer.offsetCrvX.Evaluate(alpha); } if (layer.useOffsetY) { offy = layer.offsetCrvY.Evaluate(alpha); } if (layer.useOffsetZ) { offz += layer.offsetCrvZ.Evaluate(alpha); } //if ( layer.useTwistCrv ) //{ // float twist = layer.twistCrv.Evaluate(alpha); // MegaShapeUtils.RotateZ(ref twisttm, Mathf.Deg2Rad * twist); // tm = pathtm * layer.GetDeformMat(pathspline, alpha, layer.layerPath.normalizedInterp) * twisttm; //} //else // tm = pathtm * layer.GetDeformMat(pathspline, alpha, layer.layerPath.normalizedInterp); if (layer.useTwistCrv) { float twist = layer.twistCrv.Evaluate(section.alpha); float tw1 = pathspline.GetTwist(section.alpha); MegaShapeUtils.RotateZ(ref twisttm, Mathf.Deg2Rad * (twist - tw1)); if (layer.frameMethod == MegaFrameMethod.Old) { tm = pathtm * layer.GetDeformMat(pathspline, section.alpha, layer.layerPath.normalizedInterp) * twisttm; } else { tm = pathtm * layer.GetDeformMatNewMethod(pathspline, section.alpha, layer.layerPath.normalizedInterp, ref lastup) * twisttm; } } else { if (layer.frameMethod == MegaFrameMethod.Old) { tm = pathtm * layer.GetDeformMat(pathspline, section.alpha, layer.layerPath.normalizedInterp); } else { tm = pathtm * layer.GetDeformMatNewMethod(pathspline, section.alpha, layer.layerPath.normalizedInterp, ref lastup); } } Vector3 p = section.crossverts[0]; if (layer.useScaleXCrv) { p.x *= layer.scaleCrvX.Evaluate(alpha); } if (layer.useScaleYCrv) { p.y *= layer.scaleCrvY.Evaluate(alpha); } p.x += offx; p.y += offy; p.z += offz; Vector3 tp = p; p = tm.MultiplyPoint3x4(p); p += layer.offset; Matrix4x4 tantm = pathtm * layer.GetDeformMat(pathspline, alpha + 0.01f, layer.layerPath.normalizedInterp); Vector3 tan = tantm.MultiplyPoint3x4(tp); tan += layer.offset; tan = (tan - p).normalized; Vector3 p1 = section.crossverts[section.crossverts.Length - 1]; if (layer.useScaleXCrv) { p1.x *= layer.scaleCrvX.Evaluate(alpha); } if (layer.useScaleYCrv) { p1.y *= layer.scaleCrvY.Evaluate(alpha); } p1.x += offx; p1.y += offy; p1.z += offz; tp = p1; p1 = tm.MultiplyPoint3x4(p1); p1 += layer.offset; Handles.color = Color.yellow; p = loft.transform.TransformPoint(p); Vector3 pn = Handles.Slider(p, tan, layer.handlesize, Handles.SphereCap, 0.0f); pn = pn - p; float delta = pn.magnitude; if (Vector3.Dot(tan, pn) < 0.0f) { delta = -delta; } section.alpha += delta * 0.0005f; float al = section.alpha; // + delta * 0.0005f; if (al != layer.sections[i].alpha) { if (i > 0) { if (al < layer.sections[i - 1].alpha) { al = layer.sections[i - 1].alpha; } } if (i < layer.sections.Count - 1) { if (al > layer.sections[i + 1].alpha) { al = layer.sections[i + 1].alpha; } } layer.sections[i].alpha = al; } if (delta != 0.0f) { GUI.changed = true; loft.rebuild = true; EditorUtility.SetDirty(target); } tan = tantm.MultiplyPoint3x4(tp); tan += layer.offset; tan = (tan - p1).normalized; p1 = loft.transform.TransformPoint(p1); pn = Handles.Slider(p1, tan, layer.handlesize, Handles.SphereCap, 0.0f); pn = pn - p1; delta = pn.magnitude; //Vector3.Distance(p, pn); if (Vector3.Dot(tan, pn) < 0.0f) { delta = -delta; } al = section.alpha + delta * 0.0005f; if (al != layer.sections[i].alpha) { if (i > 0) { if (al < layer.sections[i - 1].alpha) { al = layer.sections[i - 1].alpha; } } if (i < layer.sections.Count - 1) { if (al > layer.sections[i + 1].alpha) { al = layer.sections[i + 1].alpha; } } layer.sections[i].alpha = al; } if (delta != 0.0f) { GUI.changed = true; loft.rebuild = true; EditorUtility.SetDirty(target); } } if (layer.sections.Count > 0) { if (layer.sections[0].alpha != 0.0f) { layer.sections[0].alpha = 0.0f; } for (int i = 1; i < layer.sections.Count - 1; i++) { if (layer.sections[i].alpha <= layer.sections[i - 1].alpha) { layer.sections[i - 1].alpha = layer.sections[i].alpha; } if (layer.sections[i].alpha >= layer.sections[i + 1].alpha) { layer.sections[i].alpha = layer.sections[i + 1].alpha; } } if (layer.sections[layer.sections.Count - 1].alpha != 1.0f) { layer.sections[layer.sections.Count - 1].alpha = 1.0f; } } }
static void DrawPath(MegaLoftLayerComplex layer) { MegaShapeLoft loft = layer.gameObject.GetComponent <MegaShapeLoft>(); if (loft == null) { return; } if (layer.layerPath == null) { return; } if (layer.sections == null || layer.sections.Count < 2) { return; } for (int i = 0; i < layer.sections.Count; i++) { if (layer.sections[i].crossverts == null || layer.sections[i].crossverts.Length == 0) { return; } } MegaSpline pathspline = layer.layerPath.splines[layer.curve]; Matrix4x4 pathtm = Matrix4x4.identity; if (layer.SnapToPath) { pathtm = layer.layerPath.transform.localToWorldMatrix; } Color col = Gizmos.color; Matrix4x4 twisttm = Matrix4x4.identity; Matrix4x4 tm; Vector3 lastup = locup; for (int i = 0; i < layer.sections.Count; i++) { MegaLoftSection section = layer.sections[i]; //if ( layer.useTwistCrv ) //{ // float twist = layer.twistCrv.Evaluate(section.alpha); // MegaShapeUtils.RotateZ(ref twisttm, Mathf.Deg2Rad * twist); // tm = pathtm * layer.GetDeformMat(pathspline, section.alpha, layer.layerPath.normalizedInterp) * twisttm; //loft.); //} //else // tm = pathtm * layer.GetDeformMat(pathspline, section.alpha, layer.layerPath.normalizedInterp); //loft.); if (layer.useTwistCrv) { float twist = layer.twistCrv.Evaluate(section.alpha); float tw1 = pathspline.GetTwist(section.alpha); MegaShapeUtils.RotateZ(ref twisttm, Mathf.Deg2Rad * (twist - tw1)); if (layer.frameMethod == MegaFrameMethod.Old) { tm = pathtm * layer.GetDeformMat(pathspline, section.alpha, layer.layerPath.normalizedInterp) * twisttm; } else { tm = pathtm * layer.GetDeformMatNewMethod(pathspline, section.alpha, layer.layerPath.normalizedInterp, ref lastup) * twisttm; } } else { if (layer.frameMethod == MegaFrameMethod.Old) { tm = pathtm * layer.GetDeformMat(pathspline, section.alpha, layer.layerPath.normalizedInterp); } else { tm = pathtm * layer.GetDeformMatNewMethod(pathspline, section.alpha, layer.layerPath.normalizedInterp, ref lastup); } } Vector3 p1 = section.crossverts[0]; float offx = 0.0f; float offy = 0.0f; float offz = 0.0f; float sclx = 1.0f; float scly = 1.0f; if (layer.useScaleXCrv) { p1.x *= layer.scaleCrvX.Evaluate(section.alpha); } if (layer.useScaleYCrv) { p1.y *= layer.scaleCrvY.Evaluate(section.alpha); } if (layer.useOffsetX) { offx = layer.offsetCrvX.Evaluate(section.alpha); } if (layer.useOffsetY) { offy = layer.offsetCrvY.Evaluate(section.alpha); } if (layer.useOffsetZ) { offz = layer.offsetCrvZ.Evaluate(section.alpha); } if (layer.useScaleXCrv) { sclx = layer.scaleCrvX.Evaluate(section.alpha); } if (layer.useScaleYCrv) { scly = layer.scaleCrvY.Evaluate(section.alpha); } p1 = tm.MultiplyPoint3x4(p1); p1 += layer.offset; Gizmos.color = seccol; //Color.red; Vector3 mid = Vector3.zero; for (int v = 1; v < section.crossverts.Length; v++) { Vector3 p = section.crossverts[v]; p.x *= sclx; p.y *= scly; p.x += offx; p.y += offy; p.z += offz; p = tm.MultiplyPoint3x4(p); p += layer.offset; Gizmos.DrawLine(loft.transform.TransformPoint(p1), loft.transform.TransformPoint(p)); p1 = p; if (v == section.crossverts.Length / 2) { mid = p; } } Handles.color = Color.white; Handles.Label(loft.transform.TransformPoint(mid), "Cross: " + i); Gizmos.color = col; } // Draw outside edge Vector3 sclc = Vector3.one; float lerp = 0.0f; // The position stuff here is waht we could use instead of mesh verts Vector3 last = Vector3.zero; Vector3 last1 = Vector3.zero; lastup = locup; for (float alpha = 0.0f; alpha <= 1.0f; alpha += 0.005f) { if (layer.useScaleXCrv) { sclc.x = layer.scaleCrvX.Evaluate(alpha); } if (layer.useScaleYCrv) { sclc.y = layer.scaleCrvY.Evaluate(alpha); } //if ( layer.useTwistCrv ) //{ // float twist = layer.twistCrv.Evaluate(alpha); // MegaShapeUtils.RotateZ(ref twisttm, Mathf.Deg2Rad * twist); // tm = pathtm * layer.GetDeformMat(layer.layerPath.splines[layer.curve], alpha, layer.layerPath.normalizedInterp) * twisttm; //loft.); //} //else // tm = pathtm * layer.GetDeformMat(layer.layerPath.splines[layer.curve], alpha, layer.layerPath.normalizedInterp); //loft.); if (layer.useTwistCrv) { float twist = layer.twistCrv.Evaluate(alpha); float tw1 = pathspline.GetTwist(alpha); MegaShapeUtils.RotateZ(ref twisttm, Mathf.Deg2Rad * (twist - tw1)); if (layer.frameMethod == MegaFrameMethod.Old) { tm = pathtm * layer.GetDeformMat(pathspline, alpha, layer.layerPath.normalizedInterp) * twisttm; } else { tm = pathtm * layer.GetDeformMatNewMethod(pathspline, alpha, layer.layerPath.normalizedInterp, ref lastup) * twisttm; } } else { if (layer.frameMethod == MegaFrameMethod.Old) { tm = pathtm * layer.GetDeformMat(pathspline, alpha, layer.layerPath.normalizedInterp); } else { tm = pathtm * layer.GetDeformMatNewMethod(pathspline, alpha, layer.layerPath.normalizedInterp, ref lastup); } } // Need to get the crosssection for the given alpha and the lerp value int csect = layer.GetSection(alpha, out lerp); lerp = layer.ease.easing(0.0f, 1.0f, lerp); MegaLoftSection cs1 = layer.sections[csect]; MegaLoftSection cs2 = layer.sections[csect + 1]; Vector3 p = Vector3.Lerp(cs1.crossverts[0], cs2.crossverts[0], lerp); // * sclc; Vector3 p1 = Vector3.Lerp(cs1.crossverts[cs1.crossverts.Length - 1], cs2.crossverts[cs2.crossverts.Length - 1], lerp); // * sclc; if (layer.useScaleXCrv) { p.x *= sclc.x; p1.x *= sclc.x; } if (layer.useScaleYCrv) { p.y *= sclc.y; p1.y *= sclc.y; } if (layer.useOffsetX) { p.x += layer.offsetCrvX.Evaluate(alpha); p1.x += layer.offsetCrvX.Evaluate(alpha); } if (layer.useOffsetY) { p.y += layer.offsetCrvY.Evaluate(alpha); p1.y += layer.offsetCrvY.Evaluate(alpha); } if (layer.useOffsetZ) { p.z += layer.offsetCrvZ.Evaluate(alpha); p1.z += layer.offsetCrvZ.Evaluate(alpha); } p = tm.MultiplyPoint3x4(p); p += layer.offset; p1 = tm.MultiplyPoint3x4(p1); p1 += layer.offset; if (alpha > 0.0f) { Gizmos.DrawLine(loft.transform.TransformPoint(last), loft.transform.TransformPoint(p)); Gizmos.DrawLine(loft.transform.TransformPoint(last1), loft.transform.TransformPoint(p1)); } last = p; last1 = p1; } }
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); } 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(); } } }
static public float veccalc(float angstep) { if ( lastin == angstep ) return lastout; float totdist; float sinfac = Mathf.Sin(angstep); float cosfac = Mathf.Cos(angstep); float test; int ix; MegaSpline work = new MegaSpline(); Vector3 k1 = new Vector3(Mathf.Cos(0.0f), Mathf.Sin(0.0f), 0.0f); Vector3 k2 = new Vector3(cosfac, sinfac, 0.0f); float hi = 1.5f; float lo = 0.0f; int count = 200; // Loop thru test vectors loop: work.knots.Clear(); test = (hi + lo) / 2.0f; Vector3 outv = k1 + new Vector3(0.0f, test, 0.0f); Vector3 inv = k2 + new Vector3(sinfac * test, -cosfac * test, 0.0f); work.AddKnot(k1, k1, outv); work.AddKnot(k2, inv, k2); totdist = 0.0f; int k = 0; //totdist = work.CalcLength(10); for ( ix = 0; ix < 10; ++ix ) { Vector3 terp = work.Interpolate((float)ix / 10.0f, false, ref k); totdist += Mathf.Sqrt(terp.x * terp.x + terp.y * terp.y); } totdist /= 10.0f; count--; if ( totdist == 1.0f || count <= 0 ) goto done; if ( totdist > 1.0f ) { hi = test; goto loop; } lo = test; goto loop; done: lastin = angstep; lastout = test; return test; }
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; } } }
// Get the different id sections void FindSections(MegaSpline spline) { if ( spline != null ) { meshsections.Clear(); int id = spline.knots[0].id - 1; for ( int i = 0; i < spline.knots.Count; i++ ) { if ( spline.knots[i].id != id ) { id = spline.knots[i].id; MegaMeshSection msect = new MegaMeshSection(); msect.mat = FindMaterial(id); meshsections.Add(msect); } } } }
void AddKnot(MegaSpline spline, Vector3 p, Vector3 invec, Vector3 outvec, MegaAxis axis) { spline.AddKnot(SwapAxis(p, axis), SwapAxis(invec, axis), SwapAxis(outvec, axis)); }
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); }
public override void MakeShape() { Matrix4x4 tm = GetMatrix(); radius = Mathf.Clamp(radius, 0, float.MaxValue); sides = Mathf.Clamp(sides, 3, 100); //LimitValue( circular, MIN_CIRCULAR, MAX_CIRCULAR ); //fillet = Mathf.Clamp(fillet, 0.0f, float.MaxValue); //LimitValue( scribe, CIRCUMSCRIBED, INSCRIBED ); float vector; float userad = radius; // If circumscribed, modify the radius if (scribe) //== CIRCUMSCRIBED ) { userad = radius / Mathf.Cos((Mathf.PI * 2.0f) / ((float)sides * 2.0f)); } MegaSpline spline = NewSpline(); // Determine the vector length if circular if (circular) { vector = veccalc(6.2831853f / (float)sides) * userad; } else { vector = 0.0f; } // Now add all the necessary points if ((fillet == 0.0f) || circular) { for (int ix = 0; ix < sides; ++ix) { float angle = 6.2831853f * (float)ix / (float)sides; float sinfac = Mathf.Sin(angle); float cosfac = Mathf.Cos(angle); Vector3 p = new Vector3(cosfac * userad, sinfac * userad, 0.0f); Vector3 rotvec = new Vector3(sinfac * vector, -cosfac * vector, 0.0f); spline.AddKnot(p, p + rotvec, p - rotvec, tm); } spline.closed = true; } else { for (int ix = 0; ix < sides; ++ix) { float angle = 6.2831853f * (float)ix / (float)sides; float theta2 = (Mathf.PI * (sides - 2) / sides) / 2.0f; float fang = angle + theta2; float fang2 = angle - theta2; float f = fillet * Mathf.Tan((Mathf.PI * 0.5f) - theta2); float sinfac = Mathf.Sin(angle); float cosfac = Mathf.Cos(angle); Vector3 p = new Vector3(cosfac * userad, sinfac * userad, 0.0f); //,p1,p2; Vector3 fvec1 = new Vector3(-Mathf.Cos(fang), -Mathf.Sin(fang), 0.0f) * f; Vector3 fvec2 = new Vector3(-Mathf.Cos(fang2), -Mathf.Sin(fang2), 0.0f) * f; Vector3 p1 = p + fvec1; Vector3 p2 = p + fvec2; Vector3 p1vec = fvec1 * CIRCLE_VECTOR_LENGTH; Vector3 p2vec = fvec2 * CIRCLE_VECTOR_LENGTH; spline.AddKnot(p1, p1 + p1vec, p1 - p1vec, tm); spline.AddKnot(p2, p2 - p2vec, p2 + p2vec, tm); } spline.closed = true; //spline->ComputeBezPoints(); } CalcLength(); //10); }
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; }
void Update() { if ( building ) { Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit info; bool hit = Physics.Raycast(mouseRay, out info); if ( Input.GetMouseButtonUp(0) || hit == false ) { building = false; // Finish of line and make spline if ( ValidSpline() ) FinishSpline(obj, lasthitpos); else Destroy(obj); obj = null; cspline = null; cshape = null; cloft = null; } else { if ( hit ) { Vector3 hp = info.point; hp.y += offset; float dist = Vector3.Distance(lasthitpos, hp); travelled += dist; if ( travelled > updatedist ) { cspline.AddKnot(cshape.transform.worldToLocalMatrix.MultiplyPoint3x4(hp), Vector3.zero, Vector3.zero); cshape.AutoCurve(); travelled -= updatedist; } else { cspline.knots[cspline.knots.Count - 1].p = cshape.transform.worldToLocalMatrix.MultiplyPoint3x4(hp); cshape.AutoCurve(); if ( cspline.knots.Count == 2 ) { float dist1 = cspline.KnotDistance(0, 1); if ( dist1 > 0.1f ) { if ( cloft ) cloft.rebuild = true; } } else { if ( cspline.knots.Count > 2 ) { if ( cloft ) cloft.rebuild = true; } } } lasthitpos = hp; } } } else { if ( Input.GetMouseButtonDown(0) ) { Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit info; bool hit = Physics.Raycast(mouseRay, out info); if ( hit ) { Vector3 hp = info.point; hp.y += offset; lasthitpos = hp; travelled = 0.0f; obj = CreateSpline(hp); building = true; if ( cloft ) cloft.rebuild = true; } } } }
public Matrix4x4 GetDeformMatNew(MegaSpline spline, float alpha, bool interp, float align) { int k = -1; Vector3 ps; Vector3 ps1; if ( spline.closed ) { alpha = Mathf.Repeat(alpha, 1.0f); ps = spline.Interpolate(alpha, interp, ref k); } else ps = spline.InterpCurve3D(alpha, interp, ref k); alpha += tangent; //0.01f; if ( spline.closed ) { alpha = alpha % 1.0f; ps1 = spline.Interpolate(alpha, interp, ref k); } else ps1 = spline.InterpCurve3D(alpha, interp, ref k); ps1.x -= ps.x; ps1.y -= ps.y; // * align; ps1.z -= ps.z; ps1.y *= align; //wtm.SetTRS(ps, Quaternion.LookRotation(ps1, up), Vector3.one); Quaternion rot = lastrot; if ( ps1 != Vector3.zero ) rot = Quaternion.LookRotation(ps1, up); MegaMatrix.SetTR(ref wtm, ps, rot); lastrot = rot; return wtm; }
void Update() { if (Application.isPlaying) { distance += speed * Time.deltaTime; } if (path) { float cdist = distance; MegaSpline spline = path.splines[curve]; // Start at front of train and work backwards, first obj in list is head of train for (int i = 0; i < carriages.Count; i++) { float alpha = cdist / spline.length; MegaCarriage car = carriages[i]; float tw = 0.0f; float tw1 = 0.0f; car.b1 = path.transform.TransformPoint(path.InterpCurve3D(curve, alpha, true, ref tw)); float d = cdist - car.length; //bogey2Offset.z; float alpha1 = d / spline.length; car.b2 = path.transform.TransformPoint(path.InterpCurve3D(curve, alpha1, true, ref tw1)); car.cp = (car.b1 + car.b2) * 0.5f; //if ( showrays ) // Debug.DrawLine(b1, b2); // Carriage is positioned half way between the bogeys if (car.carriage) { // Offset can adjust this, so lerp from b1 to b2 based on offset //Vector3 cp = (b1 + b2) * 0.5f; //float twist = (tw + tw1) * 0.5f; //Quaternion trot = Quaternion.Euler(0.0f, twist, 0.0f); car.carriage.transform.position = car.cp + car.carriageOffset; Quaternion erot = Quaternion.Euler(car.rot); Quaternion rot = Quaternion.LookRotation(car.b1 - car.b2); car.carriage.transform.rotation = rot * erot; // * trot; } if (car.bogey1 && car.carriage) { car.bogey1.transform.position = car.carriage.transform.localToWorldMatrix.MultiplyPoint(car.bogey1Offset); //b1; Quaternion erot = Quaternion.Euler(car.bogey1Rot); float a = alpha - (car.bogeyoff / spline.length); car.bp1 = path.transform.TransformPoint(path.InterpCurve3D(curve, a, true)); Vector3 p2 = path.transform.TransformPoint(path.InterpCurve3D(curve, a + 0.0001f, true)); Quaternion rot = Quaternion.LookRotation(p2 - car.bp1); car.bogey1.transform.rotation = rot * erot; //if ( showrays ) // Debug.DrawLine(car.cp, car.bp1, Color.red); } if (car.bogey2 && car.carriage) { car.bogey2.transform.position = car.carriage.transform.localToWorldMatrix.MultiplyPoint(car.bogey2Offset); //b1; Quaternion erot = Quaternion.Euler(car.bogey2Rot); //float tw = 0.0f; float a = alpha1 + (car.bogeyoff / spline.length); car.bp2 = path.transform.TransformPoint(path.InterpCurve3D(curve, a, true, ref tw)); Vector3 p2 = path.transform.TransformPoint(path.InterpCurve3D(curve, a + 0.0001f, true)); Quaternion rot = Quaternion.LookRotation(p2 - car.bp2); //Quaternion trot = Quaternion.Euler(-tw, 0.0f, 0.0f); car.bogey2.transform.rotation = rot * erot; // * trot; //if ( showrays ) // Debug.DrawLine(cp, p1, Color.blue); } // Then move back a bit for next car cdist -= car.length; } } }
GameObject CreateSpline(Vector3 pos) { GameObject obj = new GameObject(); obj.name = name + " - Spline " + splinecount++; obj.transform.position = transform.position; //obj.transform.parent = transform; MegaShape shape = obj.AddComponent<MegaShape>(); shape.smoothness = smooth; shape.drawHandles = false; MegaSpline spline = shape.splines[0]; spline.knots.Clear(); spline.constantSpeed = constantspd; spline.subdivs = 40; shape.cap = true; Vector3[] ps = new Vector3[2]; ps[0] = obj.transform.worldToLocalMatrix.MultiplyPoint3x4(pos); ps[1] = obj.transform.worldToLocalMatrix.MultiplyPoint3x4(pos); shape.BuildSpline(0, ps, false); shape.CalcLength(); shape.drawTwist = true; shape.boxwidth = width; shape.boxheight = height; shape.offset = -height * 0.5f; shape.stepdist = meshstep; //width * 2.0f * 10.0f; shape.SetMats(); spline.closed = closed; cspline = spline; cshape = shape; if ( loft ) { MegaShapeLoft newloft = loft.GetClone(); newloft.name = name + " - Loft " + (splinecount - 1); newloft.transform.position = obj.transform.position; newloft.transform.rotation = obj.transform.rotation; MegaLoftLayerBase[] layers = newloft.GetComponents<MegaLoftLayerBase>(); for ( int i = 0; i < layers.Length; i++ ) { layers[i].layerPath = cshape; } newloft.rebuild = true; //newloft.gameObject.SetActiveRecursively(true); #if UNITY_3_5 newloft.gameObject.SetActiveRecursively(true); #else newloft.gameObject.SetActive(true); #endif cloft = newloft; } return obj; }
public void LoadXML(string sxldata, float scale, bool cspeed, string name, float smoothness, bool combine) { GameObject root = new GameObject(); root.name = name; // Get bounds for imports AdjustPoints(scale); // Create a new shape object for each way for (int i = 0; i < osmways.Count; i++) { MegaShapeOSMWay way = osmways[i]; if (way.nodes.Count > 1 && CanImport(way)) { GameObject osmobj = new GameObject(); osmobj.transform.position = Vector3.zero; osmobj.transform.parent = root.transform; if (way.name.Length == 0) { way.name = "No Name"; } osmobj.name = GetName(way); //way.name; MegaShape shape = (MegaShape)osmobj.AddComponent <MegaShape>(); shape.smoothness = smoothness; //smooth; shape.drawHandles = false; MegaSpline spline = shape.splines[0]; //NewSpline(shape); spline.knots.Clear(); spline.constantSpeed = cspeed; spline.subdivs = 40; bool closed = false; int count = way.nodes.Count; if (way.nodes[0] == way.nodes[count - 1]) { count--; closed = true; } Vector3[] points = new Vector3[count]; for (int j = 0; j < count; j++) { MegaShapeOSMNode nd = FindNode(way.nodes[j]); points[j] = nd.pos; } Bounds bounds = new Bounds(points[0], Vector3.zero); for (int k = 0; k < points.Length; k++) { bounds.Encapsulate(points[k]); } for (int k = 0; k < points.Length; k++) { points[k] -= bounds.center; } osmobj.transform.position = bounds.center; //Vector3.zero; shape.BuildSpline(0, points, closed); shape.drawTwist = true; shape.makeMesh = false; shape.imported = true; shape.CoordAdjust(scale, new Vector3(1.0f, 1.0f, 1.0f), 0); shape.CalcLength(); //10); shape.stepdist = (shape.splines[0].length / shape.splines[0].knots.Count) / 1.0f; } } osmnodes.Clear(); osmways.Clear(); tags.Clear(); }
public Matrix4x4 GetDeformMat(MegaSpline spline, float alpha, bool interp) { int k = -1; //Vector3 ps = spline.Interpolate(alpha, interp, ref k); Vector3 ps = spline.InterpCurve3D(alpha, interp, ref k); alpha += 0.01f; // TODO: Tangent value if ( spline.closed ) alpha = alpha % 1.0f; //Vector3 ps1 = spline.Interpolate(alpha, interp, ref k); Vector3 ps1 = spline.InterpCurve3D(alpha, interp, ref k); ps1.x -= ps.x; ps1.y -= ps.y; ps1.z -= ps.z; ps1.y *= PathTeeter; //wtm1.SetTRS(ps, Quaternion.LookRotation(ps1, locup), Vector3.one); MegaMatrix.SetTR(ref wtm1, ps, Quaternion.LookRotation(ps1, locup)); return wtm1; }
static void DrawGizmos(MegaTracks track, Color modcol1) { Matrix4x4 RingTM = Matrix4x4.identity; Matrix4x4 tm = track.transform.localToWorldMatrix; if (track.shape == null) { return; } MegaSpline spl = track.shape.splines[track.curve]; float ldist = 1.0f * 0.1f; if (ldist < 0.01f) { ldist = 0.01f; } Color modcol = modcol1; if (spl.length / ldist > 500.0f) { ldist = spl.length / 500.0f; } float ds = spl.length / (spl.length / ldist); if (ds > spl.length) { ds = spl.length; } int c = 0; int k = -1; int lk = -1; Vector3 first = spl.Interpolate(0.0f, true, ref lk); RingTM = tm * RingTM; for (float dist = ds; dist < spl.length; dist += ds) { float alpha = dist / spl.length; Vector3 pos = spl.Interpolate(alpha, true, ref k); if ((c & 1) == 1) { Gizmos.color = Color.black * modcol; } else { Gizmos.color = Color.yellow * modcol; } if (k != lk) { for (lk = lk + 1; lk <= k; lk++) { Gizmos.DrawLine(RingTM.MultiplyPoint(first), RingTM.MultiplyPoint(spl.knots[lk].p)); first = spl.knots[lk].p; } } lk = k; Gizmos.DrawLine(RingTM.MultiplyPoint(first), RingTM.MultiplyPoint(pos)); c++; first = pos; } if ((c & 1) == 1) { Gizmos.color = Color.blue * modcol; } else { Gizmos.color = Color.yellow * modcol; } Vector3 lastpos; if (spl.closed) { lastpos = spl.Interpolate(0.0f, true, ref k); } else { lastpos = spl.Interpolate(1.0f, true, ref k); } Gizmos.DrawLine(RingTM.MultiplyPoint(first), RingTM.MultiplyPoint(lastpos)); }
public Matrix4x4 GetDeformMatNewMethod(MegaSpline spline, float alpha, bool interp, ref Vector3 lastup) { int k = -1; //Vector3 ps = spline.Interpolate(alpha, interp, ref k); Vector3 ps = spline.InterpCurve3D(alpha, interp, ref k); alpha += 0.01f; // TODO: Tangent value if ( spline.closed ) alpha = alpha % 1.0f; //Vector3 ps1 = spline.Interpolate(alpha, interp, ref k); Vector3 ps1 = spline.InterpCurve3D(alpha, interp, ref k); Vector3 ps2; ps1.x = ps2.x = ps1.x - ps.x; ps1.y = ps2.y = ps1.y - ps.y; ps1.z = ps2.z = ps1.z - ps.z; //ps1.x -= ps.x; //ps1.y -= ps.y; //ps1.z -= ps.z; ps1.y *= PathTeeter; //wtm1.SetTRS(ps, Quaternion.LookRotation(ps1, locup), Vector3.one); MegaMatrix.SetTR(ref wtm1, ps, Quaternion.LookRotation(ps1, lastup)); //locup)); // calc new up value ps2 = ps2.normalized; Vector3 cross = Vector3.Cross(ps2, lastup); lastup = Vector3.Cross(cross, ps2); return wtm1; }
void Update() { if (building) { Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit info; bool hit = Physics.Raycast(mouseRay, out info); if (Input.GetMouseButtonUp(0) || hit == false) { building = false; // Finish of line and make spline if (ValidSpline()) { FinishSpline(obj, lasthitpos); } else { Destroy(obj); } obj = null; cspline = null; cshape = null; } else { if (hit) { Vector3 hp = info.point; hp.y += offset; float dist = Vector3.Distance(lasthitpos, hp); travelled += dist; if (travelled > updatedist) { cspline.AddKnot(cshape.transform.worldToLocalMatrix.MultiplyPoint3x4(hp), Vector3.zero, Vector3.zero); cshape.AutoCurve(); travelled -= updatedist; } else { cspline.knots[cspline.knots.Count - 1].p = cshape.transform.worldToLocalMatrix.MultiplyPoint3x4(hp); cshape.AutoCurve(); if (cspline.knots.Count == 2) { float dist1 = cspline.KnotDistance(0, 1); if (dist1 > 0.1f) { cshape.BuildMesh(); } } else { if (cspline.knots.Count > 2) { cshape.BuildMesh(); } } } lasthitpos = hp; } } } else { if (Input.GetMouseButtonDown(0)) { Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit info; bool hit = Physics.Raycast(mouseRay, out info); if (hit) { Vector3 hp = info.point; hp.y += offset; lasthitpos = hp; travelled = 0.0f; obj = CreateSpline(hp); building = true; } } } }
public override void MakeShape() { Matrix4x4 tm = GetMatrix(); Vector3 p = Vector3.zero; // The actual point float angle; // Angle of the current point radius1 = Mathf.Clamp(radius1, MIN_RADIUS, MAX_RADIUS); radius2 = Mathf.Clamp(radius2, MIN_RADIUS, MAX_RADIUS); distortion = Mathf.Clamp(distortion, MIN_DIST, MAX_DIST); points = Mathf.Clamp(points, MIN_POINTS, MAX_POINTS); fillet1 = Mathf.Clamp(fillet1, MIN_RADIUS, MAX_RADIUS); fillet2 = Mathf.Clamp(fillet2, MIN_RADIUS, MAX_RADIUS); // Delete the existing shape and create a new spline in it if ( splines.Count == 0 ) { MegaSpline newspline = new MegaSpline(); splines.Add(newspline); } MegaSpline spline = splines[0]; spline.knots.Clear(); float distort = PI180 * distortion; float PIpts = Mathf.PI / (float)points; // Now add all the necessary points for ( int ix = 0; ix < (2 * points); ++ix ) { if ( (ix % 2) != 0 ) // Points for radius 1 { angle = Mathf.PI * (float)ix / (float)points; p.x = Mathf.Cos(angle) * radius1; p.y = Mathf.Sin(angle) * radius1; p.z = 0.0f; if ( fillet1 > 0.0f ) { float theta1 = angle - PIpts; float theta2 = angle + PIpts; float stheta1 = Mathf.Sin(theta1); float stheta2 = Mathf.Sin(theta2); float ctheta1 = Mathf.Cos(theta1); float ctheta2 = Mathf.Cos(theta2); Vector3 plast = new Vector3(radius2 * ctheta1, radius2 * stheta1, 0.0f); Vector3 pnext = new Vector3(radius2 * ctheta2, radius2 * stheta2, 0.0f); Vector3 n1 = Vector3.Normalize(plast - p) * fillet1; Vector3 n2 = Vector3.Normalize(pnext - p) * fillet1; Vector3 nk1 = n1 * CIRCLE_VECTOR_LENGTH; Vector3 nk2 = n2 * CIRCLE_VECTOR_LENGTH; Vector3 p1 = p + n1; Vector3 p2 = p + n2; spline.AddKnot(p1, p1 + nk1, p1 - nk1, tm); spline.AddKnot(p2, p2 - nk2, p2 + nk2, tm); } else spline.AddKnot(p, p, p, tm); } else // Points for radius 2 with optional angular offset { angle = PIpts * (float)ix + distort; p.x = Mathf.Cos(angle) * radius2; p.y = Mathf.Sin(angle) * radius2; p.z = 0.0f; if ( fillet2 > 0.0f ) { float theta1 = angle - PIpts - distort; float theta2 = angle + PIpts + distort; float stheta1 = Mathf.Sin(theta1); float stheta2 = Mathf.Sin(theta2); float ctheta1 = Mathf.Cos(theta1); float ctheta2 = Mathf.Cos(theta2); Vector3 plast = new Vector3(radius1 * ctheta1, radius1 * stheta1, 0.0f); Vector3 pnext = new Vector3(radius1 * ctheta2, radius1 * stheta2, 0.0f); Vector3 n1 = Vector3.Normalize(plast - p) * fillet2; Vector3 n2 = Vector3.Normalize(pnext - p) * fillet2; Vector3 nk1 = n1 * CIRCLE_VECTOR_LENGTH; Vector3 nk2 = n2 * CIRCLE_VECTOR_LENGTH; Vector3 p1 = p + n1; Vector3 p2 = p + n2; spline.AddKnot(p1, p1 + nk1, p1 - nk1, tm); spline.AddKnot(p2, p2 - nk2, p2 + nk2, tm); } else spline.AddKnot(p, p, p, tm); } } spline.closed = true; CalcLength(10); //stepdist = spline.length / 80.0f; }
//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)); }
// Calc tangents for knots public void AutoCurve(MegaSpline spline) { if ( spline.closed ) { Vector3 premid = (spline.knots[spline.knots.Count - 1].p + spline.knots[0].p) * 0.5f; for ( int k = 0; k < spline.knots.Count; k++ ) { int nk = (k + 1) % spline.knots.Count; Vector3 mid = (spline.knots[nk].p + spline.knots[k].p) * 0.5f; Vector3 mp = (mid + premid) * 0.5f; spline.knots[k].invec = spline.knots[k].p + (premid - mp); spline.knots[k].outvec = spline.knots[k].p + (mid - mp); premid = mid; } } else { Vector3 premid = (spline.knots[1].p + spline.knots[0].p) * 0.5f; for ( int k = 1; k < spline.knots.Count - 1; k++ ) { Vector3 mid = (spline.knots[k + 1].p + spline.knots[k].p) * 0.5f; Vector3 mp = (mid + premid) * 0.5f; spline.knots[k].invec = spline.knots[k].p + (premid - mp); spline.knots[k].outvec = spline.knots[k].p + (mid - mp); premid = mid; } } spline.CalcLength(); //10); }
static public float veccalc(float angstep) { if (lastin == angstep) { return(lastout); } float totdist; float sinfac = Mathf.Sin(angstep); float cosfac = Mathf.Cos(angstep); float test; int ix; MegaSpline work = new MegaSpline(); Vector3 k1 = new Vector3(Mathf.Cos(0.0f), Mathf.Sin(0.0f), 0.0f); Vector3 k2 = new Vector3(cosfac, sinfac, 0.0f); float hi = 1.5f; float lo = 0.0f; int count = 200; // Loop thru test vectors loop: work.knots.Clear(); test = (hi + lo) / 2.0f; Vector3 outv = k1 + new Vector3(0.0f, test, 0.0f); Vector3 inv = k2 + new Vector3(sinfac * test, -cosfac * test, 0.0f); work.AddKnot(k1, k1, outv); work.AddKnot(k2, inv, k2); totdist = 0.0f; int k = 0; //totdist = work.CalcLength(10); for (ix = 0; ix < 10; ++ix) { Vector3 terp = work.Interpolate((float)ix / 10.0f, false, ref k); totdist += Mathf.Sqrt(terp.x * terp.x + terp.y * terp.y); } totdist /= 10.0f; count--; if (totdist == 1.0f || count <= 0) { goto done; } if (totdist > 1.0f) { hi = test; goto loop; } lo = test; goto loop; done: lastin = angstep; lastout = test; return(test); }
public void BuildSpline(Vector3[] points, bool closed) { MegaSpline spline = new MegaSpline(); spline.knots = new List<MegaKnot>(points.Length); for ( int i = 0; i < points.Length; i++ ) { MegaKnot knot = new MegaKnot(); knot.p = points[i]; spline.knots.Add(knot); } spline.closed = closed; splines.Add(spline); AutoCurve(spline); }
// Keep track of up method public Matrix4x4 GetDeformMatNewMethod(MegaSpline spline, float alpha, bool interp, float align, ref Vector3 lastup) { int k = -1; Vector3 ps; Vector3 ps1; Vector3 ps2; if (spline.closed) { alpha = Mathf.Repeat(alpha, 1.0f); ps = spline.Interpolate(alpha, interp, ref k); } else { ps = spline.InterpCurve3D(alpha, interp, ref k); } alpha += tangent; //0.01f; if (spline.closed) { alpha = alpha % 1.0f; ps1 = spline.Interpolate(alpha, interp, ref k); } else { ps1 = spline.InterpCurve3D(alpha, interp, ref k); } ps1.x = ps2.x = ps1.x - ps.x; ps1.y = ps2.y = ps1.y - ps.y; ps1.z = ps2.z = ps1.z - ps.z; //ps1.x -= ps.x; //ps1.y -= ps.y; // * align; //ps1.z -= ps.z; ps1.y *= align; Quaternion rot = lastrot; if (ps1 != Vector3.zero) { rot = Quaternion.LookRotation(ps1, lastup); } //wtm.SetTRS(ps, Quaternion.LookRotation(ps1, up), Vector3.one); //Debug.Log("lupin: " + lastup); MegaMatrix.SetTR(ref wtm, ps, rot); //Quaternion.LookRotation(ps1, lastup)); lastrot = rot; // calc new up value ps2 = ps2.normalized; Vector3 cross = Vector3.Cross(ps2, lastup); lastup = Vector3.Cross(cross, ps2); //Debug.Log("lupout: " + lastup); return(wtm); }
//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); }
public override void MakeShape() { float totalRadians = Mathf.PI * 2.0f * turns; if (clockwise) { totalRadians *= -1.0f; } float deltaRadius = radius2 - radius1; //float quarterTurns = turns * 4.0f; float power = 1.0f; if (bias > 0.0f) { power = bias * 9.0f + 1.0f; } else { if (bias < 0.0f) { power = -bias * 9.0f + 1.0f; } } MegaSpline spline = NewSpline(); // Compute some helpful stuff... int points = (int)(turns * (float)PointsPerTurn); if (points == 0) { points = 1; } float fPoints = (float)points; float vector1 = veccalc(totalRadians / fPoints); //.5f; for (int i = 0; i <= points; ++i) { float pct = (float)i / fPoints; float r = radius1 + deltaRadius * pct; float hpct = pct; if (bias > 0.0f) { hpct = 1.0f - Mathf.Pow(1.0f - pct, power); } else { if (bias < 0.0f) { hpct = Mathf.Pow(pct, power); } } float angle = totalRadians * pct; float cosfac = Mathf.Cos(angle); float sinfac = Mathf.Sin(angle); //float vector = CIRCLE_VECTOR_LENGTH * r; float vector = vector1 * r * adjust; //veccalc(totalRadians / fPoints) * r * 0.4f; //.5f; Vector3 p = new Vector3(cosfac * r, height * hpct, sinfac * r); Vector3 rotvec = new Vector3(sinfac * vector, 0.0f, -cosfac * vector); spline.AddKnot(p, p - rotvec, p + rotvec); } spline.closed = false; CalcLength(10); }
public override void MakeShape() { Matrix4x4 tm = GetMatrix(); //lastout = 0.0f; //lastin = -9999.0f; PointsPerTurn = Mathf.Clamp(PointsPerTurn, 3, 100); // Delete all points in the existing spline MegaSpline spline = NewSpline(); float fromrad = 0.0f; //from * Mathf.Deg2Rad; float torad = turns * Mathf.PI * 2.0f; //to * Mathf.Deg2Rad; // Order angles properly if (fromrad > torad) { torad += Mathf.PI * 2.0f; } float totalRadians = Mathf.PI * 2.0f * turns; if (clockwise) { totalRadians *= -1.0f; } int points = (int)(turns * (float)PointsPerTurn); if (points == 0) { points = 1; } float fPoints = (float)points; float totAngle = torad - fromrad; float vector1 = veccalc(totAngle / fPoints); // * radius1; float deltaRadius = radius2 - radius1; float power = 1.0f; if (bias > 0.0f) { power = bias * 9.0f + 1.0f; } else { if (bias < 0.0f) { power = -bias * 9.0f + 1.0f; } } // Now add all the necessary points //float angStep = totAngle / (float)POINTS_PER_TURN; for (int ix = 0; ix <= points; ++ix) { float pct = (float)ix / fPoints; float r = radius1 + deltaRadius * pct; float hpct = pct; if (bias > 0.0f) { hpct = 1.0f - Mathf.Pow(1.0f - pct, power); } else { if (bias < 0.0f) { hpct = Mathf.Pow(pct, power); } } float angle = totalRadians * pct; //fromrad + (float)ix * angStep; float sinfac = Mathf.Sin(angle); float cosfac = Mathf.Cos(angle); float vector = vector1 * r; Vector3 p = new Vector3(cosfac * r, sinfac * r, height * hpct); Vector3 rotvec = new Vector3(sinfac * vector, -cosfac * vector, 0.0f); Vector3 invec = (ix == 0) ? p : p + rotvec; Vector3 outvec = (ix == 3) ? p : p - rotvec; if (!clockwise) { spline.AddKnot(p, invec, outvec, tm); } else { spline.AddKnot(p, outvec, invec, tm); } } CalcLength(10); //if ( reverse ) //spline->Reverse(TRUE); }
public override void OnInspectorGUI() { //undoManager.CheckUndo(); bool buildmesh = false; bool recalc = false; MegaShape shape = (MegaShape)target; EditorGUILayout.BeginHorizontal(); int curve = shape.selcurve; if ( GUILayout.Button("Add Knot") ) { if ( shape.splines == null || shape.splines.Count == 0 ) { MegaSpline spline = new MegaSpline(); // Have methods for these shape.splines.Add(spline); } MegaKnot knot = new MegaKnot(); float per = shape.CursorPercent * 0.01f; CursorTangent = shape.splines[curve].Interpolate(per + 0.01f, true, ref CursorKnot); //this.GetPositionOnSpline(i) - p; CursorPos = shape.splines[curve].Interpolate(per, true, ref CursorKnot); //this.GetPositionOnSpline(i) - p; knot.p = CursorPos; knot.outvec = (CursorTangent - knot.p); knot.outvec.Normalize(); knot.outvec *= shape.splines[curve].knots[CursorKnot].seglength * 0.25f; knot.invec = -knot.outvec; knot.invec += knot.p; knot.outvec += knot.p; knot.twist = shape.splines[curve].knots[CursorKnot].twist; knot.id = shape.splines[curve].knots[CursorKnot].id; shape.splines[curve].knots.Insert(CursorKnot + 1, knot); shape.CalcLength(); //10); EditorUtility.SetDirty(target); buildmesh = true; } if ( GUILayout.Button("Delete Knot") ) { if ( selected != -1 ) { shape.splines[curve].knots.RemoveAt(selected); selected--; shape.CalcLength(); //10); } EditorUtility.SetDirty(target); buildmesh = true; } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); if ( GUILayout.Button("Match Handles") ) { if ( selected != -1 ) { Vector3 p = shape.splines[curve].knots[selected].p; Vector3 d = shape.splines[curve].knots[selected].outvec - p; shape.splines[curve].knots[selected].invec = p - d; shape.CalcLength(); //10); } EditorUtility.SetDirty(target); buildmesh = true; } if ( GUILayout.Button("Load") ) { LoadShape(ImportScale); buildmesh = true; } if ( GUILayout.Button("Load SXL") ) { LoadSXL(ImportScale); buildmesh = true; } if ( GUILayout.Button("Load KML") ) { LoadKML(ImportScale); buildmesh = true; } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); if ( GUILayout.Button("AutoCurve") ) { shape.AutoCurve(); EditorUtility.SetDirty(target); buildmesh = true; } if ( GUILayout.Button("Reverse") ) { shape.Reverse(curve); //shape.CalcLength(); EditorUtility.SetDirty(target); buildmesh = true; } EditorGUILayout.EndHorizontal(); if ( GUILayout.Button("Centre Shape") ) { shape.Centre(1.0f, Vector3.one); } if ( GUILayout.Button("Apply Scaling") ) { shape.Scale(shape.transform.localScale); EditorUtility.SetDirty(target); shape.transform.localScale = Vector3.one; buildmesh = true; } if ( GUILayout.Button("Import SVG") ) { LoadSVG(ImportScale); buildmesh = true; } showcommon = EditorGUILayout.Foldout(showcommon, "Common Params"); bool rebuild = false; //Params(); if ( showcommon ) { shape.CursorPercent = EditorGUILayout.FloatField("Cursor", shape.CursorPercent); shape.CursorPercent = Mathf.Repeat(shape.CursorPercent, 100.0f); ImportScale = EditorGUILayout.FloatField("Import Scale", ImportScale); MegaAxis av = (MegaAxis)EditorGUILayout.EnumPopup("Axis", shape.axis); if ( av != shape.axis ) { shape.axis = av; rebuild = true; } if ( shape.splines.Count > 1 ) shape.selcurve = EditorGUILayout.IntSlider("Curve", shape.selcurve, 0, shape.splines.Count - 1); if ( shape.selcurve < 0 ) shape.selcurve = 0; if ( shape.selcurve > shape.splines.Count - 1 ) shape.selcurve = shape.splines.Count - 1; EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Colors"); shape.col1 = EditorGUILayout.ColorField(shape.col1); shape.col2 = EditorGUILayout.ColorField(shape.col2); EditorGUILayout.EndHorizontal(); shape.VecCol = EditorGUILayout.ColorField("Vec Col", shape.VecCol); shape.KnotSize = EditorGUILayout.FloatField("Knot Size", shape.KnotSize); shape.stepdist = EditorGUILayout.FloatField("Step Dist", shape.stepdist); MegaSpline spline = shape.splines[shape.selcurve]; if ( shape.stepdist < 0.01f ) shape.stepdist = 0.01f; shape.dolateupdate = EditorGUILayout.Toggle("Do Late Update", shape.dolateupdate); shape.normalizedInterp = EditorGUILayout.Toggle("Normalized Interp", shape.normalizedInterp); spline.constantSpeed = EditorGUILayout.Toggle("Constant Speed", spline.constantSpeed); int subdivs = EditorGUILayout.IntField("Calc Subdivs", spline.subdivs); if ( subdivs < 2 ) subdivs = 2; if ( subdivs != spline.subdivs ) spline.CalcLength(subdivs); shape.drawHandles = EditorGUILayout.Toggle("Draw Handles", shape.drawHandles); shape.drawKnots = EditorGUILayout.Toggle("Draw Knots", shape.drawKnots); shape.drawTwist = EditorGUILayout.Toggle("Draw Twist", shape.drawTwist); shape.drawspline = EditorGUILayout.Toggle("Draw Spline", shape.drawspline); shape.showorigin = EditorGUILayout.Toggle("Origin Handle", shape.showorigin); shape.lockhandles = EditorGUILayout.Toggle("Lock Handles", shape.lockhandles); shape.updateondrag = EditorGUILayout.Toggle("Update On Drag", shape.updateondrag); shape.usesnap = EditorGUILayout.BeginToggleGroup("Use Snap", shape.usesnap); shape.usesnaphandles = EditorGUILayout.Toggle("Snap Handles", shape.usesnaphandles); shape.snap = EditorGUILayout.Vector3Field("Snap", shape.snap); if ( shape.snap.x < 0.0f ) shape.snap.x = 0.0f; if ( shape.snap.y < 0.0f ) shape.snap.y = 0.0f; if ( shape.snap.z < 0.0f ) shape.snap.z = 0.0f; EditorGUILayout.EndToggleGroup(); //shape.autosmooth = EditorGUILayout.Toggle("Auto Smooth", shape.autosmooth); //shape.smoothness = EditorGUILayout.FloatField("Smoothness", shape.smoothness); float smoothness = EditorGUILayout.Slider("Smoothness", shape.smoothness, 0.0f, 1.5f); if ( smoothness != shape.smoothness ) { shape.smoothness = smoothness; shape.AutoCurve(); recalc = true; } shape.handleType = (MegaHandleType)EditorGUILayout.EnumPopup("Handle Type", shape.handleType); showlabels = EditorGUILayout.Toggle("Labels", showlabels); bool hidewire1 = EditorGUILayout.Toggle("Hide Wire", hidewire); if ( hidewire1 != hidewire ) { hidewire = hidewire1; EditorUtility.SetSelectedWireframeHidden(shape.GetComponent<Renderer>(), hidewire); } shape.animate = EditorGUILayout.Toggle("Animate", shape.animate); if ( shape.animate ) { shape.time = EditorGUILayout.FloatField("Time", shape.time); shape.MaxTime = EditorGUILayout.FloatField("Loop Time", shape.MaxTime); shape.speed = EditorGUILayout.FloatField("Speed", shape.speed); shape.LoopMode = (MegaRepeatMode)EditorGUILayout.EnumPopup("Loop Mode", shape.LoopMode); } AnimationKeyFrames(shape); if ( shape.splines.Count > 0 ) { if ( spline.outlineSpline != -1 ) { int outlineSpline = EditorGUILayout.IntSlider("Outline Spl", spline.outlineSpline, 0, shape.splines.Count - 1); float outline = EditorGUILayout.FloatField("Outline", spline.outline); if ( outline != spline.outline || outlineSpline != spline.outlineSpline ) { spline.outlineSpline = outlineSpline; spline.outline = outline; if ( outlineSpline != shape.selcurve ) { shape.OutlineSpline(shape.splines[spline.outlineSpline], spline, spline.outline, true); spline.CalcLength(); //10); EditorUtility.SetDirty(target); buildmesh = true; } } } else { outline = EditorGUILayout.FloatField("Outline", outline); if ( GUILayout.Button("Outline") ) { shape.OutlineSpline(shape, shape.selcurve, outline, true); shape.splines[shape.splines.Count - 1].outline = outline; shape.splines[shape.splines.Count - 1].outlineSpline = shape.selcurve; shape.selcurve = shape.splines.Count - 1; EditorUtility.SetDirty(target); buildmesh = true; } } } // Mesher shape.makeMesh = EditorGUILayout.Toggle("Make Mesh", shape.makeMesh); if ( shape.makeMesh ) { shape.meshType = (MeshShapeType)EditorGUILayout.EnumPopup("Mesh Type", shape.meshType); shape.Pivot = EditorGUILayout.Vector3Field("Pivot", shape.Pivot); shape.CalcTangents = EditorGUILayout.Toggle("Calc Tangents", shape.CalcTangents); shape.GenUV = EditorGUILayout.Toggle("Gen UV", shape.GenUV); if ( GUILayout.Button("Build LightMap") ) { MegaShapeLightMapWindow.Init(); } EditorGUILayout.BeginVertical("Box"); switch ( shape.meshType ) { case MeshShapeType.Fill: shape.DoubleSided = EditorGUILayout.Toggle("Double Sided", shape.DoubleSided); shape.Height = EditorGUILayout.FloatField("Height", shape.Height); shape.UseHeightCurve = EditorGUILayout.Toggle("Use Height Crv", shape.UseHeightCurve); if ( shape.UseHeightCurve ) { shape.heightCrv = EditorGUILayout.CurveField("Height Curve", shape.heightCrv); shape.heightOff = EditorGUILayout.Slider("Height Off", shape.heightOff, -1.0f, 1.0f); } shape.mat1 = (Material)EditorGUILayout.ObjectField("Top Mat", shape.mat1, typeof(Material), true); shape.mat2 = (Material)EditorGUILayout.ObjectField("Bot Mat", shape.mat2, typeof(Material), true); shape.mat3 = (Material)EditorGUILayout.ObjectField("Side Mat", shape.mat3, typeof(Material), true); shape.PhysUV = EditorGUILayout.Toggle("Physical UV", shape.PhysUV); shape.UVOffset = EditorGUILayout.Vector2Field("UV Offset", shape.UVOffset); shape.UVRotate = EditorGUILayout.Vector2Field("UV Rotate", shape.UVRotate); shape.UVScale = EditorGUILayout.Vector2Field("UV Scale", shape.UVScale); shape.UVOffset1 = EditorGUILayout.Vector2Field("UV Offset1", shape.UVOffset1); shape.UVRotate1 = EditorGUILayout.Vector2Field("UV Rotate1", shape.UVRotate1); shape.UVScale1 = EditorGUILayout.Vector2Field("UV Scale1", shape.UVScale1); break; case MeshShapeType.Tube: shape.TubeStart = EditorGUILayout.Slider("Start", shape.TubeStart, -1.0f, 2.0f); shape.TubeLength = EditorGUILayout.Slider("Length", shape.TubeLength, 0.0f, 1.0f); shape.rotate = EditorGUILayout.FloatField("Rotate", shape.rotate); shape.tsides = EditorGUILayout.IntField("Sides", shape.tsides); shape.tradius = EditorGUILayout.FloatField("Radius", shape.tradius); shape.offset = EditorGUILayout.FloatField("Offset", shape.offset); shape.scaleX = EditorGUILayout.CurveField("Scale X", shape.scaleX); shape.unlinkScale = EditorGUILayout.BeginToggleGroup("unlink Scale", shape.unlinkScale); shape.scaleY = EditorGUILayout.CurveField("Scale Y", shape.scaleY); EditorGUILayout.EndToggleGroup(); shape.strands = EditorGUILayout.IntField("Strands", shape.strands); if ( shape.strands > 1 ) { shape.strandRadius = EditorGUILayout.FloatField("Strand Radius", shape.strandRadius); shape.TwistPerUnit = EditorGUILayout.FloatField("Twist", shape.TwistPerUnit); shape.startAng = EditorGUILayout.FloatField("Start Twist", shape.startAng); } shape.UVOffset = EditorGUILayout.Vector2Field("UV Offset", shape.UVOffset); shape.uvtilex = EditorGUILayout.FloatField("UV Tile X", shape.uvtilex); shape.uvtiley = EditorGUILayout.FloatField("UV Tile Y", shape.uvtiley); shape.cap = EditorGUILayout.Toggle("Cap", shape.cap); shape.RopeUp = (MegaAxis)EditorGUILayout.EnumPopup("Up", shape.RopeUp); shape.mat1 = (Material)EditorGUILayout.ObjectField("Mat", shape.mat1, typeof(Material), true); shape.flipNormals = EditorGUILayout.Toggle("Flip Normals", shape.flipNormals); break; case MeshShapeType.Ribbon: shape.TubeStart = EditorGUILayout.Slider("Start", shape.TubeStart, -1.0f, 2.0f); shape.TubeLength = EditorGUILayout.Slider("Length", shape.TubeLength, 0.0f, 1.0f); shape.boxwidth = EditorGUILayout.FloatField("Width", shape.boxwidth); shape.raxis = (MegaAxis)EditorGUILayout.EnumPopup("Axis", shape.raxis); shape.rotate = EditorGUILayout.FloatField("Rotate", shape.rotate); shape.ribsegs = EditorGUILayout.IntField("Segs", shape.ribsegs); if ( shape.ribsegs < 1 ) shape.ribsegs = 1; shape.offset = EditorGUILayout.FloatField("Offset", shape.offset); shape.scaleX = EditorGUILayout.CurveField("Scale X", shape.scaleX); shape.strands = EditorGUILayout.IntField("Strands", shape.strands); if ( shape.strands > 1 ) { shape.strandRadius = EditorGUILayout.FloatField("Strand Radius", shape.strandRadius); shape.TwistPerUnit = EditorGUILayout.FloatField("Twist", shape.TwistPerUnit); shape.startAng = EditorGUILayout.FloatField("Start Twist", shape.startAng); } shape.UVOffset = EditorGUILayout.Vector2Field("UV Offset", shape.UVOffset); shape.uvtilex = EditorGUILayout.FloatField("UV Tile X", shape.uvtilex); shape.uvtiley = EditorGUILayout.FloatField("UV Tile Y", shape.uvtiley); shape.RopeUp = (MegaAxis)EditorGUILayout.EnumPopup("Up", shape.RopeUp); shape.mat1 = (Material)EditorGUILayout.ObjectField("Mat", shape.mat1, typeof(Material), true); shape.flipNormals = EditorGUILayout.Toggle("Flip Normals", shape.flipNormals); break; case MeshShapeType.Box: shape.TubeStart = EditorGUILayout.Slider("Start", shape.TubeStart, -1.0f, 2.0f); shape.TubeLength = EditorGUILayout.Slider("Length", shape.TubeLength, 0.0f, 1.0f); shape.rotate = EditorGUILayout.FloatField("Rotate", shape.rotate); shape.boxwidth = EditorGUILayout.FloatField("Box Width", shape.boxwidth); shape.boxheight = EditorGUILayout.FloatField("Box Height", shape.boxheight); shape.offset = EditorGUILayout.FloatField("Offset", shape.offset); shape.scaleX = EditorGUILayout.CurveField("Scale X", shape.scaleX); shape.unlinkScale = EditorGUILayout.BeginToggleGroup("unlink Scale", shape.unlinkScale); shape.scaleY = EditorGUILayout.CurveField("Scale Y", shape.scaleY); EditorGUILayout.EndToggleGroup(); shape.strands = EditorGUILayout.IntField("Strands", shape.strands); if ( shape.strands > 1 ) { shape.tradius = EditorGUILayout.FloatField("Radius", shape.tradius); shape.TwistPerUnit = EditorGUILayout.FloatField("Twist", shape.TwistPerUnit); shape.startAng = EditorGUILayout.FloatField("Start Twist", shape.startAng); } shape.UVOffset = EditorGUILayout.Vector2Field("UV Offset", shape.UVOffset); shape.uvtilex = EditorGUILayout.FloatField("UV Tile X", shape.uvtilex); shape.uvtiley = EditorGUILayout.FloatField("UV Tile Y", shape.uvtiley); shape.cap = EditorGUILayout.Toggle("Cap", shape.cap); shape.RopeUp = (MegaAxis)EditorGUILayout.EnumPopup("Up", shape.RopeUp); shape.mat1 = (Material)EditorGUILayout.ObjectField("Mat", shape.mat1, typeof(Material), true); shape.flipNormals = EditorGUILayout.Toggle("Flip Normals", shape.flipNormals); break; } if ( shape.strands < 1 ) shape.strands = 1; EditorGUILayout.EndVertical(); // Conform shape.conform = EditorGUILayout.BeginToggleGroup("Conform", shape.conform); GameObject contarget = (GameObject)EditorGUILayout.ObjectField("Target", shape.target, typeof(GameObject), true); if ( contarget != shape.target ) shape.SetTarget(contarget); shape.conformAmount = EditorGUILayout.Slider("Amount", shape.conformAmount, 0.0f, 1.0f); shape.raystartoff = EditorGUILayout.FloatField("Ray Start Off", shape.raystartoff); shape.conformOffset = EditorGUILayout.FloatField("Conform Offset", shape.conformOffset); shape.raydist = EditorGUILayout.FloatField("Ray Dist", shape.raydist); EditorGUILayout.EndToggleGroup(); } else { shape.ClearMesh(); } showsplines = EditorGUILayout.Foldout(showsplines, "Spline Data"); if ( showsplines ) { EditorGUILayout.BeginVertical("Box"); if ( shape.splines != null && shape.splines.Count > 0 ) DisplaySpline(shape, shape.splines[shape.selcurve]); EditorGUILayout.EndVertical(); } EditorGUILayout.BeginHorizontal(); Color col = GUI.backgroundColor; GUI.backgroundColor = Color.green; if ( GUILayout.Button("Add") ) { // Create a new spline in the shape MegaSpline spl = MegaSpline.Copy(shape.splines[shape.selcurve]); shape.splines.Add(spl); shape.selcurve = shape.splines.Count - 1; EditorUtility.SetDirty(shape); buildmesh = true; } if ( shape.splines.Count > 1 ) { GUI.backgroundColor = Color.red; if ( GUILayout.Button("Delete") ) { // Delete current spline shape.splines.RemoveAt(shape.selcurve); for ( int i = 0; i < shape.splines.Count; i++ ) { if ( shape.splines[i].outlineSpline == shape.selcurve ) shape.splines[i].outlineSpline = -1; if ( shape.splines[i].outlineSpline > shape.selcurve ) shape.splines[i].outlineSpline--; } shape.selcurve--; if ( shape.selcurve < 0 ) shape.selcurve = 0; EditorUtility.SetDirty(shape); buildmesh = true; } } GUI.backgroundColor = col; EditorGUILayout.EndHorizontal(); } if ( !shape.imported ) { if ( Params() ) { rebuild = true; } } showfuncs = EditorGUILayout.Foldout(showfuncs, "Extra Functions"); if ( showfuncs ) { if ( GUILayout.Button("Flatten") ) { shape.SetHeight(shape.selcurve, 0.0f); shape.CalcLength(); EditorUtility.SetDirty(target); } if ( GUILayout.Button("Remove Twist") ) { shape.SetTwist(shape.selcurve, 0.0f); EditorUtility.SetDirty(target); } if ( GUILayout.Button("Copy IDS") ) { shape.CopyIDS(shape.selcurve); EditorUtility.SetDirty(target); } } export = EditorGUILayout.Foldout(export, "Export Options"); if ( export ) { xaxis = (MegaAxis)EditorGUILayout.EnumPopup("X Axis", xaxis); yaxis = (MegaAxis)EditorGUILayout.EnumPopup("Y Axis", yaxis); strokewidth = EditorGUILayout.FloatField("Stroke Width", strokewidth); strokecol = EditorGUILayout.ColorField("Stroke Color", strokecol); if ( GUILayout.Button("Export") ) { Export(shape); } } if ( recalc ) { shape.CalcLength(); //10); shape.BuildMesh(); //MegaLoftLayerSimple[] layers = (MegaLoftLayerSimple[])FindObjectsOfType(typeof(MegaLoftLayerSimple)); //for ( int i = 0; i < layers.Length; i++ ) //{ //layers[i].Notify(shape.splines[shape.selcurve], 0); //} EditorUtility.SetDirty(shape); } if ( GUI.changed ) { EditorUtility.SetDirty(target); buildmesh = true; } if ( rebuild ) { shape.MakeShape(); EditorUtility.SetDirty(target); buildmesh = true; } if ( buildmesh ) { if ( shape.makeMesh ) { shape.SetMats(); shape.BuildMesh(); } } //undoManager.CheckDirty(); }
public override void OnInspectorGUI() { bool buildmesh = false; MegaShape shape = (MegaShape)target; EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Add Knot")) { if (shape.splines == null || shape.splines.Count == 0) { MegaSpline spline = new MegaSpline(); // Have methods for these shape.splines.Add(spline); } //Undo.RegisterUndo(target, "Add Knot"); MegaKnot knot = new MegaKnot(); #if true // Add a point at CursorPos //sp = selected + 1; //Debug.Log("CursorPos " + CursorPos + " CursorKnot " + CursorKnot); float per = CursorPercent * 0.01f; CursorTangent = shape.splines[0].Interpolate(per + 0.01f, true, ref CursorKnot); //this.GetPositionOnSpline(i) - p; CursorPos = shape.splines[0].Interpolate(per, true, ref CursorKnot); //this.GetPositionOnSpline(i) - p; knot.p = CursorPos; //CursorTangent = //Vector3 t = shape.splines[0].knots[selected].Interpolate(0.51f, shape.splines[0].knots[0]); knot.outvec = (CursorTangent - knot.p); knot.outvec.Normalize(); knot.outvec *= shape.splines[0].knots[CursorKnot].seglength * 0.25f; knot.invec = -knot.outvec; knot.invec += knot.p; knot.outvec += knot.p; shape.splines[0].knots.Insert(CursorKnot + 1, knot); #else int sp = 0; if (selected == -1 || shape.splines[0].knots.Count == 1) { shape.splines[0].knots.Add(knot); selected = shape.splines[0].knots.Count - 1; } else { if (selected < shape.splines[0].knots.Count - 1) { sp = selected + 1; knot.p = shape.splines[0].knots[selected].Interpolate(0.5f, shape.splines[0].knots[selected + 1]); Vector3 t = shape.splines[0].knots[selected].Interpolate(0.51f, shape.splines[0].knots[selected + 1]); knot.outvec = (t - knot.p); //.Normalize(); knot.outvec.Normalize(); knot.outvec *= shape.splines[0].knots[selected].seglength * 0.25f; knot.invec = -knot.outvec; knot.invec += knot.p; knot.outvec += knot.p; } else { if (shape.splines[0].closed) { sp = selected + 1; knot.p = shape.splines[0].knots[selected].Interpolate(0.5f, shape.splines[0].knots[0]); Vector3 t = shape.splines[0].knots[selected].Interpolate(0.51f, shape.splines[0].knots[0]); knot.outvec = (t - knot.p); //.Normalize(); knot.outvec.Normalize(); knot.outvec *= shape.splines[0].knots[selected].seglength * 0.25f; knot.invec = -knot.outvec; knot.invec += knot.p; knot.outvec += knot.p; } else { sp = selected - 1; //Debug.Log("selected " + selected + " count " + shape.splines[0].knots.Count + " sp " + sp); knot.p = shape.splines[0].knots[sp].Interpolate(0.5f, shape.splines[0].knots[sp + 1]); Vector3 t = shape.splines[0].knots[sp].Interpolate(0.51f, shape.splines[0].knots[sp + 1]); knot.outvec = (t - knot.p); //.Normalize(); knot.outvec.Normalize(); knot.outvec *= shape.splines[0].knots[sp].seglength * 0.25f; knot.invec = -knot.outvec; knot.invec += knot.p; knot.outvec += knot.p; sp++; } } shape.splines[0].knots.Insert(sp, knot); selected = sp; //++; } #endif shape.CalcLength(10); EditorUtility.SetDirty(target); buildmesh = true; } if (GUILayout.Button("Delete Knot")) { if (selected != -1) { //Undo.RegisterUndo(target, "Delete Knot"); shape.splines[0].knots.RemoveAt(selected); selected--; shape.CalcLength(10); } EditorUtility.SetDirty(target); buildmesh = true; } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Match Handles")) { if (selected != -1) { //Undo.RegisterUndo(target, "Match Handles"); Vector3 p = shape.splines[0].knots[selected].p; Vector3 d = shape.splines[0].knots[selected].outvec - p; shape.splines[0].knots[selected].invec = p - d; shape.CalcLength(10); } EditorUtility.SetDirty(target); buildmesh = true; } if (GUILayout.Button("Load")) { // Load a spl file from max, so delete everything and replace LoadShape(ImportScale); buildmesh = true; } EditorGUILayout.EndHorizontal(); showcommon = EditorGUILayout.Foldout(showcommon, "Common Params"); bool rebuild = false; //Params(); if (showcommon) { //CursorPos = EditorGUILayout.Vector3Field("Cursor", CursorPos); CursorPercent = EditorGUILayout.FloatField("Cursor", CursorPercent); CursorPercent = Mathf.Repeat(CursorPercent, 100.0f); ImportScale = EditorGUILayout.FloatField("Import Scale", ImportScale); MegaAxis av = (MegaAxis)EditorGUILayout.EnumPopup("Axis", shape.axis); if (av != shape.axis) { shape.axis = av; rebuild = true; } shape.col1 = EditorGUILayout.ColorField("Col 1", shape.col1); shape.col2 = EditorGUILayout.ColorField("Col 2", shape.col2); shape.KnotCol = EditorGUILayout.ColorField("Knot Col", shape.KnotCol); shape.HandleCol = EditorGUILayout.ColorField("Handle Col", shape.HandleCol); shape.VecCol = EditorGUILayout.ColorField("Vec Col", shape.VecCol); shape.KnotSize = EditorGUILayout.FloatField("Knot Size", shape.KnotSize); shape.stepdist = EditorGUILayout.FloatField("Step Dist", shape.stepdist); if (shape.stepdist < 0.01f) { shape.stepdist = 0.01f; } shape.normalizedInterp = EditorGUILayout.Toggle("Normalized Interp", shape.normalizedInterp); shape.drawHandles = EditorGUILayout.Toggle("Draw Handles", shape.drawHandles); shape.drawKnots = EditorGUILayout.Toggle("Draw Knots", shape.drawKnots); shape.drawspline = EditorGUILayout.Toggle("Draw Spline", shape.drawspline); shape.lockhandles = EditorGUILayout.Toggle("Lock Handles", shape.lockhandles); shape.animate = EditorGUILayout.Toggle("Animate", shape.animate); if (shape.animate) { shape.time = EditorGUILayout.FloatField("Time", shape.time); shape.MaxTime = EditorGUILayout.FloatField("Loop Time", shape.MaxTime); shape.speed = EditorGUILayout.FloatField("Speed", shape.speed); shape.LoopMode = (MegaRepeatMode)EditorGUILayout.EnumPopup("Loop Mode", shape.LoopMode); } // Mesher shape.makeMesh = EditorGUILayout.Toggle("Make Mesh", shape.makeMesh); if (shape.makeMesh) { shape.meshType = (MeshShapeType)EditorGUILayout.EnumPopup("Mesh Type", shape.meshType); shape.Pivot = EditorGUILayout.Vector3Field("Pivot", shape.Pivot); shape.CalcTangents = EditorGUILayout.Toggle("Calc Tangents", shape.CalcTangents); shape.GenUV = EditorGUILayout.Toggle("Gen UV", shape.GenUV); shape.PhysUV = EditorGUILayout.Toggle("Physical UV", shape.PhysUV); shape.UVOffset = EditorGUILayout.Vector2Field("UV Offset", shape.UVOffset); shape.UVRotate = EditorGUILayout.Vector2Field("UV Rotate", shape.UVRotate); shape.UVScale = EditorGUILayout.Vector2Field("UV Scale", shape.UVScale); shape.UVOffset1 = EditorGUILayout.Vector2Field("UV Offset1", shape.UVOffset1); shape.UVRotate1 = EditorGUILayout.Vector2Field("UV Rotate1", shape.UVRotate1); shape.UVScale1 = EditorGUILayout.Vector2Field("UV Scale1", shape.UVScale1); switch (shape.meshType) { case MeshShapeType.Fill: shape.DoubleSided = EditorGUILayout.Toggle("Double Sided", shape.DoubleSided); shape.Height = EditorGUILayout.FloatField("Height", shape.Height); shape.HeightSegs = EditorGUILayout.IntField("HeightSegs", shape.HeightSegs); shape.UseHeightCurve = EditorGUILayout.Toggle("Use Height Crv", shape.UseHeightCurve); if (shape.UseHeightCurve) { shape.heightCrv = EditorGUILayout.CurveField("Height Curve", shape.heightCrv); } break; case MeshShapeType.Line: shape.DoubleSided = EditorGUILayout.Toggle("Double Sided", shape.DoubleSided); shape.Height = EditorGUILayout.FloatField("Height", shape.Height); shape.HeightSegs = EditorGUILayout.IntField("HeightSegs", shape.HeightSegs); shape.heightCrv = EditorGUILayout.CurveField("Height Curve", shape.heightCrv); shape.Start = EditorGUILayout.FloatField("Start", shape.Start); shape.End = EditorGUILayout.FloatField("End", shape.End); shape.Rotate = EditorGUILayout.FloatField("Rotate", shape.Rotate); break; case MeshShapeType.Tube: shape.Sides = EditorGUILayout.IntField("Sides", shape.Sides); shape.TubeStep = EditorGUILayout.FloatField("TubeStep", shape.TubeStep); shape.Start = EditorGUILayout.FloatField("Start", shape.Start); shape.End = EditorGUILayout.FloatField("End", shape.End); break; } } showsplines = EditorGUILayout.Foldout(showsplines, "Splines"); if (showsplines) { for (int i = 0; i < shape.splines.Count; i++) { DisplaySpline(shape, shape.splines[i]); } } } if (Params()) { rebuild = true; } if (GUI.changed) { EditorUtility.SetDirty(target); //shape.CalcLength(10); buildmesh = true; } if (rebuild) { shape.MakeShape(); EditorUtility.SetDirty(target); buildmesh = true; } if (buildmesh) { shape.BuildMesh(); } }
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(); } } }
GameObject CreateSpline(Vector3 pos) { GameObject obj = new GameObject(); obj.name = name + " - Spline " + splinecount++; obj.transform.position = transform.position; //obj.transform.parent = transform; MegaShape shape = obj.AddComponent <MegaShape>(); shape.smoothness = smooth; shape.drawHandles = false; MegaSpline spline = shape.splines[0]; spline.knots.Clear(); spline.constantSpeed = constantspd; spline.subdivs = 40; shape.cap = true; Vector3[] ps = new Vector3[2]; ps[0] = obj.transform.worldToLocalMatrix.MultiplyPoint3x4(pos); ps[1] = obj.transform.worldToLocalMatrix.MultiplyPoint3x4(pos); shape.BuildSpline(0, ps, false); shape.CalcLength(); shape.drawTwist = true; shape.boxwidth = width; shape.boxheight = height; shape.offset = -height * 0.5f; shape.stepdist = meshstep; //width * 2.0f * 10.0f; shape.SetMats(); spline.closed = closed; cspline = spline; cshape = shape; if (loft) { MegaShapeLoft newloft = loft.GetClone(); newloft.name = name + " - Loft " + (splinecount - 1); newloft.transform.position = obj.transform.position; newloft.transform.rotation = obj.transform.rotation; MegaLoftLayerBase[] layers = newloft.GetComponents <MegaLoftLayerBase>(); for (int i = 0; i < layers.Length; i++) { layers[i].layerPath = cshape; } newloft.rebuild = true; //newloft.gameObject.SetActiveRecursively(true); #if UNITY_3_5 newloft.gameObject.SetActiveRecursively(true); #else newloft.gameObject.SetActive(true); #endif cloft = newloft; } return(obj); }