Hello,
I need help trying to find if two triangles are touching each other. If vertices are repeated twice in the mesh triangles setup it means they are connected. I then try to calculate the direction between each vertices of the mesh to remove the Vector3 direction which is the sides of the two triangles that are touching each other… which leaves me the edges of the polygon quad… the thing is it doesn’t seem to be working… anyone got any ideas?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class example : MonoBehaviour {
int[] triangles;
Vector3[] vertices;
Mesh mesh;
List<Vector3> vertsTrigConnections = new List<Vector3>();
List<Vector3> vertTrigDirToNotCalculate = new List<Vector3>();
List<Vector3> totalDirections = new List<Vector3>();
void Start ()
{
mesh = GetComponent<MeshFilter>().mesh;
vertices = mesh.vertices;
triangles = mesh.triangles;
for (int i = 0; i < triangles.Length; i += 3)
{
Vector3 p0 = vertices[triangles[i + 0]];
Vector3 p1 = vertices[triangles[i + 1]];
Vector3 p2 = vertices[triangles[i + 2]];
vertsTrigConnections.Add(p0);
vertsTrigConnections.Add(p1);
vertsTrigConnections.Add(p2);
Vector3 direction0 = p1 - p0;
Vector3 direction1 = p2 - p1;
Vector3 direction2 = p0 - p2;
for (int h = 0; h < vertsTrigConnections.Count - 1; h++)
{
for (int f = h + 1; f < vertsTrigConnections.Count; f++)
{
if (vertsTrigConnections[h] == vertsTrigConnections[f])
{
vertTrigDirToNotCalculate.Add(vertsTrigConnections[h]);
}
}
}
for (int h = 0; h < vertTrigDirToNotCalculate.Count - 1; h++)
{
for (int f = h + 1; f < vertTrigDirToNotCalculate.Count; f++)
{
if (direction0 == vertTrigDirToNotCalculate[h] - vertTrigDirToNotCalculate[f] ||
direction0 == vertTrigDirToNotCalculate[f] - vertTrigDirToNotCalculate[h])
{
totalDirections.Remove(direction0);
}
if (direction1 == vertTrigDirToNotCalculate[h] - vertTrigDirToNotCalculate[f] ||
direction1 == vertTrigDirToNotCalculate[f] - vertTrigDirToNotCalculate[h])
{
totalDirections.Remove(direction1);
}
if (direction2 == vertTrigDirToNotCalculate[h] - vertTrigDirToNotCalculate[f] ||
direction2 == vertTrigDirToNotCalculate[f] - vertTrigDirToNotCalculate[h])
{
totalDirections.Remove(direction2);
}
}
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class example : MonoBehaviour
{
private static Plane blade;
List<Vector3> vertsTrigConnections = new List<Vector3>();
List<Vector3> vertTrigDirToNotCalculate0 = new List<Vector3>();
List<Vector3> vertTrigDirToNotCalculate1 = new List<Vector3>();
List<Vector3> totalDirections = new List<Vector3>();
List<Vector3> rightSideVertTemp = new List<Vector3>();
List<Vector3> leftSideVertTemp = new List<Vector3>();
List<Vector3> newVertices = new List<Vector3>();
public GameObject sphere0;
public GameObject sphere1;
int[] triangles;
Vector3[] vertices;
Mesh mesh;
void Update()
{
float mouseVert = Input.GetAxisRaw("Mouse Y");
float mouseHorz = Input.GetAxisRaw("Mouse X");
transform.Rotate(new Vector3(-mouseVert, mouseHorz, 0));
Debug.DrawRay(new Vector3(transform.position.x, transform.position.y, transform.position.z), transform.forward, Color.red, 0.1f);
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, 20f))
{
mesh = hit.transform.GetComponent<MeshFilter>().mesh;
GameObject hitObject = hit.transform.gameObject;
blade = new Plane(hit.transform.InverseTransformDirection(transform.right), hit.transform.InverseTransformPoint(transform.position));
for (int i = 0; i < hit.transform.GetComponent<MeshFilter>().mesh.vertices.Length; i++)
{
if (!blade.GetSide(hit.transform.GetComponent<MeshFilter>().mesh.vertices))
{
leftSideVertTemp.Add(hit.transform.GetComponent<MeshFilter>().mesh.vertices);
}
if (blade.GetSide(hit.transform.GetComponent<MeshFilter>().mesh.vertices))
{
rightSideVertTemp.Add(hit.transform.GetComponent<MeshFilter>().mesh.vertices);
}
}
vertices = hitObject.transform.GetComponent<MeshFilter>().mesh.vertices;
triangles = hitObject.transform.GetComponent<MeshFilter>().mesh.triangles;
for (int i = 0; i < triangles.Length; i += 3)
{
Vector3 dir0;
Vector3 dir1;
Vector3 dir2;
Vector3 worldPt0 = vertices[triangles[i + 0]];
Vector3 worldPt1 = vertices[triangles[i + 1]];
Vector3 worldPt2 = vertices[triangles[i + 2]];
vertsTrigConnections.Add(worldPt0);
vertsTrigConnections.Add(worldPt1);
vertsTrigConnections.Add(worldPt2);
dir0 = worldPt1 - worldPt0;
dir1 = worldPt2 - worldPt1;
dir2 = worldPt0 - worldPt2;
if (!totalDirections.Contains(dir0))
{
totalDirections.Add(dir0);
}
if (!totalDirections.Contains(dir1))
{
totalDirections.Add(dir1);
}
if (!totalDirections.Contains(dir2))
{
totalDirections.Add(dir2);
}
for (int h = 0; h < vertsTrigConnections.Count - 1; h++)
{
for (int f = h + 1; f < vertsTrigConnections.Count; f++)
{
if (vertsTrigConnections[h] == vertsTrigConnections[f])
{
vertTrigDirToNotCalculate0.Add(vertsTrigConnections[h]);
vertTrigDirToNotCalculate1.Add(vertsTrigConnections[h]);
}
}
}
for (int h = 0; h < vertTrigDirToNotCalculate0.Count; h++)
{
for (int f = 0; f < vertTrigDirToNotCalculate1.Count; f++)
{
if (dir0 == vertTrigDirToNotCalculate0[h] - vertTrigDirToNotCalculate0[f])
{
totalDirections.Remove(dir0);
}
if (dir0 == vertTrigDirToNotCalculate0[f] - vertTrigDirToNotCalculate0[h])
{
totalDirections.Remove(dir0);
}
if (dir1 == vertTrigDirToNotCalculate0[h] - vertTrigDirToNotCalculate0[f])
{
totalDirections.Remove(dir1);
}
if (dir1 == vertTrigDirToNotCalculate0[f] - vertTrigDirToNotCalculate0[h])
{
totalDirections.Remove(dir1);
}
if (dir2 == vertTrigDirToNotCalculate0[h] - vertTrigDirToNotCalculate0[f])
{
totalDirections.Remove(dir2);
}
if (dir2 == vertTrigDirToNotCalculate0[f] - vertTrigDirToNotCalculate0[h])
{
totalDirections.Remove(dir2);
}
}
}
var enumerator999 = rightSideVertTemp.GetEnumerator();
while (enumerator999.MoveNext())
{
var currentVert = enumerator999.Current;
var enumerator888 = leftSideVertTemp.GetEnumerator();
while (enumerator888.MoveNext())
{
var currentVert1 = enumerator888.Current;
Vector3 dir = (currentVert1 - currentVert);
Vector3 diri = (currentVert - currentVert1);
for (var g = 0; g < vertices.Length; g++)
{
for (int j = 0; j < totalDirections.Count; j++)
{
if ( totalDirections[j] == dir)
{
Ray vecRay0 = new Ray(worldPt0, totalDirections[j]);
float dist0;
if (blade.Raycast(vecRay0, out dist0))
{
if (!newVertices.Contains(vecRay0.GetPoint(dist0)))
{
newVertices.Add(vecRay0.GetPoint(dist0) + hit.transform.position);
}
if (worldPt0 == vertices[g])
{
vertices[g] = vecRay0.GetPoint(dist0);
}
}
}
}
for (int j = 0; j < totalDirections.Count; j++)
{
if (totalDirections[j] == dir)
{
Ray vecRay1 = new Ray(worldPt1, totalDirections[j]);
float dist1;
if (blade.Raycast(vecRay1, out dist1))
{
if (!newVertices.Contains(vecRay1.GetPoint(dist1)))
{
newVertices.Add(vecRay1.GetPoint(dist1) + hit.transform.position);
}
if (worldPt1 == vertices[g])
{
vertices[g] = vecRay1.GetPoint(dist1);
}
}
}
}
for (int j = 0; j < totalDirections.Count; j++)
{
if (totalDirections[j] == dir)
{
Ray vecRay2 = new Ray(worldPt2, totalDirections[j]);
float dist2;
if (blade.Raycast(vecRay2, out dist2))
{
if (!newVertices.Contains(vecRay2.GetPoint(dist2)))
{
newVertices.Add(vecRay2.GetPoint(dist2) + hit.transform.position);
}
if (worldPt2 == vertices[g])
{
vertices[g] = vecRay2.GetPoint(dist2);
}
}
}
}
}
}
}
}
}
}
mesh.vertices = vertices;
mesh.RecalculateBounds();
rightSideVertTemp.Clear();
leftSideVertTemp.Clear();
newVertices.Clear();
vertsTrigConnections.Clear();
vertTrigDirToNotCalculate0.Clear();
vertTrigDirToNotCalculate1.Clear();
totalDirections.Clear();
}
}
The code is based on two examples of mesh cutting. One from MGear found here http://1darray.com/blog/2011/08/09/fake-mesh-slicer-v3-0/ and one from YouTube found here https://www.youtube.com/watch?v=xgoUmrhXyYE. My goal was to find a way of cutting objects and I couldn’t figure out how to put back triangles where they shouldve been. although the code below is far from beeing perfect it moves Vertices at the point of the cut made with a Struct plane. It doesn’t work from all angles and it lags as hell. Some vertices were not following the cut i was making but by removing 3 if’s statements: worldPt0 == currentVert && worldPt1 == currentVert && worldPt2 == currentVert It works ok but with lag. Sorry for the long code. If anyone got any ideas on how to make this better and got the time to check it feel free to give an input. Sorry for the long code. Thank you, Nine.