diff options
author | Bastien Montagne <montagne29@wanadoo.fr> | 2015-02-05 16:49:44 +0300 |
---|---|---|
committer | Bastien Montagne <montagne29@wanadoo.fr> | 2015-02-05 16:59:52 +0300 |
commit | f6e798946092c5d30ad0c011bd8c2920971471b2 (patch) | |
tree | fec4dc809a96a4c531256140804a8fa7ad2af9b8 | |
parent | 2c3e4fbd7e91e7faeaaed3946874beb4be4e7817 (diff) |
Custom Loop Normals: Add NormalEdit modifier
Nothing much to say here, basic tool to make normals point toward a target,
or to make them point 'outward' as if object was a spheroid (useful for game bushes etc.).
Also, forgot a big thank you to Campbell for the extensive review work he has done on this project!
-rw-r--r-- | release/datafiles/blender_icons.svg | 232 | ||||
-rw-r--r-- | release/datafiles/blender_icons16/icon16_mod_normaledit.dat | bin | 0 -> 1048 bytes | |||
-rw-r--r-- | release/datafiles/blender_icons32/icon32_mod_normaledit.dat | bin | 0 -> 4120 bytes | |||
-rw-r--r-- | release/datafiles/prvicons.png | bin | 15991 -> 15781 bytes | |||
-rw-r--r-- | release/scripts/startup/bl_ui/properties_data_modifier.py | 32 | ||||
-rw-r--r-- | source/blender/editors/include/UI_icons.h | 2 | ||||
-rw-r--r-- | source/blender/editors/space_outliner/outliner_draw.c | 2 | ||||
-rw-r--r-- | source/blender/makesdna/DNA_modifier_types.h | 33 | ||||
-rw-r--r-- | source/blender/makesrna/RNA_access.h | 1 | ||||
-rw-r--r-- | source/blender/makesrna/intern/rna_modifier.c | 79 | ||||
-rw-r--r-- | source/blender/modifiers/CMakeLists.txt | 1 | ||||
-rw-r--r-- | source/blender/modifiers/MOD_modifiertypes.h | 1 | ||||
-rw-r--r-- | source/blender/modifiers/intern/MOD_normal_edit.c | 493 | ||||
-rw-r--r-- | source/blender/modifiers/intern/MOD_util.c | 1 |
14 files changed, 871 insertions, 6 deletions
diff --git a/release/datafiles/blender_icons.svg b/release/datafiles/blender_icons.svg index fc60d6ce412..f67d67f0b1a 100644 --- a/release/datafiles/blender_icons.svg +++ b/release/datafiles/blender_icons.svg @@ -30087,6 +30087,130 @@ y1="244.14676" x2="174.75458" y2="226.33672" /> + <linearGradient + inkscape:collect="always" + xlink:href="#linearGradient23974-39" + id="linearGradient18721-2" + gradientUnits="userSpaceOnUse" + gradientTransform="matrix(-1.004219,0,0,0.980922,244.3928,19.4113)" + x1="-88.73024" + y1="-120.6127" + x2="-78.787354" + y2="-128.30418" /> + <linearGradient + id="linearGradient23974-39"> + <stop + id="stop23976-8" + offset="0" + style="stop-color:#2561b7;stop-opacity:1;" /> + <stop + id="stop23978-23" + offset="1" + style="stop-color:#f9fbff;stop-opacity:1" /> + </linearGradient> + <linearGradient + inkscape:collect="always" + xlink:href="#linearGradient31320-9" + id="linearGradient18728-1" + gradientUnits="userSpaceOnUse" + gradientTransform="matrix(-1.004219,0,0,0.980922,222.97812,19.5574)" + x1="68.688324" + y1="51.42366" + x2="72.671516" + y2="55.501457" /> + <linearGradient + id="linearGradient31320-9"> + <stop + style="stop-color:white;stop-opacity:1;" + offset="0" + id="stop31322-6" /> + <stop + style="stop-color:white;stop-opacity:0;" + offset="1" + id="stop31324-2" /> + </linearGradient> + <linearGradient + inkscape:collect="always" + xlink:href="#linearGradient14262-6" + id="linearGradient18765-5" + gradientUnits="userSpaceOnUse" + gradientTransform="matrix(-1.004219,0,0,0.980922,309.42934,-349.44584)" + x1="-26.207859" + y1="252.77303" + x2="-5.4963508" + y2="253.15045" /> + <linearGradient + id="linearGradient14262-6"> + <stop + id="stop14264-29" + offset="0" + style="stop-color:#2661b6;stop-opacity:1;" /> + <stop + id="stop14266-9" + offset="1" + style="stop-color:#c1d7f8;stop-opacity:0;" /> + </linearGradient> + <linearGradient + inkscape:collect="always" + xlink:href="#linearGradient319-61" + id="linearGradient18712-6" + gradientUnits="userSpaceOnUse" + gradientTransform="matrix(0.8167109,0,0,0.8433415,239.34332,-149.78578)" + x1="103.65562" + y1="49.547874" + x2="120.79755" + y2="57.84819" /> + <linearGradient + id="linearGradient319-61"> + <stop + style="stop-color:#ffffff;stop-opacity:1;" + offset="0" + id="stop320-47" /> + <stop + style="stop-color:#ffffff;stop-opacity:0;" + offset="1" + id="stop321-45" /> + </linearGradient> + <linearGradient + inkscape:collect="always" + xlink:href="#linearGradient23974-39" + id="linearGradient16862" + gradientUnits="userSpaceOnUse" + gradientTransform="matrix(-1.004219,0,0,0.980922,244.3928,19.4113)" + x1="-88.73024" + y1="-120.6127" + x2="-78.787354" + y2="-128.30418" /> + <linearGradient + inkscape:collect="always" + xlink:href="#linearGradient31320-9" + id="linearGradient16864" + gradientUnits="userSpaceOnUse" + gradientTransform="matrix(-1.004219,0,0,0.980922,222.97812,19.5574)" + x1="68.688324" + y1="51.42366" + x2="72.671516" + y2="55.501457" /> + <linearGradient + inkscape:collect="always" + xlink:href="#linearGradient14262-6" + id="linearGradient16866" + gradientUnits="userSpaceOnUse" + gradientTransform="matrix(-1.004219,0,0,0.980922,309.42934,-349.44584)" + x1="-26.207859" + y1="252.77303" + x2="-5.4963508" + y2="253.15045" /> + <linearGradient + inkscape:collect="always" + xlink:href="#linearGradient319-61" + id="linearGradient16868" + gradientUnits="userSpaceOnUse" + gradientTransform="matrix(0.8167109,0,0,0.8433415,239.34332,-149.78578)" + x1="103.65562" + y1="49.547874" + x2="120.79755" + y2="57.84819" /> </defs> <sodipodi:namedview id="base" @@ -30098,11 +30222,11 @@ objecttolerance="10000" inkscape:pageopacity="0.0" inkscape:pageshadow="2" - inkscape:zoom="11.740919" - inkscape:cx="291.42828" - inkscape:cy="63.791344" + inkscape:zoom="7.2069341" + inkscape:cx="381.96928" + inkscape:cy="408.8621" inkscape:document-units="px" - inkscape:current-layer="ICON_BORDER_LASSO" + inkscape:current-layer="layer1" showgrid="true" inkscape:window-width="1920" inkscape:window-height="995" @@ -90320,6 +90444,106 @@ id="rect18509" style="fill:none;stroke:none" /> </g> + <g + style="display:inline;enable-background:new" + id="ICON_MOD_NORMALEDIT" + transform="translate(105,-19)"> + <rect + style="opacity:0;fill:#b3b3b3;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:2.79999995;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate" + id="rect18695-8" + width="16" + height="16" + x="236" + y="260" /> + <g + id="g18697-9" + transform="translate(-86,370.75)"> + <path + style="fill:none;stroke:#0b1728;stroke-width:1.70000005;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none" + d="m 330,-107.75 -5,2 0.0372,6.324398 5,2.71875 4.99999,-2.71875 L 335,-105.75 l -5,-2 z" + id="path18699-5" + sodipodi:nodetypes="ccccccc" + inkscape:connector-curvature="0" /> + <path + id="path18719-5" + d="m 330.03717,-107.6131 -5,1.875 0,6.312498 5,2.71875 4.99999,-2.71875 0,-6.312498 -4.99999,-1.875 z" + style="fill:url(#linearGradient16862);fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:1.06666696px;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate" + inkscape:connector-curvature="0" /> + <g + id="g18703-7" + transform="translate(179,-179)"> + <path + id="path18707-1" + d="m 146.0019,73.295281 5,-1.894157 5,1.894157 -5,2.073959 -5,-2.073959 z" + style="fill:url(#linearGradient16864);fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:1.06666696px;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate" + sodipodi:nodetypes="ccccc" + inkscape:export-filename="C:\Documents and Settings\Tata\Pulpit\4.png" + inkscape:export-xdpi="90" + inkscape:export-ydpi="90" + inkscape:connector-curvature="0" /> + </g> + <path + id="path18763-1" + d="m 335,-105.5 -5,2 0,6.75 5,-2.75 0,-6 z" + style="fill:url(#linearGradient16866);fill-opacity:1;fill-rule:evenodd;stroke:none;display:inline" + sodipodi:nodetypes="ccccc" + inkscape:connector-curvature="0" /> + <path + sodipodi:nodetypes="ccccccc" + id="path18709-8" + d="m 334.5,-105.25 0.002,5.587357 -4.5,2.480073 -4.5,-2.480073 -0.002,-5.587357 4.5,-1.75 4.5,1.75 z" + style="fill:none;stroke:url(#linearGradient16868);stroke-width:0.99999982px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + inkscape:connector-curvature="0" /> + <path + id="path18758-5" + style="opacity:0.8;fill:none;stroke:#d7e3f4;stroke-width:3.29999995;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;display:inline" + d="m 330.25,-103.25 3.25,-1.5" + sodipodi:nodetypes="cc" + inkscape:export-filename="C:\Documents and Settings\Tata\Pulpit\Kopia blender\.blender\icons\blender's iconset.png" + inkscape:export-xdpi="90" + inkscape:export-ydpi="90" + inkscape:connector-curvature="0" /> + <g + id="g18737-4" + style="opacity:0.7" /> + <path + inkscape:export-ydpi="90" + inkscape:export-xdpi="90" + inkscape:export-filename="C:\Documents and Settings\Tata\Pulpit\Kopia blender\.blender\icons\blender's iconset.png" + sodipodi:nodetypes="cc" + d="M 330.25,-103.25 335,-105.5" + style="fill:#0b1728;fill-opacity:1;fill-rule:evenodd;stroke:#0b1728;stroke-width:1;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:0;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate" + id="path18760-7" + inkscape:connector-curvature="0" /> + <path + inkscape:export-ydpi="90" + inkscape:export-xdpi="90" + inkscape:export-filename="C:\Documents and Settings\Tata\Pulpit\Kopia blender\.blender\icons\blender's iconset.png" + sodipodi:nodetypes="cc" + d="m 325.34712,-100.90914 4.75,-2.25" + style="fill:#dd23dd;fill-opacity:1;fill-rule:evenodd;stroke:#dd23dd;stroke-width:1;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:0;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate" + id="path18760-7-5" + inkscape:connector-curvature="0" /> + <path + inkscape:export-ydpi="90" + inkscape:export-xdpi="90" + inkscape:export-filename="C:\Documents and Settings\Tata\Pulpit\Kopia blender\.blender\icons\blender's iconset.png" + sodipodi:nodetypes="cc" + d="m 330.29093,-103.24155 -0.0238,-5.2559" + style="fill:#dd23dd;fill-opacity:1;fill-rule:evenodd;stroke:#dd23dd;stroke-width:1;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:0;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate" + id="path18760-7-5-6" + inkscape:connector-curvature="0" /> + <path + inkscape:export-ydpi="90" + inkscape:export-xdpi="90" + inkscape:export-filename="C:\Documents and Settings\Tata\Pulpit\Kopia blender\.blender\icons\blender's iconset.png" + sodipodi:nodetypes="cc" + d="m 330.36378,-103.20969 4.94432,1.78292" + style="fill:#dd23dd;fill-opacity:1;fill-rule:evenodd;stroke:#dd23dd;stroke-width:1;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:0;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate" + id="path18760-7-5-6-3" + inkscape:connector-curvature="0" /> + </g> + </g> </g> <g inkscape:groupmode="layer" diff --git a/release/datafiles/blender_icons16/icon16_mod_normaledit.dat b/release/datafiles/blender_icons16/icon16_mod_normaledit.dat Binary files differnew file mode 100644 index 00000000000..1c4898f80d8 --- /dev/null +++ b/release/datafiles/blender_icons16/icon16_mod_normaledit.dat diff --git a/release/datafiles/blender_icons32/icon32_mod_normaledit.dat b/release/datafiles/blender_icons32/icon32_mod_normaledit.dat Binary files differnew file mode 100644 index 00000000000..26cce5507be --- /dev/null +++ b/release/datafiles/blender_icons32/icon32_mod_normaledit.dat diff --git a/release/datafiles/prvicons.png b/release/datafiles/prvicons.png Binary files differindex f8f744aadb3..7209385da9c 100644 --- a/release/datafiles/prvicons.png +++ b/release/datafiles/prvicons.png diff --git a/release/scripts/startup/bl_ui/properties_data_modifier.py b/release/scripts/startup/bl_ui/properties_data_modifier.py index 92e9e02ffca..283340bd9e1 100644 --- a/release/scripts/startup/bl_ui/properties_data_modifier.py +++ b/release/scripts/startup/bl_ui/properties_data_modifier.py @@ -1350,6 +1350,38 @@ class DATA_PT_modifiers(ModifierButtonsPanel, Panel): sub.active = bool(md.vertex_group) sub.prop(md, "invert_vertex_group", text="", icon='ARROW_LEFTRIGHT') + def NORMAL_EDIT(self, layout, ob, md): + has_vgroup = bool(md.vertex_group) + needs_object_offset = (((md.mode == 'RADIAL') and not md.target) or + ((md.mode == 'DIRECTIONAL') and md.use_direction_parallel)) + + row = layout.row() + row.prop(md, "mode", expand=True) + + split = layout.split() + + col = split.column() + col.prop(md, "target", text="") + sub = col.column(align=True) + sub.active = needs_object_offset + sub.prop(md, "offset") + row = col.row(align=True) + + col = split.column() + row = col.row() + row.active = (md.mode == 'DIRECTIONAL') + row.prop(md, "use_direction_parallel") + + subcol = col.column(align=True) + subcol.label("Mix Mode:") + subcol.prop(md, "mix_mode", text="") + subcol.prop(md, "mix_factor") + row = subcol.row(align=True) + row.prop_search(md, "vertex_group", ob, "vertex_groups", text="") + sub = row.row(align=True) + sub.active = has_vgroup + sub.prop(md, "use_invert_vertex_group", text="", icon='ARROW_LEFTRIGHT') + if __name__ == "__main__": # only for live edit. bpy.utils.register_module(__name__) diff --git a/source/blender/editors/include/UI_icons.h b/source/blender/editors/include/UI_icons.h index 7c60933a1a3..bfb7a3420c9 100644 --- a/source/blender/editors/include/UI_icons.h +++ b/source/blender/editors/include/UI_icons.h @@ -590,8 +590,8 @@ DEF_ICON(MOD_SKIN) DEF_ICON(MOD_TRIANGULATE) DEF_ICON(MOD_WIREFRAME) DEF_ICON(MOD_DATA_TRANSFER) +DEF_ICON(MOD_NORMALEDIT) #ifndef DEF_ICON_BLANK_SKIP - DEF_ICON(BLANK168) DEF_ICON(BLANK169) DEF_ICON(BLANK170) DEF_ICON(BLANK171) diff --git a/source/blender/editors/space_outliner/outliner_draw.c b/source/blender/editors/space_outliner/outliner_draw.c index ab6e371072e..1aa3fc26f6f 100644 --- a/source/blender/editors/space_outliner/outliner_draw.c +++ b/source/blender/editors/space_outliner/outliner_draw.c @@ -1020,6 +1020,8 @@ static void tselem_draw_icon(uiBlock *block, int xmax, float x, float y, TreeSto UI_icon_draw(x, y, ICON_MOD_MESHDEFORM); break; /* XXX, needs own icon */ case eModifierType_DataTransfer: UI_icon_draw(x, y, ICON_MOD_DATA_TRANSFER); break; + case eModifierType_NormalEdit: + UI_icon_draw(x, y, ICON_MOD_NORMALEDIT); break; /* Default */ case eModifierType_None: case eModifierType_ShapeKey: diff --git a/source/blender/makesdna/DNA_modifier_types.h b/source/blender/makesdna/DNA_modifier_types.h index 47273d3c713..b32b228fda4 100644 --- a/source/blender/makesdna/DNA_modifier_types.h +++ b/source/blender/makesdna/DNA_modifier_types.h @@ -83,6 +83,7 @@ typedef enum ModifierType { eModifierType_LaplacianDeform = 47, eModifierType_Wireframe = 48, eModifierType_DataTransfer = 49, + eModifierType_NormalEdit = 50, NUM_MODIFIER_TYPES } ModifierType; @@ -1443,5 +1444,37 @@ enum { MOD_DATATRANSFER_USE_POLY = 1 << 31, }; +/* Set Split Normals modifier */ +typedef struct NormalEditModifierData { + ModifierData modifier; + char defgrp_name[64]; /* MAX_VGROUP_NAME */ + struct Object *target; /* Source of normals, or center of ellipsoid. */ + short mode; + short flag; + short mix_mode; + char pad[2]; + float mix_factor; + float offset[3]; +} NormalEditModifierData; + +/* NormalEditModifierData.mode */ +enum { + MOD_NORMALEDIT_MODE_RADIAL = 0, + MOD_NORMALEDIT_MODE_DIRECTIONAL = 1, +}; + +/* NormalEditModifierData.flags */ +enum { + MOD_NORMALEDIT_INVERT_VGROUP = (1 << 0), + MOD_NORMALEDIT_USE_DIRECTION_PARALLEL = (1 << 1), +}; + +/* NormalEditModifierData.mix_mode */ +enum { + MOD_NORMALEDIT_MIX_COPY = 0, + MOD_NORMALEDIT_MIX_ADD = 1, + MOD_NORMALEDIT_MIX_SUB = 2, + MOD_NORMALEDIT_MIX_MUL = 3, +}; #endif /* __DNA_MODIFIER_TYPES_H__ */ diff --git a/source/blender/makesrna/RNA_access.h b/source/blender/makesrna/RNA_access.h index 34c2e5a3f25..a62a06f1613 100644 --- a/source/blender/makesrna/RNA_access.h +++ b/source/blender/makesrna/RNA_access.h @@ -494,6 +494,7 @@ extern StructRNA RNA_SequenceEditor; extern StructRNA RNA_SequenceElement; extern StructRNA RNA_SequenceProxy; extern StructRNA RNA_SequenceTransform; +extern StructRNA RNA_NormalEditModifier; extern StructRNA RNA_ShaderNode; extern StructRNA RNA_ShaderNodeCameraData; extern StructRNA RNA_ShaderNodeCombineRGB; diff --git a/source/blender/makesrna/intern/rna_modifier.c b/source/blender/makesrna/intern/rna_modifier.c index 3458d84a85c..47a394d1c3b 100644 --- a/source/blender/makesrna/intern/rna_modifier.c +++ b/source/blender/makesrna/intern/rna_modifier.c @@ -63,8 +63,9 @@ EnumPropertyItem modifier_type_items[] = { {0, "", 0, N_("Modify"), ""}, - {eModifierType_DataTransfer, "DATA_TRANSFER", ICON_MOD_DATA_TRANSFER, "Data Transfer", ""}, + {eModifierType_DataTransfer, "DATA_TRANSFER", ICON_MOD_DATA_TRANSFER, "Data Transfer", ""}, {eModifierType_MeshCache, "MESH_CACHE", ICON_MOD_MESHDEFORM, "Mesh Cache", ""}, + {eModifierType_NormalEdit, "NORMAL_EDIT", ICON_MOD_NORMALEDIT, "Normal Edit", ""}, {eModifierType_UVProject, "UV_PROJECT", ICON_MOD_UVPROJECT, "UV Project", ""}, {eModifierType_UVWarp, "UV_WARP", ICON_MOD_UVPROJECT, "UV Warp", ""}, {eModifierType_WeightVGEdit, "VERTEX_WEIGHT_EDIT", ICON_MOD_VERTEX_WEIGHT, "Vertex Weight Edit", ""}, @@ -374,6 +375,8 @@ static StructRNA *rna_Modifier_refine(struct PointerRNA *ptr) return &RNA_WireframeModifier; case eModifierType_DataTransfer: return &RNA_DataTransferModifier; + case eModifierType_NormalEdit: + return &RNA_NormalEditModifier; /* Default */ case eModifierType_None: case eModifierType_ShapeKey: @@ -448,6 +451,7 @@ RNA_MOD_VGROUP_NAME_SET(LaplacianSmooth, defgrp_name); RNA_MOD_VGROUP_NAME_SET(Lattice, name); RNA_MOD_VGROUP_NAME_SET(Mask, vgroup); RNA_MOD_VGROUP_NAME_SET(MeshDeform, defgrp_name); +RNA_MOD_VGROUP_NAME_SET(NormalEdit, defgrp_name); RNA_MOD_VGROUP_NAME_SET(Shrinkwrap, vgroup_name); RNA_MOD_VGROUP_NAME_SET(SimpleDeform, vgroup_name); RNA_MOD_VGROUP_NAME_SET(Smooth, defgrp_name); @@ -534,6 +538,7 @@ RNA_MOD_OBJECT_SET(DataTransfer, ob_source, OB_MESH); RNA_MOD_OBJECT_SET(Lattice, object, OB_LATTICE); RNA_MOD_OBJECT_SET(Mask, ob_arm, OB_ARMATURE); RNA_MOD_OBJECT_SET(MeshDeform, object, OB_MESH); +RNA_MOD_OBJECT_SET(NormalEdit, target, OB_EMPTY); RNA_MOD_OBJECT_SET(Shrinkwrap, target, OB_MESH); RNA_MOD_OBJECT_SET(Shrinkwrap, auxTarget, OB_MESH); @@ -4361,6 +4366,77 @@ static void rna_def_modifier_datatransfer(BlenderRNA *brna) RNA_def_property_update(prop, 0, "rna_Modifier_update"); } +static void rna_def_modifier_normaledit(BlenderRNA *brna) +{ + StructRNA *srna; + PropertyRNA *prop; + + static EnumPropertyItem prop_mode_items[] = { + {MOD_NORMALEDIT_MODE_RADIAL, "RADIAL", 0, "Radial", + "From an ellipsoid (shape defined by the boundbox's dimensions, target is optional)"}, + {MOD_NORMALEDIT_MODE_DIRECTIONAL, "DIRECTIONAL", 0, "Directional", + "Normals 'track' (point to) the target object"}, + {0, NULL, 0, NULL, NULL} + }; + + static EnumPropertyItem prop_mix_mode_items[] = { + {MOD_NORMALEDIT_MIX_COPY, "COPY", 0, "Copy", "Copy new normals (overwrite existing)"}, + {MOD_NORMALEDIT_MIX_ADD, "ADD", 0, "Add", "Copy sum of new and old normals"}, + {MOD_NORMALEDIT_MIX_SUB, "SUB", 0, "Substract", "Copy new normals minus old normals"}, + {MOD_NORMALEDIT_MIX_MUL, "MUL", 0, "Multiply", "Copy product of old and new normals (*not* cross product)"}, + {0, NULL, 0, NULL, NULL} + }; + + srna = RNA_def_struct(brna, "NormalEditModifier", "Modifier"); + RNA_def_struct_ui_text(srna, "Normal Edit Modifier", "Modifier affecting/generating custom normals"); + RNA_def_struct_sdna(srna, "NormalEditModifierData"); + RNA_def_struct_ui_icon(srna, ICON_MOD_NORMALEDIT); + + prop = RNA_def_property(srna, "mode", PROP_ENUM, PROP_NONE); + RNA_def_property_enum_items(prop, prop_mode_items); + RNA_def_property_ui_text(prop, "Mode", "How to affect (generate) normals"); + RNA_def_property_update(prop, 0, "rna_Modifier_update"); + + prop = RNA_def_float_array(srna, "offset", 3, NULL, -FLT_MAX, FLT_MAX, "Offset", + "Offset from object's center", -100.0f, 100.0f); + RNA_def_property_subtype(prop, PROP_COORDS); + RNA_def_property_update(prop, 0, "rna_Modifier_update"); + + prop = RNA_def_property(srna, "mix_mode", PROP_ENUM, PROP_NONE); + RNA_def_property_enum_items(prop, prop_mix_mode_items); + RNA_def_property_ui_text(prop, "Mix Mode", "How to mix generated normals with existing ones"); + RNA_def_property_update(prop, 0, "rna_Modifier_update"); + + prop = RNA_def_float(srna, "mix_factor", 1.0f, 0.0f, 1.0f, "Mix Factor", + "How much of generated normals to mix with exiting ones", 0.0f, 1.0f); + RNA_def_property_update(prop, 0, "rna_Modifier_update"); + + prop = RNA_def_property(srna, "vertex_group", PROP_STRING, PROP_NONE); + RNA_def_property_string_sdna(prop, NULL, "defgrp_name"); + RNA_def_property_ui_text(prop, "Vertex Group", "Vertex group name for selecting/weighting the affected areas"); + RNA_def_property_string_funcs(prop, NULL, NULL, "rna_NormalEditModifier_defgrp_name_set"); + RNA_def_property_update(prop, 0, "rna_Modifier_update"); + + prop = RNA_def_property(srna, "use_invert_vertex_group", PROP_BOOLEAN, PROP_NONE); + RNA_def_property_boolean_sdna(prop, NULL, "flag", MOD_NORMALEDIT_INVERT_VGROUP); + RNA_def_property_ui_text(prop, "Invert", "Invert vertex group influence"); + RNA_def_property_update(prop, 0, "rna_Modifier_update"); + + prop = RNA_def_property(srna, "target", PROP_POINTER, PROP_NONE); + RNA_def_property_ui_text(prop, "Target", "Target object used to affect normals"); + RNA_def_property_pointer_funcs(prop, NULL, "rna_NormalEditModifier_target_set", NULL, NULL); + RNA_def_property_flag(prop, PROP_EDITABLE | PROP_ID_SELF_CHECK); + RNA_def_property_update(prop, 0, "rna_Modifier_dependency_update"); + + prop = RNA_def_property(srna, "use_direction_parallel", PROP_BOOLEAN, PROP_NONE); + RNA_def_property_boolean_sdna(prop, NULL, "flag", MOD_NORMALEDIT_USE_DIRECTION_PARALLEL); + RNA_def_property_boolean_default(prop, true); + RNA_def_property_ui_text(prop, "Parallel Normals", + "Use same direction for all normals, from origin to target's center " + "(Track Object mode only)"); + RNA_def_property_update(prop, 0, "rna_Modifier_update"); +} + void RNA_def_modifier(BlenderRNA *brna) { StructRNA *srna; @@ -4475,6 +4551,7 @@ void RNA_def_modifier(BlenderRNA *brna) rna_def_modifier_laplaciandeform(brna); rna_def_modifier_wireframe(brna); rna_def_modifier_datatransfer(brna); + rna_def_modifier_normaledit(brna); } #endif diff --git a/source/blender/modifiers/CMakeLists.txt b/source/blender/modifiers/CMakeLists.txt index a93dc76a655..57e927fffed 100644 --- a/source/blender/modifiers/CMakeLists.txt +++ b/source/blender/modifiers/CMakeLists.txt @@ -73,6 +73,7 @@ set(SRC intern/MOD_mirror.c intern/MOD_multires.c intern/MOD_none.c + intern/MOD_normal_edit.c intern/MOD_ocean.c intern/MOD_particleinstance.c intern/MOD_particlesystem.c diff --git a/source/blender/modifiers/MOD_modifiertypes.h b/source/blender/modifiers/MOD_modifiertypes.h index aceee441098..66e613be68a 100644 --- a/source/blender/modifiers/MOD_modifiertypes.h +++ b/source/blender/modifiers/MOD_modifiertypes.h @@ -82,6 +82,7 @@ extern ModifierTypeInfo modifierType_MeshCache; extern ModifierTypeInfo modifierType_LaplacianDeform; extern ModifierTypeInfo modifierType_Wireframe; extern ModifierTypeInfo modifierType_DataTransfer; +extern ModifierTypeInfo modifierType_NormalEdit; /* MOD_util.c */ void modifier_type_init(ModifierTypeInfo *types[]); diff --git a/source/blender/modifiers/intern/MOD_normal_edit.c b/source/blender/modifiers/intern/MOD_normal_edit.c new file mode 100644 index 00000000000..39881f474b0 --- /dev/null +++ b/source/blender/modifiers/intern/MOD_normal_edit.c @@ -0,0 +1,493 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributor(s): Bastien Montagne + * + * ***** END GPL LICENSE BLOCK ***** + * + */ + +/** \file blender/modifiers/intern/MOD_normal_edit.c + * \ingroup modifiers + */ + +#include <string.h> + +#include "MEM_guardedalloc.h" + +#include "DNA_object_types.h" +#include "DNA_meshdata_types.h" +#include "DNA_mesh_types.h" + +#include "BLI_math.h" +#include "BLI_utildefines.h" +#include "BLI_bitmap.h" + +#include "BKE_cdderivedmesh.h" +#include "BKE_mesh.h" +#include "BKE_deform.h" + +#include "depsgraph_private.h" + +#include "MOD_util.h" + + +static void generate_vert_coordinates( + DerivedMesh *dm, Object *ob, Object *ob_center, const float offset[3], + const int num_verts, float (*r_cos)[3], float r_size[3]) +{ + float min_co[3], max_co[3]; + float diff[3]; + bool do_diff = false; + + INIT_MINMAX(min_co, max_co); + + dm->getVertCos(dm, r_cos); + + /* Get size (i.e. deformation of the spheroid generating normals), either from target object, or own geometry. */ + if (ob_center) { + copy_v3_v3(r_size, ob_center->size); + } + else { + minmax_v3v3_v3_array(min_co, max_co, r_cos, num_verts); + /* Set size. */ + sub_v3_v3v3(r_size, max_co, min_co); + } + + /* Error checks - we do not want one or more of our sizes to be null! */ + if (is_zero_v3(r_size)) { + r_size[0] = r_size[1] = r_size[2] = 1.0f; + } + else { + CLAMP_MIN(r_size[0], FLT_EPSILON); + CLAMP_MIN(r_size[1], FLT_EPSILON); + CLAMP_MIN(r_size[2], FLT_EPSILON); + } + + if (ob_center) { + /* Translate our coordinates so that center of ob_center is at (0, 0, 0). */ + float mat[4][4]; + + /* Get ob_center coordinates in ob local coordinates. */ + invert_m4_m4(mat, ob_center->obmat); + mul_m4_m4m4(mat, mat, ob->obmat); + copy_v3_v3(diff, mat[3]); + + do_diff = true; + } + else if (!is_zero_v3(offset)) { + negate_v3_v3(diff, offset); + + do_diff = true; + } + /* Else, no need to change coordinates! */ + + if (do_diff) { + int i = num_verts; + while (i--) { + add_v3_v3(r_cos[i], diff); + } + } +} + +/* Note this modifies nos_new in-place. */ +static void mix_normals( + const float mix_factor, MDeformVert *dvert, const int defgrp_index, const bool use_invert_vgroup, + const short mix_mode, + const int num_verts, MLoop *mloop, float (*nos_old)[3], float (*nos_new)[3], const int num_loops) +{ + /* Mix with org normals... */ + float *facs = NULL, *wfac; + float (*no_new)[3], (*no_old)[3]; + int i; + + if (dvert) { + facs = MEM_mallocN(sizeof(*facs) * (size_t)num_loops, __func__); + BKE_defvert_extract_vgroup_to_loopweights( + dvert, defgrp_index, num_verts, mloop, num_loops, facs, use_invert_vgroup); + } + + for (i = num_loops, no_new = nos_new, no_old = nos_old, wfac = facs; i--; no_new++, no_old++, wfac++) { + const float fac = facs ? *wfac * mix_factor : mix_factor; + + switch (mix_mode) { + case MOD_NORMALEDIT_MIX_ADD: + add_v3_v3(*no_new, *no_old); + normalize_v3(*no_new); + break; + case MOD_NORMALEDIT_MIX_SUB: + sub_v3_v3(*no_new, *no_old); + normalize_v3(*no_new); + break; + case MOD_NORMALEDIT_MIX_MUL: + mul_v3_v3(*no_new, *no_old); + normalize_v3(*no_new); + break; + case MOD_NORMALEDIT_MIX_COPY: + break; + } + interp_v3_v3v3_slerp_safe(*no_new, *no_old, *no_new, fac); + } + + MEM_SAFE_FREE(facs); +} + +static void normalEditModifier_do_radial( + NormalEditModifierData *smd, Object *ob, DerivedMesh *dm, + short (*clnors)[2], float (*loopnors)[3], float (*polynors)[3], + const short mix_mode, const float mix_factor, + MDeformVert *dvert, const int defgrp_index, const bool use_invert_vgroup, + MVert *mvert, const int num_verts, MEdge *medge, const int num_edges, + MLoop *mloop, const int num_loops, MPoly *mpoly, const int num_polys) +{ + int i; + + float (*cos)[3] = MEM_mallocN(sizeof(*cos) * num_verts, __func__); + float (*nos)[3] = MEM_mallocN(sizeof(*nos) * num_loops, __func__); + float size[3]; + + BLI_bitmap *done_verts = BLI_BITMAP_NEW((size_t)num_verts, __func__); + + generate_vert_coordinates(dm, ob, smd->target, smd->offset, num_verts, cos, size); + + /* size gives us our spheroid coefficients (A, B, C). + * Then, we want to find out for each vert its (a, b, c) triple (proportional to (A, B, C) one). + * + * Ellipsoid basic equation: (x^2/a^2) + (y^2/b^2) + (z^2/c^2) = 1. + * Since we want to find (a, b, c) matching this equation and proportional to (A, B, C), we can do: + * m = B / A + * n = C / A + * hence: + * (x^2/a^2) + (y^2/b^2) + (z^2/c^2) = 1 + * -> b^2*c^2*x^2 + a^2*c^2*y^2 + a^2*b^2*z^2 = a^2*b^2*c^2 + * b = ma + * c = na + * -> m^2*a^2*n^2*a^2*x^2 + a^2*n^2*a^2*y^2 + a^2*m^2*a^2*z^2 = a^2*m^2*a^2*n^2*a^2 + * -> m^2*n^2*a^4*x^2 + n^2*a^4*y^2 + m^2*a^4*z^2 = m^2*n^2*a^6 + * -> a^2 = (m^2*n^2*x^2 + n^2y^2 + m^2z^2) / (m^2*n^2) = x^2 + (y^2 / m^2) + (z^2 / n^2) + * -> b^2 = (m^2*n^2*x^2 + n^2y^2 + m^2z^2) / (n^2) = (m^2 * x^2) + y^2 + (m^2 * z^2 / n^2) + * -> c^2 = (m^2*n^2*x^2 + n^2y^2 + m^2z^2) / (m^2) = (n^2 * x^2) + (n^2 * y^2 / m^2) + z^2 + * + * All we have to do now is compute normal of the spheroid at that point: + * n = (x / a^2, y / b^2, z / c^2) + * And we are done! + */ + { + const float a = size[0], b = size[1], c = size[2]; + const float m2 = (b * b) / (a * a); + const float n2 = (c * c) / (a * a); + + MLoop *ml; + float (*no)[3]; + + /* We reuse cos to now store the ellipsoid-normal of the verts! */ + for (i = num_loops, ml = mloop, no = nos; i-- ; ml++, no++) { + const int vidx = ml->v; + float *co = cos[vidx]; + + if (!BLI_BITMAP_TEST(done_verts, vidx)) { + const float x2 = co[0] * co[0]; + const float y2 = co[1] * co[1]; + const float z2 = co[2] * co[2]; + const float a2 = x2 + (y2 / m2) + (z2 / n2); + const float b2 = (m2 * x2) + y2 + (m2 * z2 / n2); + const float c2 = (n2 * x2) + (n2 * y2 / m2) + z2; + + co[0] /= a2; + co[1] /= b2; + co[2] /= c2; + normalize_v3(co); + + BLI_BITMAP_ENABLE(done_verts, vidx); + } + copy_v3_v3(*no, co); + } + } + + if (loopnors) { + mix_normals(mix_factor, dvert, defgrp_index, use_invert_vgroup, + mix_mode, num_verts, mloop, loopnors, nos, num_loops); + } + + BKE_mesh_normals_loop_custom_set(mvert, num_verts, medge, num_edges, mloop, nos, num_loops, + mpoly, (const float(*)[3])polynors, num_polys, clnors); + + MEM_freeN(cos); + MEM_freeN(nos); + MEM_freeN(done_verts); +} + +static void normalEditModifier_do_directional( + NormalEditModifierData *smd, Object *ob, DerivedMesh *dm, + short (*clnors)[2], float (*loopnors)[3], float (*polynors)[3], + const short mix_mode, const float mix_factor, + MDeformVert *dvert, const int defgrp_index, const bool use_invert_vgroup, + MVert *mvert, const int num_verts, MEdge *medge, const int num_edges, + MLoop *mloop, const int num_loops, MPoly *mpoly, const int num_polys) +{ + const bool use_parallel_normals = (smd->flag & MOD_NORMALEDIT_USE_DIRECTION_PARALLEL) != 0; + + float (*cos)[3] = MEM_mallocN(sizeof(*cos) * num_verts, __func__); + float (*nos)[3] = MEM_mallocN(sizeof(*nos) * num_loops, __func__); + + float target_co[3]; + int i; + + dm->getVertCos(dm, cos); + + /* Get target's center coordinates in ob local coordinates. */ + { + float mat[4][4]; + + invert_m4_m4(mat, ob->obmat); + mul_m4_m4m4(mat, mat, smd->target->obmat); + copy_v3_v3(target_co, mat[3]); + } + + if (use_parallel_normals) { + float no[3]; + + sub_v3_v3v3(no, target_co, smd->offset); + normalize_v3(no); + + for (i = num_loops; i--; ) { + copy_v3_v3(nos[i], no); + } + } + else { + BLI_bitmap *done_verts = BLI_BITMAP_NEW((size_t)num_verts, __func__); + MLoop *ml; + float (*no)[3]; + + /* We reuse cos to now store the 'to target' normal of the verts! */ + for (i = num_loops, no = nos, ml = mloop; i--; no++, ml++) { + const int vidx = ml->v; + float *co = cos[vidx]; + + if (!BLI_BITMAP_TEST(done_verts, vidx)) { + sub_v3_v3v3(co, target_co, co); + normalize_v3(co); + + BLI_BITMAP_ENABLE(done_verts, vidx); + } + + copy_v3_v3(*no, co); + } + + MEM_freeN(done_verts); + } + + if (loopnors) { + mix_normals(mix_factor, dvert, defgrp_index, use_invert_vgroup, + mix_mode, num_verts, mloop, loopnors, nos, num_loops); + } + + BKE_mesh_normals_loop_custom_set(mvert, num_verts, medge, num_edges, mloop, nos, num_loops, + mpoly, (const float(*)[3])polynors, num_polys, clnors); + + MEM_freeN(cos); + MEM_freeN(nos); +} + +static bool is_valid_target(NormalEditModifierData *smd) +{ + if (smd->mode == MOD_NORMALEDIT_MODE_RADIAL) { + return true; + } + else if ((smd->mode == MOD_NORMALEDIT_MODE_DIRECTIONAL) && smd->target) { + return true; + } + modifier_setError((ModifierData *)smd, "Invalid target settings"); + return false; +} + +static void normalEditModifier_do(NormalEditModifierData *smd, Object *ob, DerivedMesh *dm) +{ + Mesh *me = ob->data; + + const int num_verts = dm->getNumVerts(dm); + const int num_edges = dm->getNumEdges(dm); + const int num_loops = dm->getNumLoops(dm); + const int num_polys = dm->getNumPolys(dm); + MVert *mvert = dm->getVertArray(dm); + MEdge *medge = dm->getEdgeArray(dm); + MLoop *mloop = dm->getLoopArray(dm); + MPoly *mpoly = dm->getPolyArray(dm); + + const bool use_invert_vgroup = ((smd->flag & MOD_NORMALEDIT_INVERT_VGROUP) != 0); + const bool use_current_clnors = !((smd->mix_mode == MOD_NORMALEDIT_MIX_COPY) && + (smd->mix_factor == 1.0f) && + (smd->defgrp_name[0] == '\0')); + + int defgrp_index; + MDeformVert *dvert; + + float (*loopnors)[3] = NULL; + short (*clnors)[2]; + + float (*polynors)[3]; + bool free_polynors = false; + + /* Do not run that modifier at all if autosmooth is disabled! */ + if (!is_valid_target(smd) || !num_loops) { + return; + } + + if (!(me->flag & ME_AUTOSMOOTH)) { + modifier_setError((ModifierData *)smd, "Enable 'Auto Smooth' option in mesh settings"); + return; + } + + + if (use_current_clnors) { + dm->calcLoopNormals(dm, (me->flag & ME_AUTOSMOOTH) != 0, me->smoothresh); + loopnors = dm->getLoopDataArray(dm, CD_NORMAL); + } + + clnors = CustomData_duplicate_referenced_layer(&dm->loopData, CD_CUSTOMLOOPNORMAL, num_loops); + if (!clnors) { + DM_add_loop_layer(dm, CD_CUSTOMLOOPNORMAL, CD_CALLOC, NULL); + clnors = dm->getLoopDataArray(dm, CD_CUSTOMLOOPNORMAL); + } + + polynors = dm->getPolyDataArray(dm, CD_NORMAL); + if (!polynors) { + polynors = MEM_mallocN(sizeof(*polynors) * num_polys, __func__); + BKE_mesh_calc_normals_poly(mvert, num_verts, mloop, mpoly, num_loops, num_polys, polynors, false); + free_polynors = true; + } + + modifier_get_vgroup(ob, dm, smd->defgrp_name, &dvert, &defgrp_index); + + if (smd->mode == MOD_NORMALEDIT_MODE_RADIAL) { + normalEditModifier_do_radial( + smd, ob, dm, clnors, loopnors, polynors, + smd->mix_mode, smd->mix_factor, dvert, defgrp_index, use_invert_vgroup, + mvert, num_verts, medge, num_edges, mloop, num_loops, mpoly, num_polys); + } + else if (smd->mode == MOD_NORMALEDIT_MODE_DIRECTIONAL) { + normalEditModifier_do_directional( + smd, ob, dm, clnors, loopnors, polynors, + smd->mix_mode, smd->mix_factor, dvert, defgrp_index, use_invert_vgroup, + mvert, num_verts, medge, num_edges, mloop, num_loops, mpoly, num_polys); + } + + if (free_polynors) { + MEM_freeN(polynors); + } +} + +static void initData(ModifierData *md) +{ + NormalEditModifierData *smd = (NormalEditModifierData *)md; + + smd->mode = MOD_NORMALEDIT_MODE_RADIAL; + + smd->mix_mode = MOD_NORMALEDIT_MIX_COPY; + smd->mix_factor = 1.0f; +} + +static void copyData(ModifierData *md, ModifierData *target) +{ + modifier_copyData_generic(md, target); +} + +static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md) +{ + NormalEditModifierData *smd = (NormalEditModifierData *)md; + CustomDataMask dataMask = CD_CUSTOMLOOPNORMAL; + + /* Ask for vertexgroups if we need them. */ + if (smd->defgrp_name[0]) { + dataMask |= (CD_MASK_MDEFORMVERT); + } + + return dataMask; +} + +static bool dependsOnNormals(ModifierData *UNUSED(md)) +{ + return true; +} + +static void foreachObjectLink(ModifierData *md, Object *ob, ObjectWalkFunc walk, void *userData) +{ + NormalEditModifierData *smd = (NormalEditModifierData *) md; + + walk(userData, ob, &smd->target); +} + +static void foreachIDLink(ModifierData *md, Object *ob, IDWalkFunc walk, void *userData) +{ + NormalEditModifierData *smd = (NormalEditModifierData *) md; + + walk(userData, ob, (ID **)&smd->target); +} + +static bool isDisabled(ModifierData *md, int UNUSED(useRenderParams)) +{ + NormalEditModifierData *smd = (NormalEditModifierData *)md; + + return !is_valid_target(smd); +} + +static void updateDepgraph(ModifierData *md, DagForest *forest, struct Scene *UNUSED(scene), + Object *UNUSED(ob), DagNode *obNode) +{ + NormalEditModifierData *smd = (NormalEditModifierData *) md; + + if (smd->target) { + DagNode *Node = dag_get_node(forest, smd->target); + + dag_add_relation(forest, Node, obNode, DAG_RL_OB_DATA, "NormalEdit Modifier"); + } +} + +static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *dm, ModifierApplyFlag UNUSED(flag)) +{ + normalEditModifier_do((NormalEditModifierData *)md, ob, dm); + return dm; +} + +ModifierTypeInfo modifierType_NormalEdit = { + /* name */ "Set Split Normals", + /* structName */ "NormalEditModifierData", + /* structSize */ sizeof(NormalEditModifierData), + /* type */ eModifierTypeType_Constructive, + /* flags */ eModifierTypeFlag_AcceptsMesh | + eModifierTypeFlag_AcceptsCVs | + eModifierTypeFlag_SupportsMapping | + eModifierTypeFlag_SupportsEditmode | + eModifierTypeFlag_EnableInEditmode, + /* copyData */ copyData, + /* deformVerts */ NULL, + /* deformMatrices */ NULL, + /* deformVertsEM */ NULL, + /* deformMatricesEM */ NULL, + /* applyModifier */ applyModifier, + /* applyModifierEM */ NULL, + /* initData */ initData, + /* requiredDataMask */ requiredDataMask, + /* freeData */ NULL, + /* isDisabled */ isDisabled, + /* updateDepgraph */ updateDepgraph, + /* dependsOnTime */ NULL, + /* dependsOnNormals */ dependsOnNormals, + /* foreachObjectLink */ foreachObjectLink, + /* foreachIDLink */ foreachIDLink, + /* foreachTexLink */ NULL, +}; diff --git a/source/blender/modifiers/intern/MOD_util.c b/source/blender/modifiers/intern/MOD_util.c index fe83166c09f..c23c22b5fec 100644 --- a/source/blender/modifiers/intern/MOD_util.c +++ b/source/blender/modifiers/intern/MOD_util.c @@ -305,5 +305,6 @@ void modifier_type_init(ModifierTypeInfo *types[]) INIT_TYPE(LaplacianDeform); INIT_TYPE(Wireframe); INIT_TYPE(DataTransfer); + INIT_TYPE(NormalEdit); #undef INIT_TYPE } |