Search Unity

Infill Solution [Solved]

Discussion in 'Scripting' started by turndapage, Jul 4, 2015.

  1. turndapage

    turndapage

    Joined:
    Jun 25, 2015
    Posts:
    12
    Hi!

    I'm writing a function to create an object where a sphere and another object collide. The script is attached to the sphere and copy is the new object. I first duplicate the object, then go through every triangle of the new sphere to check if it is inside the collision area of the object it is colliding with. If it isn't, it will delete the triangle. so far, this works great.

    I am having trouble now though because I am left with empty space. I know Turbo Slicer has an infill solution, but I don't know if that would work with my model since it will not always be cut in a straight line.

    Here is my code:

    Code (csharp):
    1.  
    2. void CreateCopy()
    3.     {
    4.         DestroyCopy();
    5.  
    6.         GameObject collidingObject = collidingObjects[collidingObjects.Count - 1];
    7.  
    8.         // Create new game object of the sphere
    9.         copy = Instantiate(transform.gameObject);
    10.         copy.name = "Copy";
    11.  
    12.         // Make it a normal sphere
    13.         Destroy(copy.GetComponent<CopySphere>());
    14.         Destroy(copy.GetComponent<Collider>());
    15.  
    16.         // SetMaterial to match copied object
    17.         copyMeshRenderer = copy.GetComponent<MeshRenderer>();
    18.         copyMeshRenderer.material = collidingObject.GetComponent<MeshRenderer>().material;
    19.  
    20.         Mesh mesh = copy.GetComponent<MeshFilter>().mesh;
    21.         int[] triangles = mesh.triangles;
    22.         Vector3[] vertices = mesh.vertices;
    23.         Vector2[] uv = mesh.uv;
    24.         Vector3[] normals = mesh.normals;
    25.         List<Vector3> vertList = new List<Vector3>();
    26.         List<Vector2> uvList = new List<Vector2>();
    27.         List<Vector3> normalsList = new List<Vector3>();
    28.         List<int> trianglesList = new List<int>();
    29.  
    30.  
    31.         int i = 0;
    32.         while (i < vertices.Length)
    33.         {
    34.             vertList.Add(vertices[i]);
    35.             uvList.Add(uv[i]);
    36.             normalsList.Add(normals[i]);
    37.             i++;
    38.         }
    39.         for (int triCount = 0; triCount < triangles.Length; triCount += 3)
    40.         {
    41.             Bounds bounds = collidingObject.GetComponent<Collider>().bounds;
    42.  
    43.             if (bounds.Contains(copy.transform.TransformPoint(vertices[triangles[triCount]])) && bounds.Contains(copy.transform.TransformPoint(vertices[triangles[triCount + 1]])) && bounds.Contains(copy.transform.TransformPoint(vertices[triangles[triCount + 2]])))
    44.             {
    45.                 trianglesList.Add(triangles[triCount]);
    46.                 trianglesList.Add(triangles[triCount + 1]);
    47.                 trianglesList.Add(triangles[triCount + 2]);
    48.             }
    49.         }
    50.        
    51.         triangles = trianglesList.ToArray();
    52.         vertices = vertList.ToArray();
    53.         uv = uvList.ToArray();
    54.         normals = normalsList.ToArray();
    55.        
    56.         mesh.triangles = triangles;
    57.         mesh.vertices = vertices;
    58.         mesh.uv = uv;
    59.         mesh.normals = normals;
    60.  
    61.         mesh.RecalculateBounds();
    62.         mesh.RecalculateNormals();
    63.         mesh.Optimize();
    64.  
    65.         copyMeshRenderer.enabled = true;
    66.         copy.GetComponent<Rigidbody>().useGravity = true;
    67.  
    68.         SphereCollider copyMeshCollider = copy.AddComponent<SphereCollider>();
    69.     }
    70.  
    Here is the result:



    Thank you for the help!
     
  2. TEBZ_22

    TEBZ_22

    Joined:
    Jan 28, 2014
    Posts:
    37
  3. turndapage

    turndapage

    Joined:
    Jun 25, 2015
    Posts:
    12
    This looks like exactly what I'm looking for. It looks like a plugin for the editor though, when I want to do the action in run-time. I will investigate it further.
     
  4. TEBZ_22

    TEBZ_22

    Joined:
    Jan 28, 2014
    Posts:
    37
  5. turndapage

    turndapage

    Joined:
    Jun 25, 2015
    Posts:
    12
    Thanks so much! It will take some time perfecting the script, but that works like I want.