Implement normal map generator

This commit is contained in:
2024-05-24 00:37:08 +09:00
parent d5797c313c
commit 6fa5629403
3 changed files with 184 additions and 1 deletions
+63
View File
@@ -1633,3 +1633,66 @@ class IMAGE_EDITOR_PLUS_OT_make_seamless(bpy.types.Operator):
app.refresh_image(context)
return {'FINISHED'}
class IMAGE_EDITOR_PLUS_OT_normal_map(bpy.types.Operator):
"""Apply settings"""
bl_idname = 'image_editor_plus.normal_map'
bl_label = "Normal Map"
scale: bpy.props.FloatProperty()
flip_x: bpy.props.BoolProperty()
flip_y: bpy.props.BoolProperty()
full_z: bpy.props.BoolProperty()
def execute(self, context):
scale = self.scale
flip_x = self.flip_x
flip_y = self.flip_y
full_z = self.full_z
pixels, hsl = app.get_image_cache()
if pixels is None:
return {'CANCELLED'}
selection = app.get_target_selection(context)
if selection:
target_hsl = hsl[selection[0][1]:selection[1][1],
selection[0][0]:selection[1][0]]
elif selection == []:
return {'CANCELLED'}
else:
target_hsl = hsl
target_width, target_height = target_hsl.shape[1], target_hsl.shape[0]
target_hsl = np.pad(target_hsl, ((1, 1), (1, 1), (0, 0)), 'edge')
new_pixels = np.zeros((target_height, target_width, 4))
nx = (target_hsl[1:target_height + 1, 0:target_width, 2]
- target_hsl[1:target_height + 1, 2:target_width + 2, 2]) * scale
ny = (target_hsl[0:target_height, 1:target_width + 1, 2]
- target_hsl[2:target_height + 2, 1:target_width + 1, 2]) * scale
nz = 1.0 / np.sqrt(nx * nx + ny * ny + 1.0)
nx *= nz
ny *= nz
new_pixels = np.dstack([
0.5 + (-0.5 if flip_x else 0.5) * nx,
0.5 + (-0.5 if flip_y else 0.5) * ny,
(0 if full_z else 0.5) + (1.0 if full_z else 0.5) * nz,
])
new_pixels = np.clip(new_pixels, 0, None)
if selection:
pixels[selection[0][1]:selection[1][1],
selection[0][0]:selection[1][0], 0:3] = new_pixels
else:
pixels[:,:,0:3] = new_pixels
img = app.get_target_image(context)
utils.write_pixels_to_image(img, pixels)
app.refresh_image(context)
return {'FINISHED'}