public void SubdivideMeshes() { // delete previous children var oldChildren = Enumerable.Range(0, this.transform.childCount) .Select(i => this.transform.GetChild(i)).ToArray(); foreach (var c in oldChildren) { Object.DestroyImmediate(c.gameObject); } // create 4x4 array of subdivided objects for (int holes = 0; holes < 4; ++holes) { Mesh cube = CreatePrimitiveCube(holeCount: holes); cube.name = string.Format("Cube_holes{0}", holes); cube.RecalculateNormals(); for (int iter = 0; iter < 4; ++iter) { Mesh mesh = CatmullClark.Subdivide(cube, iter, new CatmullClark.Options { boundaryInterpolation = m_boundaryInterpolation, }); string name = string.Format("Cube_holes{0}_subdiv{1}", holes, iter); var obj = new GameObject(name); obj.transform.SetParent(this.transform); obj.transform.position = new Vector3(iter * 3f, holes * 3f, 0); obj.AddComponent <MeshFilter>().sharedMesh = mesh; obj.AddComponent <MeshRenderer>().material = m_material; } } }
void ObjectCreate() { if (Input.GetButtonDown("Fire2")) { // Create a new Mesh GameObject and add Catmull-Clark to it GameObject target = new GameObject("Snowball"); myRay = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(myRay, out hit)) { target.transform.position = hit.point + new Vector3(0, 0.75f, 0); } else { Debug.Log("emptyarea"); Destroy(target); } Vector3[] Vertices = new Vector3[8]; Vertices[0] = new Vector3(-1, -1, -1) * Size; Vertices[1] = new Vector3(1, -1, -1) * Size; Vertices[2] = new Vector3(1, -1, 1) * Size; Vertices[3] = new Vector3(-1, -1, 1) * Size; Vertices[4] = new Vector3(-1, 1, -1) * Size; Vertices[5] = new Vector3(1, 1, -1) * Size; Vertices[6] = new Vector3(1, 1, 1) * Size; Vertices[7] = new Vector3(-1, 1, 1) * Size; int[] Indices = { 0, 1, 2, 3, 7, 6, 5, 4, 0, 4, 5, 1, 1, 5, 6, 2, 2, 6, 7, 3, 3, 7, 4, 0, }; Mesh Control_mesh = new Mesh(); Control_mesh.Clear(); Control_mesh.vertices = Vertices; Control_mesh.SetIndices(Indices, MeshTopology.Quads, 0); Control_mesh.RecalculateNormals(); MeshFilter MF = target.AddComponent <MeshFilter>(); MF.mesh = Control_mesh; MeshRenderer MR = target.AddComponent <MeshRenderer>(); MR.material.color = Color.white; MeshCollider MC = target.AddComponent <MeshCollider>(); MC.material = null; CatmullClark cc = target.AddComponent <CatmullClark>(); cc.subdiv_level = ChangeLevel; } }
// Use this for initialization void Start() { mesh = CatmullClark.Subdivide(mesh, iter, new CatmullClark.Options { boundaryInterpolation = m_boundaryInterpolation, }); var obj = new GameObject("cell"); obj.transform.SetParent(this.transform); obj.transform.position = new Vector3(0, 0, 0); }
public void SubdivideMesh() { Mesh mesh = GetComponent <MeshFilter>().mesh; GameObject obj = new GameObject(); obj.name = "created"; GetComponent <MeshFilter>().mesh = CatmullClark.Subdivide(mesh, 1, new CatmullClark.Options { boundaryInterpolation = m_boundaryInterpolation, }); }
public static void MakeExtrusion(GameObject snowball, RaycastHit hit) { CatmullClark cc_subdiv = snowball.GetComponent <CatmullClark>(); if (cc_subdiv == null) { return; } ParametricLocation loc = cc_subdiv.param_location(hit.triangleIndex, hit.barycentricCoordinate); // the face-id for the control mesh is now in `loc.face_index` // the vertex indices of that face are then for (int i = 0; i < 4; i++) { control_idices[i] = cc_subdiv.orig_control_mesh_indices[loc.face_index * 4 + i]; Debug.Log(control_idices[i]); } int[] bottom = { 0, 1, 2, 3, }; int[] top = { 7, 6, 5, 4, }; int[] front = { 0, 4, 5, 1, }; int[] right = { 1, 5, 6, 2, }; int[] left = { 2, 6, 7, 3, }; int[] back = { 3, 7, 4, 0, }; for (int i = 0; i < 8; i++) { vertices[i] = cc_subdiv.cur_control_mesh_vertices[i]; } string CurrFace = null; int[] var = { 0, 0, 0, 0 }; float coef = 1f; if (control_idices[0] == 0 && control_idices[1] == 1) { CurrFace = "Bottom"; vertices[8] = vertices[0] + new Vector3(0, -1f * coef, 0); vertices[9] = vertices[1] + new Vector3(0, -1f * coef, 0); vertices[10] = vertices[2] + new Vector3(0, -1f * coef, 0); vertices[11] = vertices[3] + new Vector3(0, -1f * coef, 0); var = bottom; } if (control_idices[0] == 7) { CurrFace = "Top"; vertices[8] = vertices[7] + new Vector3(0, 1f * coef, 0); vertices[9] = vertices[6] + new Vector3(0, 1f * coef, 0); vertices[10] = vertices[5] + new Vector3(0, 1f * coef, 0); vertices[11] = vertices[4] + new Vector3(0, 1f * coef, 0); var = top; } if (control_idices[0] == 0 && control_idices[1] == 4) { CurrFace = "Front"; vertices[8] = vertices[0] + new Vector3(0, 0, -1f * coef); vertices[9] = vertices[4] + new Vector3(0, 0, -1f * coef); vertices[10] = vertices[5] + new Vector3(0, 0, -1f * coef); vertices[11] = vertices[1] + new Vector3(0, 0, -1f * coef); var = front; } if (control_idices[0] == 1) { CurrFace = "Right"; vertices[8] = vertices[1] + new Vector3(1.5f * coef, 0, 0); vertices[9] = vertices[5] + new Vector3(1.5f * coef, 0, 0); vertices[10] = vertices[6] + new Vector3(1.5f * coef, 0, 0); vertices[11] = vertices[2] + new Vector3(1.5f * coef, 0, 0); var = right; } if (control_idices[0] == 3) { CurrFace = "Left"; vertices[8] = vertices[3] + new Vector3(-1.5f * coef, 0, 0); vertices[9] = vertices[7] + new Vector3(-1.5f * coef, 0, 0); vertices[10] = vertices[4] + new Vector3(-1.5f * coef, 0, 0); vertices[11] = vertices[0] + new Vector3(-1.5f * coef, 0, 0); var = left; } if (control_idices[0] == 2) { CurrFace = "Back"; vertices[8] = vertices[3] + new Vector3(0, 0, 1f * coef); vertices[9] = vertices[2] + new Vector3(0, 0, 1f * coef); vertices[10] = vertices[6] + new Vector3(0, 0, 1f * coef); vertices[11] = vertices[7] + new Vector3(0, 0, 1f * coef); var = back; } Debug.Log(CurrFace); int[] Indices = { 0, 1, 2, 3, 7, 6, 5, 4, 0, 4, 5, 1, 1, 5, 6, 2, 2, 6, 7, 3, 3, 7, 4, 0, 8, 9, 10, 11, 8, 11, var[3], var[0], var[0], var[1], 9, 8, 11, 10, var[2], var[3], 10, 9, var[1], var[2], }; if (GameObject.FindWithTag("Manipulator")) { Destroy(GameObject.FindGameObjectWithTag("Manipulator")); } else { CreateManupulator(snowball); } cc_subdiv.cur_control_mesh_vertices = vertices; cc_subdiv.cur_control_mesh_indices = Indices; snowball.transform.parent.GetComponent <MeshFilter>().sharedMesh.RecalculateNormals(); }
public void Subdivide() { meshData = CatmullClark.Subdivide(meshData); meshFilter.mesh = meshData.ToUnityMesh(); }