public override void OnInspectorGUI() { MegaLoftLayerSimple layer = (MegaLoftLayerSimple)target; MegaShapeLoft loft = layer.GetComponent <MegaShapeLoft>(); if (loft && loft.undo) { undoManager.CheckUndo(); } DisplayGUI(); CommonGUI(); if (GUI.changed) { if (loft) { loft.rebuild = true; EditorUtility.SetDirty(loft); } if (layer) { EditorUtility.SetDirty(layer); } } if (loft && loft.undo) { undoManager.CheckDirty(); } }
public override bool PrepareLoft(MegaShapeLoft loft, int sc) { Init(); int vcount = 0; int tcount = 0; if (scatterMesh) { vcount += sverts.Length * Count; tcount += stris.Length * Count; } if (loftverts == null || loftverts.Length != vcount) { loftverts = new Vector3[vcount]; } if (loftuvs == null || loftuvs.Length != vcount) { loftuvs = new Vector2[vcount]; } if (lofttris == null || lofttris.Length != tcount) { lofttris = new int[tcount]; } return(true); }
public override bool PrepareLoft(MegaShapeLoft loft, int sc) { Init(); int vcount = 0; int tcount = 0; if (mainObj && MainEnabled) { vcount += (mverts.Length * Count); tcount += (maintris * Count); for (int i = 0; i < mainlofttris.Count; i++) { mainlofttris[i].tris = new int[mainlofttris[i].sourcetris.Length * Count]; } } if (loftverts == null || loftverts.Length != vcount) { loftverts = new Vector3[vcount]; } if (loftuvs == null || loftuvs.Length != vcount) { loftuvs = new Vector2[vcount]; } if (lofttris == null || lofttris.Length != tcount) { lofttris = new int[tcount]; } return(true); }
public override Vector3 GetPosAndLookOld(MegaShapeLoft loft, float ca, float a, float at, out Vector3 p) { ca = Mathf.Repeat(ca, 1.0f); a = Mathf.Repeat(a, 1.0f); int count = ActualCrossVerts; float findex = (count - 1) * ca; int cindex = (int)findex; float interp = findex - cindex; int cindex1 = cindex + 1; float pfindex = (ActualPathSteps - 1) * a; int pindex = (int)pfindex; float pinterp = pfindex - pindex; int pindex1 = pindex + 1; Vector3 p1 = loftverts[(pindex * count) + cindex]; Vector3 p2 = loftverts[(pindex * count) + cindex1]; Vector3 p3 = loftverts[(pindex1 * count) + cindex]; Vector3 p4 = loftverts[(pindex1 * count) + cindex1]; Vector3 pm1 = Vector3.Lerp(p1, p2, interp); Vector3 pm2 = Vector3.Lerp(p3, p4, interp); float pa = pinterp + at; p.x = pm1.x + ((pm2.x - pm1.x) * pa); p.y = pm1.y + ((pm2.y - pm1.y) * pa); p.z = pm1.z + ((pm2.z - pm1.z) * pa); return(Vector3.Lerp(pm1, pm2, pinterp)); }
public override void Notify(MegaSpline spline, int reason) { if (layerPath && layerPath.splines != null) { if (curve < layerPath.splines.Count) { if (layerPath.splines[curve] == spline) { MegaShapeLoft loft = GetComponent <MegaShapeLoft>(); loft.rebuild = true; loft.BuildMeshFromLayersNew(); return; } } else { curve = 0; } } for (int i = 0; i < loftsections.Count; i++) { if (loftsections[i].shape) { if (loftsections[i].shape.splines[loftsections[i].curve] == spline) { MegaShapeLoft loft = GetComponent <MegaShapeLoft>(); loft.rebuild = true; loft.BuildMeshFromLayersNew(); return; } } } }
public MegaShapeLoft GetClone() { GameObject to = new GameObject(); MegaShapeLoft loft = to.AddComponent <MegaShapeLoft>(); Copy(loft); loft.mesh = null; MegaLoftLayerBase[] layers = GetComponents <MegaLoftLayerBase>(); for (int i = 0; i < layers.Length; i++) { layers[i].Copy(to); } loft.verts = null; loft.uvs = null; loft.cols = null; loft.rebuild = true; loft.BuildMeshFromLayersNew(); to.name = name + " clone"; return(loft); }
string[] GetLayers(MegaShapeLoft loft) { if (loft.Layers == null) { string[] lyers1 = new string[1]; lyers1[0] = "None"; return(lyers1); } string[] lyers = new string[loft.Layers.Length + 1]; lyers[0] = "None"; for (int i = 0; i < loft.Layers.Length; i++) { if (loft.Layers[i] != null) { lyers[i + 1] = loft.Layers[i].LayerName; } else { lyers[i + 1] = "Deleted"; } } return(lyers); }
public override bool LoftNotify(MegaShapeLoft loft, int reason) { if (surfaceLoft != null && surfaceLoft == loft) { return(true); } return(false); }
static void RenderGizmo(MegaShapeLoft shape, GizmoType gizmoType) { if ((gizmoType & GizmoType.NotSelected) != 0) { if ((gizmoType & GizmoType.Active) != 0) { //DrawPath(shape); } } }
void Update() { if (Application.isPlaying && LayerEnabled && Speed != 0.0f) { Alpha += (Speed * LayerLength) * Time.deltaTime; Alpha = Mathf.Repeat(Alpha, 1.0f); MegaShapeLoft loft = (MegaShapeLoft)GetComponent <MegaShapeLoft>(); if (loft) { loft.rebuild = true; } } }
public virtual bool LoftNotify(MegaShapeLoft loft, int reason) { if (Layers != null) { for (int i = 0; i < Layers.Length; i++) { if (Layers[i].LoftNotify(loft, reason)) { rebuild = true; break; } } } return(rebuild); }
void OnGUI() { if (Selection.activeGameObject == null) { return; } MegaShapeLoft loft = Selection.activeGameObject.GetComponent <MegaShapeLoft>(); if (loft == null) { return; } //UnwrapParam uv1 = new UnwrapParam(); //UnwrapParam.SetDefaults(out uv1); //loft.genLightMap = EditorGUILayout.BeginToggleGroup("Gen LightMap", loft.genLightMap); loft.angleError = EditorGUILayout.Slider("Angle Error", loft.angleError, 0.0f, 1.0f); loft.areaError = EditorGUILayout.Slider("Area Error", loft.areaError, 0.0f, 1.0f); loft.hardAngle = EditorGUILayout.FloatField("Hard Angle", loft.hardAngle); loft.packMargin = EditorGUILayout.FloatField("Pack Margin", loft.packMargin); EditorStyles.textField.wordWrap = false; EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Build")) { UnwrapParam uv = new UnwrapParam(); //UnwrapParam.SetDefaults(out uv); uv.angleError = loft.angleError; uv.areaError = loft.areaError; uv.hardAngle = loft.hardAngle; uv.packMargin = loft.packMargin; Unwrapping.GenerateSecondaryUVSet(loft.mesh, uv); this.Close(); } if (GUILayout.Button("Cancel")) { this.Close(); } EditorGUILayout.EndHorizontal(); }
// We could do a bary centric thing if we grid up the bounds void DoConform(MegaShapeLoft loft, Vector3[] verts) { InitConform(verts); if (target && conformCollider) { Matrix4x4 loctoworld = transform.localToWorldMatrix; Matrix4x4 tm = loctoworld; // * worldtoloc; Matrix4x4 invtm = tm.inverse; Ray ray = new Ray(); RaycastHit hit; float ca = conformAmount * loft.conformAmount; // When calculating alpha need to do caps sep for (int i = 0; i < verts.Length; i++) { Vector3 origin = tm.MultiplyPoint(verts[i]); origin.y += raystartoff; ray.origin = origin; ray.direction = Vector3.down; //loftverts[i] = loftverts1[i]; if (conformCollider.Raycast(ray, out hit, raydist)) { Vector3 lochit = invtm.MultiplyPoint(hit.point); verts[i].y = Mathf.Lerp(verts[i].y, lochit.y + offsets[i] + conformOffset, ca); //conformAmount); last[i] = verts[i].y; } else { Vector3 ht = ray.origin; ht.y -= raydist; verts[i].y = last[i]; //lochit.z + offsets[i] + offset; } } } else { } }
public override bool PrepareLoft(MegaShapeLoft loft, int sc) { //Debug.Log("PrepareLoft"); if (layerPath == null || layerPath.splines == null || layerPath.splines.Count == 0) { return(false); } float loftdist = (layerPath.splines[curve].length * pathLength); LoftLength = loftdist; ease.SetEasing(easeType); locup = loft.up; Vector3 off; float width = 1.0f; if (loftsections.Count > 0) { width = loftsections[0].shape.splines[loftsections[0].curve].length; } //Debug.Log("Build Poly shapes"); for (int c = 0; c < loftsections.Count; c++) { if (loftsections[c].snap) { off = loftsections[0].shape.splines[loftsections[0].curve].knots[0].p - loftsections[c].shape.splines[loftsections[c].curve].knots[0].p; } else { off = Vector3.zero; } BuildPolyShape(loftsections[c], 0, off, width); } Prepare(loft); return(true); }
static public int FindLayer(MegaShapeLoft loft, int lay) { if (loft && lay < loft.Layers.Length) { int rval = -1; for (int i = 0; i <= lay; i++) { //if ( loft.Layers[i].GetType() == typeof(MegaLoftLayerSimple) ) if (loft.Layers[i] is MegaLoftLayerSimple) { rval++; } } //Debug.Log("lay " + lay + " found " + rval); return(rval); } return(-1); }
// Dont need this as a seperate method me thinks void Prepare(MegaShapeLoft loft) { MegaShape path = layerPath; float loftdist = (path.splines[curve].length * pathLength); LoftLength = loftdist; crosses = Mathf.CeilToInt(loftdist / pathDist); if (crosses < 2) { crosses = 2; } if (enabled) { uvtm = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(UVRotate.x, UVRotate.y, 0.0f), Vector3.one); } }
public void SetLimits(GameObject gobj) { MegaShapeLoft loft = gobj.GetComponent <MegaShapeLoft>(); if (loft) { sl = loft.startLow; sh = loft.startHigh; ll = loft.lenLow; lh = loft.lenHigh; csl = loft.crossLow; csh = loft.crossHigh; cll = loft.crossLenLow; clh = loft.crossLenHigh; dl = loft.distlow; dh = loft.disthigh; cdl = loft.cdistlow; cdh = loft.cdisthigh; } }
public override Vector3 GetPosAndFrame(MegaShapeLoft loft, float ca, float a, float at, out Vector3 p, out Vector3 up, out Vector3 right, out Vector3 fwd) { ca = Mathf.Clamp(ca, 0.0f, 0.9999f); a = Mathf.Repeat(a, 0.9999f); int count = ActualCrossVerts; float findex = (count - 1) * ca; int cindex = (int)findex; float interp = findex - cindex; int cindex1 = cindex + 1; float pfindex = (ActualPathSteps - 1) * a; int pindex = (int)pfindex; float pinterp = pfindex - pindex; int pindex1 = pindex + 1; Vector3 p1 = loftverts[(pindex * count) + cindex]; Vector3 p2 = loftverts[(pindex * count) + cindex1]; Vector3 p3 = loftverts[(pindex1 * count) + cindex]; Vector3 p4 = loftverts[(pindex1 * count) + cindex1]; Vector3 pm1 = Vector3.Lerp(p1, p2, interp); Vector3 pm2 = Vector3.Lerp(p3, p4, interp); float pa = pinterp + at; p.x = pm1.x + ((pm2.x - pm1.x) * pa); p.y = pm1.y + ((pm2.y - pm1.y) * pa); p.z = pm1.z + ((pm2.z - pm1.z) * pa); right.x = p2.x - p1.x; right.y = p2.y - p1.y; right.z = p2.z - p1.z; fwd.x = p3.x - p1.x; fwd.y = p3.y - p1.y; fwd.z = p3.z - p1.z; up = Vector3.Cross(right, fwd); return(Vector3.Lerp(pm1, pm2, pinterp)); }
public void CommonGUI() { EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Duplicate")) { MegaLoftLayerBase layer = (MegaLoftLayerBase)target; MegaLoftLayerBase newl = layer.Copy(layer.gameObject); if (newl != null) { newl.LayerName = layer.LayerName + " Copy"; } MegaShapeLoft loft = layer.GetComponent <MegaShapeLoft>(); if (loft) { loft.rebuild = true; EditorUtility.SetDirty(loft); } } if (GUILayout.Button("Delete")) { MegaLoftLayerBase layer = (MegaLoftLayerBase)target; MegaShapeLoft loft = layer.GetComponent <MegaShapeLoft>(); DestroyImmediate(layer); if (loft) { loft.rebuild = true; EditorUtility.SetDirty(loft); } } EditorGUILayout.EndHorizontal(); }
public void Copy(MegaShapeLoft to) { #if !UNITY_FLASH && !UNITY_METRO && !UNITY_WP8 Type tp = this.GetType(); FieldInfo[] fields = tp.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Default); //claredOnly); PropertyInfo[] properties = tp.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Default); //claredOnly); for (int j = 0; j < fields.Length; j++) { fields[j].SetValue(to, fields[j].GetValue(this)); } for (int j = 0; j < properties.Length; j++) { if (properties[j].CanWrite) { properties[j].SetValue(to, properties[j].GetValue(this, null), null); } } #endif }
static public string[] GetLayers(MegaShapeLoft loft) { string[] lyers; if (loft) { MegaLoftLayerSimple[] layers = loft.GetComponents <MegaLoftLayerSimple>(); lyers = new string[layers.Length + 1]; lyers[0] = "None"; for (int i = 0; i < layers.Length; i++) { lyers[i + 1] = layers[i].LayerName; } } else { lyers = new string[1]; lyers[0] = "None"; } return(lyers); }
static public GUIContent[] GetLayersAsContent(MegaShapeLoft loft) { GUIContent[] lyers; if (loft) { MegaLoftLayerSimple[] layers = loft.GetComponents <MegaLoftLayerSimple>(); lyers = new GUIContent[layers.Length + 1]; lyers[0] = new GUIContent("None"); for (int i = 0; i < layers.Length; i++) { lyers[i + 1] = new GUIContent(layers[i].LayerName); } } else { lyers = new GUIContent[1]; lyers[0] = new GUIContent("None"); } return(lyers); }
Vector3 GetCross(MegaShapeLoft loft, float ca) { MegaShape section = layerSection; Matrix4x4 tm1 = Matrix4x4.identity; MegaMatrix.Translate(ref tm1, pivot); MegaMatrix.Rotate(ref tm1, new Vector3(Mathf.Deg2Rad * crossRot.x, Mathf.Deg2Rad * crossRot.y, Mathf.Deg2Rad * crossRot.z)); int lk = -1; float alpha = crossStart + ca; MegaSpline cspl = section.splines[crosscurve]; if (cspl.closed) { if (alpha < 0.0f) { alpha += 1.0f; } } Vector3 off = Vector3.zero; if (snap) { off = section.splines[0].knots[0].p - cspl.knots[0].p; } if (cspl.closed) { alpha = Mathf.Repeat(alpha, 1.0f); } return(tm1.MultiplyPoint3x4(section.splines[crosscurve].InterpCurve3D(alpha, section.normalizedInterp, ref lk) + off)); }
public override bool PrepareLoft(MegaShapeLoft loft, int sc) { // If we dont have any meshes then return null for not building if (scatterMesh == null && surfaceLoft == null && surfaceLayer < 0) { return(false); } Init(); int vcount = 0; int tcount = 0; if (scatterMesh) { vcount += sverts.Length * Count; tcount += stris.Length * Count; } if (loftverts == null || loftverts.Length != vcount) { loftverts = new Vector3[vcount]; } if (loftuvs == null || loftuvs.Length != vcount) { loftuvs = new Vector2[vcount]; } if (lofttris == null || lofttris.Length != tcount) { lofttris = new int[tcount]; } return(true); }
Vector3 Deform(Vector3 p, MegaShapeLoft loft, MegaLoftLayerSimple layer, float percent, float ca, float off, Vector3 scale, float removeDof, Vector3 locoff) { p = tm.MultiplyPoint3x4(p); p.x *= scale.x; p.y *= scale.y; p.z *= scale.z; p.z += off; p += locoff; float alpha = (p.z * LayerLength) + percent; if (useCrossCrv) { ca += CrossCrv.Evaluate(alpha); } Vector3 ps1; Vector3 ps; if (CalcUp) { Vector3 upv = Vector3.zero; Vector3 right = Vector3.zero; Vector3 fwd = Vector3.zero; ps = layer.GetPosAndFrame(loft, ca, alpha, (tangent * 0.001f), out ps1, out upv, out right, out fwd); tw = Quaternion.LookRotation(fwd, upv); Quaternion rot = tw * meshrot; if (useTwist) { rot *= Quaternion.AngleAxis(180.0f + (twist * twistCrv.Evaluate(alpha)), Vector3.forward); } else { rot *= Quaternion.AngleAxis(180.0f, Vector3.forward); } //wtm.SetTRS(ps, rot, Vector3.one); MegaMatrix.SetTR(ref wtm, ps, rot); wtm = mat * wtm; p.z = 0.0f; return(wtm.MultiplyPoint3x4(p)); } else { ps = layer.GetPosAndLook(loft, ca, alpha, (tangent * 0.001f), out ps1); if (useTwist) { tw = meshrot * Quaternion.AngleAxis((twist * twistCrv.Evaluate(alpha)), Vector3.forward); // * meshrot; } else { tw = meshrot * Quaternion.AngleAxis(0.0f, Vector3.forward); // * meshrot; } } Vector3 relativePos = ps1 - ps; relativePos.y *= removeDof; if (relativePos == Vector3.zero) { relativePos = lastrel; //Vector3.forward; } lastrel = relativePos; Quaternion rotation = Quaternion.LookRotation(relativePos) * tw; // * meshrot; MegaMatrix.SetTR(ref wtm, ps, rotation); //wtm.SetTRS(ps, rotation, Vector3.one); wtm = mat * wtm; p.z = 0.0f; return(wtm.MultiplyPoint3x4(p)); }
public override bool PrepareLoft(MegaShapeLoft loft, int sc) { Init(); int vcount = 0; int tcount = 0; if ( scatterMesh ) { vcount += sverts.Length * Count; tcount += stris.Length * Count; } if ( loftverts == null || loftverts.Length != vcount ) loftverts = new Vector3[vcount]; if ( loftuvs == null || loftuvs.Length != vcount ) loftuvs = new Vector2[vcount]; if ( lofttris == null || lofttris.Length != tcount ) lofttris = new int[tcount]; return true; }
public override bool PrepareLoft(MegaShapeLoft loft, int sc) { // If we dont have any meshes then return null for not building if ( scatterMesh == null && surfaceLoft == null && surfaceLayer < 0 ) return false; Init(); int vcount = 0; int tcount = 0; if ( scatterMesh ) { vcount += sverts.Length * Count; tcount += stris.Length * Count; } if ( loftverts == null || loftverts.Length != vcount ) loftverts = new Vector3[vcount]; if ( loftuvs == null || loftuvs.Length != vcount ) loftuvs = new Vector2[vcount]; if ( lofttris == null || lofttris.Length != tcount ) lofttris = new int[tcount]; return true; }
public override bool LoftNotify(MegaShapeLoft loft, int reason) { if ( surfaceLoft != null && surfaceLoft == loft ) return true; return false; }
Vector3 Deform(Vector3 p, MegaShapeLoft loft, MegaLoftLayerSimple layer, float percent, float ca, float off, Vector3 scale, float removeDof, Vector3 locoff) { p = tm.MultiplyPoint3x4(p); p.x *= scale.x; p.y *= scale.y; p.z *= scale.z; p.z += off; p += locoff; float alpha = (p.z * LayerLength) + percent; if ( useCrossCrv ) ca += CrossCrv.Evaluate(alpha); Vector3 ps1; Vector3 ps; if ( CalcUp ) { Vector3 upv = Vector3.zero; Vector3 right = Vector3.zero; Vector3 fwd = Vector3.zero; ps = layer.GetPosAndFrame(loft, ca, alpha, (tangent * 0.001f), out ps1, out upv, out right, out fwd); tw = Quaternion.LookRotation(fwd, upv); Quaternion rot = tw * meshrot; if ( useTwist ) rot *= Quaternion.AngleAxis(180.0f + (twist * twistCrv.Evaluate(alpha)), Vector3.forward); else rot *= Quaternion.AngleAxis(180.0f, Vector3.forward); //wtm.SetTRS(ps, rot, Vector3.one); MegaMatrix.SetTR(ref wtm, ps, rot); wtm = mat * wtm; p.z = 0.0f; return wtm.MultiplyPoint3x4(p); } else { ps = layer.GetPosAndLook(loft, ca, alpha, (tangent * 0.001f), out ps1); if ( useTwist ) tw = meshrot * Quaternion.AngleAxis((twist * twistCrv.Evaluate(alpha)), Vector3.forward); // * meshrot; else tw = meshrot * Quaternion.AngleAxis(0.0f, Vector3.forward); // * meshrot; } Vector3 relativePos = ps1 - ps; relativePos.y *= removeDof; if ( relativePos == Vector3.zero ) { relativePos = lastrel; //Vector3.forward; } lastrel = relativePos; Quaternion rotation = Quaternion.LookRotation(relativePos) * tw; // * meshrot; MegaMatrix.SetTR(ref wtm, ps, rotation); //wtm.SetTRS(ps, rotation, Vector3.one); wtm = mat * wtm; p.z = 0.0f; return wtm.MultiplyPoint3x4(p); }
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 override int BuildMesh(MegaShapeLoft loft, int triindex) { if ( layerPath == null ) return triindex; LayerLength = 1.0f / layerPath.splines[curve].length; if ( tangent < 0.1f ) tangent = 0.1f; //mat = loft.transform.localToWorldMatrix; mat = transform.localToWorldMatrix * layerPath.transform.worldToLocalMatrix; // * transform.worldToLocalMatrix; //mat = surfaceLoft.transform.localToWorldMatrix; //mat = surfaceLoft.transform.localToWorldMatrix * transform.worldToLocalMatrix; tm = Matrix4x4.identity; switch ( axis ) { case MegaAxis.X: MegaMatrix.RotateY(ref tm, -Mathf.PI * 0.5f); break; case MegaAxis.Y: MegaMatrix.RotateX(ref tm, -Mathf.PI * 0.5f); break; case MegaAxis.Z: break; } //meshtm = Matrix4x4.identity; //MegaMatrix.Rotate(ref meshtm, Mathf.Deg2Rad * rot); meshrot = Quaternion.Euler(rot); tw = meshrot; //meshrot = Quaternion.Euler(rot); int trioff = 0; int vi = 0; int fi = 0; pathtm.SetTRS(offPath, Quaternion.Euler(rotPath), sclPath); Matrix4x4 omat = Matrix4x4.identity; Random.seed = Seed; Vector3 pivot; Vector3 scl = Vector3.zero; Vector3 sclmin = Vector3.Scale(scaleRangeMin, scale); Vector3 sclmax = Vector3.Scale(scaleRangeMax, scale); Vector3 sploff = Vector3.zero; if ( snap ) sploff = layerPath.splines[0].knots[0].p - layerPath.splines[curve].knots[0].p; float a = 0.0f; for ( int r = 0; r < Count; r++ ) { scl.x = (scale.x + Mathf.Lerp(sclmin.x, sclmax.x, Random.value)) * GlobalScale; scl.y = (scale.y + Mathf.Lerp(sclmin.y, sclmax.y, Random.value)) * GlobalScale; scl.z = (scale.z + Mathf.Lerp(sclmin.z, sclmax.z, Random.value)) * GlobalScale; if ( useDensity ) a = FindScatterAlpha(); else a = Random.value; float alpha = start + (a * length) + Alpha; Vector3 rt = rot + (((Random.value - 0.5f) * 2.0f) * rotRange); meshrot = Quaternion.Euler(rt); //if ( CalcUp ) pivot = Deform(layerPath, alpha, 0.0f, scl, RemoveDof, Vector3.zero, out omat, sploff); //else // pivot = Deform(layerPath, alpha, 0.0f, scl, 0.0f, Vector3.zero, out omat, sploff); Vector3 pp = Vector3.zero; for ( int i = 0; i < sverts.Length; i++ ) { pp.x = sverts[i].x + Offset.x; pp.y = sverts[i].y + Offset.y; pp.z = sverts[i].z + Offset.z; pp = omat.MultiplyPoint3x4(pp); loftverts[vi].x = pp.x + pivot.x; loftverts[vi].y = pp.y + pivot.y; loftverts[vi].z = pp.z + pivot.z; loftuvs[vi++] = suvs[i]; } for ( int i = 0; i < stris.Length; i++ ) lofttris[fi++] = stris[i] + trioff + triindex; trioff = vi; } if ( conform ) { CalcBounds(loftverts); DoConform(loft, loftverts); } return triindex; }
// Dont need this as a seperate method me thinks void Prepare(MegaShapeLoft loft) { MegaShape path = layerPath; float loftdist = (path.splines[curve].length * pathLength); LoftLength = loftdist; crosses = Mathf.CeilToInt(loftdist / pathDist); if ( crosses < 2 ) crosses = 2; if ( enabled ) uvtm = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(UVRotate.x, UVRotate.y, 0.0f), Vector3.one); }
// This will need changing public override Vector3 GetPos(MegaShapeLoft loft, float ca, float a) { ca = Mathf.Repeat(ca, 1.0f); a = Mathf.Repeat(a, 1.0f); // first need to find the material section we are in from ca MegaMeshSection ms = null; for ( int i = 0; i < meshsections.Count; i++ ) { ms = meshsections[i]; if ( ca >= ms.castart && ca <= ms.caend ) break; } ca = (ca - ms.castart) / (ms.caend - ms.castart); // Ok got the section float findex = (ms.cverts.Count - 1) * ca; int cindex = (int)findex; float interp = findex - cindex; int cindex1 = cindex + 1; bool flip = false; int pindex; int pindex1; float pinterp; if ( pathLength < 0.9999f || layerPath.splines[curve].closed == false ) { if ( a < 0.0f ) { pindex = 0; pindex1 = 1; pinterp = a * GetLength(loft); // / crosses); } else { if ( a >= 0.9999f ) { pindex = crosses - 1; pindex1 = crosses - 2; pinterp = (a - 1.0f) * GetLength(loft); flip = true; } else { float pfindex = (crosses - 1) * a; pindex = (int)pfindex; pinterp = pfindex - pindex; pindex1 = pindex + 1; } } } else { a = Mathf.Repeat(a, 0.9999f); float pfindex = (crosses - 1) * a; pindex = (int)pfindex; pinterp = pfindex - pindex; pindex1 = pindex + 1; } Vector3 p1 = ms.verts[(pindex * ms.cverts.Count) + cindex]; Vector3 p2 = ms.verts[(pindex * ms.cverts.Count) + cindex1]; Vector3 p3 = ms.verts[(pindex1 * ms.cverts.Count) + cindex]; Vector3 p4 = ms.verts[(pindex1 * ms.cverts.Count) + cindex1]; Vector3 pm1 = Vector3.Lerp(p1, p2, interp); Vector3 pm2 = Vector3.Lerp(p3, p4, interp); Vector3 delta = pm2 - pm1; if ( flip ) { p3.x = pm2.x + (delta.x * pinterp); p3.y = pm2.y + (delta.y * pinterp); p3.z = pm2.z + (delta.z * pinterp); } else { p3.x = pm1.x + (delta.x * pinterp); p3.y = pm1.y + (delta.y * pinterp); p3.z = pm1.z + (delta.z * pinterp); } return p3; //Vector3.Lerp(pm1, pm2, pinterp); }
// We could do a bary centric thing if we grid up the bounds void DoConform(MegaShapeLoft loft) { InitConform(); if ( target && conformCollider ) { Matrix4x4 loctoworld = transform.localToWorldMatrix; Matrix4x4 tm = loctoworld; Matrix4x4 invtm = tm.inverse; Ray ray = new Ray(); RaycastHit hit; float ca = conformAmount * loft.conformAmount; // When calculating alpha need to do caps sep for ( int m = 0; m < meshsections.Count; m++ ) { MegaMeshSection ms = meshsections[m]; for ( int i = 0; i < ms.verts1.Count; i++ ) { Vector3 origin = tm.MultiplyPoint(ms.verts1[i]); origin.y += raystartoff; ray.origin = origin; ray.direction = Vector3.down; ms.verts.Add(ms.verts1[i]); if ( conformCollider.Raycast(ray, out hit, raydist) ) { Vector3 lochit = invtm.MultiplyPoint(hit.point); Vector3 p = ms.verts[i]; p.y = Mathf.Lerp(p.y, lochit.y + ms.offsets[i] + conformOffset, ca); //conformAmount); ms.verts[i] = p; ms.last[i] = p.y; } else { Vector3 ht = ray.origin; ht.y -= raydist; Vector3 p = ms.verts[i]; p.y = ms.last[i]; ms.verts[i] = p; } } } } else { for ( int m = 0; m < meshsections.Count; m++ ) { MegaMeshSection ms = meshsections[m]; for ( int i = 0; i < ms.verts1.Count; i++ ) ms.verts.Add(ms.verts1[i]); } } }
public override Vector3 SampleSplines(MegaShapeLoft loft, float ca, float pa) { Vector3 p = Vector3.zero; float lerp = 0.0f; Matrix4x4 pathtm = Matrix4x4.identity; if ( SnapToPath ) pathtm = layerPath.transform.localToWorldMatrix; Matrix4x4 twisttm = Matrix4x4.identity; Vector3 sclc = Vector2.one; Matrix4x4 tm; float offx = 0.0f; float offy = 0.0f; float offz = 0.0f; bool clsd = layerPath.splines[curve].closed; Vector3 lastup = locup; float alpha = pa; //(float)pi / (float)PathSteps; float pathalpha = pathStart + (pathLength * alpha); if ( clsd ) pathalpha = Mathf.Repeat(pathalpha, 1.0f); if ( useScaleXCrv ) sclc.x = scaleCrvX.Evaluate(pathalpha); if ( useScaleYCrv ) sclc.y = scaleCrvY.Evaluate(pathalpha); if ( useTwistCrv ) { float twist = twistCrv.Evaluate(pathalpha); float tw1 = layerPath.splines[curve].GetTwist(pathalpha); MegaShapeUtils.RotateZ(ref twisttm, Mathf.Deg2Rad * (twist - tw1)); if ( frameMethod == MegaFrameMethod.Old ) tm = pathtm * GetDeformMat(layerPath.splines[curve], pathalpha, layerPath.normalizedInterp) * twisttm; else tm = pathtm * GetDeformMatNewMethod(layerPath.splines[curve], pathalpha, layerPath.normalizedInterp, ref lastup) * twisttm; } else { if ( frameMethod == MegaFrameMethod.Old ) tm = pathtm * GetDeformMat(layerPath.splines[curve], pathalpha, layerPath.normalizedInterp); else tm = pathtm * GetDeformMatNewMethod(layerPath.splines[curve], pathalpha, layerPath.normalizedInterp, ref lastup); } // Need to get the crosssection for the given alpha and the lerp value int csect = GetSection(pathalpha, out lerp); lerp = ease.easing(0.0f, 1.0f, lerp); Vector3 off = Vector3.zero; if ( sections[csect].snap ) off = sections[0].shape.splines[sections[0].curve].knots[0].p - sections[csect].shape.splines[sections[csect].curve].knots[0].p; else off = Vector3.zero; Vector3 crossp1 = GetCross(csect, ca, off); if ( sections[csect + 1].snap ) off = sections[0].shape.splines[sections[0].curve].knots[0].p - sections[csect + 1].shape.splines[sections[csect + 1].curve].knots[0].p; else off = Vector3.zero; Vector3 crossp2 = GetCross(csect + 1, ca, off); if ( useOffsetX ) offx = offsetCrvX.Evaluate(pathalpha); if ( useOffsetY ) offy = offsetCrvY.Evaluate(pathalpha); if ( useOffsetZ ) offz = offsetCrvZ.Evaluate(pathalpha); //float size = 1.0f / layerPath.splines[0].length; p = Vector3.Lerp(crossp1, crossp2, lerp); if ( useScaleXCrv ) p.x *= sclc.x; if ( useScaleYCrv ) p.y *= sclc.y; p.x += offx; p.y += offy; p.z += offz; p = tm.MultiplyPoint3x4(p); p += offset; return p; }
static void RenderGizmo(MegaShapeLoft shape, GizmoType gizmoType) { if ( (gizmoType & GizmoType.NotSelected) != 0 ) { if ( (gizmoType & GizmoType.Active) != 0 ) { //DrawPath(shape); } } }
static void DrawPath(MegaShapeLoft loft) { if ( loft.Layers == null ) return; if ( layernum >= loft.Layers.Length ) layernum = loft.Layers.Length - 1; if ( layernum >= 0 ) { MegaLoftLayerBase blayer = loft.Layers[layernum]; if ( blayer.LayerEnabled && blayer.layerPath != null ) { MegaSpline pathspline; pathspline = blayer.layerPath.splines[0]; // Method to get layer length or path float len = pathspline.length; float dst = pathdist / len; if ( dst < 0.002f ) dst = 0.002f; float ca = pathcrossalpha; Vector3 first = blayer.GetPos(loft, ca, pathstart); Color col = Gizmos.color; int i = 0; for ( float alpha = pathstart + dst; alpha < pathlength; alpha += dst ) { ca = pathcrossalpha + (twist * alpha); Vector3 p = blayer.GetPos(loft, ca, alpha); if ( (i & 1) == 0 ) Gizmos.color = Color.yellow; else Gizmos.color = Color.blue; Gizmos.DrawLine(loft.transform.TransformPoint(first), loft.transform.TransformPoint(p)); first = p; i++; } Gizmos.color = col; } } }
string[] GetLayers(MegaShapeLoft loft) { if ( loft.Layers == null ) { string[] lyers1 = new string[1]; lyers1[0] = "None"; return lyers1; } string[] lyers = new string[loft.Layers.Length + 1]; lyers[0] = "None"; for ( int i = 0; i < loft.Layers.Length; i++ ) { if ( loft.Layers[i] != null ) lyers[i + 1] = loft.Layers[i].LayerName; else lyers[i + 1] = "Deleted"; } return lyers; }
public override Vector3 GetPos(MegaShapeLoft loft, float ca, float a) { return(Vector3.zero); }
public override int BuildMesh(MegaShapeLoft loft, int triindex) { trisstart = triindex; if ( Lock ) return triindex + ((crosses - 2) * (evert - svert)); if ( layerPath == null || layerSection == null ) return triindex; MegaSpline pathspline = layerPath.splines[curve]; MegaSpline sectionspline = layerSection.splines[crosscurve]; // so for each loft section run through int vi = 0; Vector2 uv = Vector2.zero; Vector3 p = Vector3.zero; Vector3 scl = Vector3.one; float scalemultx = 1.0f; float scalemulty = 1.0f; Vector3 cmax = crossmax; cmax.x = 0.0f; cmax.z = 0.0f; Vector3 cmin = crossmin; cmin.x = 0.0f; cmin.z = 0.0f; Vector3 totaloff = Vector3.zero; float uvstart = pathStart; if ( UVOrigin == MegaLoftUVOrigin.SplineStart ) uvstart = 0.0f; Matrix4x4 twisttm = Matrix4x4.identity; Color col1 = color; Matrix4x4 tm; Vector3 lastup = loft.up; float calpha = 0.0f; for ( int cr = 0; cr < crosses; cr++ ) { float a = ((float)cr / (float)(crosses - 1)); float alpha = pathStart + (pathLength * a); totaloff = offset; if ( useOffsetX ) totaloff.x += offsetCrvX.Evaluate(alpha); if ( useOffsetY ) totaloff.y += offsetCrvY.Evaluate(alpha); if ( useOffsetZ ) totaloff.z += offsetCrvZ.Evaluate(alpha); // get the point on the spline if ( frameMethod == MegaFrameMethod.New ) tm = loft.GetDeformMatNewMethod(pathspline, alpha, true, alignCross, ref lastup); else tm = loft.GetDeformMatNew(pathspline, alpha, true, alignCross); if ( useTwistCrv ) { float twist = twistCrv.Evaluate(alpha) * twistAmt; float tw1 = pathspline.GetTwist(alpha); MegaShapeUtils.RotateZ(ref twisttm, Mathf.Deg2Rad * (twist - tw1)); tm = tm * twisttm; } if ( useCrossScaleCrv ) { float sa = Mathf.Repeat(a + scaleoff, 1.0f); scalemultx = crossScaleCrv.Evaluate(sa); } if ( !sepscale ) scalemulty = scalemultx; else { if ( useCrossScaleCrvY ) { float sa = Mathf.Repeat(a + scaleoffY, 1.0f); scalemulty = crossScaleCrvY.Evaluate(sa); } } scl.x = crossScale.x * scalemultx; // Use plus here and have curve as 0010 scl.y = crossScale.y * scalemulty; Vector3 crrot = cmax; crrot.y *= scl.y; Vector3 cminrot = cmin; cminrot.y *= scl.y; // Now need to loop through all the meshsections for ( int m = 0; m < meshsections.Count; m++ ) { MegaMeshSection ms = meshsections[m]; MegaMaterialSection mats = sections[ms.mat]; if ( mats.Enabled ) { if ( loft.useColors ) { if ( mats.colmode == MegaLoftColMode.Loft ) calpha = a; else calpha = alpha; calpha = Mathf.Repeat(calpha + mats.coloffset, 1.0f); col1.r = mats.colR.Evaluate(calpha); col1.g = mats.colG.Evaluate(calpha); col1.b = mats.colB.Evaluate(calpha); col1.a = mats.colA.Evaluate(calpha); } for ( int v = 0; v < ms.cverts.Count; v++ ) { p.x = ms.cverts[v].x * scl.x; p.y = ms.cverts[v].y * scl.y; // Curve for this value p.z = ms.cverts[v].z * scl.z; p = tm.MultiplyPoint3x4(p); uv.x = alpha - uvstart; //pathStart; uv.y = ms.cuvs[v].y; // - crossStart; // again not sure here start; if ( mats.physuv ) { uv.x *= pathspline.length; uv.y *= sectionspline.length; } else { if ( mats.uvcalcy ) { //uv.x = ((a * LoftLength) / sectionspline.length) - uvstart; uv.x = ((alpha * pathspline.length) / sectionspline.length) - uvstart; } } if ( conform ) ms.verts1.Add(p + totaloff); else ms.verts.Add(p + totaloff); if ( mats.swapuv ) { float ux = uv.x; uv.x = uv.y; uv.y = ux; } uv.x *= mats.UVScale.x; uv.y *= mats.UVScale.y; uv.x += mats.UVOffset.x; uv.y += mats.UVOffset.y; ms.uvs.Add(uv); //[vi] = uv; if ( loft.useColors ) ms.cols.Add(col1); vi++; } } } } //OptmizeMesh(); // Faces int index = triindex; int fi = 0; // Calc this if ( enabled ) { if ( flip ) { for ( int m = 0; m < meshsections.Count; m++ ) { MegaMeshSection ms = meshsections[m]; MegaMaterialSection mats = sections[ms.mat]; if ( mats.Enabled ) { for ( int cr = 0; cr < crosses - 1; cr++ ) { for ( int f = 0; f < ms.cverts.Count - 1; f++ ) { ms.tris.Add(index + f); ms.tris.Add(index + f + 1); ms.tris.Add(index + f + 1 + ms.cverts.Count); ms.tris.Add(index + f); ms.tris.Add(index + f + 1 + ms.cverts.Count); ms.tris.Add(index + f + ms.cverts.Count); fi += 6; } index += ms.cverts.Count; } index += ms.cverts.Count; } } } else { for ( int m = 0; m < meshsections.Count; m++ ) { MegaMeshSection ms = meshsections[m]; MegaMaterialSection mats = sections[ms.mat]; if ( mats.Enabled ) { for ( int cr = 0; cr < crosses - 1; cr++ ) { for ( int f = 0; f < ms.cverts.Count - 1; f++ ) { ms.tris.Add(index + f + 1 + ms.cverts.Count); ms.tris.Add(index + f + 1); ms.tris.Add(index + f); ms.tris.Add(index + f + ms.cverts.Count); ms.tris.Add(index + f + 1 + ms.cverts.Count); ms.tris.Add(index + f); fi += 6; } index += ms.cverts.Count; } index += ms.cverts.Count; } } } } if ( conform ) { CalcBounds(); DoConform(loft); } return triindex + fi; //triindex; }
public override int BuildMesh(MegaShapeLoft loft, int triindex) { MegaShape path = layerPath; if (tangent < 0.1f) { tangent = 0.1f; } //if ( snaptopath ) //{ //mat = path.transform.worldToLocalMatrix * transform.localToWorldMatrix; // * transform.worldToLocalMatrix; //mat = surfaceLoft.transform.localToWorldMatrix; //mat = transform.localToWorldMatrix * layerPath.transform.worldToLocalMatrix; // * transform.worldToLocalMatrix; //mat = surfaceLoft.transform.localToWorldMatrix; //mat = layerPath.transform.worldToLocalMatrix; // * transform.localToWorldMatrix; // * transform.worldToLocalMatrix; //mat = surfaceLoft.transform.localToWorldMatrix; //} //else //{ mat = Matrix4x4.identity; //path.transform.localToWorldMatrix; //} //mat = Matrix4x4.identity; //transform.worldToLocalMatrix; //mat = transform.localToWorldMatrix * path.transform.worldToLocalMatrix; // * transform.worldToLocalMatrix; //mat = surfaceLoft.transform.localToWorldMatrix; //mat = path.transform.worldToLocalMatrix * transform.localToWorldMatrix; // * transform.worldToLocalMatrix; //mat = surfaceLoft.transform.localToWorldMatrix; tm = Matrix4x4.identity; switch (axis) { case MegaAxis.X: MegaMatrix.RotateY(ref tm, -Mathf.PI * 0.5f); break; case MegaAxis.Y: MegaMatrix.RotateX(ref tm, -Mathf.PI * 0.5f); break; case MegaAxis.Z: break; } meshtm = Matrix4x4.identity; MegaMatrix.Rotate(ref meshtm, Mathf.Deg2Rad * rot); meshrot = Quaternion.Euler(rot); tw = meshrot; pathtm.SetTRS(offPath, Quaternion.Euler(rotPath), sclPath); float off = 0.0f; int trioff = 0; int vi = 0; int fi = 0; Vector3 sploff = Vector3.zero; if (snap) { sploff = path.splines[0].knots[0].p - path.splines[curve].knots[0].p; } int ax = (int)axis; float palpha = Start; // * 0.01f; if (startObj != null && StartEnabled) { Vector3 sscl = StartScale * GlobalScale; Vector3 soff = Vector3.Scale(StartOff, sscl); off -= startObj.bounds.min[(int)axis] * sscl[ax]; for (int i = 0; i < sverts.Length; i++) { Vector3 p = sverts[i]; p = Deform(p, path, palpha, off, sscl, RemoveDof, soff, sploff); // + sploff; loftverts[vi] = p; loftuvs[vi++] = suvs[i]; } for (int i = 0; i < stris.Length; i++) { lofttris[fi++] = stris[i] + triindex; } off += startObj.bounds.max[(int)axis] * sscl[ax]; off += StartGap; trioff = vi; } if (mainObj != null && MainEnabled) { float mw = mainObj.bounds.size[(int)axis]; Vector3 mscl = MainScale * GlobalScale; Vector3 moff = Vector3.Scale(MainOff, mscl); off -= mainObj.bounds.min[(int)axis] * mscl[ax]; mw *= mscl[(int)axis]; for (int r = 0; r < repeat; r++) { for (int i = 0; i < mverts.Length; i++) { Vector3 p = mverts[i]; p = Deform(p, path, palpha, off, mscl, RemoveDof, moff, sploff); // + sploff; loftverts[vi] = p; loftuvs[vi++] = muvs[i]; } for (int i = 0; i < mtris.Length; i++) { lofttris[fi++] = mtris[i] + trioff + triindex; } off += mw; off += Gap; trioff = vi; } off -= Gap; off += (mainObj.bounds.max[(int)axis] * mscl[ax]) - mw; } if (endObj != null && EndEnabled) { Vector3 escl = EndScale * GlobalScale; Vector3 eoff = Vector3.Scale(EndOff, escl); off -= endObj.bounds.min[(int)axis] * escl[ax]; off += EndGap; for (int i = 0; i < everts.Length; i++) { Vector3 p = everts[i]; p = Deform(p, path, palpha, off, escl, RemoveDof, eoff, sploff); // + sploff; loftverts[vi] = p; loftuvs[vi++] = euvs[i]; } for (int i = 0; i < etris.Length; i++) { lofttris[fi++] = etris[i] + trioff + triindex; } trioff += everts.Length; } if (conform) { CalcBounds(loftverts); DoConform(loft, loftverts); } return(triindex); }
// If beyond the start or end then need to extrapolate last or first rows public override Vector3 GetPosAndLook(MegaShapeLoft loft, float ca, float a, float at, out Vector3 p) { MegaMeshSection ms = null; ca = Mathf.Clamp(ca, 0.0f, 0.9999f); for ( int i = 0; i < meshsections.Count; i++ ) { ms = meshsections[i]; if ( ca >= ms.castart && ca <= ms.caend ) break; } ca = (ca - ms.castart) / (ms.caend - ms.castart); ca = Mathf.Clamp(ca, 0.0f, 0.9999f); float findex = (ms.cverts.Count - 1) * ca; int cindex = (int)findex; float interp = findex - cindex; int cindex1 = cindex + 1; bool flip = false; int pindex; int pindex1; float pinterp; if ( pathLength < 0.9999f || layerPath.splines[curve].closed == false ) { if ( a < 0.0f ) { pindex = 0; pindex1 = 1; pinterp = a * GetLength(loft); // / crosses); } else { if ( a >= 0.9999f ) { pindex = crosses - 1; pindex1 = crosses - 2; pinterp = (a - 1.0f) * GetLength(loft); flip = true; } else { float pfindex = (crosses - 1) * a; pindex = (int)pfindex; pinterp = pfindex - pindex; pindex1 = pindex + 1; } } } else { a = Mathf.Repeat(a, 0.9999f); float pfindex = (crosses - 1) * a; pindex = (int)pfindex; pinterp = pfindex - pindex; pindex1 = pindex + 1; } Vector3 p1 = ms.verts[(pindex * ms.cverts.Count) + cindex]; Vector3 p2 = ms.verts[(pindex * ms.cverts.Count) + cindex1]; Vector3 p3 = ms.verts[(pindex1 * ms.cverts.Count) + cindex]; Vector3 p4 = ms.verts[(pindex1 * ms.cverts.Count) + cindex1]; float pa = pinterp + at; p1.x = p1.x + (p2.x - p1.x) * interp; p1.y = p1.y + (p2.y - p1.y) * interp; p1.z = p1.z + (p2.z - p1.z) * interp; p2.x = p3.x + (p4.x - p3.x) * interp; p2.y = p3.y + (p4.y - p3.y) * interp; p2.z = p3.z + (p4.z - p3.z) * interp; if ( flip ) { p2.x = p1.x - p2.x; p2.y = p1.y - p2.y; p2.z = p1.z - p2.z; } else { p2.x = p2.x - p1.x; p2.y = p2.y - p1.y; p2.z = p2.z - p1.z; } p3.x = p1.x + (p2.x * pinterp); p3.y = p1.y + (p2.y * pinterp); p3.z = p1.z + (p2.z * pinterp); p.x = p1.x + (p2.x * pa); p.y = p1.y + (p2.y * pa); p.z = p1.z + (p2.z * pa); return p3; //Vector3.Lerp(pm1, pm2, pinterp); }
public void Copy(MegaShapeLoft to) { #if !UNITY_FLASH && !UNITY_METRO && !UNITY_WP8 Type tp = this.GetType(); FieldInfo[] fields = tp.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Default); //claredOnly); PropertyInfo[] properties = tp.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Default); //claredOnly); for ( int j = 0; j < fields.Length; j++ ) fields[j].SetValue(to, fields[j].GetValue(this)); for ( int j = 0; j < properties.Length; j++ ) { if ( properties[j].CanWrite ) properties[j].SetValue(to, properties[j].GetValue(this, null), null); } #endif }
// Do these from sample splines now? public override Vector3 GetPosAndUp(MegaShapeLoft loft, float ca, float a, float at, out Vector3 p, out Vector3 up) { MegaMeshSection ms = null; ca = Mathf.Clamp(ca, 0.0f, 0.9999f); for ( int i = 0; i < meshsections.Count; i++ ) { ms = meshsections[i]; if ( ca >= ms.castart && ca <= ms.caend ) break; } ca = (ca - ms.castart) / (ms.caend - ms.castart); ca = Mathf.Clamp(ca, 0.0f, 0.9999f); //ca = Mathf.Repeat(ca, 0.9999f); a = Mathf.Repeat(a, 0.9999f); float findex = (float)(ms.cverts.Count - 1) * ca; int cindex = (int)findex; float interp = findex - cindex; int cindex1 = cindex + 1; int pindex; int pindex1; bool flip = false; float pinterp; if ( pathLength < 0.9999f || layerPath.splines[curve].closed == false ) { if ( a < 0.0f ) { pindex = 0; pinterp = a * GetLength(loft); pindex1 = pindex + 1; } else { if ( a >= 0.999f ) { pindex = crosses - 2; pindex1 = crosses - 1; pinterp = (a - 1.0f) * GetLength(loft); flip = true; } else { float pfindex = (crosses - 1) * a; pindex = (int)pfindex; pinterp = pfindex - pindex; pindex1 = pindex + 1; } } } else { a = Mathf.Repeat(a, 0.9999f); float pfindex = (float)(crosses - 1) * a; pindex = (int)pfindex; pinterp = pfindex - pindex; pindex1 = pindex + 1; } Vector3 p1 = ms.verts[(pindex * ms.cverts.Count) + cindex]; Vector3 p2 = ms.verts[(pindex * ms.cverts.Count) + cindex1]; Vector3 p3 = ms.verts[(pindex1 * ms.cverts.Count) + cindex]; Vector3 p4 = ms.verts[(pindex1 * ms.cverts.Count) + cindex1]; Vector3 pm1 = Vector3.Lerp(p1, p2, interp); Vector3 pm2 = Vector3.Lerp(p3, p4, interp); Vector3 delta = pm2 - pm1; float pa = pinterp + at; // Quick calc of face normal Vector3 n1 = p2 - p1; // right Vector3 n2 = p3 - p1; // forward up = Vector3.Cross(n1, n2).normalized; if ( flip ) { p.x = pm2.x + (delta.x * pa); p.y = pm2.y + (delta.y * pa); p.z = pm2.z + (delta.z * pa); p3.x = pm2.x + (delta.x * pinterp); p3.y = pm2.y + (delta.y * pinterp); p3.z = pm2.z + (delta.z * pinterp); } else { p.x = pm1.x + (delta.x * pa); p.y = pm1.y + (delta.y * pa); p.z = pm1.z + (delta.z * pa); p3.x = pm1.x + (delta.x * pinterp); p3.y = pm1.y + (delta.y * pinterp); p3.z = pm1.z + (delta.z * pinterp); } return p3; }
public override Vector3 GetPos(MegaShapeLoft loft, float ca, float a) { return Vector3.zero; }
// Have cdist per material // Best way is to treat each material section like a layer public override bool PrepareLoft(MegaShapeLoft loft, int sc) { MegaShape section = layerSection; // Look at section and find out how many changes in material there are, ie matid // That will be the number of materials // Each change will have its own cdist, uv mapping, material // We seem to do this below as well FindSections(section.splines[crosscurve]); Matrix4x4 tm1 = Matrix4x4.identity; MegaMatrix.Translate(ref tm1, pivot); MegaMatrix.Rotate(ref tm1, new Vector3(Mathf.Deg2Rad * crossRot.x, Mathf.Deg2Rad * crossRot.y, Mathf.Deg2Rad * crossRot.z)); // Build the cross for each section if ( enabled ) { float dst = crossDist; if ( dst < 0.01f ) dst = 0.01f; int k = -1; int lk = -1; svert = verts.Count; Vector2 uv = Vector2.zero; float alpha = crossStart; float cend = crossStart + crossEnd; if ( alpha < 0.0f ) alpha = 0.0f; if ( cend > 1.0f ) cend = 1.0f; MegaSpline cspl = section.splines[crosscurve]; if ( cspl.closed ) { if ( alpha < 0.0f ) alpha += 1.0f; } Vector3 off = Vector3.zero; if ( snap ) off = cspl.knots[0].p - cspl.knots[0].p; if ( cspl.closed ) alpha = Mathf.Repeat(alpha, 1.0f); meshsections.Clear(); Vector3 pos = tm1.MultiplyPoint3x4(cspl.InterpCurve3D(alpha, section.normalizedInterp, ref lk) + off); int currentid = cspl.knots[lk].id; MegaMeshSection msect = new MegaMeshSection(); msect.castart = 0.0f; float uvalpha = crossStart; uv.y = uvalpha; //crossStart; msect.vertstart = 0; msect.mat = FindMaterial(currentid); meshsections.Add(msect); // Method to get cdist MegaMaterialSection ms = sections[msect.mat]; msect.cverts.Add(pos); bool loop = true; //float lastalpha = alpha; while ( alpha <= cend ) { if ( loop ) { alpha += ms.cdist; loop = false; } if ( alpha > cend ) alpha = cend; pos = tm1.MultiplyPoint3x4(cspl.InterpCurve3D(alpha, section.normalizedInterp, ref k) + off); if ( k != lk ) { while ( lk != k ) { //bool looped = false; lk++; int lk1 = lk % cspl.knots.Count; lk = lk1; if ( cspl.knots[lk].id != currentid ) { msect.cverts.Add(tm1.MultiplyPoint3x4(cspl.knots[lk].p + off)); float caend = ((cspl.knots[lk].length / cspl.length) - crossStart) / (crossEnd - crossStart); msect.caend = caend; // New material msect = new MegaMeshSection(); msect.castart = caend; pos = tm1.MultiplyPoint3x4(cspl.knots[lk].p + off); msect.vertstart = 0; currentid = cspl.knots[lk].id; msect.mat = FindMaterial(currentid); meshsections.Add(msect); ms = sections[msect.mat]; msect.cverts.Add(pos); lk1 = lk - 1; if ( lk1 < 0 ) lk1 = cspl.knots.Count - 1; alpha = (cspl.knots[lk1].length / cspl.length); // + crossStart; break; } else { if ( ms.includeknots ) msect.cverts.Add(tm1.MultiplyPoint3x4(cspl.knots[lk].p + off)); else { if ( cspl.knots[k].id != currentid ) { int kk = lk; while ( cspl.knots[kk].id == currentid && kk < cspl.knots.Count ) { kk++; } if ( kk >= cspl.knots.Count ) kk = cspl.knots.Count - 1; msect.cverts.Add(tm1.MultiplyPoint3x4(cspl.knots[kk].p + off)); break; } else msect.cverts.Add(pos); } } } } else msect.cverts.Add(pos); if ( alpha == cend ) { if ( msect.cverts[msect.cverts.Count - 1] != pos ) msect.cverts.Add(pos); break; } alpha += ms.cdist; if ( alpha > cend ) alpha = cend; } msect.caend = 1.0f; // Do uv and col now for ( int i = 0; i < meshsections.Count; i++ ) { MegaMeshSection ms1 = meshsections[i]; Vector2 uv1 = Vector2.zero; ms1.cuvs.Add(uv1); ms1.ccols.Add(Color.white); for ( int v = 1; v < ms1.cverts.Count; v++ ) { uv1.y += Vector3.Distance(ms1.cverts[v], ms1.cverts[v - 1]); ms1.cuvs.Add(uv1); ms1.ccols.Add(Color.white); } } // Add end point if ( section.splines[crosscurve].closed ) alpha = Mathf.Repeat(cend, 1.0f); } // Calc normals Vector3 up = Vector3.zero; Vector3 n1 = Vector3.zero; for ( int i = 0; i < meshsections.Count; i++ ) { MegaMeshSection ms1 = meshsections[i]; if ( ms1.cverts.Count > 1 ) { for ( int v = 0; v < ms1.cverts.Count; v++ ) { if ( v < ms1.cverts.Count - 1 ) n1 = (ms1.cverts[v + 1] - ms1.cverts[v]); else n1 = (ms1.cverts[v] - ms1.cverts[v - 1]); up.x = -n1.y; up.y = n1.x; up.z = 0.0f; ms1.cnorms.Add(up); } } } Prepare(loft); return true; }
public override bool PrepareLoft(MegaShapeLoft loft, int sc) { if ( surfaceLayer < 0 || surfaceLoft == null ) return false; // If we dont have any meshes then return null for not building if ( startObj == null && mainObj == null && endObj == null ) return false; Init(); int vcount = 0; int tcount = 0; if ( startObj && StartEnabled ) { vcount += sverts.Length; tcount += starttris; //stris.Length; for ( int i = 0; i < startlofttris.Count; i++ ) startlofttris[i].tris = new int[startlofttris[i].sourcetris.Length]; } if ( mainObj && MainEnabled ) { if ( Length != 0.0f ) { //Debug.Log("layer " + surfaceLayer); MegaLoftLayerSimple layer = (MegaLoftLayerSimple)surfaceLoft.Layers[surfaceLayer]; MegaShape path = layer.layerPath; if ( path ) { float dist = layer.LoftLength * Length; Vector3 scl = MainScale * GlobalScale; Vector3 size = Vector3.zero; size.x = (mainBounds.size.x * scl.x) + (Gap * GlobalScale); size.y = (mainBounds.size.y * scl.y) + (Gap * GlobalScale); size.z = (mainBounds.size.z * scl.z) + (Gap * GlobalScale); repeat = (int)(dist / size[(int)axis]); // + Gap)); } } vcount += (mverts.Length * repeat); tcount += (maintris * repeat); for ( int i = 0; i < mainlofttris.Count; i++ ) mainlofttris[i].tris = new int[mainlofttris[i].sourcetris.Length * repeat]; } if ( endObj && EndEnabled ) { vcount += everts.Length; tcount += endtris; //etris.Length; for ( int i = 0; i < endlofttris.Count; i++ ) endlofttris[i].tris = new int[endlofttris[i].sourcetris.Length]; } if ( loftverts == null || loftverts.Length != vcount ) loftverts = new Vector3[vcount]; if ( loftuvs == null || loftuvs.Length != vcount ) loftuvs = new Vector2[vcount]; if ( lofttris == null || lofttris.Length != tcount ) lofttris = new int[tcount]; return true; }
public override int BuildMesh(MegaShapeLoft loft, int triindex) { MegaShape path = layerPath; if ( path != null ) { if ( tangent < 0.1f ) tangent = 0.1f; mat = Matrix4x4.identity; //path.transform.localToWorldMatrix; //mat = transform.localToWorldMatrix * path.transform.worldToLocalMatrix; // * transform.worldToLocalMatrix; //mat = surfaceLoft.transform.localToWorldMatrix; tm = Matrix4x4.identity; tw = Quaternion.identity; switch ( axis ) { case MegaAxis.X: MegaMatrix.RotateY(ref tm, -Mathf.PI * 0.5f); break; case MegaAxis.Y: MegaMatrix.RotateX(ref tm, -Mathf.PI * 0.5f); break; case MegaAxis.Z: break; } meshtm = Matrix4x4.identity; MegaMatrix.Rotate(ref meshtm, Mathf.Deg2Rad * rot); meshrot = Quaternion.Euler(rot); pathtm.SetTRS(offPath, Quaternion.Euler(rotPath), sclPath); float off = 0.0f; int trioff = 0; int vi = 0; int ax = (int)axis; Vector3 sploff = Vector3.zero; if ( snap ) sploff = path.splines[0].knots[0].p - path.splines[curve].knots[0].p; if ( startObj != null && StartEnabled ) { Vector3 sscl = StartScale * GlobalScale; Vector3 soff = Vector3.Scale(StartOff + Offset, sscl); off -= startBounds.min[(int)axis] * sscl[ax]; for ( int i = 0; i < sverts.Length; i++ ) { Vector3 p = sverts[i]; p = Deform(p, path, start, off, sscl, RemoveDof, soff, sploff); loftverts[vi] = p; loftuvs[vi++] = suvs[i]; } for ( int i = 0; i < startlofttris.Count; i++ ) { for ( int t = 0; t < startlofttris[i].sourcetris.Length; t++ ) startlofttris[i].tris[t] = startlofttris[i].sourcetris[t] + triindex; } off += startBounds.max[(int)axis] * sscl[ax]; off += StartGap * GlobalScale; trioff = vi; } if ( mainObj != null && MainEnabled ) { for ( int i = 0; i < mainlofttris.Count; i++ ) mainlofttris[i].offset = 0; float mw = mainBounds.size[(int)axis]; Vector3 mscl = MainScale * GlobalScale; Vector3 moff = Vector3.Scale(MainOff + Offset, mscl); off -= mainBounds.min[(int)axis] * mscl[ax]; mw *= mscl[(int)axis]; float gaps = Gap * GlobalScale; for ( int r = 0; r < repeat; r++ ) { for ( int i = 0; i < mverts.Length; i++ ) { Vector3 p = mverts[i]; p = Deform(p, path, start, off, mscl, RemoveDof, moff, sploff); loftverts[vi] = p; loftuvs[vi++] = muvs[i]; } for ( int i = 0; i < mainlofttris.Count; i++ ) { int toff = mainlofttris[i].offset; for ( int t = 0; t < mainlofttris[i].sourcetris.Length; t++ ) mainlofttris[i].tris[toff++] = mainlofttris[i].sourcetris[t] + trioff + triindex; mainlofttris[i].offset = toff; } off += mw; off += gaps; trioff = vi; } off -= gaps; off += (mainBounds.max[(int)axis] * mscl[ax]) - mw; } if ( endObj != null && EndEnabled ) { Vector3 escl = EndScale * GlobalScale; Vector3 eoff = Vector3.Scale(EndOff + Offset, escl); off -= endBounds.min[(int)axis] * escl[ax]; off += EndGap * GlobalScale; for ( int i = 0; i < everts.Length; i++ ) { Vector3 p = everts[i]; p = Deform(p, path, start, off, escl, RemoveDof, eoff, sploff); loftverts[vi] = p; loftuvs[vi++] = euvs[i]; } for ( int i = 0; i < endlofttris.Count; i++ ) { for ( int t = 0; t < endlofttris[i].sourcetris.Length; t++ ) endlofttris[i].tris[t] = endlofttris[i].sourcetris[t] + triindex + trioff; } trioff += everts.Length; } } if ( conform ) { CalcBounds(loftverts); DoConform(loft, loftverts); } return triindex; }
public override int BuildMesh(MegaShapeLoft loft, int triindex) { MegaLoftLayerSimple layer = (MegaLoftLayerSimple)surfaceLoft.Layers[surfaceLayer]; LayerLength = 1.0f / layer.GetLength(surfaceLoft); if ( tangent < 0.1f ) tangent = 0.1f; //mat = surfaceLoft.transform.localToWorldMatrix; //mat = transform.localToWorldMatrix * surfaceLoft.transform.worldToLocalMatrix; // * transform.worldToLocalMatrix; //mat = surfaceLoft.transform.localToWorldMatrix; mat = surfaceLoft.transform.localToWorldMatrix * transform.worldToLocalMatrix; tm = Matrix4x4.identity; MegaMatrix.Rotate(ref tm, Mathf.Deg2Rad * tmrot); MegaMatrix.Rotate(ref meshtm, Mathf.Deg2Rad * rot); meshtm = Matrix4x4.identity; MegaMatrix.Rotate(ref meshtm, Mathf.Deg2Rad * rot); meshrot = Quaternion.Euler(rot); float off = 0.0f; int trioff = 0; int vi = 0; float ca = CrossAlpha; if ( ca > 0.99999f ) ca = 0.99999f; int ax = (int)axis; if ( startObj != null && StartEnabled ) { Vector3 sscl = StartScale * GlobalScale; Vector3 soff = Vector3.Scale(StartOff + Offset, sscl); off -= startBounds.min[(int)axis] * sscl[ax]; for ( int i = 0; i < sverts.Length; i++ ) { Vector3 p = sverts[i]; p = Deform(p, surfaceLoft, layer, start, ca, off, sscl, RemoveDof, soff); loftverts[vi] = p; loftuvs[vi++] = suvs[i]; } for ( int i = 0; i < startlofttris.Count; i++ ) { int toff = startlofttris[i].offset; for ( int t = 0; t < startlofttris[i].sourcetris.Length; t++ ) startlofttris[i].tris[toff++] = startlofttris[i].sourcetris[t] + trioff + triindex; startlofttris[i].offset = toff; } off += startBounds.max[(int)axis] * sscl[ax]; off += StartGap * GlobalScale; trioff = vi; } if ( mainObj != null && MainEnabled ) { for ( int i = 0; i < mainlofttris.Count; i++ ) mainlofttris[i].offset = 0; float mw = mainBounds.size[(int)axis]; Vector3 mscl = MainScale * GlobalScale; Vector3 moff = Vector3.Scale(MainOff + Offset, mscl); off -= mainBounds.min[(int)axis] * mscl[ax]; mw *= mscl[(int)axis]; float gaps = Gap * GlobalScale; for ( int r = 0; r < repeat; r++ ) { for ( int i = 0; i < mverts.Length; i++ ) { Vector3 p = mverts[i]; p = Deform(p, surfaceLoft, layer, start, ca, off, mscl, RemoveDof, moff); loftverts[vi] = p; loftuvs[vi++] = muvs[i]; } for ( int i = 0; i < mainlofttris.Count; i++ ) { int toff = mainlofttris[i].offset; for ( int t = 0; t < mainlofttris[i].sourcetris.Length; t++ ) mainlofttris[i].tris[toff++] = mainlofttris[i].sourcetris[t] + trioff + triindex; mainlofttris[i].offset = toff; } off += mw; off += gaps; trioff = vi; } off -= gaps; off += (mainBounds.max[(int)axis] * mscl[ax]) - mw; } if ( endObj != null && EndEnabled ) { Vector3 escl = EndScale * GlobalScale; Vector3 eoff = Vector3.Scale(EndOff + Offset, escl); off -= endBounds.min[(int)axis] * escl[ax]; off += EndGap * GlobalScale; for ( int i = 0; i < everts.Length; i++ ) { Vector3 p = everts[i]; p = Deform(p, surfaceLoft, layer, start, ca, off, escl, RemoveDof, eoff); loftverts[vi] = p; loftuvs[vi++] = euvs[i]; } for ( int i = 0; i < endlofttris.Count; i++ ) { int toff = endlofttris[i].offset; for ( int t = 0; t < endlofttris[i].sourcetris.Length; t++ ) endlofttris[i].tris[toff++] = endlofttris[i].sourcetris[t] + trioff + triindex; endlofttris[i].offset = toff; } trioff += everts.Length; } return triindex; }
// We could do a bary centric thing if we grid up the bounds void DoConform(MegaShapeLoft loft, Vector3[] verts) { InitConform(verts); if ( target && conformCollider ) { Matrix4x4 loctoworld = transform.localToWorldMatrix; Matrix4x4 tm = loctoworld; // * worldtoloc; Matrix4x4 invtm = tm.inverse; Ray ray = new Ray(); RaycastHit hit; float ca = conformAmount * loft.conformAmount; // When calculating alpha need to do caps sep for ( int i = 0; i < verts.Length; i++ ) { Vector3 origin = tm.MultiplyPoint(verts[i]); origin.y += raystartoff; ray.origin = origin; ray.direction = Vector3.down; //loftverts[i] = loftverts1[i]; if ( conformCollider.Raycast(ray, out hit, raydist) ) { Vector3 lochit = invtm.MultiplyPoint(hit.point); verts[i].y = Mathf.Lerp(verts[i].y, lochit.y + offsets[i] + conformOffset, ca); //conformAmount); last[i] = verts[i].y; } else { Vector3 ht = ray.origin; ht.y -= raydist; verts[i].y = last[i]; //lochit.z + offsets[i] + offset; } } } else { } }
public override int BuildMesh(MegaShapeLoft loft, int triindex) { if ( surfaceLoft == null && surfaceLayer < 0 ) return triindex; MegaLoftLayerSimple layer = (MegaLoftLayerSimple)surfaceLoft.Layers[surfaceLayer]; LayerLength = 1.0f / layer.GetLength(surfaceLoft); if ( tangent < 0.1f ) tangent = 0.1f; //mat = surfaceLoft.transform.localToWorldMatrix; //mat = transform.localToWorldMatrix * surfaceLoft.transform.worldToLocalMatrix; // * transform.worldToLocalMatrix; //mat = surfaceLoft.transform.localToWorldMatrix; mat = surfaceLoft.transform.localToWorldMatrix * transform.worldToLocalMatrix; tm = Matrix4x4.identity; switch ( axis ) { case MegaAxis.X: MegaMatrix.RotateY(ref tm, -Mathf.PI * 0.5f); break; case MegaAxis.Y: MegaMatrix.RotateX(ref tm, -Mathf.PI * 0.5f); break; case MegaAxis.Z: break; } meshrot = Quaternion.Euler(rot); int trioff = 0; int vi = 0; int fi = 0; Matrix4x4 omat = Matrix4x4.identity; Random.seed = Seed; Vector3 newup = Vector3.up; Vector3 pivot; Vector3 ps1; Vector3 scl = Vector3.zero; Vector3 sclmin = Vector3.Scale(scaleRangeMin, scale); Vector3 sclmax = Vector3.Scale(scaleRangeMax, scale); float a = 0.0f; for ( int r = 0; r < Count; r++ ) { scl.x = (scale.x + Mathf.Lerp(sclmin.x, sclmax.x, Random.value)) * GlobalScale; scl.y = (scale.y + Mathf.Lerp(sclmin.y, sclmax.y, Random.value)) * GlobalScale; scl.z = (scale.z + Mathf.Lerp(sclmin.z, sclmax.z, Random.value)) * GlobalScale; if ( useDensity ) a = FindScatterAlpha(); else a = Random.value; float alpha = start + (a * length) + Alpha; float calpha = cstart + (Random.value * clength) + CAlpha; Vector3 rt = rot + (((Random.value - 0.5f) * 2.0f) * rotRange); meshrot = Quaternion.Euler(rt); //Vector3 pivot = GetPos(surfaceLoft, layer, alpha, calpha, out normal, RemoveDof); if ( CalcUp ) { pivot = layer.GetPos1(surfaceLoft, calpha, alpha, 0.001f, out ps1, out newup); Quaternion uprot = Quaternion.FromToRotation(Vector3.up, newup); tw = Quaternion.AngleAxis(180.0f, Vector3.forward) * uprot; Vector3 relativePos = ps1 - pivot; relativePos.y *= RemoveDof; if ( relativePos == Vector3.zero ) { relativePos = Vector3.forward; } Quaternion rotation = Quaternion.LookRotation(relativePos) * tw * meshrot; //wtm.SetTRS(Vector3.zero, rotation, scl); wtm.SetTRS(pivot, rotation, scl); omat = mat * wtm; } else { pivot = layer.GetPos1(surfaceLoft, calpha, alpha, 0.001f, out ps1, out newup); tw = Quaternion.AngleAxis(180.0f, Vector3.forward); Vector3 relativePos = ps1 - pivot; relativePos.y *= RemoveDof; Quaternion rotation = Quaternion.LookRotation(relativePos) * tw * meshrot; //wtm.SetTRS(Vector3.zero, rotation, scl); wtm.SetTRS(pivot, rotation, scl); omat = mat * wtm; } Vector3 pp = Vector3.zero; for ( int i = 0; i < sverts.Length; i++ ) { pp.x = sverts[i].x + Offset.x; pp.y = sverts[i].y + Offset.y; pp.z = sverts[i].z + Offset.z; pp = omat.MultiplyPoint3x4(pp); loftverts[vi].x = pp.x; // + pivot.x; loftverts[vi].y = pp.y; // + pivot.y; loftverts[vi].z = pp.z; // + pivot.z; loftuvs[vi++] = suvs[i]; } for ( int i = 0; i < stris.Length; i++ ) lofttris[fi++] = stris[i] + trioff + triindex; trioff = vi; } return triindex; }
public override bool PrepareLoft(MegaShapeLoft loft, int sc) { if ( layerPath == null ) return false; if ( startObj == null && mainObj == null && endObj == null ) return false; Init(); int vcount = 0; int tcount = 0; if ( startObj && StartEnabled ) { vcount += sverts.Length; tcount += starttris; for ( int i = 0; i < startlofttris.Count; i++ ) startlofttris[i].tris = new int[startlofttris[i].sourcetris.Length]; } if ( mainObj && MainEnabled ) { if ( Length != 0.0f ) { MegaShape path = layerPath; float dist = path.splines[curve].length * Length; Vector3 scl = MainScale * GlobalScale; Vector3 size = Vector3.zero; size.x = (mainBounds.size.x * scl.x) + (Gap * GlobalScale); size.y = (mainBounds.size.y * scl.y) + (Gap * GlobalScale); size.z = (mainBounds.size.z * scl.z) + (Gap * GlobalScale); repeat = (int)(dist / size[(int)axis]); // + Gap)); if ( repeat < 0 ) repeat = 0; } vcount += (mverts.Length * repeat); tcount += (maintris * repeat); for ( int i = 0; i < mainlofttris.Count; i++ ) mainlofttris[i].tris = new int[mainlofttris[i].sourcetris.Length * repeat]; } if ( endObj && EndEnabled ) { vcount += everts.Length; tcount += endtris; for ( int i = 0; i < endlofttris.Count; i++ ) endlofttris[i].tris = new int[endlofttris[i].sourcetris.Length]; } if ( loftverts == null || loftverts.Length != vcount ) loftverts = new Vector3[vcount]; if ( loftuvs == null || loftuvs.Length != vcount ) loftuvs = new Vector2[vcount]; if ( lofttris == null || lofttris.Length != tcount ) lofttris = new int[tcount]; return true; }
public override bool PrepareLoft(MegaShapeLoft loft, int sc) { if (surfaceLayer < 0 || surfaceLoft == null) { return(false); } // If we dont have any meshes then return null for not building if (startObj == null && mainObj == null && endObj == null) { return(false); } Init(); int vcount = 0; int tcount = 0; if (startObj && StartEnabled) { vcount += sverts.Length; tcount += starttris; //stris.Length; for (int i = 0; i < startlofttris.Count; i++) { startlofttris[i].tris = new int[startlofttris[i].sourcetris.Length]; } } if (mainObj && MainEnabled) { if (Length != 0.0f) { //Debug.Log("layer " + surfaceLayer); MegaLoftLayerSimple layer = (MegaLoftLayerSimple)surfaceLoft.Layers[surfaceLayer]; MegaShape path = layer.layerPath; if (path) { float dist = layer.LoftLength * Length; Vector3 scl = MainScale * GlobalScale; Vector3 size = Vector3.zero; size.x = (mainBounds.size.x * scl.x) + (Gap * GlobalScale); size.y = (mainBounds.size.y * scl.y) + (Gap * GlobalScale); size.z = (mainBounds.size.z * scl.z) + (Gap * GlobalScale); repeat = (int)(dist / size[(int)axis]); // + Gap)); } } vcount += (mverts.Length * repeat); tcount += (maintris * repeat); for (int i = 0; i < mainlofttris.Count; i++) { mainlofttris[i].tris = new int[mainlofttris[i].sourcetris.Length * repeat]; } } if (endObj && EndEnabled) { vcount += everts.Length; tcount += endtris; //etris.Length; for (int i = 0; i < endlofttris.Count; i++) { endlofttris[i].tris = new int[endlofttris[i].sourcetris.Length]; } } if (loftverts == null || loftverts.Length != vcount) { loftverts = new Vector3[vcount]; } if (loftuvs == null || loftuvs.Length != vcount) { loftuvs = new Vector2[vcount]; } if (lofttris == null || lofttris.Length != tcount) { lofttris = new int[tcount]; } firstVerts = loftverts.Length; return(true); }
public override int BuildMesh(MegaShapeLoft loft, int triindex) { MegaShape path = layerPath; if ( tangent < 0.1f ) tangent = 0.1f; //if ( snaptopath ) //{ //mat = path.transform.worldToLocalMatrix * transform.localToWorldMatrix; // * transform.worldToLocalMatrix; //mat = surfaceLoft.transform.localToWorldMatrix; //mat = transform.localToWorldMatrix * layerPath.transform.worldToLocalMatrix; // * transform.worldToLocalMatrix; //mat = surfaceLoft.transform.localToWorldMatrix; //mat = layerPath.transform.worldToLocalMatrix; // * transform.localToWorldMatrix; // * transform.worldToLocalMatrix; //mat = surfaceLoft.transform.localToWorldMatrix; //} //else //{ mat = Matrix4x4.identity; //path.transform.localToWorldMatrix; //} //mat = Matrix4x4.identity; //transform.worldToLocalMatrix; //mat = transform.localToWorldMatrix * path.transform.worldToLocalMatrix; // * transform.worldToLocalMatrix; //mat = surfaceLoft.transform.localToWorldMatrix; //mat = path.transform.worldToLocalMatrix * transform.localToWorldMatrix; // * transform.worldToLocalMatrix; //mat = surfaceLoft.transform.localToWorldMatrix; tm = Matrix4x4.identity; switch ( axis ) { case MegaAxis.X: MegaMatrix.RotateY(ref tm, -Mathf.PI * 0.5f); break; case MegaAxis.Y: MegaMatrix.RotateX(ref tm, -Mathf.PI * 0.5f); break; case MegaAxis.Z: break; } meshtm = Matrix4x4.identity; MegaMatrix.Rotate(ref meshtm, Mathf.Deg2Rad * rot); meshrot = Quaternion.Euler(rot); tw = meshrot; pathtm.SetTRS(offPath, Quaternion.Euler(rotPath), sclPath); float off = 0.0f; int trioff = 0; int vi = 0; int fi = 0; Vector3 sploff = Vector3.zero; if ( snap ) sploff = path.splines[0].knots[0].p - path.splines[curve].knots[0].p; int ax = (int)axis; float palpha = Start; // * 0.01f; if ( startObj != null && StartEnabled ) { Vector3 sscl = StartScale * GlobalScale; Vector3 soff = Vector3.Scale(StartOff, sscl); off -= startObj.bounds.min[(int)axis] * sscl[ax]; for ( int i = 0; i < sverts.Length; i++ ) { Vector3 p = sverts[i]; p = Deform(p, path, palpha, off, sscl, RemoveDof, soff, sploff); // + sploff; loftverts[vi] = p; loftuvs[vi++] = suvs[i]; } for ( int i = 0; i < stris.Length; i++ ) lofttris[fi++] = stris[i] + triindex; off += startObj.bounds.max[(int)axis] * sscl[ax]; off += StartGap; trioff = vi; } if ( mainObj != null && MainEnabled ) { float mw = mainObj.bounds.size[(int)axis]; Vector3 mscl = MainScale * GlobalScale; Vector3 moff = Vector3.Scale(MainOff, mscl); off -= mainObj.bounds.min[(int)axis] * mscl[ax]; mw *= mscl[(int)axis]; for ( int r = 0; r < repeat; r++ ) { for ( int i = 0; i < mverts.Length; i++ ) { Vector3 p = mverts[i]; p = Deform(p, path, palpha, off, mscl, RemoveDof, moff, sploff); // + sploff; loftverts[vi] = p; loftuvs[vi++] = muvs[i]; } for ( int i = 0; i < mtris.Length; i++ ) lofttris[fi++] = mtris[i] + trioff + triindex; off += mw; off += Gap; trioff = vi; } off -= Gap; off += (mainObj.bounds.max[(int)axis] * mscl[ax]) - mw; } if ( endObj != null && EndEnabled ) { Vector3 escl = EndScale * GlobalScale; Vector3 eoff = Vector3.Scale(EndOff, escl); off -= endObj.bounds.min[(int)axis] * escl[ax]; off += EndGap; for ( int i = 0; i < everts.Length; i++ ) { Vector3 p = everts[i]; p = Deform(p, path, palpha, off, escl, RemoveDof, eoff, sploff); // + sploff; loftverts[vi] = p; loftuvs[vi++] = euvs[i]; } for ( int i = 0; i < etris.Length; i++ ) lofttris[fi++] = etris[i] + trioff + triindex; trioff += everts.Length; } if ( conform ) { CalcBounds(loftverts); DoConform(loft, loftverts); } return triindex; }
public override int BuildMesh(MegaShapeLoft loft, int triindex) { MegaLoftLayerSimple layer = (MegaLoftLayerSimple)surfaceLoft.Layers[surfaceLayer]; LayerLength = 1.0f / layer.GetLength(surfaceLoft); if (tangent < 0.1f) { tangent = 0.1f; } //mat = surfaceLoft.transform.localToWorldMatrix; //mat = transform.localToWorldMatrix * surfaceLoft.transform.worldToLocalMatrix; // * transform.worldToLocalMatrix; //mat = surfaceLoft.transform.localToWorldMatrix; mat = surfaceLoft.transform.localToWorldMatrix * transform.worldToLocalMatrix; tm = Matrix4x4.identity; MegaMatrix.Rotate(ref tm, Mathf.Deg2Rad * tmrot); MegaMatrix.Rotate(ref meshtm, Mathf.Deg2Rad * rot); meshtm = Matrix4x4.identity; MegaMatrix.Rotate(ref meshtm, Mathf.Deg2Rad * rot); meshrot = Quaternion.Euler(rot); float off = 0.0f; int trioff = 0; int vi = 0; float ca = CrossAlpha; if (ca > 0.99999f) { ca = 0.99999f; } int ax = (int)axis; if (startObj != null && StartEnabled) { Vector3 sscl = StartScale * GlobalScale; Vector3 soff = Vector3.Scale(StartOff + Offset, sscl); off -= startBounds.min[(int)axis] * sscl[ax]; for (int i = 0; i < sverts.Length; i++) { Vector3 p = sverts[i]; p = Deform(p, surfaceLoft, layer, start, ca, off, sscl, RemoveDof, soff); loftverts[vi] = p; loftuvs[vi++] = suvs[i]; } for (int i = 0; i < startlofttris.Count; i++) { int toff = startlofttris[i].offset; for (int t = 0; t < startlofttris[i].sourcetris.Length; t++) { startlofttris[i].tris[toff++] = startlofttris[i].sourcetris[t] + trioff + triindex; } startlofttris[i].offset = toff; } off += startBounds.max[(int)axis] * sscl[ax]; off += StartGap * GlobalScale; trioff = vi; } if (mainObj != null && MainEnabled) { for (int i = 0; i < mainlofttris.Count; i++) { mainlofttris[i].offset = 0; } float mw = mainBounds.size[(int)axis]; Vector3 mscl = MainScale * GlobalScale; Vector3 moff = Vector3.Scale(MainOff + Offset, mscl); off -= mainBounds.min[(int)axis] * mscl[ax]; mw *= mscl[(int)axis]; float gaps = Gap * GlobalScale; for (int r = 0; r < repeat; r++) { for (int i = 0; i < mverts.Length; i++) { Vector3 p = mverts[i]; p = Deform(p, surfaceLoft, layer, start, ca, off, mscl, RemoveDof, moff); loftverts[vi] = p; loftuvs[vi++] = muvs[i]; } for (int i = 0; i < mainlofttris.Count; i++) { int toff = mainlofttris[i].offset; for (int t = 0; t < mainlofttris[i].sourcetris.Length; t++) { mainlofttris[i].tris[toff++] = mainlofttris[i].sourcetris[t] + trioff + triindex; } mainlofttris[i].offset = toff; } off += mw; off += gaps; trioff = vi; } off -= gaps; off += (mainBounds.max[(int)axis] * mscl[ax]) - mw; } if (endObj != null && EndEnabled) { Vector3 escl = EndScale * GlobalScale; Vector3 eoff = Vector3.Scale(EndOff + Offset, escl); off -= endBounds.min[(int)axis] * escl[ax]; off += EndGap * GlobalScale; for (int i = 0; i < everts.Length; i++) { Vector3 p = everts[i]; p = Deform(p, surfaceLoft, layer, start, ca, off, escl, RemoveDof, eoff); loftverts[vi] = p; loftuvs[vi++] = euvs[i]; } for (int i = 0; i < endlofttris.Count; i++) { int toff = endlofttris[i].offset; for (int t = 0; t < endlofttris[i].sourcetris.Length; t++) { endlofttris[i].tris[toff++] = endlofttris[i].sourcetris[t] + trioff + triindex; } endlofttris[i].offset = toff; } trioff += everts.Length; } return(triindex); }
public override bool PrepareLoft(MegaShapeLoft loft, int sc) { Init(); int vcount = 0; int tcount = 0; if ( startObj && StartEnabled ) { vcount += sverts.Length; tcount += stris.Length; } if ( endObj && EndEnabled ) { vcount += everts.Length; tcount += etris.Length; } if ( mainObj && MainEnabled ) { if ( Length != 0.0f ) { MegaShape path = layerPath; if ( path ) { float dist = path.splines[curve].length * Length; Vector3 scl = MainScale * GlobalScale; Vector3 size = Vector3.zero; size.x = (mainObj.bounds.size.x + Gap) * scl.x; size.y = (mainObj.bounds.size.y + Gap) * scl.y; size.z = (mainObj.bounds.size.z + Gap) * scl.z; repeat = (int)(dist / size[(int)axis]); if ( repeat < 0 ) repeat = 0; } } vcount += (mverts.Length * repeat); tcount += (mtris.Length * repeat); } if ( loftverts == null || loftverts.Length != vcount ) loftverts = new Vector3[vcount]; if ( loftuvs == null || loftuvs.Length != vcount ) loftuvs = new Vector2[vcount]; if ( lofttris == null || lofttris.Length != tcount ) lofttris = new int[tcount]; return true; }
public override bool PrepareLoft(MegaShapeLoft loft, int sc) { Init(); int vcount = 0; int tcount = 0; if (startObj && StartEnabled) { vcount += sverts.Length; tcount += stris.Length; } if (endObj && EndEnabled) { vcount += everts.Length; tcount += etris.Length; } if (mainObj && MainEnabled) { if (Length != 0.0f) { MegaShape path = layerPath; if (path) { float dist = path.splines[curve].length * Length; Vector3 scl = MainScale * GlobalScale; Vector3 size = Vector3.zero; size.x = (mainObj.bounds.size.x + Gap) * scl.x; size.y = (mainObj.bounds.size.y + Gap) * scl.y; size.z = (mainObj.bounds.size.z + Gap) * scl.z; repeat = (int)(dist / size[(int)axis]); if (repeat < 0) { repeat = 0; } } } vcount += (mverts.Length * repeat); tcount += (mtris.Length * repeat); } if (loftverts == null || loftverts.Length != vcount) { loftverts = new Vector3[vcount]; } if (loftuvs == null || loftuvs.Length != vcount) { loftuvs = new Vector2[vcount]; } if (lofttris == null || lofttris.Length != tcount) { lofttris = new int[tcount]; } return(true); }
public virtual bool LoftNotify(MegaShapeLoft loft, int reason) { if ( Layers != null ) { for ( int i = 0; i < Layers.Length; i++ ) { if ( Layers[i].LoftNotify(loft, reason) ) { rebuild = true; break; } } } return rebuild; }
Vector3 GetCross(MegaShapeLoft loft, float ca) { MegaShape section = layerSection; Matrix4x4 tm1 = Matrix4x4.identity; MegaMatrix.Translate(ref tm1, pivot); MegaMatrix.Rotate(ref tm1, new Vector3(Mathf.Deg2Rad * crossRot.x, Mathf.Deg2Rad * crossRot.y, Mathf.Deg2Rad * crossRot.z)); int lk = -1; float alpha = crossStart + ca; MegaSpline cspl = section.splines[crosscurve]; if ( cspl.closed ) { if ( alpha < 0.0f ) alpha += 1.0f; } Vector3 off = Vector3.zero; if ( snap ) off = section.splines[0].knots[0].p - cspl.knots[0].p; if ( cspl.closed ) alpha = Mathf.Repeat(alpha, 1.0f); return tm1.MultiplyPoint3x4(section.splines[crosscurve].InterpCurve3D(alpha, section.normalizedInterp, ref lk) + off); }
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; }