diff options
author | Antony Riakiotakis <kalast@gmail.com> | 2014-10-31 16:37:36 +0300 |
---|---|---|
committer | Antony Riakiotakis <kalast@gmail.com> | 2014-10-31 16:37:55 +0300 |
commit | a6a3989617e680327bda2357fe506d86574e2618 (patch) | |
tree | c03ce3912491cdd1713043b8448b38ad92f024a6 /source/blender/editors/uvedit/uvedit_unwrap_ops.c | |
parent | 46c11c7b7dfc2fd59b1de337cb311617e111012e (diff) |
Texture Paint Add Simple UVs:
Add simple uvs now does a cube unwrap and pack operation. Result is not
optimal by far but it should not result in crashes and it will be quite
usable for simple cases.
Diffstat (limited to 'source/blender/editors/uvedit/uvedit_unwrap_ops.c')
-rw-r--r-- | source/blender/editors/uvedit/uvedit_unwrap_ops.c | 95 |
1 files changed, 52 insertions, 43 deletions
diff --git a/source/blender/editors/uvedit/uvedit_unwrap_ops.c b/source/blender/editors/uvedit/uvedit_unwrap_ops.c index 335d8e6589e..bc08782e54b 100644 --- a/source/blender/editors/uvedit/uvedit_unwrap_ops.c +++ b/source/blender/editors/uvedit/uvedit_unwrap_ops.c @@ -190,21 +190,21 @@ static bool uvedit_have_selection(Scene *scene, BMEditMesh *em, bool implicit) return false; } -void uvedit_get_aspect(Scene *scene, Object *ob, BMEditMesh *em, float *aspx, float *aspy) +void uvedit_get_aspect(Scene *scene, Object *ob, BMesh *bm, float *aspx, float *aspy) { bool sloppy = true; bool selected = false; BMFace *efa; Image *ima; - efa = BM_mesh_active_face_get(em->bm, sloppy, selected); + efa = BM_mesh_active_face_get(bm, sloppy, selected); if (efa) { if (BKE_scene_use_new_shading_nodes(scene)) { ED_object_get_active_image(ob, efa->mat_nr + 1, &ima, NULL, NULL, NULL); } else { - MTexPoly *tf = CustomData_bmesh_get(&em->bm->pdata, efa->head.data, CD_MTEXPOLY); + MTexPoly *tf = CustomData_bmesh_get(&bm->pdata, efa->head.data, CD_MTEXPOLY); ima = tf->tpage; } @@ -247,11 +247,10 @@ static void construct_param_handle_face_add(ParamHandle *handle, Scene *scene, param_face_add(handle, key, i, vkeys, co, uv, pin, select, efa->no); } -static ParamHandle *construct_param_handle(Scene *scene, Object *ob, BMEditMesh *em, +static ParamHandle *construct_param_handle(Scene *scene, Object *ob, BMesh *bm, const bool implicit, const bool fill, const bool sel, const bool correct_aspect) { - BMesh *bm = em->bm; ParamHandle *handle; BMFace *efa; BMLoop *l; @@ -262,20 +261,19 @@ static ParamHandle *construct_param_handle(Scene *scene, Object *ob, BMEditMesh handle = param_construct_begin(); - if (correct_aspect) { float aspx, aspy; - uvedit_get_aspect(scene, ob, em, &aspx, &aspy); + uvedit_get_aspect(scene, ob, bm, &aspx, &aspy); if (aspx != aspy) param_aspect_ratio(handle, aspx, aspy); } /* we need the vert indices */ - BM_mesh_elem_index_ensure(em->bm, BM_VERT); + BM_mesh_elem_index_ensure(bm, BM_VERT); - BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) { + BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) { if ((BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) || (sel && BM_elem_flag_test(efa, BM_ELEM_SELECT) == 0)) { continue; @@ -299,7 +297,7 @@ static ParamHandle *construct_param_handle(Scene *scene, Object *ob, BMEditMesh } if (!implicit) { - BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) { + BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) { if (BM_elem_flag_test(eed, BM_ELEM_SEAM)) { ParamKey vkeys[2]; vkeys[0] = (ParamKey)BM_elem_index_get(eed->v1); @@ -378,7 +376,7 @@ static ParamHandle *construct_param_handle_subsurfed(Scene *scene, Object *ob, B if (correct_aspect) { float aspx, aspy; - uvedit_get_aspect(scene, ob, em, &aspx, &aspy); + uvedit_get_aspect(scene, ob, em->bm, &aspx, &aspy); if (aspx != aspy) param_aspect_ratio(handle, aspx, aspy); @@ -522,7 +520,7 @@ static bool minimize_stretch_init(bContext *C, wmOperator *op) ms->blend = RNA_float_get(op->ptr, "blend"); ms->iterations = RNA_int_get(op->ptr, "iterations"); ms->i = 0; - ms->handle = construct_param_handle(scene, obedit, em, implicit, fill_holes, 1, 1); + ms->handle = construct_param_handle(scene, obedit, em->bm, implicit, fill_holes, 1, 1); ms->lasttime = PIL_check_seconds_timer(); param_stretch_begin(ms->handle); @@ -701,16 +699,23 @@ void UV_OT_minimize_stretch(wmOperatorType *ot) /* ******************** Pack Islands operator **************** */ +void ED_uvedit_pack_islands(Scene *scene, Object *ob, BMesh *bm, bool selected, bool correct_aspect, bool do_rotate) +{ + ParamHandle *handle; + handle = construct_param_handle(scene, ob, bm, true, false, selected, correct_aspect); + param_pack(handle, scene->toolsettings->uvcalc_margin, do_rotate); + param_flush(handle); + param_delete(handle); +} + static int pack_islands_exec(bContext *C, wmOperator *op) { Scene *scene = CTX_data_scene(C); Object *obedit = CTX_data_edit_object(C); BMEditMesh *em = BKE_editmesh_from_object(obedit); - ParamHandle *handle; - bool implicit = true; bool do_rotate = RNA_boolean_get(op->ptr, "rotate"); - if (!uvedit_have_selection(scene, em, implicit)) { + if (!uvedit_have_selection(scene, em, true)) { return OPERATOR_CANCELLED; } @@ -719,10 +724,7 @@ static int pack_islands_exec(bContext *C, wmOperator *op) else RNA_float_set(op->ptr, "margin", scene->toolsettings->uvcalc_margin); - handle = construct_param_handle(scene, obedit, em, implicit, 0, 1, 1); - param_pack(handle, scene->toolsettings->uvcalc_margin, do_rotate); - param_flush(handle); - param_delete(handle); + ED_uvedit_pack_islands(scene, obedit, em->bm, true, true, do_rotate); DAG_id_tag_update(obedit->data, 0); WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data); @@ -762,7 +764,7 @@ static int average_islands_scale_exec(bContext *C, wmOperator *UNUSED(op)) return OPERATOR_CANCELLED; } - handle = construct_param_handle(scene, obedit, em, implicit, 0, 1, 1); + handle = construct_param_handle(scene, obedit, em->bm, implicit, 0, 1, 1); param_average(handle); param_flush(handle); param_delete(handle); @@ -807,7 +809,7 @@ void ED_uvedit_live_unwrap_begin(Scene *scene, Object *obedit) if (use_subsurf) liveHandle = construct_param_handle_subsurfed(scene, obedit, em, fillholes, false, true); else - liveHandle = construct_param_handle(scene, obedit, em, false, fillholes, false, true); + liveHandle = construct_param_handle(scene, obedit, em->bm, false, fillholes, false, true); param_lscm_begin(liveHandle, PARAM_TRUE, abf); } @@ -1008,7 +1010,7 @@ static void correct_uv_aspect(Scene *scene, Object *ob, BMEditMesh *em) const int cd_loop_uv_offset = CustomData_get_offset(&em->bm->ldata, CD_MLOOPUV); - uvedit_get_aspect(scene, ob, em, &aspx, &aspy); + uvedit_get_aspect(scene, ob, em->bm, &aspx, &aspy); if (aspx == aspy) return; @@ -1072,7 +1074,7 @@ static void uv_map_clip_correct(Scene *scene, Object *ob, BMEditMesh *em, wmOper if (scale_to_bounds) { INIT_MINMAX2(min, max); - + BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) { if (!BM_elem_flag_test(efa, BM_ELEM_SELECT)) continue; @@ -1082,7 +1084,7 @@ static void uv_map_clip_correct(Scene *scene, Object *ob, BMEditMesh *em, wmOper minmax_v2v2_v2(min, max, luv->uv); } } - + /* rescale UV to be in 1/1 */ dx = (max[0] - min[0]); dy = (max[1] - min[1]); @@ -1098,7 +1100,7 @@ static void uv_map_clip_correct(Scene *scene, Object *ob, BMEditMesh *em, wmOper BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) { luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset); - + luv->uv[0] = (luv->uv[0] - min[0]) * dx; luv->uv[1] = (luv->uv[1] - min[1]) * dy; } @@ -1136,7 +1138,7 @@ void ED_unwrap_lscm(Scene *scene, Object *obedit, const short sel) if (use_subsurf) handle = construct_param_handle_subsurfed(scene, obedit, em, fill_holes, sel, correct_aspect); else - handle = construct_param_handle(scene, obedit, em, false, fill_holes, sel, correct_aspect); + handle = construct_param_handle(scene, obedit, em->bm, false, fill_holes, sel, correct_aspect); param_lscm_begin(handle, PARAM_FALSE, scene->toolsettings->unwrapper == 0); param_lscm_solve(handle); @@ -1599,39 +1601,30 @@ void UV_OT_cylinder_project(wmOperatorType *ot) /******************* Cube Project operator ****************/ -static int cube_project_exec(bContext *C, wmOperator *op) +void ED_uvedit_cube_project_unwrap(Object *ob, BMesh *bm, float cube_size, bool use_select) { - Scene *scene = CTX_data_scene(C); - Object *obedit = CTX_data_edit_object(C); - BMEditMesh *em = BKE_editmesh_from_object(obedit); BMFace *efa; BMLoop *l; BMIter iter, liter; /* MTexPoly *tf; */ /* UNUSED */ MLoopUV *luv; - float cube_size, *loc, dx, dy; + float *loc, dx, dy; int cox, coy; int cd_loop_uv_offset; - /* add uvs if they don't exist yet */ - if (!ED_uvedit_ensure_uvs(C, scene, obedit)) { - return OPERATOR_CANCELLED; - } + cd_loop_uv_offset = CustomData_get_offset(&bm->ldata, CD_MLOOPUV); - cd_loop_uv_offset = CustomData_get_offset(&em->bm->ldata, CD_MLOOPUV); - - loc = obedit->obmat[3]; - cube_size = RNA_float_get(op->ptr, "cube_size"); + loc = ob->obmat[3]; /* choose x,y,z axis for projection depending on the largest normal * component, but clusters all together around the center of map. */ - BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) { + BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) { int first = 1; /* tf = CustomData_bmesh_get(&em->bm->pdata, efa->head.data, CD_MTEXPOLY); */ /* UNUSED */ - if (!BM_elem_flag_test(efa, BM_ELEM_SELECT)) + if (use_select && !BM_elem_flag_test(efa, BM_ELEM_SELECT)) continue; axis_dominant_v3(&cox, &coy, efa->no); @@ -1642,19 +1635,35 @@ static int cube_project_exec(bContext *C, wmOperator *op) luv->uv[0] = 0.5f + 0.5f * cube_size * (loc[cox] + l->v->co[cox]); luv->uv[1] = 0.5f + 0.5f * cube_size * (loc[coy] + l->v->co[coy]); - + if (first) { dx = floor(luv->uv[0]); dy = floor(luv->uv[1]); first = 0; } - + luv->uv[0] -= dx; luv->uv[1] -= dy; } } +} + +static int cube_project_exec(bContext *C, wmOperator *op) +{ + Scene *scene = CTX_data_scene(C); + Object *obedit = CTX_data_edit_object(C); + BMEditMesh *em = BKE_editmesh_from_object(obedit); + float cube_size = RNA_float_get(op->ptr, "cube_size"); + + /* add uvs if they don't exist yet */ + if (!ED_uvedit_ensure_uvs(C, scene, obedit)) { + return OPERATOR_CANCELLED; + } + + ED_uvedit_cube_project_unwrap(obedit, em->bm, cube_size, true); + uv_map_clip_correct(scene, obedit, em, op); DAG_id_tag_update(obedit->data, 0); |