Skip to content

Instantly share code, notes, and snippets.

@sczerwinski
Last active January 5, 2025 00:41
Show Gist options
  • Save sczerwinski/2923c4e23fe8f9adcfa0118f4397a215 to your computer and use it in GitHub Desktop.
Save sczerwinski/2923c4e23fe8f9adcfa0118f4397a215 to your computer and use it in GitHub Desktop.
Plugin for GIMP, generating spherical normal map.
"""
Copyright 2025 Slawomir Czerwinski
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
"""
Sphere Normal Map
Plugin for GIMP, generating spherical normal map.
Works best with texture sizes between 128-256.
"""
import math
from gimpfu import *
def sphere_normal_map(image, drawable, flip_x, flip_y):
image.undo_group_start()
width = pdb.gimp_image_width(image)
height = pdb.gimp_image_height(image)
layer = pdb.gimp_layer_new(image, width, height, RGB_IMAGE, "Sphere Normal Map", 100, LAYER_MODE_NORMAL)
pdb.gimp_progress_init("Generating sphere normal map...", None)
cx = width / 2.0
cy = height / 2.0
width_big_half = width / 2 + width % 2
height_big_half = height / 2 + height % 2
for y in range(height_big_half):
pdb.gimp_progress_update(y / cy * 0.99)
ry = (cy - y) / cy
ry_sq = ry * ry
for x in range(width_big_half):
rx = (cx - x) / cx
r = math.sqrt(rx * rx + ry_sq)
phi = math.atan2(ry, rx)
theta = math.asin(r) if r < 1.0 else math.pi - math.asin(2.0 - r)
sin_theta = math.sin(theta)
red = int(127 * math.cos(phi) * sin_theta)
green = int(127 * math.sin(phi) * sin_theta)
blue = int(127 * math.cos(theta))
normal_x = 128 + red if flip_x else 128 - red
normal_y = 128 + green if flip_y else 128 - green
normal_z = 128 + blue
pdb.gimp_drawable_set_pixel(layer, x, y, 3, (normal_x, normal_y, normal_z))
pdb.gimp_image_insert_layer(image, layer, None, -1)
pdb.gimp_progress_update(0.99)
pdb.gimp_context_set_feather(0)
pdb.gimp_context_set_feather_radius(0, 0)
pdb.gimp_image_select_rectangle(image, 2, 0, 0, width / 2, height_big_half)
pdb.gimp_edit_copy(layer)
floating_selection = pdb.gimp_edit_paste(layer, 0)
floating_selection = pdb.gimp_item_transform_flip_simple(floating_selection, 0, 1, 0)
floating_selection = pdb.gimp_item_transform_translate(floating_selection, width_big_half, 0)
pdb.gimp_drawable_curves_spline(floating_selection, 1, 4, [0, 254, 254, 0])
pdb.gimp_floating_sel_anchor(floating_selection)
pdb.gimp_image_select_rectangle(image, 2, 0, 0, width, height / 2)
pdb.gimp_edit_copy(layer)
floating_selection = pdb.gimp_edit_paste(layer, 0)
floating_selection = pdb.gimp_item_transform_flip_simple(floating_selection, 1, 1, 0)
floating_selection = pdb.gimp_item_transform_translate(floating_selection, 0, height_big_half)
pdb.gimp_drawable_curves_spline(floating_selection, 2, 4, [0, 254, 254, 0])
pdb.gimp_floating_sel_anchor(floating_selection)
pdb.gimp_progress_update(1)
pdb.gimp_progress_end()
pdb.gimp_displays_flush()
image.undo_group_end()
register(
"sphere_normal_map",
"Generate spherical normal map",
"Generate spherical normal map",
"Slawomir Czerwinski",
"Slawomir Czerwinski",
"2025",
"<Image>/Filters/Generic/_Sphere Normal Map",
"RGB*",
[
(PF_BOOL, "flip_x", "Flip X", False),
(PF_BOOL, "flip_y", "Flip Y", False),
],
[],
sphere_normal_map
)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment