private MeshCutResult CreatePlaneMesh() { MeshCutResult r = new MeshCutResult(); r.AddRectangle(0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0); return(r); }
private MeshCutResult ConvertMeshToMeshCutResult(Mesh m) { MeshCutResult r = new MeshCutResult(); r.vertices.AddRange(m.vertices); r.colors.AddRange(m.colors32); r.indices.AddRange(m.GetIndices(0)); r.uv.AddRange(m.uv); //Debug.Log(string.Join(",", r.vertices)); return(r); }
private Mesh ConvertMeshCutResultToMesh(MeshCutResult r) { Mesh m = new Mesh(); m.SetVertices(r.vertices); m.SetColors(r.colors); m.SetIndices(r.indices.ToArray(), MeshTopology.Triangles, 0); m.SetUVs(0, r.uv); m.RecalculateBounds(); m.RecalculateNormals(); return(m); }
// Update is called once per frame void Update() { // Vector3でマウス位置座標を取得する Vector3 screenPos = Input.mousePosition; // Z軸修正 screenPos.z = 10f; // マウス位置座標をスクリーン座標からワールド座標に変換する mousePos = Camera.main.ScreenToWorldPoint(screenPos); if (Input.GetMouseButtonDown(0)) { origin = mousePos; } if (Input.GetMouseButton(0)) { //Debug.DrawLine(origin, mousePos); } if (Input.GetMouseButtonUp(0)) { MeshCutResult result1 = new MeshCutResult(), result2 = new MeshCutResult(); MeshCut2D.MeshCut2D.Cut(meshCR.vertices, meshCR.uv, meshCR.indices, meshCR.indices.Count, origin.x - transform.position.x, origin.y - transform.position.y, mousePos.x - transform.position.x, mousePos.y - transform.position.y, result1, result2); Mesh m = ConvertMeshCutResultToMesh(result1); if (m.vertexCount != 0) { GameObject obj = Instantiate(meshPrefab, transform.position, transform.rotation); MeshFilter fil = obj.GetComponent <MeshFilter>(); fil.sharedMesh = m; MeshCollider col = obj.GetComponent <MeshCollider>(); col.sharedMesh = m; } m = ConvertMeshCutResultToMesh(result2); if (m.vertexCount != 0) { GameObject obj = Instantiate(meshPrefab, transform.position, transform.rotation); MeshFilter fil = obj.GetComponent <MeshFilter>(); fil.sharedMesh = m; MeshCollider col = obj.GetComponent <MeshCollider>(); col.sharedMesh = m; } Destroy(meshFilter.gameObject); } /* * for (int i = 0; i < result1.indices.Count / 3; i++) * { * for (int j = 0; j < 3; j++) * { * Vector2 from = result1.vertices[result1.indices[i * 3 + j]]; * Vector2 to = result1.vertices[result1.indices[i * 3 + (j + 1) % 3]]; * Debug.DrawLine(from, to); * } * } * * for (int i = 0; i < result2.indices.Count / 3; i++) * { * for (int j = 0; j < 3; j++) * { * Vector2 from = result2.vertices[result2.indices[i * 3 + j]]; * Vector2 to = result2.vertices[result2.indices[i * 3 + (j + 1) % 3]]; * Debug.DrawLine(from, to); * } * } */ /* * private void OnDrawGizmos() * { * if (meshFilter != null) * { * Mesh mesh = meshFilter.mesh; * * for (int i = 0; i < mesh.vertexCount / 3; i++) * { * for (int j = 0; j < 3; j++) * { * Vector2 from = mesh.vertices[i * 3 + j]; * Vector2 to = mesh.vertices[i * 3 + (j + 1) % 3]; * Vector2 v = from - originPos; * Vector2 v1 = (Vector2)transform.position - originPos; * Vector2 v2 = to - from; * float cross = Cross2D(v1, v2); * if (cross != 0f) * { * float t1 = Cross2D(v, v2) / cross; * float t2 = Cross2D(v, v1) / cross; * if (t1 >= 0 && t1 <= 1 && t2 >= 0 && t2 <= 1) * { * Vector2 hitPos = from + t2 * v2; * Gizmos.color = Color.red; * GizmosExtensions2D.DrawWireCircle2D(hitPos, 0.1f); * Debug.Log("point:" + hitPos); * } * } * Gizmos.color = Color.white; * Gizmos.DrawLine(from, to); * } * } * } * Gizmos.color = Color.white; * GizmosExtensions2D.DrawArrow2D(originPos, transform.position); * } */ }
// Use this for initialization void Start() { meshFilter = GetComponent <MeshFilter>(); meshCR = ConvertMeshToMeshCutResult(meshFilter.mesh); }