private void UpdateMesh(Mesh mesh, ClippingPlaneSource clippingPlaneSource, Transform sourceTransform, Mesh sourceMesh) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); if (!targetObjects.TryGetValue(clippingPlaneSource, out GameObject newGameObject)) { newGameObject = new GameObject(); newGameObject.AddComponent <MeshFilter>(); newGameObject.AddComponent <MeshRenderer>(); targetObjects.Add(clippingPlaneSource, newGameObject); } var newFilter = newGameObject.GetComponent <MeshFilter>(); var renderer = newGameObject.GetComponent <MeshRenderer>(); newGameObject.transform.position = sourceTransform.position; newGameObject.transform.rotation = sourceTransform.rotation; newFilter.mesh = mesh; MeshClipper.Clip(sourceMesh, sourcesToClip[clippingPlaneSource], sourceTransform.worldToLocalMatrix, mesh) .ThenOnMainThread((x) => UpdateMesh(x, clippingPlaneSource, sourceTransform, sourceMesh)); stopwatch.Stop(); Debug.Log($"Update Time: {stopwatch.ElapsedMilliseconds}"); }
private void Start() { if (clippingPlaneSourceObject != null) { var sources = clippingPlaneSourceObject.GetComponentsInChildren <ClippingPlaneSource>(); foreach (var source in sources) { MeshFilter meshFilter = source?.GetComponent <MeshFilter>(); Mesh mesh = meshFilter?.mesh; if (mesh != null) { sourcesToClip.Add(source, new ClippingPlaneSet(source.transform.localToWorldMatrix, mesh)); } } } foreach (var source in sourcesToClip) { foreach (var filter in targetObjectToBeClipped.GetComponentsInChildren <MeshFilter>()) { MeshClipper.Clip(filter.mesh, source.Value, filter.transform.worldToLocalMatrix) .ThenOnMainThread(x => UpdateMesh(x, source.Key, filter.transform, filter.mesh)); } } }
private IEnumerator createFile(Bounds UnityBounds, List <Layer> layerList) { freezeLayers(layerList, true); Debug.Log(layerList.Count); Vector3RD bottomLeftRD = CoordConvert.UnitytoRD(UnityBounds.min); Vector3RD topRightRD = CoordConvert.UnitytoRD(UnityBounds.max); boundingbox = new MeshClipper.RDBoundingBox(bottomLeftRD.x, bottomLeftRD.y, topRightRD.x, topRightRD.y); DxfFile file = new DxfFile(); file.SetupDXF(); yield return(null); MeshClipper meshClipper = new MeshClipper(); loadingScreen.ShowMessage("dxf-bestand genereren..."); loadingScreen.ProgressBar.Percentage(0f); int layercounter = 0; foreach (var layer in layerList) { List <GameObject> gameObjectsToClip = getTilesInLayer(layer, bottomLeftRD, topRightRD); if (gameObjectsToClip.Count == 0) { continue; } foreach (var gameObject in gameObjectsToClip) { meshClipper.SetGameObject(gameObject); for (int submeshID = 0; submeshID < gameObject.GetComponent <MeshFilter>().sharedMesh.subMeshCount; submeshID++) { meshClipper.clipSubMesh(boundingbox, submeshID); string layerName = gameObject.GetComponent <MeshRenderer>().sharedMaterials[submeshID].name.Replace(" (Instance)", ""); file.AddLayer(meshClipper.clippedVerticesRD, layerName, getColor(gameObject.GetComponent <MeshRenderer>().sharedMaterials[submeshID])); yield return(null); } } loadingScreen.ProgressBar.Percentage(50 * layercounter / layerList.Count); layercounter++; } freezeLayers(layerList, false); file.Save(); loadingScreen.Hide(); Debug.Log("file saved"); }
// Start is called before the first frame update void Start() { //preparation setup the boundingBox MeshClipper.RDBoundingBox bbox = new MeshClipper.RDBoundingBox(minX, minY, maxX, maxY); // start the new meshclipper MeshClipper clipper = new MeshClipper(); // tell the clipper which gameObject the mesh is attached to. // at this point the clipper will translate the unityCoordinates (mesh + gameObject.origin) to RD-coordinates clipper.SetGameObject(go); // clip the desired submesh clipper.ClipSubMesh(bbox, 0); // after clipping the clipper contains a list of Vector3RD coordinates. each set of 3 coordinates describes a triangle (orientation is counterClockwise CreateMesh(clipper.clippedVerticesRD); // it is now possible to clip the next submesh ( if there is one) without having to translate coordinates }
private void AppendWMOandM2Info(ref List <Vector3> verts, ref List <int> indices) { if (verts == null) { verts = new List <Vector3>(); } if (indices == null) { indices = new List <int>(); } var clipper = new MeshClipper(Bounds); List <Vector3> newVertices; List <int> newIndices; // Add WMO & M2 vertices int offset; foreach (var wmo in WMOs) { clipper.ClipMesh(wmo.WmoVertices, wmo.WmoIndices, out newVertices, out newIndices); offset = verts.Count; if (newVertices.Count > 0 && newIndices.Count > 0) { verts.AddRange(newVertices); foreach (var index in newIndices) { indices.Add(offset + index); } } clipper.ClipMesh(wmo.WmoM2Vertices, wmo.WmoM2Indices, out newVertices, out newIndices); offset = verts.Count; if (newVertices.Count <= 0 || newIndices.Count <= 0) { continue; } verts.AddRange(newVertices); foreach (var index in newIndices) { indices.Add(offset + index); } } foreach (var m2 in M2s) { clipper.ClipMesh(m2.Vertices, m2.Indices, out newVertices, out newIndices); offset = verts.Count; if (newVertices.Count == 0 || newIndices.Count == 0) { continue; } verts.AddRange(newVertices); foreach (var index in newIndices) { indices.Add(offset + index); } } }
private void AppendWMOandM2Info(ref List<Vector3> verts, ref List<int> indices) { if (verts == null) verts = new List<Vector3>(); if (indices == null) indices = new List<int>(); var clipper = new MeshClipper(Bounds); List<Vector3> newVertices; List<int> newIndices; // Add WMO & M2 vertices int offset; foreach (var wmo in WMOs) { clipper.ClipMesh(wmo.WmoVertices, wmo.WmoIndices, out newVertices, out newIndices); offset = verts.Count; if (newVertices.Count > 0 && newIndices.Count > 0) { verts.AddRange(newVertices); foreach (var index in newIndices) { indices.Add(offset + index); } } clipper.ClipMesh(wmo.WmoM2Vertices, wmo.WmoM2Indices, out newVertices, out newIndices); offset = verts.Count; if (newVertices.Count <= 0 || newIndices.Count <= 0) continue; verts.AddRange(newVertices); foreach (var index in newIndices) { indices.Add(offset + index); } } foreach (var m2 in M2s) { clipper.ClipMesh(m2.Vertices, m2.Indices, out newVertices, out newIndices); offset = verts.Count; if (newVertices.Count == 0 || newIndices.Count == 0) continue; verts.AddRange(newVertices); foreach (var index in newIndices) { indices.Add(offset + index); } } }
private IEnumerator CreateFile(Bounds UnityBounds, List <Layer> layerList) { FreezeLayers(layerList, true); Debug.Log(layerList.Count); Vector3RD bottomLeftRD = CoordConvert.UnitytoRD(UnityBounds.min); Vector3RD topRightRD = CoordConvert.UnitytoRD(UnityBounds.max); boundingbox = new MeshClipper.RDBoundingBox(bottomLeftRD.x, bottomLeftRD.y, topRightRD.x, topRightRD.y); DxfFile dxfFile = new DxfFile(); dxfFile.SetupDXF(); yield return(null); MeshClipper meshClipper = new MeshClipper(); loadingScreen.ShowMessage("DXF-bestand genereren..."); loadingScreen.ProgressBar.SetMessage(""); loadingScreen.ProgressBar.Percentage(0.1f); yield return(new WaitForEndOfFrame()); int layercounter = 0; foreach (var layer in layerList) { layercounter++; loadingScreen.ProgressBar.Percentage((float)layercounter / ((float)layerList.Count + 1)); loadingScreen.ProgressBar.SetMessage("Laag '" + layer.name + "' wordt omgezet..."); yield return(new WaitForEndOfFrame()); List <GameObject> gameObjectsToClip = GetTilesInLayer(layer, bottomLeftRD, topRightRD); if (gameObjectsToClip.Count == 0) { continue; } foreach (var gameObject in gameObjectsToClip) { meshClipper.SetGameObject(gameObject); for (int submeshID = 0; submeshID < gameObject.GetComponent <MeshFilter>().sharedMesh.subMeshCount; submeshID++) { string layerName = gameObject.GetComponent <MeshRenderer>().sharedMaterials[submeshID].name.Replace(" (Instance)", ""); loadingScreen.ProgressBar.SetMessage("Laag '" + layer.name + "' object " + layerName + " wordt uitgesneden..."); yield return(new WaitForEndOfFrame()); meshClipper.ClipSubMesh(boundingbox, submeshID); dxfFile.AddLayer(meshClipper.clippedVerticesRD, layerName, GetColor(gameObject.GetComponent <MeshRenderer>().sharedMaterials[submeshID])); yield return(new WaitForEndOfFrame()); } yield return(new WaitForEndOfFrame()); } yield return(new WaitForEndOfFrame()); } loadingScreen.ProgressBar.Percentage((float)layerList.Count / ((float)layerList.Count + 1)); loadingScreen.ProgressBar.SetMessage("Het AutoCAD DXF (.dxf) bestand wordt afgerond..."); yield return(new WaitForEndOfFrame()); dxfFile.Save(); loadingScreen.Hide(); FreezeLayers(layerList, false); Debug.Log("file saved"); }