void Reset() { if (this.GetComponent <Renderer>() != null) { Mesh ms = MegaUtils.GetSharedMesh(gameObject); if (ms != null) { CalcNormals(ms); Bounds b = ms.bounds; Offset = -b.center; bbox.min = b.center - b.extents; bbox.max = b.center + b.extents; } } }
public override Vector3 Map(int i, Vector3 p) { p = tm.MultiplyPoint3x4(p); Vector3 ip = p; float dist = p.magnitude; float dcy = Mathf.Exp(-totaldecay * Mathf.Abs(dist)); float u = Mathf.Abs(2.0f * p.x / dist); u = u * u; p.z += flex * MegaUtils.WaveFunc(p.y, time, amp * (1.0f - u) + amp2 * u, wave, phase, dy); p = Vector3.Lerp(ip, p, dcy); return(invtm.MultiplyPoint3x4(p)); }
void Reset() { MegaModifyObject modobj = (MegaModifyObject)gameObject.GetComponent <MegaModifyObject>(); if (modobj != null) { modobj.ModReset(this); } Renderer rend = GetComponent <Renderer>(); if (rend != null) { Mesh ms = MegaUtils.GetSharedMesh(gameObject); if (ms != null) { Bounds b = ms.bounds; Offset = -b.center; bbox.min = b.center - b.extents; bbox.max = b.center + b.extents; } } if (modobj.selection != null) { Bounds bb = new Bounds(); for (int i = 0; i < modobj.verts.Length; i++) { if (modobj.selection[i] > 0.001f) { bb.Encapsulate(modobj.verts[i]); } } Offset = -bb.center; bbox.min = bb.center - bb.extents; bbox.max = bb.center + bb.extents; } bsize = bbox.Size(); bcenter = bbox.center; Init(); }
public void FitFFDToMesh() { if (GetComponent <Renderer>() != null) { Mesh ms = MegaUtils.GetSharedMesh(gameObject); if (ms != null) { Bounds b = ms.bounds; Offset = -b.center; bbox.min = b.center - b.extents; bbox.max = b.center + b.extents; } } bsize = bbox.Size(); bcenter = bbox.center; Init(); }
public override Vector3 Map(int i, Vector3 p) { p = tm.MultiplyPoint3x4(p); Vector3 ip = p; float dist = p.magnitude; float dcy = Mathf.Exp(-totaldecay * Mathf.Abs(dist)); float a; if (amp != amp2) { float len = p.magnitude; if (len == 0.0f) { a = amp; } else { float u = (Mathf.Acos(p.x / len)) / Mathf.PI; u = (u > 0.5f) ? (1.0f - u) : u; u *= 2.0f; //u = u*u; u = Mathf.SmoothStep(0.0f, 1.0f, u); a = amp * (1.0f - u) + amp2 * u; } } else { a = amp; } float oldZ = p.y; p.y = 0.0f; float r = p.magnitude; p.y = oldZ + flex * MegaUtils.WaveFunc(r, time, a, wave, phase, dy); p = Vector3.Lerp(ip, p, dcy); return(invtm.MultiplyPoint3x4(p)); }
public bool recalcBounds = true; // recalc bounds // Call this to set the source game object which has the working point cache modifier attached public void SetSource(GameObject srcobj) { if (srcobj) { if (mesh == null) { mesh = MegaUtils.GetMesh(gameObject); } Mesh srcmesh = MegaUtils.GetMesh(srcobj); if (srcmesh.vertexCount == mesh.vertexCount) { obj = srcobj; mod = (MegaPointCache)srcobj.GetComponent <MegaPointCache>(); modobj = (MegaModifyObject)srcobj.GetComponent <MegaModifyObject>(); mesh = MegaUtils.GetMesh(gameObject); } } }
// Remove morph and pass tolerance then can morph to Utils // TODO: report error if target vert counts dont match base mapping bool DoMapping(MegaModifiers mod, MegaMorphOMatic morph, MegaTargetMesh tm, float scale, bool flipyz, bool negx) { for (int i = 0; i < mod.verts.Length; i++) { float a = (float)i / (float)mod.verts.Length; EditorUtility.DisplayProgressBar("Mapping", "Mapping vertex " + i, a); int map = MegaUtils.FindVert(mod.verts[i], tm.verts, morph.tolerance, scale, flipyz, negx, i); if (map == -1) { // Failed EditorUtility.ClearProgressBar(); return(false); } } EditorUtility.ClearProgressBar(); return(true); }
public void Rebuild(MegaRope rope) { if (source) { Mesh smesh = MegaUtils.GetSharedMesh(source); if (smesh) { bounds = smesh.bounds; sverts = smesh.vertices; overts = new Vector3[smesh.vertexCount]; rope.mesh.Clear(); rope.mesh.vertices = smesh.vertices; rope.mesh.normals = smesh.normals; rope.mesh.uv = smesh.uv; //rope.mesh.uv1 = smesh.uv1; rope.mesh.subMeshCount = smesh.subMeshCount; for (int i = 0; i < smesh.subMeshCount; i++) { rope.mesh.SetTriangles(smesh.GetTriangles(i), i); } MeshRenderer mr = source.GetComponent <MeshRenderer>(); MeshRenderer mr1 = rope.GetComponent <MeshRenderer>(); if (mr1 != null && mr != null) { mr1.sharedMaterials = mr.sharedMaterials; } // Calc the alphas BuildNormalMapping(smesh, true); BuildMesh(rope); } } }
void BuildMeshOld(Mesh mesh) { Width = Mathf.Clamp(Width, 0.0f, float.MaxValue); Length = Mathf.Clamp(Length, 0.0f, float.MaxValue); Height = Mathf.Clamp(Height, 0.0f, float.MaxValue); LengthSegs = Mathf.Clamp(LengthSegs, 1, 200); HeightSegs = Mathf.Clamp(HeightSegs, 1, 200); WidthSegs = Mathf.Clamp(WidthSegs, 1, 200); Vector3 vb = new Vector3(Width, Height, Length) / 2.0f; Vector3 va = -vb; if (PivotBase) { va.y = 0.0f; vb.y = Height; } if (PivotEdge) { va.x = 0.0f; vb.x = Width; } float dx = Width / (float)WidthSegs; float dy = Height / (float)HeightSegs; float dz = Length / (float)LengthSegs; Vector3 p = va; // Lists should be static, clear out to reuse List <Vector3> verts = new List <Vector3>(); List <Vector2> uvs = new List <Vector2>(); List <int> tris = new List <int>(); List <int> tris1 = new List <int>(); List <int> tris2 = new List <int>(); Vector2 uv = Vector2.zero; // Do we have top and bottom if (Width > 0.0f && Length > 0.0f) { //Matrix4x4 tm1 = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0.0f, rotate, 0.0f), Vector3.one); p.y = vb.y; for (int iz = 0; iz <= LengthSegs; iz++) { p.x = va.x; for (int ix = 0; ix <= WidthSegs; ix++) { verts.Add(p); if (genUVs) { uv.x = (p.x + vb.x) / Width; uv.y = (p.z + vb.z) / Length; uvs.Add(uv); } p.x += dx; } p.z += dz; } for (int iz = 0; iz < LengthSegs; iz++) { int kv = iz * (WidthSegs + 1); for (int ix = 0; ix < WidthSegs; ix++) { MakeQuad1(tris, kv, kv + WidthSegs + 1, kv + WidthSegs + 2, kv + 1); kv++; } } int index = verts.Count; p.y = va.y; p.z = va.z; for (int iy = 0; iy <= LengthSegs; iy++) { p.x = va.x; for (int ix = 0; ix <= WidthSegs; ix++) { verts.Add(p); if (genUVs) { uv.x = 1.0f - ((p.x + vb.x) / Width); uv.y = ((p.z + vb.z) / Length); uvs.Add(uv); } p.x += dx; } p.z += dz; } for (int iy = 0; iy < LengthSegs; iy++) { int kv = iy * (WidthSegs + 1) + index; for (int ix = 0; ix < WidthSegs; ix++) { MakeQuad1(tris1, kv, kv + 1, kv + WidthSegs + 2, kv + WidthSegs + 1); kv++; } } } // Front back if (Width > 0.0f && Height > 0.0f) { int index = verts.Count; p.z = va.z; p.y = va.y; for (int iz = 0; iz <= HeightSegs; iz++) { p.x = va.x; for (int ix = 0; ix <= WidthSegs; ix++) { verts.Add(p); if (genUVs) { uv.x = (p.x + vb.x) / Width; uv.y = (p.y + vb.y) / Height; uvs.Add(uv); } p.x += dx; } p.y += dy; } for (int iz = 0; iz < HeightSegs; iz++) { int kv = iz * (WidthSegs + 1) + index; for (int ix = 0; ix < WidthSegs; ix++) { MakeQuad1(tris2, kv, kv + WidthSegs + 1, kv + WidthSegs + 2, kv + 1); kv++; } } index = verts.Count; p.z = vb.z; p.y = va.y; for (int iy = 0; iy <= HeightSegs; iy++) { p.x = va.x; for (int ix = 0; ix <= WidthSegs; ix++) { verts.Add(p); if (genUVs) { uv.x = (p.x + vb.x) / Width; uv.y = (p.y + vb.y) / Height; uvs.Add(uv); } p.x += dx; } p.y += dy; } for (int iy = 0; iy < HeightSegs; iy++) { int kv = iy * (WidthSegs + 1) + index; for (int ix = 0; ix < WidthSegs; ix++) { MakeQuad1(tris2, kv, kv + 1, kv + WidthSegs + 2, kv + WidthSegs + 1); kv++; } } } // Left Right if (Length > 0.0f && Height > 0.0f) { int index = verts.Count; p.x = vb.x; p.y = va.y; for (int iz = 0; iz <= HeightSegs; iz++) { p.z = va.z; for (int ix = 0; ix <= LengthSegs; ix++) { verts.Add(p); if (genUVs) { uv.x = (p.z + vb.z) / Length; uv.y = (p.y + vb.y) / Height; uvs.Add(uv); } p.z += dz; } p.y += dy; } for (int iz = 0; iz < HeightSegs; iz++) { int kv = iz * (LengthSegs + 1) + index; for (int ix = 0; ix < LengthSegs; ix++) { MakeQuad1(tris2, kv, kv + LengthSegs + 1, kv + LengthSegs + 2, kv + 1); kv++; } } index = verts.Count; p.x = va.x; p.y = va.y; for (int iy = 0; iy <= HeightSegs; iy++) { p.z = va.z; for (int ix = 0; ix <= LengthSegs; ix++) { verts.Add(p); if (genUVs) { uv.x = (p.z + vb.z) / Length; uv.y = (p.y + vb.y) / Height; uvs.Add(uv); } p.z += dz; } p.y += dy; } for (int iy = 0; iy < HeightSegs; iy++) { int kv = iy * (LengthSegs + 1) + index; for (int ix = 0; ix < LengthSegs; ix++) { MakeQuad1(tris2, kv, kv + 1, kv + LengthSegs + 2, kv + LengthSegs + 1); kv++; } } } mesh.Clear(); mesh.subMeshCount = 3; mesh.vertices = verts.ToArray(); mesh.uv = uvs.ToArray(); mesh.SetTriangles(tris.ToArray(), 0); mesh.SetTriangles(tris1.ToArray(), 1); mesh.SetTriangles(tris2.ToArray(), 2); mesh.RecalculateNormals(); if (tangents) { MegaUtils.BuildTangents(mesh); } #if UNITY_5_5 || UNITY_5_6 || UNITY_2017 || UNITY_2018 || UNITY_2019 #else if (optimize) { mesh.Optimize(); } #endif mesh.RecalculateBounds(); }
public void RefreshMesh() { if (!rebuild) { return; } int offset = 0; // Check on vertex count here, if over 65000 then start a new object // TODO: Some layers wont add to mesh, ie colliders and object scatters for (int i = 0; i < Layers.Length; i++) { if (Layers[i].Valid()) { // Only call if verts need rebuilding, so per layer rebuild (spline change will do all) Layers[i].BuildMesh(this, offset); //Debug.Log("copy " + verts.Length); if (useColors) { Layers[i].CopyVertData(ref verts, ref uvs, ref cols, offset); } else { Layers[i].CopyVertData(ref verts, ref uvs, offset); } offset += Layers[i].NumVerts(); //loftverts.Length; } } //Vector2[] lmuvs = mesh.uv2; //mesh.Clear(); mesh.vertices = verts; mesh.uv = uvs; //if ( lmuvs.Length == uvs.Length ) //{ //Debug.Log("Setting uv2"); //mesh.uv2 = lmuvs; //} if (useColors) { mesh.colors = cols; } mesh.RecalculateNormals(); if (Tangents) { MegaUtils.BuildTangents(mesh); } #if UNITY_5_5 || UNITY_5_6 || UNITY_6 #else if (Optimize) { mesh.Optimize(); } #endif if (DoBounds) { mesh.RecalculateBounds(); } if (DoCollider) { RefreshCollider(); } }
void BuildMesh(Mesh mesh) { page_width = Mathf.Clamp(page_width, 0.0f, float.MaxValue); page_length = Mathf.Clamp(page_length, 0.0f, float.MaxValue); page_height = Mathf.Clamp(page_height, 0.0f, float.MaxValue); length_segs = Mathf.Clamp(length_segs, 1, 200); height_segs = Mathf.Clamp(height_segs, 1, 200); width_segs = Mathf.Clamp(width_segs, 1, 200); Vector3 vb = new Vector3(page_width, page_height, page_length) / 2.0f; Vector3 va = -vb; va.y = 0.0f; vb.y = page_height; float dx = page_width / (float)width_segs; float dy = page_height / (float)height_segs; float dz = page_length / (float)length_segs; Vector3 p = va; // Lists should be static, clear out to reuse List <Vector3> verts = new List <Vector3>(); List <Vector2> uvs = new List <Vector2>(); List <int> tris = new List <int>(); List <int> tris1 = new List <int>(); List <int> tris2 = new List <int>(); Vector2 uv = Vector2.zero; // Do we have top and bottom if (page_width > 0.0f && page_length > 0.0f) { Matrix4x4 tm1 = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0.0f, rotate, 0.0f), Vector3.one); Vector3 uv1 = Vector3.zero; p.y = vb.y; for (int iz = 0; iz <= length_segs; iz++) { p.x = va.x; for (int ix = 0; ix <= width_segs; ix++) { verts.Add(p); uv.x = (p.x - va.x) / page_width; uv.y = (p.z + vb.z) / page_length; uv1.x = uv.x - 0.5f; uv1.y = 0.0f; uv1.z = uv.y - 0.5f; uv1 = tm1.MultiplyPoint3x4(uv1); uv.x = 0.5f + uv1.x; uv.y = 0.5f + uv1.z; uvs.Add(uv); p.x += dx; } p.z += dz; } for (int iz = 0; iz < length_segs; iz++) { int kv = iz * (width_segs + 1); for (int ix = 0; ix < width_segs; ix++) { MakeQuad1(tris, kv, kv + width_segs + 1, kv + width_segs + 2, kv + 1); kv++; } } int index = verts.Count; p.y = va.y; p.z = va.z; for (int iy = 0; iy <= length_segs; iy++) { p.x = va.x; for (int ix = 0; ix <= width_segs; ix++) { verts.Add(p); uv.x = 1.0f - ((p.x + vb.x) / page_width); uv.y = ((p.z + vb.z) / page_length); uv1.x = uv.x - 0.5f; uv1.y = 0.0f; uv1.z = uv.y - 0.5f; uv1 = tm1.MultiplyPoint3x4(uv1); uv.x = 0.5f + uv1.x; uv.y = 0.5f + uv1.z; uvs.Add(uv); p.x += dx; } p.z += dz; } for (int iy = 0; iy < length_segs; iy++) { int kv = iy * (width_segs + 1) + index; for (int ix = 0; ix < width_segs; ix++) { MakeQuad1(tris1, kv, kv + 1, kv + width_segs + 2, kv + width_segs + 1); kv++; } } } // Front back if (page_width > 0.0f && page_height > 0.0f) { int index = verts.Count; p.z = va.z; p.y = va.y; for (int iz = 0; iz <= height_segs; iz++) { p.x = va.x; for (int ix = 0; ix <= width_segs; ix++) { verts.Add(p); uv.x = (p.x + vb.x) / page_width; uv.y = (p.y + vb.y) / page_height; uvs.Add(uv); p.x += dx; } p.y += dy; } for (int iz = 0; iz < height_segs; iz++) { int kv = iz * (width_segs + 1) + index; for (int ix = 0; ix < width_segs; ix++) { MakeQuad1(tris2, kv, kv + width_segs + 1, kv + width_segs + 2, kv + 1); kv++; } } index = verts.Count; p.z = vb.z; p.y = va.y; for (int iy = 0; iy <= height_segs; iy++) { p.x = va.x; for (int ix = 0; ix <= width_segs; ix++) { verts.Add(p); uv.x = (p.x + vb.x) / page_width; uv.y = (p.y + vb.y) / page_height; uvs.Add(uv); p.x += dx; } p.y += dy; } for (int iy = 0; iy < height_segs; iy++) { int kv = iy * (width_segs + 1) + index; for (int ix = 0; ix < width_segs; ix++) { MakeQuad1(tris2, kv, kv + 1, kv + width_segs + 2, kv + width_segs + 1); kv++; } } } // Left Right if (page_length > 0.0f && page_height > 0.0f) { int index = verts.Count; p.x = vb.x; p.y = va.y; for (int iz = 0; iz <= height_segs; iz++) { p.z = va.z; for (int ix = 0; ix <= length_segs; ix++) { verts.Add(p); uv.x = (p.z + vb.z) / page_length; uv.y = (p.y + vb.y) / page_height; uvs.Add(uv); p.z += dz; } p.y += dy; } for (int iz = 0; iz < height_segs; iz++) { int kv = iz * (length_segs + 1) + index; for (int ix = 0; ix < length_segs; ix++) { MakeQuad1(tris2, kv, kv + length_segs + 1, kv + length_segs + 2, kv + 1); kv++; } } index = verts.Count; p.x = va.x; p.y = va.y; for (int iy = 0; iy <= height_segs; iy++) { p.z = va.z; for (int ix = 0; ix <= length_segs; ix++) { verts.Add(p); uv.x = (p.z + vb.z) / page_length; uv.y = (p.y + vb.y) / page_height; uvs.Add(uv); p.z += dz; } p.y += dy; } for (int iy = 0; iy < height_segs; iy++) { int kv = iy * (length_segs + 1) + index; for (int ix = 0; ix < length_segs; ix++) { MakeQuad1(tris2, kv, kv + 1, kv + length_segs + 2, kv + length_segs + 1); kv++; } } } mesh.Clear(); mesh.subMeshCount = 3; mesh.vertices = verts.ToArray(); mesh.uv = uvs.ToArray(); mesh.SetTriangles(tris.ToArray(), 0); mesh.SetTriangles(tris1.ToArray(), 1); mesh.SetTriangles(tris2.ToArray(), 2); mesh.RecalculateNormals(); if (tangents) { MegaUtils.BuildTangents(mesh); } if (optimize) { mesh.Optimize(); } mesh.RecalculateBounds(); }
public override bool Prepare(MegaModContext mc) { if (colmesh == null) { colmesh = new MegaColliderMesh(); } if (colmesh.obj != hitObject && hitObject != null) { //Debug.Log("building"); colmesh = new MegaColliderMesh(); // This is colldier mesh not this mesh colmesh.mesh = MegaUtils.GetMesh(hitObject); //Debug.Log("verts " + colmesh.mesh.vertexCount); colmesh.verts = colmesh.mesh.vertices; colmesh.tris = colmesh.mesh.triangles; colmesh.normals = colmesh.mesh.normals; //colmesh.tree = KDTree.MakeFromPoints(colmesh.verts); //colmesh.trilist = new VertTriList(colmesh.mesh); colmesh.obj = hitObject; //Debug.Log("Col info built"); } //Debug.Log("Done"); if (hitObject) { //localPos = transform.worldToLocalMatrix.MultiplyPoint(hitObject.transform.position); col = hitObject.collider; } if (hitObject == null) { return(false); } //if ( col == null ) //return false; affected.Clear(); distances.Clear(); if (offsets == null || offsets.Length != mc.mod.verts.Length) { offsets = new Vector3[mc.mod.verts.Length]; } if (normals == null || normals.Length != verts.Length) { normals = mc.mod.mesh.normals; // get current normals } if (penetration == null || penetration.Length != mc.mod.verts.Length) { penetration = new float[mc.mod.verts.Length]; } mat = Matrix4x4.identity; SetAxis(mat); return(true); }
public void BuildMeshFromLayersNew() { // Check for any valid layers if (rebuild) //&& Layers.Length > 0 ) { //Debug.Log("*************** Build Mesh ****************"); Layers = GetComponents <MegaLoftLayerBase>(); if (Layers.Length > 0) { if (mesh == null) // Should be shapemesh, and this should be a util function { MeshFilter mf = gameObject.GetComponent <MeshFilter>(); if (mf == null) { mf = gameObject.AddComponent <MeshFilter>(); mf.name = gameObject.name; } mf.sharedMesh = new Mesh(); MeshRenderer mr = gameObject.GetComponent <MeshRenderer>(); if (mr == null) { mr = gameObject.AddComponent <MeshRenderer>(); } mesh = mf.sharedMesh; //Utils.GetMesh(gameObject); } if (!realtime) { rebuild = false; } int smcount = 0; int numverts = 0; for (int i = 0; i < Layers.Length; i++) { if (Layers[i].Valid()) { //smcount++; //smcount += Layers[i].NumMaterials(); Layers[i].PrepareLoft(this, i); smcount += Layers[i].NumMaterials(); numverts += Layers[i].NumVerts(); } } //Debug.Log("1 " + gameObject.name); if (numverts > 65535) { Debug.LogWarning("Loft Layer will have too many vertices. Lower the detail settings or disable a layer."); return; } //Debug.Log("verts " + verts.Length + " numverts " + numverts); if (verts == null || verts.Length != numverts) { verts = new Vector3[numverts]; } if (uvs == null || uvs.Length != numverts) { uvs = new Vector2[numverts]; } if (useColors && (cols == null || cols.Length != numverts)) { cols = new Color[numverts]; } //if ( useColor ) //{ // if ( colors == null || colors.Length != numverts ) // colors = new Color[numverts]; //} int offset = 0; // Check on vertex count here, if over 65000 then start a new object // TODO: Some layers wont add to mesh, ie colliders and object scatters for (int i = 0; i < Layers.Length; i++) { if (Layers[i].Valid()) { // Only call if verts need rebuilding, so per layer rebuild (spline change will do all) Layers[i].BuildMesh(this, offset); //Debug.Log("copy " + verts.Length); if (useColors) { Layers[i].CopyVertData(ref verts, ref uvs, ref cols, offset); } else { Layers[i].CopyVertData(ref verts, ref uvs, offset); } offset += Layers[i].NumVerts(); //loftverts.Length; } } //Vector2[] lmuvs = mesh.uv2; mesh.Clear(); mesh.subMeshCount = smcount; mesh.vertices = verts; mesh.uv = uvs; //if ( lmuvs.Length == uvs.Length ) //{ //Debug.Log("Setting uv2"); //mesh.uv2 = lmuvs; //} if (useColors) { mesh.colors = cols; } vertcount = verts.Length; polycount = 0; Material[] mats = new Material[smcount]; int mi = 0; for (int i = 0; i < Layers.Length; i++) { if (Layers[i].Valid()) { for (int m = 0; m < Layers[i].NumMaterials(); m++) { mats[mi] = Layers[i].GetMaterial(m); // We should be able to strip this int[] tris = Layers[i].GetTris(m); if (tris == null || tris.Length < 3) { mesh.SetTriangles(emptytris, mi); } else { mesh.SetTriangles(tris, mi); } polycount += tris.Length; mi++; } } } mesh.RecalculateNormals(); if (Tangents) { MegaUtils.BuildTangents(mesh); } #if UNITY_5_5 || UNITY_5_6 || UNITY_6 #else if (Optimize) { mesh.Optimize(); } #endif if (DoBounds) { mesh.RecalculateBounds(); } MeshRenderer mr1 = gameObject.GetComponent <MeshRenderer>(); if (mr1 != null) { mr1.sharedMaterials = mats; } meshCol = GetComponent <MeshCollider>(); if (DoCollider) { RefreshCollider(); } if (!updating) { updating = true; //MegaShapeLoft[] lofts = (MegaShapeLoft[])FindSceneObjectsOfType(typeof(MegaShapeLoft)); MegaShapeLoft[] lofts = (MegaShapeLoft[])FindObjectsOfType(typeof(MegaShapeLoft)); for (int i = 0; i < lofts.Length; i++) { if (lofts[i] != this && lofts[i].LoftNotify(this, 0)) { lofts[i].BuildMeshFromLayersNew(); } } updating = false; } } } }
public float Init(float tw) { Mesh ms = null; if (obj != null) { ms = MegaUtils.GetSharedMesh(obj); } if (ms != null) { Matrix4x4 tm = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(prerot), Vector3.one); verts = ms.vertices; miny = float.MaxValue; maxy = float.MinValue; for (int i = 0; i < verts.Length; i++) { verts[i] = tm.MultiplyPoint3x4(verts[i]); if (verts[i].y < miny) { miny = verts[i].y; } if (verts[i].y > maxy) { maxy = verts[i].y; } } uvs = ms.uv; normals = ms.normals; colors = ms.colors; for (int i = 0; i < normals.Length; i++) { normals[i] = tm.MultiplyVector(normals[i]); } tris = new MegaTriList[ms.subMeshCount]; for (int i = 0; i < ms.subMeshCount; i++) { tris[i].tris = ms.GetTriangles(i); } tw += weight; tweight = tw; subcount = ms.subMeshCount; area = ms.bounds.size.x * scale * ms.bounds.size.z * scale; places.Clear(); } // Proxy if (proxymesh) { Matrix4x4 tm = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(prerot), Vector3.one); proxyverts = proxymesh.vertices; //miny = float.MaxValue; //maxy = float.MinValue; for (int i = 0; i < proxyverts.Length; i++) { proxyverts[i] = tm.MultiplyPoint3x4(proxyverts[i]); //if ( verts[i].y < miny ) // miny = verts[i].y; //if ( verts[i].y > maxy ) // maxy = verts[i].y; } //uvs = ms.uv; proxynorms = proxymesh.normals; //colors = ms.colors; for (int i = 0; i < proxynorms.Length; i++) { proxynorms[i] = tm.MultiplyVector(proxynorms[i]); } proxytris = proxymesh.triangles; //for ( int i = 0; i < ms.subMeshCount; i++ ) // tris[i].tris = ms.GetTriangles(i); //tw += weight; //tweight = tw; //subcount = ms.subMeshCount; //area = ms.bounds.size.x * scale * ms.bounds.size.z * scale; //places.Clear(); } return(tw); }
// We only need to collision with outside masses so any mass not used 6 times, not if a shell // plus we give external links a different stiffness // should work from centre of mesh // Can easily thread this // We have spring builder in my max destruction soft body so use that public void VoxelMesh(GameObject obj, float size, bool fill) { if (obj == null) { return; } voxobj = obj; Mesh mesh = MegaUtils.GetMesh(voxobj); basemesh = mesh; int[] tris = mesh.triangles; Vector3[] verts = mesh.vertices; Vector3 meshsize = mesh.bounds.size; xs = Mathf.CeilToInt(meshsize.x / size); if (xs == 0) { xs = 1; } //Debug.Log("ys " + (meshsize.y / size).ToString("0.000")); ys = Mathf.CeilToInt(meshsize.y / size); if (ys == 0) { ys = 1; } zs = Mathf.CeilToInt(meshsize.z / size); if (zs == 0) { zs = 1; } origin.x = -((float)xs * 0.5f * size); //mesh.bounds.min + Jitter; origin.y = -((float)ys * 0.5f * size); //mesh.bounds.min + Jitter; origin.z = -((float)zs * 0.5f * size); //mesh.bounds.min + Jitter; // So first check all tris against each cell // first get bounds of tri // is it quicker to do each tri, get bounds and check those // or do each cell and check all tris, (can early out on this) // alloc array to hold cell data cells = new int[xs, ys, zs]; // Mmmm looks like a lot quicker otherway for 30 verts on a 4x4x4 we loop 4x4x4x30= 1920 // if we do tri and get bounds then a lot quicker could be as low as 30 or 2x2x2x30 = 240 Bounds box = new Bounds(); int gxs, gxe; int gys, gye; int gzs, gze; Vector3 min = Vector3.zero; Vector3 half = new Vector3(size * 0.5f, size * 0.5f, size * 0.5f); cellsize = new Vector3(size, size, size); //Debug.Log("Origin " + origin.ToString("0.000")); for (int i = 0; i < tris.Length; i += 3) { // do bounds on tri GetTriBounds(verts, tris, i, origin, out gxs, out gxe, out gys, out gye, out gzs, out gze); //Debug.Log("b[" + i + "] " + gxs + " " + gxe + " " + gys + " " + gye + " " + gzs + " " + gze); //for ( int z = 0; z < zs; z++ ) for (int z = gzs; z <= gze; z++) { min.z = origin.z + (z * size); //for ( int y = 0; y < ys; y++ ) for (int y = gys; y <= gye; y++) { min.y = origin.y + (y * size); //for ( int x = 0; x < xs; x++ ) for (int x = gxs; x <= gxe; x++) { min.x = origin.x + (x * size); box.SetMinMax(min, min + cellsize); //Debug.Log("box " + box.min.ToString("0.000")); // build box for cell if (AABB_Triangle_Intersection.TriangleBoxOverlap(verts[tris[i]], verts[tris[i + 1]], verts[tris[i + 2]], box)) { if (x < xs && y < ys && z < zs) { cells[x, y, z] = 1; } //Debug.Log("hit " + x + " " + y + " " + z); } } } } } // We should have a shell now, so now to fill line by line // can do for each axis and combine result (bit per axis) any bit means voxel, this will fill holes // Fill it, should do for each axis // could be a bug here here cant assume pen changes state in next block, so for pen down we get // the first block but we need end of contigous block if (fill) { for (int z = 0; z < zs; z++) { for (int y = 0; y < ys; y++) { int pd = 0; while (true) //x != -1 ) { // get pen down pd = GetXCell(cells, pd, y, z, xs); if (pd == -1) { break; } // get pen up int pu = GetXCell(cells, pd + 1, y, z, xs); if (pu == -1) { break; } for (int x = pd + 1; x < pu; x++) { cells[x, y, z] = 2; } pd = pu + 1; // pd is } } } } BuildLattice(); // Build bary coords BaryCoord[] barycoords = new BaryCoord[verts.Length]; for (int i = 0; i < verts.Length; i++) { Vector3 p = verts[i]; int x = (int)((p.x - origin.x) / CellSize); int y = (int)((p.y - origin.y) / CellSize); int z = (int)((p.z - origin.z) / CellSize); BaryCoord bc = new BaryCoord(); bc.p.x = (p.x - origin.x - (float)x * CellSize) / CellSize; bc.p.y = (p.y - origin.y - (float)y * CellSize) / CellSize; bc.p.z = (p.z - origin.z - (float)z * CellSize) / CellSize; for (int c = 0; c < 8; c++) { bc.m[c] = GetMassIndex(x, y, z, c); } //bc.m = FindMassIndex(p); //Debug.Log("[" + i + "] " + x + " " + y + " " + z + " " + p); barycoords[i] = bc; } // Rebuild verts test for (int i = 0; i < barycoords.Length; i++) { BaryCoord bc = barycoords[i]; Vector3 bl = Vector3.Lerp(voxmasses[bc.m[0]].p, voxmasses[bc.m[1]].p, bc.p.z); Vector3 tl = Vector3.Lerp(voxmasses[bc.m[4]].p, voxmasses[bc.m[5]].p, bc.p.z); Vector3 br = Vector3.Lerp(voxmasses[bc.m[3]].p, voxmasses[bc.m[2]].p, bc.p.z); Vector3 tr = Vector3.Lerp(voxmasses[bc.m[7]].p, voxmasses[bc.m[6]].p, bc.p.z); Vector3 l = Vector3.Lerp(bl, tl, bc.p.y); Vector3 r = Vector3.Lerp(br, tr, bc.p.y); verts[i] = Vector3.Lerp(l, r, bc.p.x); } //mesh.vertices = verts; }
public void BuildMesh(Mesh mesh) { if (shape == null) { return; } verts.Clear(); uvs.Clear(); tris.Clear(); dtris.Clear(); //normals.Clear(); pshape = MakePolyShape(shape, steps); Matrix4x4 axismat = Matrix4x4.identity; Quaternion q = Quaternion.identity; switch (direction) { case MegaAxis.X: q = Quaternion.Euler(90.0f, 0.0f, 0.0f); break; case MegaAxis.Y: q = Quaternion.Euler(0.0f, 90.0f, 0.0f); break; case MegaAxis.Z: q = Quaternion.Euler(0.0f, 0.0f, 90.0f); break; } int vertLevels = segments + 1; axismat.SetTRS(axis, q, Vector3.one); Matrix4x4 iaxis = axismat.inverse; Matrix4x4 rotmat = Matrix4x4.identity; float totlen = pshape.length[pshape.length.Count - 1]; Vector2 uv = Vector2.zero; Vector2 uv1 = uv; Matrix4x4 uvmat = Matrix4x4.identity; uvmat.SetTRS(Vector3.zero, Quaternion.Euler(0.0f, 0.0f, uvrotate), Vector3.one); Vector3 p = Vector3.zero; Vector3 scl = scale; //Vector3 norm = Vector3.zero; Vector3 lsp = Vector3.zero; int vindex = 0; float alphasub = 0.0f; if (!pivotbase) { alphasub = 1.0f; } for (int level = 0; level < vertLevels; level++) { uv.y = (float)level / (float)segments; float ang = (uv.y * degrees) + startang; //360.0f; rotmat = Matrix4x4.identity; MegaMatrix.RotateZ(ref rotmat, ang * Mathf.Deg2Rad); Matrix4x4 tm = iaxis * rotmat * axismat; //Vector3 lp = Vector3.zero; float cumlen = 0.0f; //int nix = normals.Count; //Debug.Log("minz " + min.z + " limz " + limits.z); for (int v = 0; v < pshape.points.Count; v++) { lsp = pshape.points[v]; //Vector3.Scale(pshape.points[v], scl); //float halpha = 1.0f - ((lsp.z - min.z) / limits.z); float halpha = alphasub - ((lsp.z - min.z) / limits.z); //lsp.x += profilecrv.Evaluate(halpha); //lsp.y += offsetycrv.Evaluate(halpha); if (usescalecrv) { //float halpha = (lsp.z - min.z) / limits.z; float adj = scaleamthgt.Evaluate(halpha) * globalscale; scl.x = 1.0f + (scalexcrv.Evaluate(uv.y) * adj); scl.y = 1.0f + (scaleycrv.Evaluate(uv.y) * adj); scl.z = 1.0f + (scalezcrv.Evaluate(uv.y) * adj); } lsp.x *= scl.x; lsp.y *= scl.y; lsp.z *= scl.z; if (twist != 0.0f) { float tang = ((twist * halpha * twistcrv.Evaluate(halpha)) + ang) * Mathf.Deg2Rad; float c = Mathf.Cos(tang); float s = Mathf.Sin(tang); rotmat[0, 0] = c; rotmat[0, 1] = s; rotmat[1, 0] = -s; rotmat[1, 1] = c; tm = iaxis * rotmat * axismat; } #if false norm.x = -(lsp.y - lp.y); norm.y = lsp.x - lp.x; norm.z = 0.0f; //lsp.z - lp.z; #endif //norm.x = -(lsp.z - lp.z); //- (lsp.y - lp.y); //norm.y = 0.0f; //lsp.x - lp.x; //norm.z = lsp.x - lp.x; //0.0f; //lsp.z - lp.z; //lp = lsp; p = tm * lsp; //Vector3.Scale(lsp, scl); //p.x += offsetxcrv.Evaluate(halpha); //p.y += offsetycrv.Evaluate(halpha); verts.Add(p); #if false if (v == 0) { } else { if (v == 1) { if (flip) { normals.Add(-tm.MultiplyVector(norm).normalized); } else { normals.Add(tm.MultiplyVector(norm).normalized); } } if (flip) { normals.Add(-tm.MultiplyVector(norm).normalized); } else { normals.Add(tm.MultiplyVector(norm).normalized); } } #endif cumlen = pshape.length[v]; uv.x = cumlen / totlen; // / cumlen; uv1 = uv; uv1.x *= uvscale.x; uv1.y *= uvscale.y; uv1 += uvoffset; uv1 = uvmat.MultiplyPoint(uv1); uvs.Add(uv1); } //if ( shape.splines[curve].closed ) //{ //normals[normals.Count - 1] = normals[nix]; //} } // Faces int vcount = pshape.points.Count; for (int level = 0; level < vertLevels - 1; level++) { int voff = level * vcount; for (int p1 = 0; p1 < pshape.points.Count - 1; p1++) { int v1 = p1 + voff; int v2 = v1 + 1; int v3 = level == vertLevels - 1 ? p1 : v1 + vcount; int v4 = v3 + 1; if (flip) { tris.Add(v1); tris.Add(v4); tris.Add(v2); tris.Add(v1); tris.Add(v3); tris.Add(v4); } else { tris.Add(v2); tris.Add(v4); tris.Add(v1); tris.Add(v4); tris.Add(v3); tris.Add(v1); } } } if (doublesided) { int vc = verts.Count; for (int i = 0; i < vc; i++) { verts.Add(verts[i]); uvs.Add(uvs[i]); normals.Add(-normals[i]); vindex++; } for (int i = 0; i < tris.Count; i += 3) { int v1 = tris[i]; int v2 = tris[i + 1]; int v3 = tris[i + 2]; dtris.Add(v3 + vc); dtris.Add(v2 + vc); dtris.Add(v1 + vc); } } //MeshFilter mf = GetComponent<MeshFilter>(); //mesh = mf.sharedMesh; //if ( mesh == null ) //{ // mesh = new Mesh(); // mesh.name = "Lathe"; // mf.sharedMesh = mesh; //} mesh.Clear(); mesh.vertices = verts.ToArray(); mesh.uv = uvs.ToArray(); if (doublesided) { mesh.subMeshCount = 2; } else { mesh.subMeshCount = 1; } mesh.SetTriangles(tris.ToArray(), 0); if (doublesided) { mesh.SetTriangles(dtris.ToArray(), 1); } mesh.RecalculateBounds(); //mesh.normals = normals.ToArray(); mesh.RecalculateNormals(); if (degrees == 360.0f) { Vector3[] n = mesh.normals; for (int v = 0; v < pshape.points.Count; v++) { Vector3 nn = n[v] + n[n.Length - pshape.points.Count + v]; n[v] = n[n.Length - pshape.points.Count + v] = nn.normalized; } mesh.normals = n; } if (buildTangents) { MegaUtils.BuildTangents(mesh); } }
public void BuildMeshNew(MegaScatterLayer inf) { if (verts.Count == 0 || inf.obj == null) { return; } GameObject cobj = new GameObject(); if (hideObjects) { cobj.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector | HideFlags.NotEditable; } cobj.name = "Scatter Mesh"; MeshFilter mf = cobj.AddComponent <MeshFilter>(); mf.sharedMesh = new Mesh(); MeshRenderer cmr = cobj.AddComponent <MeshRenderer>(); cmr.sharedMaterials = inf.obj.GetComponent <MeshRenderer>().sharedMaterials; MeshCollider meshCol = null; if (!inf.nocollider) { if (inf.buildcollider) { meshCol = cobj.AddComponent <MeshCollider>(); } } if (shape != null) { cobj.transform.position = shape.gameObject.transform.position; cobj.transform.localScale = shape.gameObject.transform.localScale; cobj.transform.rotation = shape.gameObject.transform.rotation; } else { cobj.transform.position = transform.position; cobj.transform.localScale = transform.localScale; cobj.transform.rotation = transform.rotation; } cobj.transform.parent = transform; cobj.isStatic = inf.markstatic; Mesh mesh; if (Application.isPlaying) { mesh = mf.mesh; } else { mesh = mf.sharedMesh; } mesh.Clear(); mesh.subMeshCount = submeshes.Count; mesh.vertices = verts.ToArray(); mesh.uv = uvs.ToArray(); mesh.normals = norms.ToArray(); mesh.colors = colors.ToArray(); for (int i = 0; i < submeshes.Count; i++) { mesh.SetTriangles(submeshes[i].tris.ToArray(), i); } mesh.RecalculateBounds(); if (inf.buildtangents) { MegaUtils.BuildTangents(mesh); } if (!inf.nocollider) { if (inf.buildcollider) { //MeshCollider meshCol = cobj.AddComponent<MeshCollider>(); if (inf.proxymesh) { Mesh colmesh = meshCol.sharedMesh; if (colmesh == mesh) { colmesh = new Mesh(); } colmesh.Clear(); colmesh.vertices = proxyverts.ToArray(); colmesh.normals = proxynorms.ToArray(); colmesh.triangles = proxytris.ToArray(); meshCol.sharedMesh = null; meshCol.sharedMesh = colmesh; meshCol.enabled = false; } else { meshCol.sharedMesh = null; meshCol.sharedMesh = mesh; meshCol.enabled = false; } } else { BoxCollider bc = cobj.AddComponent <BoxCollider>(); bc.enabled = false; } } objcount++; }
// we only need to build the savevertex and uvs if mesh def changes, else we can keep the uvs void BuildMesh() { if (!mesh) { mesh = GetComponent <MeshFilter>().sharedMesh; if (mesh == null) { updatemesh = true; return; } } if (hosespline.knots.Count == 0) { hosespline.AddKnot(Vector3.zero, Vector3.zero, Vector3.zero); hosespline.AddKnot(Vector3.zero, Vector3.zero, Vector3.zero); } FixHoseFillet(); bool createfree = freecreate; if ((!createfree) && ((!custnode) || (!custnode2))) { createfree = true; } if (custnode && custnode2) { createfree = false; } float Lf = 0.0f; Tlocal = Matrix4x4.identity; starty = Vector3.zero; if (createfree) { Lf = noreflength; } else { starty = custnode.transform.up; //Vector3.up; CalcSpline(); Lf = Vector3.Distance(custnode.transform.position, custnode2.transform.position); } MegaHoseType wtype = wiretype; int Segs = segments; if (Segs < 3) { Segs = 3; } if (rebuildcross) { rebuildcross = false; int nfillets = 0; int nsides = 0; if (wtype == MegaHoseType.Round) { NvertsPerRing = rndsides; if (NvertsPerRing < 3) { NvertsPerRing = 3; } } else { if (wtype == MegaHoseType.Rectangle) { nfillets = rectfilletsides; if (nfillets < 0) { nfillets = 0; } if (smooth == MegaHoseSmooth.SMOOTHNONE) { NvertsPerRing = (nfillets > 0 ? 8 + 4 * (nfillets - 1) : 8); } else { NvertsPerRing = (nfillets > 0 ? 8 + 4 * (nfillets - 1) : 4); //4); } } else { nfillets = dsecfilletsides; if (nfillets < 0) { nfillets = 0; } nsides = dsecrndsides; if (nsides < 2) { nsides = 2; } int nsm1 = nsides - 1; NvertsPerRing = (nfillets > 0 ? 6 + nsm1 + 2 * (nfillets - 1): 4 + nsm1); } } NvertsPerRing++; int NfacesPerEnd, NfacesPerRing, Nfaces = 0; //MegaHoseSmooth SMOOTH = smooth; Nverts = (Segs + 1) * (NvertsPerRing + 1); // + 2; if (capends) { Nverts += 2; } NfacesPerEnd = NvertsPerRing; NfacesPerRing = 6 * NvertsPerRing; Nfaces = Segs * NfacesPerRing; // + 2 * NfacesPerEnd; if (capends) { Nfaces += 2 * NfacesPerEnd; } if (SaveVertex == null || SaveVertex.Length != NvertsPerRing) { SaveVertex = new Vector3[NvertsPerRing]; SaveUV = new Vector2[NvertsPerRing]; } if (calcnormals) { if (SaveNormals == null || SaveNormals.Length != NvertsPerRing) { SaveNormals = new Vector3[NvertsPerRing]; } } MakeSaveVertex(NvertsPerRing, nfillets, nsides, wtype); if (verts == null || verts.Length != Nverts) { verts = new Vector3[Nverts]; uvs = new Vector2[Nverts]; faces = new int[Nfaces * 3]; } if (calcnormals && (normals == null || normals.Length != Nverts)) { normals = new Vector3[Nverts]; } } if (Nverts == 0) { return; } bool mapmenow = mapmemapme; int thisvert = 0; int last = Nverts - 1; int last2 = last - 1; int lastvpr = NvertsPerRing; // - 1; int maxseg = Segs + 1; float flexhere; float dadjust; float flexlen; float flex1 = flexstart; float flex2 = flexstop; int flexn = flexcycles; float flexd = flexdiameter; Vector3 ThisPosition; Vector2 uv = Vector2.zero; Matrix4x4 RingTM = Matrix4x4.identity; Matrix4x4 invRingTM = Matrix4x4.identity; Vector3 lastup = starty; float sizeadj = 1.0f; for (int i = 0; i < maxseg; i++) { float incr = (float)i / (float)Segs; if (createfree) { ThisPosition = new Vector3(0.0f, 0.0f, Lf * incr); } else { int k = 0; ThisPosition = hosespline.InterpCurve3D(incr, true, ref k); } RingTM = GetSplineMat(hosespline, incr, true, ref lastup); if (!createfree) { RingTM = Tlocal * RingTM; } if (calcnormals) { invRingTM = RingTM; MegaMatrix.NoTrans(ref invRingTM); } if ((incr > flex1) && (incr < flex2) && flexon) { flexlen = flex2 - flex1; if (flexlen < 0.01f) { flexlen = 0.01f; } flexhere = (incr - flex1) / flexlen; float ang = (float)flexn * flexhere * (Mathf.PI * 2.0f) + PIover2; dadjust = 1.0f + flexd * (1.0f - Mathf.Sin(ang)); //(float)flexn * flexhere * (Mathf.PI * 2.0f) + PIover2)); } else { dadjust = 0.0f; } if (usebulgecurve) { if (dadjust == 0.0f) { dadjust = 1.0f + (bulge.Evaluate(incr + bulgeoffset) * bulgeamount); } else { dadjust += bulge.Evaluate(incr + bulgeoffset) * bulgeamount; } } if (usesizecurve) { sizeadj = size.Evaluate(incr); } uv.x = 0.999999f * incr * uvscale.x; for (int j = 0; j < NvertsPerRing; j++) { int jj = j; // % NvertsPerRing; if (mapmenow) { uv.y = SaveUV[jj].y; uvs[thisvert] = uv; //new Vector2(0.999999f * incr * uvscale.x, SaveUV[jj].y); } if (dadjust != 0.0f) { verts[thisvert] = RingTM.MultiplyPoint(sizeadj * dadjust * SaveVertex[jj]); } else { verts[thisvert] = RingTM.MultiplyPoint(sizeadj * SaveVertex[jj]); } if (calcnormals) { normals[thisvert] = invRingTM.MultiplyPoint(SaveNormals[jj]).normalized; //.MultiplyPoint(-SaveNormals[jj]); } thisvert++; } if (mapmenow) { //uvs[Nverts + i] = new Vector2(0.999999f * incr, 0.999f); } if (capends) { if (i == 0) { verts[last2] = (createfree ? ThisPosition : Tlocal.MultiplyPoint(ThisPosition)); if (mapmenow) { uvs[last2] = Vector3.zero; } } else { if (i == Segs) { verts[last] = createfree ? ThisPosition : Tlocal.MultiplyPoint(ThisPosition); if (mapmenow) { uvs[last] = Vector3.zero; } } } } } // Now, set up the faces int thisface = 0, v1, v2, v3, v4; v3 = last2; if (capends) { for (int i = 0; i < NvertsPerRing - 1; i++) { v1 = i; v2 = (i < lastvpr ? v1 + 1 : v1 - lastvpr); //v5 = (i < lastvpr ? v2 : Nverts); faces[thisface++] = v2; faces[thisface++] = v1; faces[thisface++] = v3; } } int ringnum = NvertsPerRing; // + 1; for (int i = 0; i < Segs; i++) { for (int j = 0; j < NvertsPerRing - 1; j++) { v1 = i * ringnum + j; v2 = v1 + 1; //(j < lastvpr? v1 + 1 : v1 - lastvpr); v4 = v1 + ringnum; v3 = v2 + ringnum; faces[thisface++] = v1; faces[thisface++] = v2; faces[thisface++] = v3; faces[thisface++] = v1; faces[thisface++] = v3; faces[thisface++] = v4; } } int basevert = Segs * ringnum; //NvertsPerRing; v3 = Nverts - 1; if (capends) { for (int i = 0; i < NvertsPerRing - 1; i++) { v1 = i + basevert; v2 = (i < lastvpr? v1 + 1 : v1 - lastvpr); //v5 = (i < lastvpr? v2 : Nverts + Segs); faces[thisface++] = v1; faces[thisface++] = v2; faces[thisface++] = v3; } } mesh.Clear(); mesh.subMeshCount = 1; mesh.vertices = verts; mesh.uv = uvs; mesh.triangles = faces; if (calcnormals) { mesh.normals = normals; } else { mesh.RecalculateNormals(); } mesh.RecalculateBounds(); #if UNITY_5_5 || UNITY_5_6 || UNITY_2017 #else if (optimize) { ; } #endif if (calctangents) { MegaUtils.BuildTangents(mesh); } if (recalcCollider) { if (meshCol == null) { meshCol = GetComponent <MeshCollider>(); } if (meshCol != null) { meshCol.sharedMesh = null; meshCol.sharedMesh = mesh; //bool con = meshCol.convex; //meshCol.convex = con; } } }
static void AllNewCreateSimplePrefab() { if (Selection.activeGameObject != null) { if (!Directory.Exists("Assets/MegaPrefabs")) { AssetDatabase.CreateFolder("Assets", "MegaPrefabs"); } GameObject obj = Selection.activeGameObject; // Make a copy? GameObject newobj = MegaCopyObject.DoCopyObjects(obj); newobj.name = obj.name; // Get all modifyObjects in children MegaModifyObject[] mods = newobj.GetComponentsInChildren <MegaModifyObject>(); for (int i = 0; i < mods.Length; i++) { // Need method to get the base mesh GameObject pobj = mods[i].gameObject; // Get the mesh and make an asset for it Mesh mesh = MegaUtils.GetSharedMesh(pobj); if (mesh) { string mname = mesh.name; int ix = mname.IndexOf("Instance"); if (ix != -1) { mname = mname.Remove(ix); } string meshpath = "Assets/MegaPrefabs/" + mname + ".prefab"; AssetDatabase.CreateAsset(mesh, meshpath); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } } MegaWrap[] wraps = newobj.GetComponentsInChildren <MegaWrap>(); for (int i = 0; i < wraps.Length; i++) { // Need method to get the base mesh GameObject pobj = wraps[i].gameObject; // Get the mesh and make an asset for it Mesh mesh = MegaUtils.GetSharedMesh(pobj); if (mesh) { string mname = mesh.name; int ix = mname.IndexOf("Instance"); if (ix != -1) { mname = mname.Remove(ix); } string meshpath = "Assets/MegaPrefabs/" + mname + ".prefab"; AssetDatabase.CreateAsset(mesh, meshpath); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } } Object prefab = PrefabUtility.CreateEmptyPrefab("Assets/MegaPrefabs/" + newobj.name + "_Prefab.prefab"); //EditorUtility.ReplacePrefab(newobj, prefab, ReplacePrefabOptions.ConnectToPrefab); PrefabUtility.ReplacePrefab(newobj, prefab, ReplacePrefabOptions.ConnectToPrefab); DestroyImmediate(newobj); } }
// Taken from chain mesher void InitLinkObjects(MegaShape path) { if (LinkObj == null) { return; } float len = path.splines[curve].length; // Assume z axis for now float linklen = (linkOff1.y - linkOff.y) * linkScale.x * LinkSize; linkcount = (int)(len / linklen); for (int i = linkcount; i < gameObject.transform.childCount; i++) { GameObject go = gameObject.transform.GetChild(i).gameObject; if (Application.isEditor) { DestroyImmediate(go); } else { Destroy(go); } } linkobjs = new Transform[linkcount]; if (linkcount > gameObject.transform.childCount) { for (int i = 0; i < gameObject.transform.childCount; i++) { GameObject go = gameObject.transform.GetChild(i).gameObject; #if UNITY_3_5 go.SetActiveRecursively(true); #else go.SetActive(true); #endif linkobjs[i] = go.transform; } int index = gameObject.transform.childCount; for (int i = index; i < linkcount; i++) { GameObject go = new GameObject(); go.name = "Link"; GameObject obj = LinkObj; if (obj) { MeshRenderer mr = (MeshRenderer)obj.GetComponent <MeshRenderer>(); Mesh ms = MegaUtils.GetSharedMesh(obj); MeshRenderer mr1 = (MeshRenderer)go.AddComponent <MeshRenderer>(); MeshFilter mf1 = (MeshFilter)go.AddComponent <MeshFilter>(); mf1.sharedMesh = ms; mr1.sharedMaterial = mr.sharedMaterial; go.transform.parent = gameObject.transform; linkobjs[i] = go.transform; } } } else { for (int i = 0; i < linkcount; i++) { GameObject go = gameObject.transform.GetChild(i).gameObject; #if UNITY_3_5 go.SetActiveRecursively(true); #else go.SetActive(true); #endif linkobjs[i] = go.transform; } } #if UNITY_5_4 || UNITY_5_5 || UNITY_5_6 || UNITY_2017 Random.InitState(0); #else Random.seed = 0; #endif for (int i = 0; i < linkcount; i++) { GameObject obj = LinkObj; //1[oi]; GameObject go = gameObject.transform.GetChild(i).gameObject; MeshRenderer mr = (MeshRenderer)obj.GetComponent <MeshRenderer>(); Mesh ms = MegaUtils.GetSharedMesh(obj); MeshRenderer mr1 = (MeshRenderer)go.GetComponent <MeshRenderer>(); MeshFilter mf1 = (MeshFilter)go.GetComponent <MeshFilter>(); mf1.sharedMesh = ms; mr1.sharedMaterials = mr.sharedMaterials; } }
public void ModifyCompressedMT(MegaModifiers mc, int tindex, int cores) { if (!mtmorphed) { return; } int step = source.morphedVerts.Length / cores; int startvert = (tindex * step); int endvert = startvert + step; if (tindex == cores - 1) { endvert = source.morphedVerts.Length; } framenum++; Vector3 delt; // cycle through channels, searching for ones to use bool firstchan = true; Vector3[] endpoint = new Vector3[4]; // These in channel class Vector3[] splinepoint = new Vector3[4]; Vector3[] temppoint = new Vector3[2]; for (int i = 0; i < chanBank.Count; i++) { MegaMorphChan chan = chanBank[i]; if (chan.fChannelPercent != 0.0f) { if (chan.mTargetCache != null && chan.mTargetCache.Count > 0 && chan.mActiveOverride) //&& fChannelPercent != 0.0f ) { if (firstchan) { firstchan = false; for (int pointnum = startvert; pointnum < endvert; pointnum++) { int p = source.morphedVerts[pointnum]; dif[p] = source.oPoints[p]; } } if (chan.mTargetCache.Count == 1) { { for (int pointnum = startvert; pointnum < endvert; pointnum++) { int p = source.morphedVerts[pointnum]; delt = chan.mDeltas[p]; dif[p].x += delt.x * chan.fChannelPercent; dif[p].y += delt.y * chan.fChannelPercent; dif[p].z += delt.z * chan.fChannelPercent; } } } else { float targetpercent1 = chan.GetTargetPercent(chan.segment - 3); float targetpercent2 = chan.GetTargetPercent(chan.segment - 2); float top = chan.fProgression - targetpercent1; float bottom = targetpercent2 - targetpercent1; float u = top / bottom; for (int pointnum = startvert; pointnum < endvert; pointnum++) { int p = source.morphedVerts[pointnum]; Vector3 vert = source.oPoints[p]; //pointnum]; float length; Vector3 progession; endpoint[0] = chan.p1[p]; endpoint[1] = chan.p2[p]; endpoint[2] = chan.p3[p]; endpoint[3] = chan.p4[p]; if (chan.segment == 1) { splinepoint[0] = endpoint[0]; splinepoint[3] = endpoint[1]; temppoint[1] = endpoint[2] - endpoint[0]; temppoint[0] = endpoint[1] - endpoint[0]; length = temppoint[1].sqrMagnitude; if (length == 0.0f) { splinepoint[1] = endpoint[0]; splinepoint[2] = endpoint[1]; } else { splinepoint[2] = endpoint[1] - (Vector3.Dot(temppoint[0], temppoint[1]) * chan.mCurvature / length) * temppoint[1]; splinepoint[1] = endpoint[0] + chan.mCurvature * (splinepoint[2] - endpoint[0]); } } else { if (chan.segment == chan.mTargetCache.Count) //chan.totaltargs ) { splinepoint[0] = endpoint[1]; splinepoint[3] = endpoint[2]; temppoint[1] = endpoint[2] - endpoint[0]; temppoint[0] = endpoint[1] - endpoint[2]; length = temppoint[1].sqrMagnitude; if (length == 0.0f) { splinepoint[1] = endpoint[0]; splinepoint[2] = endpoint[1]; } else { splinepoint[1] = endpoint[1] - (Vector3.Dot(temppoint[1], temppoint[0]) * chan.mCurvature / length) * temppoint[1]; splinepoint[2] = endpoint[2] + chan.mCurvature * (splinepoint[1] - endpoint[2]); } } else { temppoint[1] = endpoint[2] - endpoint[0]; temppoint[0] = endpoint[1] - endpoint[0]; length = temppoint[1].sqrMagnitude; splinepoint[0] = endpoint[1]; splinepoint[3] = endpoint[2]; if (length == 0.0f) { splinepoint[1] = endpoint[0]; } else { splinepoint[1] = endpoint[1] + (Vector3.Dot(temppoint[0], temppoint[1]) * chan.mCurvature / length) * temppoint[1]; } temppoint[1] = endpoint[3] - endpoint[1]; temppoint[0] = endpoint[2] - endpoint[1]; length = temppoint[1].sqrMagnitude; if (length == 0.0f) { splinepoint[2] = endpoint[1]; } else { splinepoint[2] = endpoint[2] - (Vector3.Dot(temppoint[0], temppoint[1]) * chan.mCurvature / length) * temppoint[1]; } } } MegaUtils.Bez3D(out progession, ref splinepoint, u); dif[p].x += progession.x - vert.x; dif[p].y += progession.y - vert.y; dif[p].z += progession.z - vert.z; } } } } } if (mtmorphed) { for (int i = setStart[tindex]; i < setEnd[tindex]; i++) { sverts[source.morphMappingTo[i]] = dif[source.morphMappingFrom[i]]; } for (int i = copyStart[tindex]; i < copyEnd[tindex]; i++) { sverts[source.nonMorphMappingTo[i]] = source.oPoints[source.nonMorphMappingFrom[i]]; } } }
public void ModifyCompressed(MegaModifiers mc) { framenum++; mc.ChangeSourceVerts(); float fChannelPercent; Vector3 delt; // cycle through channels, searching for ones to use bool firstchan = true; bool morphed = false; for (int i = 0; i < source.chanBank.Count; i++) { MegaMorphChan chan = source.chanBank[i]; MegaMorphChan lchan = chanBank[i]; // copy of source banks with out the vert data chan.UpdatePercent(); if (chan.mUseLimit) { fChannelPercent = Mathf.Clamp(lchan.Percent, chan.mSpinmin, chan.mSpinmax); } else { fChannelPercent = Mathf.Clamp(lchan.Percent, 0.0f, 100.0f); } if (fChannelPercent != 0.0f || (fChannelPercent == 0.0f && chan.fChannelPercent != 0.0f)) { chan.fChannelPercent = fChannelPercent; if (chan.mTargetCache != null && chan.mTargetCache.Count > 0 && chan.mActiveOverride) //&& fChannelPercent != 0.0f ) { morphed = true; if (chan.mUseLimit) //|| glUseLimit ) { } // New bit if (firstchan) { firstchan = false; // Save a int array of morphedpoints and use that, then only dealing with changed info for (int pointnum = 0; pointnum < source.morphedVerts.Length; pointnum++) { // this will change when we remove points int p = source.morphedVerts[pointnum]; dif[p] = source.oPoints[p]; //morphedVerts[pointnum]]; } } // end new if (chan.mTargetCache.Count == 1) { // Save a int array of morphedpoints and use that, then only dealing with changed info for (int pointnum = 0; pointnum < source.morphedVerts.Length; pointnum++) { int p = source.morphedVerts[pointnum]; delt = chan.mDeltas[p]; //morphedVerts[pointnum]]; //delt = chan.mDeltas[pointnum]; //morphedVerts[pointnum]]; dif[p].x += delt.x * fChannelPercent; dif[p].y += delt.y * fChannelPercent; dif[p].z += delt.z * fChannelPercent; } } else { int totaltargs = chan.mTargetCache.Count; // + 1; // + 1; float fProgression = fChannelPercent; //Mathf.Clamp(fChannelPercent, 0.0f, 100.0f); int segment = 1; while (segment <= totaltargs && fProgression >= chan.GetTargetPercent(segment - 2)) { segment++; } if (segment > totaltargs) { segment = totaltargs; } p4 = source.oPoints; if (segment == 1) { p1 = source.oPoints; p2 = chan.mTargetCache[0].points; // mpoints p3 = chan.mTargetCache[1].points; } else { if (segment == totaltargs) { int targnum = totaltargs - 1; for (int j = 2; j >= 0; j--) { targnum--; if (targnum == -2) { SetVerts(j, source.oPoints); } else { SetVerts(j, chan.mTargetCache[targnum + 1].points); } } } else { int targnum = segment; for (int j = 3; j >= 0; j--) { targnum--; if (targnum == -2) { SetVerts(j, source.oPoints); } else { SetVerts(j, chan.mTargetCache[targnum + 1].points); } } } } float targetpercent1 = chan.GetTargetPercent(segment - 3); float targetpercent2 = chan.GetTargetPercent(segment - 2); float top = fProgression - targetpercent1; float bottom = targetpercent2 - targetpercent1; float u = top / bottom; for (int pointnum = 0; pointnum < source.morphedVerts.Length; pointnum++) { int p = source.morphedVerts[pointnum]; Vector3 vert = source.oPoints[p]; //pointnum]; float length; Vector3 progession; endpoint[0] = p1[p]; endpoint[1] = p2[p]; endpoint[2] = p3[p]; endpoint[3] = p4[p]; if (segment == 1) { splinepoint[0] = endpoint[0]; splinepoint[3] = endpoint[1]; temppoint[1] = endpoint[2] - endpoint[0]; temppoint[0] = endpoint[1] - endpoint[0]; length = temppoint[1].sqrMagnitude; if (length == 0.0f) { splinepoint[1] = endpoint[0]; splinepoint[2] = endpoint[1]; } else { splinepoint[2] = endpoint[1] - (Vector3.Dot(temppoint[0], temppoint[1]) * chan.mCurvature / length) * temppoint[1]; splinepoint[1] = endpoint[0] + chan.mCurvature * (splinepoint[2] - endpoint[0]); } } else { if (segment == totaltargs) { splinepoint[0] = endpoint[1]; splinepoint[3] = endpoint[2]; temppoint[1] = endpoint[2] - endpoint[0]; temppoint[0] = endpoint[1] - endpoint[2]; length = temppoint[1].sqrMagnitude; if (length == 0.0f) { splinepoint[1] = endpoint[0]; splinepoint[2] = endpoint[1]; } else { splinepoint[1] = endpoint[1] - (Vector3.Dot(temppoint[1], temppoint[0]) * chan.mCurvature / length) * temppoint[1]; splinepoint[2] = endpoint[2] + chan.mCurvature * (splinepoint[1] - endpoint[2]); } } else { temppoint[1] = endpoint[2] - endpoint[0]; temppoint[0] = endpoint[1] - endpoint[0]; length = temppoint[1].sqrMagnitude; splinepoint[0] = endpoint[1]; splinepoint[3] = endpoint[2]; if (length == 0.0f) { splinepoint[1] = endpoint[0]; } else { splinepoint[1] = endpoint[1] + (Vector3.Dot(temppoint[0], temppoint[1]) * chan.mCurvature / length) * temppoint[1]; } temppoint[1] = endpoint[3] - endpoint[1]; temppoint[0] = endpoint[2] - endpoint[1]; length = temppoint[1].sqrMagnitude; if (length == 0.0f) { splinepoint[2] = endpoint[1]; } else { splinepoint[2] = endpoint[2] - (Vector3.Dot(temppoint[0], temppoint[1]) * chan.mCurvature / length) * temppoint[1]; } } } MegaUtils.Bez3D(out progession, ref splinepoint, u); dif[p].x += progession.x - vert.x; dif[p].y += progession.y - vert.y; dif[p].z += progession.z - vert.z; } } } } } if (morphed) { for (int i = 0; i < source.morphMappingFrom.Length; i++) { sverts[source.morphMappingTo[i]] = dif[source.morphMappingFrom[i]]; } for (int i = 0; i < source.nonMorphMappingFrom.Length; i++) { sverts[source.nonMorphMappingTo[i]] = source.oPoints[source.nonMorphMappingFrom[i]]; } } else { for (int i = 0; i < verts.Length; i++) { sverts[i] = verts[i]; } } }
// oPoints whould be verts public override void Modify(MegaModifiers mc) { if (source == null) { return; } if (source.nonMorphedVerts != null && source.nonMorphedVerts.Length > 1) { ModifyCompressed(mc); return; } framenum++; mc.ChangeSourceVerts(); float fChannelPercent; Vector3 delt; // cycle through channels, searching for ones to use bool firstchan = true; bool morphed = false; float min = 0.0f; float max = 100.0f; if (UseLimit) { min = Min; max = Max; } for (int i = 0; i < source.chanBank.Count; i++) { MegaMorphChan chan = source.chanBank[i]; // This needs to be local chan list chan.UpdatePercent(); if (UseLimit) { fChannelPercent = Mathf.Clamp(chan.Percent, min, max); //chan.mSpinmin, chan.mSpinmax); } else { if (chan.mUseLimit) { fChannelPercent = Mathf.Clamp(chan.Percent, chan.mSpinmin, chan.mSpinmax); } else { fChannelPercent = Mathf.Clamp(chan.Percent, 0.0f, 100.0f); } } if (fChannelPercent != 0.0f || (fChannelPercent == 0.0f && chan.fChannelPercent != 0.0f)) { chan.fChannelPercent = fChannelPercent; if (chan.mTargetCache != null && chan.mTargetCache.Count > 0 && chan.mActiveOverride) //&& fChannelPercent != 0.0f ) { morphed = true; if (chan.mUseLimit) //|| glUseLimit ) { } if (firstchan) { firstchan = false; for (int pointnum = 0; pointnum < source.oPoints.Length; pointnum++) { dif[pointnum] = source.oPoints[pointnum]; } } if (chan.mTargetCache.Count == 1) { for (int pointnum = 0; pointnum < source.oPoints.Length; pointnum++) { delt = chan.mDeltas[pointnum]; dif[pointnum].x += delt.x * fChannelPercent; dif[pointnum].y += delt.y * fChannelPercent; dif[pointnum].z += delt.z * fChannelPercent; } } else { int totaltargs = chan.mTargetCache.Count; // + 1; // + 1; float fProgression = fChannelPercent; //Mathf.Clamp(fChannelPercent, 0.0f, 100.0f); int segment = 1; while (segment <= totaltargs && fProgression >= chan.GetTargetPercent(segment - 2)) { segment++; } if (segment > totaltargs) { segment = totaltargs; } p4 = source.oPoints; if (segment == 1) { p1 = source.oPoints; p2 = chan.mTargetCache[0].points; // mpoints p3 = chan.mTargetCache[1].points; } else { if (segment == totaltargs) { int targnum = totaltargs - 1; for (int j = 2; j >= 0; j--) { targnum--; if (targnum == -2) { SetVerts(j, source.oPoints); } else { SetVerts(j, chan.mTargetCache[targnum + 1].points); } } } else { int targnum = segment; for (int j = 3; j >= 0; j--) { targnum--; if (targnum == -2) { SetVerts(j, source.oPoints); } else { SetVerts(j, chan.mTargetCache[targnum + 1].points); } } } } float targetpercent1 = chan.GetTargetPercent(segment - 3); float targetpercent2 = chan.GetTargetPercent(segment - 2); float top = fProgression - targetpercent1; float bottom = targetpercent2 - targetpercent1; float u = top / bottom; { for (int pointnum = 0; pointnum < source.oPoints.Length; pointnum++) { Vector3 vert = source.oPoints[pointnum]; float length; Vector3 progession; endpoint[0] = p1[pointnum]; endpoint[1] = p2[pointnum]; endpoint[2] = p3[pointnum]; endpoint[3] = p4[pointnum]; if (segment == 1) { splinepoint[0] = endpoint[0]; splinepoint[3] = endpoint[1]; temppoint[1] = endpoint[2] - endpoint[0]; temppoint[0] = endpoint[1] - endpoint[0]; length = temppoint[1].sqrMagnitude; if (length == 0.0f) { splinepoint[1] = endpoint[0]; splinepoint[2] = endpoint[1]; } else { splinepoint[2] = endpoint[1] - (Vector3.Dot(temppoint[0], temppoint[1]) * chan.mCurvature / length) * temppoint[1]; splinepoint[1] = endpoint[0] + chan.mCurvature * (splinepoint[2] - endpoint[0]); } } else { if (segment == totaltargs) { splinepoint[0] = endpoint[1]; splinepoint[3] = endpoint[2]; temppoint[1] = endpoint[2] - endpoint[0]; temppoint[0] = endpoint[1] - endpoint[2]; length = temppoint[1].sqrMagnitude; if (length == 0.0f) { splinepoint[1] = endpoint[0]; splinepoint[2] = endpoint[1]; } else { splinepoint[1] = endpoint[1] - (Vector3.Dot(temppoint[1], temppoint[0]) * chan.mCurvature / length) * temppoint[1]; splinepoint[2] = endpoint[2] + chan.mCurvature * (splinepoint[1] - endpoint[2]); } } else { temppoint[1] = endpoint[2] - endpoint[0]; temppoint[0] = endpoint[1] - endpoint[0]; length = temppoint[1].sqrMagnitude; splinepoint[0] = endpoint[1]; splinepoint[3] = endpoint[2]; if (length == 0.0f) { splinepoint[1] = endpoint[0]; } else { splinepoint[1] = endpoint[1] + (Vector3.Dot(temppoint[0], temppoint[1]) * chan.mCurvature / length) * temppoint[1]; } temppoint[1] = endpoint[3] - endpoint[1]; temppoint[0] = endpoint[2] - endpoint[1]; length = temppoint[1].sqrMagnitude; if (length == 0.0f) { splinepoint[2] = endpoint[1]; } else { splinepoint[2] = endpoint[2] - (Vector3.Dot(temppoint[0], temppoint[1]) * chan.mCurvature / length) * temppoint[1]; } } } MegaUtils.Bez3D(out progession, ref splinepoint, u); dif[pointnum].x += progession.x - vert.x; //delt; dif[pointnum].y += progession.y - vert.y; //delt; dif[pointnum].z += progession.z - vert.z; //delt; } } } } } } if (morphed) { for (int i = 0; i < source.mapping.Length; i++) { sverts[i] = dif[source.mapping[i]]; } } else { for (int i = 0; i < verts.Length; i++) { sverts[i] = verts[i]; } } }
void BuildPolyShape(MegaLoftSection lsection, int steps, Vector3 off, float width) { int curve = lsection.curve; int k = -1; Matrix4x4 tm1 = Matrix4x4.identity; Vector2 uv = Vector2.zero; float start = crossStart; float len = crossEnd; if (lsection.uselen) { start = lsection.start; len = lsection.length; } MegaShape shape = lsection.shape; verts.Clear(); uvs.Clear(); norms.Clear(); MegaMatrix.Translate(ref tm1, pivot); Vector3 rot = crossRot + lsection.rot; MegaMatrix.Rotate(ref tm1, new Vector3(Mathf.Deg2Rad * rot.x, Mathf.Deg2Rad * rot.y, Mathf.Deg2Rad * rot.z)); svert = verts.Count; float alpha = start; if (shape.splines[curve].closed) { alpha = Mathf.Repeat(alpha, 1.0f); } uv.y = start; float dist = 0.0f; Vector3 last = Vector3.zero; for (int i = 0; i <= steps; i++) { alpha = start + (((float)i / (float)steps) * len); if (shape.splines[curve].closed) { alpha = Mathf.Repeat(alpha, 1.0f); } Vector3 pos = tm1.MultiplyPoint3x4(shape.splines[curve].InterpCurve3D(alpha, shape.normalizedInterp, ref k) + off + lsection.offset); pos.x *= lsection.scale.x; pos.y *= lsection.scale.y; pos.z *= lsection.scale.z; verts.Add(pos); if (physuv) { if (i > 0) { dist += Vector3.Distance(pos, last); } last = pos; uv.x = (dist / width); } else { uv.x = alpha; } uvs.Add(uv); } evert = verts.Count - 1; uv.y = start + len; lsection.crossverts = verts.ToArray(); lsection.crossuvs = uvs.ToArray(); lsection.crosssize = MegaUtils.Extents(lsection.crossverts, out lsection.crossmin, out lsection.crossmax); }
void BuildObjectLinks(MegaShape path) { float len = path.splines[curve].length; if (LinkSize < 0.1f) { LinkSize = 0.1f; } // Assume z axis for now float linklen = (linkOff1.y - linkOff.y) * linkScale.x * LinkSize; int lc = (int)(len / linklen); if (lc != linkcount) { InitLinkObjects(path); } Quaternion linkrot1 = Quaternion.identity; linkrot1 = Quaternion.Euler(rotate); float spos = start * 0.01f; Vector3 poff = linkPivot * linkScale.x * LinkSize; float lastalpha = spos; Vector3 pos = Vector3.zero; Matrix4x4 pmat = Matrix4x4.TRS(poff, linkrot1, Vector3.one); Vector3 lrot = Vector3.zero; Quaternion frot = Quaternion.identity; #if UNITY_5_4 || UNITY_5_5 || UNITY_5_6 || UNITY_2017 Random.InitState(seed); #else Random.seed = seed; #endif for (int i = 0; i < linkcount; i++) { float alpha = ((float)(i + 1) / (float)linkcount) + spos; Quaternion lq = GetLinkQuat(alpha, lastalpha, out pos, path); lastalpha = alpha; Quaternion lr = Quaternion.Euler(lrot); frot = lq * linkrot1 * lr; if (linkobjs[i]) { Matrix4x4 lmat = Matrix4x4.TRS(pos, lq, Vector3.one) * pmat; linkobjs[i].localPosition = lmat.GetColumn(3); linkobjs[i].localRotation = frot; linkobjs[i].localScale = linkScale * LinkSize; } if (randRot) { float r = Random.Range(0.0f, 1.0f); lrot = (int)(r * (int)(360.0f / MegaUtils.LargestValue1(linkRot))) * linkRot; } else { lrot += linkRot; } } }
// We have spring builder in my max destruction soft body so use that public void VoxelMesh(GameObject obj, float size, bool fill) { if (obj == null) { return; } voxobj = obj; Mesh mesh = MegaUtils.GetMesh(voxobj); basemesh = mesh; int[] tris = mesh.triangles; Vector3[] verts = mesh.vertices; origin = mesh.bounds.min; Vector3 meshsize = mesh.bounds.size; xs = Mathf.CeilToInt(meshsize.x / size); if (xs == 0) { xs = 1; } ys = Mathf.CeilToInt(meshsize.y / size); if (ys == 0) { ys = 1; } zs = Mathf.CeilToInt(meshsize.z / size); if (zs == 0) { zs = 1; } // So first check all tris against each cell // first get bounds of tri // is it quicker to do each tri, get bounds and check those // or do each cell and check all tris, (can early out on this) // alloc array to hold cell data cells = new int[xs, ys, zs]; // Mmmm looks like a lot quicker otherway for 30 verts on a 4x4x4 we loop 4x4x4x30= 1920 // if we do tri and get bounds then a lot quicker could be as low as 30 or 2x2x2x30 = 240 Bounds box = new Bounds(); int gxs, gxe; int gys, gye; int gzs, gze; Vector3 min = Vector3.zero; Vector3 half = new Vector3(size * 0.5f, size * 0.5f, size * 0.5f); cellsize = new Vector3(size, size, size); for (int i = 0; i < tris.Length; i += 3) { // do bounds on tri GetTriBounds(verts, tris, i, mesh.bounds, out gxs, out gxe, out gys, out gye, out gzs, out gze); for (int z = gzs; z < gze; z++) { min.z = mesh.bounds.min.z + (z * size); for (int y = gys; y < gye; y++) { min.y = mesh.bounds.min.y + (y * size); for (int x = gxs; x < gxe; x++) { min.x = mesh.bounds.min.x + (x * size); box.SetMinMax(min, min + cellsize); // build box for cell if (AABB_Triangle_Intersection.TriangleBoxOverlap(verts[tris[i]], verts[tris[i + 1]], verts[tris[i + 2]], box)) { cells[x, y, z] = 1; } } } } } // We should have a shell now, so now to fill line by line // can do for each axis and combine result (bit per axis) any bit means voxel, this will fill holes // Fill it, should do for each axis // could be a bug here here cant assume pen changes state in next block, so for pen down we get // the first block but we need end of contigous block if (fill) { for (int z = 0; z < zs; z++) { for (int y = 0; y < ys; y++) { int pd = 0; while (true) //x != -1 ) { // get pen down pd = GetXCell(cells, pd, y, z, xs); if (pd == -1) { break; } // get pen up int pu = GetXCell(cells, pd + 1, y, z, xs); if (pu == -1) { break; } for (int x = pd + 1; x < pu; x++) { cells[x, y, z] = 2; } pd = pu + 1; // pd is } } } } }
void InitLinkObjects(MegaRope rope) { rebuild = false; float len = rope.RopeLength; // Assume z axis for now float linklen = (linkOff1.y - linkOff.y) * linkScale.x * LinkSize; //Length - linkOff.y linkcount = (int)(len / linklen); for (int i = linkcount; i < rope.gameObject.transform.childCount; i++) { GameObject go = rope.gameObject.transform.GetChild(i).gameObject; if (Application.isEditor && !Application.isPlaying) { GameObject.DestroyImmediate(go); } else { GameObject.Destroy(go); } #if UNITY_4_0 || UNITY_4_1 || UNITY_4_3 || UNITY_4_7 || UNITY_4_6 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2 || UNITY_5 //go.SetActive(false); #else //go.SetActiveRecursively(false); #endif } if (LinkObj1 == null || LinkObj1.Count == 0) { return; } linkobjs = new Transform[linkcount]; int oi = 0; if (linkcount > rope.gameObject.transform.childCount) { for (int i = 0; i < rope.gameObject.transform.childCount; i++) { GameObject go = rope.gameObject.transform.GetChild(i).gameObject; #if UNITY_4_0 || UNITY_4_1 || UNITY_4_3 || UNITY_4_7 || UNITY_4_6 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2 || UNITY_5 //go.SetActive(true); #else //go.SetActiveRecursively(true); #endif linkobjs[i] = go.transform; } int index = rope.gameObject.transform.childCount; for (int i = index; i < linkcount; i++) { if (RandomOrder) { oi = (int)(Random.value * (float)LinkObj1.Count); } else { oi = (oi + 1) % LinkObj1.Count; } GameObject obj = LinkObj1[oi]; if (obj) { GameObject go = new GameObject(); //(GameObject)Instantiate(LinkObj); //linkMesh); //, Vectp, Quaternion.identity); go.name = "Link"; MeshRenderer mr = (MeshRenderer)obj.GetComponent <MeshRenderer>(); Mesh ms = MegaUtils.GetSharedMesh(obj); //go.transform.localPosition = p; MeshRenderer mr1 = (MeshRenderer)go.AddComponent <MeshRenderer>(); MeshFilter mf1 = (MeshFilter)go.AddComponent <MeshFilter>(); mf1.sharedMesh = ms; mr1.sharedMaterial = mr.sharedMaterial; go.transform.parent = rope.gameObject.transform; linkobjs[i] = go.transform; } } } else { for (int i = 0; i < linkcount; i++) { GameObject go = rope.gameObject.transform.GetChild(i).gameObject; #if UNITY_3_5 go.SetActiveRecursively(true); #else go.SetActive(true); #endif linkobjs[i] = go.transform; } } #if UNITY_5_4 || UNITY_5_5 || UNITY_5_6 || UNITY_2017 || UNITY_2018 || UNITY_2019 Random.InitState(seed); #else Random.seed = seed; #endif oi = 0; for (int i = 0; i < linkcount; i++) { if (RandomOrder) { oi = (int)(Random.value * (float)LinkObj1.Count); } else { oi = (oi + 1) % LinkObj1.Count; } GameObject obj = LinkObj1[oi]; if (obj) { GameObject go = rope.gameObject.transform.GetChild(i).gameObject; MeshRenderer mr = (MeshRenderer)obj.GetComponent <MeshRenderer>(); Mesh ms = MegaUtils.GetSharedMesh(obj); //go.transform.localPosition = p; MeshRenderer mr1 = (MeshRenderer)go.GetComponent <MeshRenderer>(); MeshFilter mf1 = (MeshFilter)go.GetComponent <MeshFilter>(); mf1.sharedMesh = ms; mr1.sharedMaterial = mr.sharedMaterial; } } }
// We should know the mapping // remove morph pass tolerance instead bool TryMapping1(MegaTargetMesh tm, MegaMorphOMatic morph) { MegaModifiers mod = morph.GetComponent <MegaModifiers>(); if (mod == null) { EditorUtility.DisplayDialog("Missing ModifyObject!", "No ModifyObject script found on the object", "OK"); return(false); } // Get extents for mod verts and for imported meshes, if not the same then scale Vector3 min1, max1; Vector3 min2, max2; Vector3 ex1 = MegaUtils.Extents(mod.verts, out min1, out max1); Vector3 ex2 = MegaUtils.Extents(tm.verts, out min2, out max2); // need min max on all axis so we can produce an offset to add float d1 = ex1.x; float d2 = ex2.x; float scl = d1 / d2; //d2 / d1; bool flipyz = false; bool negx = false; // So try to match first vert using autoscale and no flip bool mapped = DoMapping(mod, morph, tm, scl, flipyz, negx); if (!mapped) { flipyz = true; mapped = DoMapping(mod, morph, tm, scl, flipyz, negx); if (!mapped) //DoMapping(mod, morph, tm, mapping, scl, flipyz, negx) ) { flipyz = false; negx = true; mapped = DoMapping(mod, morph, tm, scl, flipyz, negx); if (!mapped) { flipyz = true; mapped = DoMapping(mod, morph, tm, scl, flipyz, negx); } } } if (mapped) { morph.importScale = scl; morph.flipyz = flipyz; morph.negx = negx; // if mapping was ok set opoints morph.oPoints = tm.verts.ToArray(); for (int i = 0; i < morph.oPoints.Length; i++) { Vector3 p = morph.oPoints[i]; if (negx) { p.x = -p.x; } if (flipyz) { float z = p.z; p.z = p.y; p.y = z; } morph.oPoints[i] = p * morph.importScale; } morph.mapping = new MegaMomVertMap[morph.oPoints.Length]; for (int i = 0; i < morph.oPoints.Length; i++) { //int[] indices = morph.FindVerts(morph.oPoints[i], mod); int[] indices = FindVerts(morph.oPoints[i], mod); morph.mapping[i] = new MegaMomVertMap(); morph.mapping[i].indices = indices; //morph.FindVerts(morph.oPoints[i], mod); } return(true); } return(false); }
// we only need to build the savevertex and uvs if mesh def changes, else we can keep the uvs void BuildMesh() { if (!mesh) { mesh = GetComponent <MeshFilter>().sharedMesh; if (mesh == null) { updatemesh = true; return; } } if (hosespline.knots.Count == 0) { hosespline.AddKnot(Vector3.zero, Vector3.zero, Vector3.zero); hosespline.AddKnot(Vector3.zero, Vector3.zero, Vector3.zero); } FixHoseFillet(); bool createfree = freecreate; if ((!createfree) && ((!custnode) || (!custnode2))) { createfree = true; } if (custnode && custnode2) { createfree = false; } Matrix4x4 mat1, mat2; float Lf = 0.0f; Tlocal = Matrix4x4.identity; Vector3 startvec, endvec, startpoint, endpoint, endy; starty = Vector3.zero; roty = Vector3.zero; yangle = 0.0f; Vector3 RV = Vector3.zero; if (createfree) { Lf = noreflength; } else { RV = up; //new Vector3(xtmp, ytmp, ztmp); mat1 = custnode.transform.localToWorldMatrix; mat2 = custnode2.transform.localToWorldMatrix; Matrix4x4 mato1 = Matrix4x4.identity; Matrix4x4 mato2 = Matrix4x4.identity; mato1 = Matrix4x4.TRS(offset, Quaternion.Euler(rotate), scale); mato1 = mato1.inverse; mato2 = Matrix4x4.TRS(offset1, Quaternion.Euler(rotate1), scale1); mato2 = mato2.inverse; S = transform.localToWorldMatrix; Matrix4x4 mat1NT, mat2NT; mat1NT = mat1; mat2NT = mat2; MegaMatrix.NoTrans(ref mat1NT); MegaMatrix.NoTrans(ref mat2NT); Vector3 P1 = mat1.MultiplyPoint(mato1.GetColumn(3)); Vector3 P2 = mat2.MultiplyPoint(mato2.GetColumn(3)); startvec = mat1NT.MultiplyPoint(mato1.GetColumn(2)); endvec = mat2NT.MultiplyPoint(mato2.GetColumn(2)); starty = mat1NT.MultiplyPoint(mato1.GetColumn(1)); endy = mat2NT.MultiplyPoint(mato2.GetColumn(1)); Matrix4x4 SI = S.inverse; Vector3 P0 = SI.MultiplyPoint(P1); Matrix4x4 T1 = mat1; MegaMatrix.NoTrans(ref T1); Vector3 RVw = T1.MultiplyPoint(RV); Lf = (P2 - P1).magnitude; Vector3 Zw; if (Lf < 0.01f) { Zw = P1.normalized; } else { Zw = (P2 - P1).normalized; } Vector3 Xw = Vector3.Cross(RVw, Zw).normalized; Vector3 Yw = Vector3.Cross(Zw, Xw).normalized; MegaMatrix.NoTrans(ref SI); Vector3 Xs = SI.MultiplyPoint(Xw); Vector3 Ys = SI.MultiplyPoint(Yw); Vector3 Zs = SI.MultiplyPoint(Zw); Tlocal.SetColumn(0, Xs); Tlocal.SetColumn(1, Ys); Tlocal.SetColumn(2, Zs); MegaMatrix.SetTrans(ref Tlocal, P0); // move z-axes of end transforms into local frame Matrix4x4 TlocalInvNT = Tlocal; MegaMatrix.NoTrans(ref TlocalInvNT); TlocalInvNT = TlocalInvNT.inverse; float tenstop = tension1; // * 0.01f; float tensbot = tension2; // * 0.01f; startvec = tensbot * (TlocalInvNT.MultiplyPoint(startvec)); endvec = tenstop * (TlocalInvNT.MultiplyPoint(endvec)); starty = TlocalInvNT.MultiplyPoint(starty); endy = TlocalInvNT.MultiplyPoint(endy); yangle = Mathf.Acos(Vector3.Dot(starty, endy)); if (yangle > EPSILON) { roty = Vector3.Cross(starty, endy).normalized; } else { roty = Vector3.zero; } startpoint = Vector3.zero; endpoint = new Vector3(0.0f, 0.0f, Lf); hosespline.knots[0].p = startpoint; hosespline.knots[0].invec = startpoint - startvec; hosespline.knots[0].outvec = startpoint + startvec; hosespline.knots[1].p = endpoint; hosespline.knots[1].invec = endpoint + endvec; hosespline.knots[1].outvec = endpoint - endvec; hosespline.CalcLength(); //10); } MegaHoseType wtype = wiretype; int Segs = segments; if (Segs < 3) { Segs = 3; } if (rebuildcross) { rebuildcross = false; int nfillets = 0; int nsides = 0; if (wtype == MegaHoseType.Round) { NvertsPerRing = rndsides; if (NvertsPerRing < 3) { NvertsPerRing = 3; } } else { if (wtype == MegaHoseType.Rectangle) { nfillets = rectfilletsides; if (nfillets < 0) { nfillets = 0; } if (smooth == MegaHoseSmooth.SMOOTHNONE) { NvertsPerRing = (nfillets > 0 ? 8 + 4 * (nfillets - 1) : 8); } else { NvertsPerRing = (nfillets > 0 ? 8 + 4 * (nfillets - 1) : 4); //4); } } else { nfillets = dsecfilletsides; if (nfillets < 0) { nfillets = 0; } nsides = dsecrndsides; if (nsides < 2) { nsides = 2; } int nsm1 = nsides - 1; NvertsPerRing = (nfillets > 0 ? 6 + nsm1 + 2 * (nfillets - 1): 4 + nsm1); } } NvertsPerRing++; int NfacesPerEnd, NfacesPerRing, Nfaces = 0; //MegaHoseSmooth SMOOTH = smooth; Nverts = (Segs + 1) * (NvertsPerRing + 1); // + 2; if (capends) { Nverts += 2; } NfacesPerEnd = NvertsPerRing; NfacesPerRing = 6 * NvertsPerRing; Nfaces = Segs * NfacesPerRing; // + 2 * NfacesPerEnd; if (capends) { Nfaces += 2 * NfacesPerEnd; } if (SaveVertex == null || SaveVertex.Length != NvertsPerRing) { SaveVertex = new Vector3[NvertsPerRing]; SaveUV = new Vector2[NvertsPerRing]; } if (calcnormals) { if (SaveNormals == null || SaveNormals.Length != NvertsPerRing) { SaveNormals = new Vector3[NvertsPerRing]; } } MakeSaveVertex(NvertsPerRing, nfillets, nsides, wtype); if (verts == null || verts.Length != Nverts) { verts = new Vector3[Nverts]; uvs = new Vector2[Nverts]; faces = new int[Nfaces * 3]; } if (calcnormals && (normals == null || normals.Length != Nverts)) { normals = new Vector3[Nverts]; } } if (Nverts == 0) { return; } bool mapmenow = mapmemapme; int thisvert = 0; int last = Nverts - 1; int last2 = last - 1; int lastvpr = NvertsPerRing; // - 1; int maxseg = Segs + 1; float flexhere; float dadjust; float flexlen; float flex1 = flexstart; float flex2 = flexstop; int flexn = flexcycles; float flexd = flexdiameter; Vector3 ThisPosition; Vector3 ThisXAxis, ThisYAxis, ThisZAxis; Vector2 uv = Vector2.zero; Matrix4x4 RingTM = Matrix4x4.identity; Matrix4x4 invRingTM = Matrix4x4.identity; for (int i = 0; i < maxseg; i++) { float incr = (float)i / (float)Segs; if (createfree) { ThisPosition = new Vector3(0.0f, 0.0f, Lf * incr); ThisXAxis = new Vector3(1.0f, 0.0f, 0.0f); ThisYAxis = new Vector3(0.0f, 1.0f, 0.0f); ThisZAxis = new Vector3(0.0f, 0.0f, 1.0f); } else { int k = 0; ThisPosition = hosespline.InterpCurve3D(incr, true, ref k); ThisZAxis = (hosespline.InterpCurve3D(incr + 0.001f, true, ref k) - ThisPosition).normalized; ThisYAxis = starty; if (yangle > EPSILON) { RotateOnePoint(ref ThisYAxis, Vector3.zero, roty, incr * yangle); } ThisXAxis = Vector3.Cross(ThisYAxis, ThisZAxis).normalized; ThisYAxis = Vector3.Cross(ThisZAxis, ThisXAxis); } RingTM.SetColumn(0, ThisXAxis); RingTM.SetColumn(1, ThisYAxis); RingTM.SetColumn(2, ThisZAxis); MegaMatrix.SetTrans(ref RingTM, ThisPosition); if (!createfree) { RingTM = Tlocal * RingTM; } if (calcnormals) { invRingTM = RingTM; MegaMatrix.NoTrans(ref invRingTM); //invRingTM = invRingTM.inverse.transpose; } if ((incr > flex1) && (incr < flex2) && flexon) { flexlen = flex2 - flex1; if (flexlen < 0.01f) { flexlen = 0.01f; } flexhere = (incr - flex1) / flexlen; float ang = (float)flexn * flexhere * (Mathf.PI * 2.0f) + PIover2; dadjust = 1.0f + flexd * (1.0f - Mathf.Sin(ang)); //(float)flexn * flexhere * (Mathf.PI * 2.0f) + PIover2)); } else { dadjust = 0.0f; } if (usebulgecurve) { if (dadjust == 0.0f) { dadjust = 1.0f + (bulge.Evaluate(incr + bulgeoffset) * bulgeamount); } else { dadjust += bulge.Evaluate(incr + bulgeoffset) * bulgeamount; } } uv.x = 0.999999f * incr * uvscale.x; for (int j = 0; j < NvertsPerRing; j++) { int jj = j; // % NvertsPerRing; if (mapmenow) { uv.y = SaveUV[jj].y; uvs[thisvert] = uv; //new Vector2(0.999999f * incr * uvscale.x, SaveUV[jj].y); } if (dadjust != 0.0f) { verts[thisvert] = RingTM.MultiplyPoint(dadjust * SaveVertex[jj]); } else { verts[thisvert] = RingTM.MultiplyPoint(SaveVertex[jj]); } if (calcnormals) { normals[thisvert] = invRingTM.MultiplyPoint(SaveNormals[jj]).normalized; //.MultiplyPoint(-SaveNormals[jj]); } //if ( j == 0 ) //{ // Debug.Log("norm " + normals[thisvert].ToString("0.000") + " save " + SaveNormals[jj].ToString("0.000")); //} thisvert++; } if (mapmenow) { //uvs[Nverts + i] = new Vector2(0.999999f * incr, 0.999f); } if (capends) { if (i == 0) { verts[last2] = (createfree ? ThisPosition : Tlocal.MultiplyPoint(ThisPosition)); if (mapmenow) { uvs[last2] = Vector3.zero; } } else { if (i == Segs) { verts[last] = createfree ? ThisPosition : Tlocal.MultiplyPoint(ThisPosition); if (mapmenow) { uvs[last] = Vector3.zero; } } } } } // Now, set up the faces int thisface = 0, v1, v2, v3, v4; v3 = last2; if (capends) { for (int i = 0; i < NvertsPerRing - 1; i++) { v1 = i; v2 = (i < lastvpr ? v1 + 1 : v1 - lastvpr); //v5 = (i < lastvpr ? v2 : Nverts); faces[thisface++] = v2; faces[thisface++] = v1; faces[thisface++] = v3; } } int ringnum = NvertsPerRing; // + 1; for (int i = 0; i < Segs; i++) { for (int j = 0; j < NvertsPerRing - 1; j++) { v1 = i * ringnum + j; v2 = v1 + 1; //(j < lastvpr? v1 + 1 : v1 - lastvpr); v4 = v1 + ringnum; v3 = v2 + ringnum; faces[thisface++] = v1; faces[thisface++] = v2; faces[thisface++] = v3; faces[thisface++] = v1; faces[thisface++] = v3; faces[thisface++] = v4; } } int basevert = Segs * ringnum; //NvertsPerRing; v3 = Nverts - 1; if (capends) { for (int i = 0; i < NvertsPerRing - 1; i++) { v1 = i + basevert; v2 = (i < lastvpr? v1 + 1 : v1 - lastvpr); //v5 = (i < lastvpr? v2 : Nverts + Segs); faces[thisface++] = v1; faces[thisface++] = v2; faces[thisface++] = v3; } } mesh.Clear(); mesh.subMeshCount = 1; mesh.vertices = verts; mesh.uv = uvs; mesh.triangles = faces; if (calcnormals) { mesh.normals = normals; } else { mesh.RecalculateNormals(); } mesh.RecalculateBounds(); #if UNITY_5_5 || UNITY_5_6 || UNITY_2017 #else if (optimize) { ; } #endif if (calctangents) { MegaUtils.BuildTangents(mesh); } if (recalcCollider) { if (meshCol == null) { meshCol = GetComponent <MeshCollider>(); } if (meshCol != null) { meshCol.sharedMesh = null; meshCol.sharedMesh = mesh; //bool con = meshCol.convex; //meshCol.convex = con; } } }
// Best if we calc the normals to avoid issues at join public void BuildMesh() { if (makeMesh) { //makeMesh = false; float sdist = stepdist * 0.1f; if (splines[0].length / sdist > 1500.0f) { sdist = splines[0].length / 1500.0f; } Vector3 size = Vector3.zero; verts.Clear(); uvs.Clear(); tris.Clear(); tris1.Clear(); tris2.Clear(); tris = MegaTriangulator.Triangulate(this, splines[0], sdist, ref verts, ref uvs, ref tris, Pivot, ref size); if (shapemesh == null) { MeshFilter mf = gameObject.GetComponent <MeshFilter>(); if (mf == null) { mf = gameObject.AddComponent <MeshFilter>(); } mf.sharedMesh = new Mesh(); MeshRenderer mr = gameObject.GetComponent <MeshRenderer>(); if (mr == null) { mr = gameObject.AddComponent <MeshRenderer>(); } Material[] mats = new Material[3]; mr.sharedMaterials = mats; shapemesh = mf.sharedMesh; //Utils.GetMesh(gameObject); } int vcount = verts.Count; int tcount = tris.Count; if (Height < 0.0f) { Height = 0.0f; } float h = Height; //Mathf.Abs(Height); Matrix4x4 tm1 = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(UVRotate.x, UVRotate.y, 0.0f), new Vector3(UVScale.x, 1.0f, UVScale.y)); //Vector3 size = shapemesh.bounds.size; if (GenUV) { uvs.Clear(); // need to stop triangulator doing uvs Vector2 uv = Vector2.zero; Vector3 uv1 = Vector3.zero; for (int i = 0; i < verts.Count; i++) { uv1.x = (verts[i].x); // * UVScale.x) + UVOffset.x; // * UVScale.x; uv1.z = (verts[i].z); // * UVScale.y) + UVOffset.y; // * UVScale.y; if (!PhysUV) { uv1.x /= size.x; uv1.z /= size.z; } uv1 = tm1.MultiplyPoint3x4(uv1); uv.x = uv1.x + UVOffset.x; uv.y = uv1.z + UVOffset.y; uvs.Add(uv); } } if (DoubleSided && h != 0.0f) { //vcount = verts.Count; for (int i = 0; i < vcount; i++) { Vector3 p = verts[i]; if (UseHeightCurve) { float alpha = MegaTriangulator.m_points[i].z / splines[0].length; p.y -= h * heightCrv.Evaluate(alpha + heightOff); } else { p.y -= h; } verts.Add(p); //verts[i]); uvs.Add(uvs[i]); } //tcount = tris.Count; for (int i = tcount - 1; i >= 0; i--) { tris1.Add(tris[i] + vcount); } } #if true // Do edge if (h != 0.0f) { int vc = verts.Count; Vector3 ep = Vector3.zero; Vector2 euv = Vector2.zero; tm1 = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(UVRotate1.x, UVRotate1.y, 0.0f), new Vector3(UVScale1.x, 1.0f, UVScale1.y)); // Top loop for (int i = 0; i < MegaTriangulator.m_points.Count; i++) { ep = verts[i]; //ep.x = MegaTriangulator.m_points[i].x; //ep.y = 0.0f; //ep.z = MegaTriangulator.m_points[i].y; verts.Add(ep); //euv.x = (MegaTriangulator.m_points[i].z / splines[0].length) * 4.0f; //euv.x = (MegaTriangulator.m_points[i].z * UVScale1.x) + UVOffset1.x; // / splines[0].length) * 4.0f; //euv.y = UVOffset1.y; //0.0f; ep.x = (MegaTriangulator.m_points[i].z); // * UVScale1.x) + UVOffset1.x; // / splines[0].length) * 4.0f; if (!PhysUV) { ep.x /= size.x; } ep.y = 0.0f; ep.z = 0.0f; //UVOffset1.y; //0.0f; ep = tm1.MultiplyPoint3x4(ep); euv.x = ep.x + UVOffset1.x; euv.y = ep.z + UVOffset1.y; uvs.Add(euv); } // Add first point again ep = verts[0]; //ep.y -= h * heightCrv.Evaluate(0.0f); verts.Add(ep); //euv.x = 1.0f * 4.0f; //MegaTriangulator.m_points[0].z / splines[0].length; euv.x = (splines[0].length * UVScale1.x) + UVOffset1.x; //1.0f * 4.0f; //MegaTriangulator.m_points[0].z / splines[0].length; if (!PhysUV) { euv.x /= size.x; } euv.y = 0.0f + UVOffset1.y; uvs.Add(euv); // Bot loop float hd = 1.0f; for (int i = 0; i < MegaTriangulator.m_points.Count; i++) { float alpha = MegaTriangulator.m_points[i].z / splines[0].length; ep = verts[i]; if (UseHeightCurve) { hd = heightCrv.Evaluate(alpha + heightOff); } ep.y -= h * hd; //heightCrv.Evaluate(alpha); verts.Add(ep); ep.x = (MegaTriangulator.m_points[i].z); // * UVScale1.x) + UVOffset1.x; // / splines[0].length) * 4.0f; ep.z = ep.y; //0.0f; //UVOffset1.y; //0.0f; ep.y = 0.0f; if (!PhysUV) { ep.x /= size.x; ep.z /= (h * hd); } ep = tm1.MultiplyPoint3x4(ep); euv.x = ep.x + UVOffset1.x; euv.y = ep.z + UVOffset1.y; //euv.x = (MegaTriangulator.m_points[i].z / splines[0].length) * 4.0f; //euv.x = (MegaTriangulator.m_points[i].z * UVScale1.x) + UVOffset1.x; //euv.y = ((h * hd) * UVScale1.y) + UVOffset1.y; //1.0f; uvs.Add(euv); } // Add first point again ep = verts[0]; if (UseHeightCurve) { hd = heightCrv.Evaluate(0.0f + heightOff); } ep.y -= h * hd; //heightCrv.Evaluate(0.0f); verts.Add(ep); ep.x = (MegaTriangulator.m_points[0].z); // * UVScale1.x) + UVOffset1.x; // / splines[0].length) * 4.0f; ep.z = ep.y; //0.0f; //UVOffset1.y; //0.0f; ep.y = 0.0f; if (!PhysUV) { ep.x /= size.x; ep.z /= (h * hd); } ep = tm1.MultiplyPoint3x4(ep); euv.x = ep.x + UVOffset1.x; euv.y = ep.z + UVOffset1.y; //euv.x = (MegaTriangulator.m_points[i].z / splines[0].length) * 4.0f; //euv.x = (MegaTriangulator.m_points[i].z * UVScale1.x) + UVOffset1.x; //euv.y = ((h * hd) * UVScale1.y) + UVOffset1.y; //1.0f; uvs.Add(euv); //euv.x = 1.0f; //MegaTriangulator.m_points[0].z / splines[0].length; //euv.x = (splines[0].length * UVScale1.x) + UVOffset1.x; //MegaTriangulator.m_points[0].z / splines[0].length; //euv.y = (h * hd * UVScale1.y) + UVOffset1.y; //1.0f; //if ( !PhysUV ) //{ // euv.x /= size.x; // euv.y /= //} //uvs.Add(euv); // Faces int ecount = MegaTriangulator.m_points.Count + 1; int ip = 0; for (ip = 0; ip < MegaTriangulator.m_points.Count; ip++) { tris2.Add(ip + vc); tris2.Add(ip + vc + ecount); tris2.Add(ip + vc + 1); tris2.Add(ip + vc + 1); tris2.Add(ip + vc + ecount); tris2.Add(ip + vc + ecount + 1); } #if false tris.Add(ip + vc); tris.Add(ip + vc + ecount); tris.Add(vc); tris.Add(vc); tris.Add(ip + vc + ecount); tris.Add(vc + ecount); #endif } #endif shapemesh.Clear(); shapemesh.vertices = verts.ToArray(); shapemesh.uv = uvs.ToArray(); shapemesh.subMeshCount = 3; shapemesh.SetTriangles(tris.ToArray(), 0); shapemesh.SetTriangles(tris1.ToArray(), 1); shapemesh.SetTriangles(tris2.ToArray(), 2); //shapemesh.triangles = tris.ToArray(); shapemesh.RecalculateNormals(); shapemesh.RecalculateBounds(); if (CalcTangents) { MegaUtils.BuildTangents(shapemesh); } //if ( mesh != null ) //{ //BuildMesh(mesh); //MegaModifyObject mo = GetComponent<MegaModifyObject>(); //if ( mo != null ) //{ // mo.MeshUpdated(); //} //} } }