Home Game Development unity – Learn how to add N vertices to a face of a Dice

unity – Learn how to add N vertices to a face of a Dice

0
unity – Learn how to add N vertices to a face of a Dice

[ad_1]

I need to add N vertices to a Dice, I am including simply 4 like a sq. to be easy, however finally I need to have the ability to add as many as doable and possibly in a round form, however I can not make the triangle half proper. The code to breed my steps are bellow.

1- After Raycasting right into a Dice, to get the mesh I do that (do not forget so as to add a MeshCollider to the Dice):

if (Enter.GetMouseButtonUp(0))
{
    RaycastHit hit;

    if (!Physics.Raycast(digicam.ScreenPointToRay(Enter.mousePosition), out hit))
    {
        return;
    }

    var meshCollider = hit.collider as MeshCollider;
    var mesh = meshCollider.sharedMesh;

    Mesh meshChanged = Instantiate(mesh);
    meshChanged.identify = "Modified Dice";
}

2- Them I get the vertices, normals, triangles and uv:

Vector3[] vertices = mesh.vertices.ToArray();
Vector3[] normals = mesh.normals.ToArray().ToArray();
int[] triangles = mesh.triangles.ToArray();
Vector2[] uvs = mesh.uv.ToArray();

3- I get the place of the hit.level, within the Dice native house and the traditional of the triangle that the hit.level hitted.

Vector3 hitPositionOnMesh = hit.rework.InverseTransformPoint(hit.level);
Vector3 hittedTriangleNormal = CalculateNormalOfTriangle(normals, triangles, hit.triangleIndex);

3.a- CalculateNormalOfTriangle Operate

personal static Vector3 CalculateNormalOfTriangle(Vector3[] normals, int[] triangles, int triangleIndex)
{
    var n0 = normals[triangles[triangleIndex * 3 + 0]];
    var n1 = normals[triangles[triangleIndex * 3 + 1]];
    var n2 = normals[triangles[triangleIndex * 3 + 2]];
    return ((n0 + n1 + n2) / 3);
}

4- To be easy, I add solely 4 vertices and add a distance (0.1f) to them.

Vector3 hitPositionOnMesh1 = new Vector3();
Vector3 hitPositionOnMesh2 = new Vector3();
Vector3 hitPositionOnMesh3 = new Vector3();
Vector3 hitPositionOnMesh4 = new Vector3();

Vector3 hitP = hit.rework.place;

Vector3 point1 = new Vector3(hitP.x + 0.1f, hitP.y + 0.1f, hitP.z);
Vector3 point2 = new Vector3(hitP.x - 0.1f, hitP.y + 0.1f, hitP.z);
Vector3 point3 = new Vector3(hitP.x - 0.1f, hitP.y - 0.1f, hitP.z);
Vector3 point4 = new Vector3(hitP.x + 0.1f, hitP.y - 0.1f, hitP.z);

hitPositionOnMesh1 = point1;
hitPositionOnMesh2 = point2;
hitPositionOnMesh3 = point3;
hitPositionOnMesh4 = point4;

5- I create the skin vertices and inside vertices lists.

Listing<Vector3> OutsideVerticesOfFace = new Listing<Vector3>();
for (int i = 0; i < normals.Size; i++)
{
    if (normals[i] == hittedTriangleNormal)
    {
        OutsideVerticesOfFace.Add(vertices[i]);
    }
}

Listing<Vector3> InsideVerticesOfFace = new Listing<Vector3>
{
    hitPositionOnMesh1,
    hitPositionOnMesh2,
    hitPositionOnMesh3,
    hitPositionOnMesh4
};

6- I take away the triangles from face of the Dice that the hit.level hitted.

triangles = RemoveFace(hittedTriangleNormal, triangles, normals);

6.a- RemoveFace Operate

personal static int[] RemoveFace(Vector3 normalToCompare, int[] triangles, Vector3[] normals)
{
    Listing<int> trianglesToRemove = new Listing<int>();

    for (int i = 0; i < triangles.Size; i += 3)
    {
        var p0 = normals[triangles[i + 0]];
        var p1 = normals[triangles[i + 1]];
        var p2 = normals[triangles[i + 2]];

        if (((p0 + p1 + p2) / 3) == normalToCompare)
        {
            trianglesToRemove.Add(triangles[i + 0]);
            trianglesToRemove.Add(triangles[i + 1]);
            trianglesToRemove.Add(triangles[i + 2]);
        }
    }

    int[] _triangles = RemoveTriangles(triangles, trianglesToRemove.ToArray());
    return _triangles;
}

6.b- RemoveTriangles Operate

personal static int[] RemoveTriangles(int[] triangles, int[] trianglesToRemove)
{
    Listing<int> tmpT = new Listing<int>(triangles.ToList());
    for (int i = 0; i < triangles.ToList().Depend; i += 3)
    {
        bool v1 = (triangles.ToList()[i + 0] == trianglesToRemove[0]);
        bool v2 = (triangles.ToList()[i + 1] == trianglesToRemove[1]);
        bool v3 = (triangles.ToList()[i + 2] == trianglesToRemove[2]);

        if ((v1 && v2 && v3))
        {
            tmpT.RemoveRange(i, trianglesToRemove.Size);
        }
    }
    triangles = tmpT.ToArray();

    return triangles;
}

7- I create a brand new listing of vertices and alter the normals and uvs lists.

Listing<Vector3> newVertices = new Listing<Vector3>(vertices.ToArray());
newVertices.AddRange(InsideVerticesOfFace);

normals = (new Listing<Vector3>(normals)
{ 
    hittedTriangleNormal,
    hittedTriangleNormal,
    hittedTriangleNormal,
    hittedTriangleNormal,
}).ToArray();

uvs = (new Listing<Vector2>(uvs)
{            
    new Vector2(hitPositionOnMesh1.x, hitPositionOnMesh1.z),
    new Vector2(hitPositionOnMesh2.x, hitPositionOnMesh2.z),
    new Vector2(hitPositionOnMesh3.x, hitPositionOnMesh3.z),
    new Vector2(hitPositionOnMesh4.x, hitPositionOnMesh4.z),
}).ToArray();

8- Now, that is the issue, that is the place it is not working, the a part of making the brand new triangles.

Listing<int> outsideTriangles;
outsideTriangles = MakeOuterTriangles(_vertices.ToArray(), InsideVerticesOfFace, OutsideVerticesOfFace);
Listing<int> newTriangles = triangles.ToList();
newTriangles.AddRange(outsideTriangles.ToArray());
triangles = newTriangles.ToArray();
meshChanged.vertices = newVertices.ToArray();
meshChanged.normals = normals;
meshChanged.triangles = triangles;
meshChanged.uv = uvs;

8.a- I attempted Trigonometry, utilizing cosine legislation, c = sqrt(a^2 + b^2 – 2ab * cos(C)). c being the space from the within vertice (0) with second outdoors vertice (1), a being the space from the skin vertice (0) with outdoors vertice (1) and b being the space from the within vertice (0) with outdoors vertice (0). C being the angle between inside vertice (0) and out of doors vertice (0), like within the picture:
image

Firstly I rearrange the outsideVertice listing, as a result of for some motive, Unity create vertices in a bizarre “0, 1, 3, 2”. So I rearrange it in order that the final merchandise becames the third, and the second merchandise becames the final, “0, 1, 2 (final), 3 (third)” and I additionally add the primary merchandise within the final place, in order that it make a loop.

Secondly I sorted the newOutside listing by distance the from the insideVertice listing, in order that outsideVertices[0] is all the time the correct merchandise.

*I attempted to alter the variables parameters to see if I misplaced one thing, it did not work in any respect, in one of the best case, the triangles of the face the place deleted or there the place no triangles in any respect.

public static Listing<int> MakeOuterTriangles(Vector3[] vertices, Listing<Vector3> insideVertices, Listing<Vector3> outsideVertices)
{
    Listing<int> newTriangles = new Listing<int>();
    Listing<Vector3> newOutside = new Listing<Vector3>() { outsideVertices[0], outsideVertices[1], outsideVertices[outsideVertices.Count - 1], outsideVertices[2], outsideVertices[0]};
    for (int i = 0; i < insideVertices.Depend - 1; i++)
    {
        newOutside = newOutside.OrderBy(x => Vector3.Distance(x, insideVertices[i])).ToList();
        float side_a = Vector3.Distance(outsideVertices[0], outsideVertices[1]);
        float side_b = Vector3.Distance(outsideVertices[0], insideVertices[i]);
        float angle = Vector3.Angle(outsideVertices[0], insideVertices[i]);
        float side_c = Mathf.Sqrt(Mathf.Pow(side_a, 2) + Mathf.Pow(side_b, 2) - 2 * (side_a * side_b) * Mathf.Cos(angle));
        if (side_c == Vector3.Distance(outsideVertices[1], insideVertices[i]))
        {
                newTriangles.Add(vertices.ToList().IndexOf(insideVertices[i]));
        }
    }
    return newTriangles;
}

8.b- I attempted to adapt this code link1, from the video link2, the code is bellow, however I assume that the variety of vertice within the inside needs to be the identical as the skin. So it did not work additionally.

public static Listing<int> MakeOuterTriangles(Vector3[] vertices, Listing<Vector3> insideVertices, Listing<Vector3> outsideVertices)
{
    Listing<int> newTriangles = new Listing<int>();
    int sides = insideVertices.Depend + outsideVertices.Depend/2;
    for(int i = 0; i<sides;i++)
    {
        int outerIndex = i;
        int innerIndex = i+sides;

        newTriangles.Add(outerIndex);
        newTriangles.Add(innerIndex);
        newTriangles.Add((i+1)%sides);

        newTriangles.Add(outerIndex);
        newTriangles.Add(sides+((sides+i-1)%sides));
        newTriangles.Add(outerIndex+sides);
    }
    return newTriangles;
}

8.c- This my new strive (code bellow), I get this picture outcome:

image2

Listing<int> outsideTriangles = new Listing<int>();
for (int i = 0; i < OutsideVerticesOfFace.Depend - 1; i++)
{           
    if (i == OutsideVerticesOfFace.Depend - 1)
    {
        InsideVerticesOfFace = InsideVerticesOfFace.OrderBy(x => Vector3.Distance(x, OutsideVerticesOfFace[i])).ToList();
        
        float distanceV = Mathf.Abs(Vector3.Distance(OutsideVerticesOfFace[0], OutsideVerticesOfFace[i]));

        if (distance == 1)
        {
                outsideTriangles.Add(newVertices.ToList().IndexOf(OutsideVerticesOfFace[0]));
                outsideTriangles.Add(newVertices.ToList().IndexOf(InsideVerticesOfFace[0]));
                outsideTriangles.Add(newVertices.ToList().IndexOf(OutsideVerticesOfFace[i]));
        }
    }
    else if (i != OutsideVerticesOfFace.Depend - 1)
    {
        InsideVerticesOfFace = InsideVerticesOfFace.OrderBy(x => Vector3.Distance(x, OutsideVerticesOfFace[i])).ToList();

       
        float distanceV = Mathf.Abs(Vector3.Distance(OutsideVerticesOfFace[i + 1], OutsideVerticesOfFace[i]));

        if (distanceV == 1)
        {
                outsideTriangles.Add(newVertices.ToList().IndexOf(OutsideVerticesOfFace[0]));
                outsideTriangles.Add(newVertices.ToList().IndexOf(InsideVerticesOfFace[0]));
                outsideTriangles.Add(newVertices.ToList().IndexOf(OutsideVerticesOfFace[i]));            
        }
    }           
}

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here