blender.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import numpy
  2. import numpy as np
  3. import pygame
  4. BLEND_MULTIPLY = -1
  5. BLEND_SCREEN = -2
  6. BLEND_OVERLAY = -3
  7. def arr_int2float(target, blend):
  8. # 将uint8的numpy数组转为float16
  9. return target.astype(np.float16), blend.astype(np.float16)
  10. def broaden_alpha(blend_alpha):
  11. # 将[w, h]的alpha数组扩展为[w, h, 3]且介于0-1之间
  12. return numpy.tile(blend_alpha.reshape((blend_alpha.shape[0], blend_alpha.shape[1], 1)), (1, 1, 3)) / 255
  13. def normal(target, blend, blend_alpha):
  14. # 正常
  15. blend_alpha = broaden_alpha(blend_alpha)
  16. target[...] = target * (1 - blend_alpha) + blend * blend_alpha
  17. def multiply(target, blend, blend_alpha):
  18. # 正片叠底
  19. if target.shape != blend.shape:
  20. return
  21. target[...] = target * (blend + (255 - blend) * (1 - blend_alpha)) / 255
  22. def screen_color(target, blend, blend_alpha):
  23. # 滤色
  24. if target.shape != blend.shape:
  25. return
  26. target[...] = 255 - (255 - target) * (255 - blend_alpha * blend) / 255
  27. def overlay(target, blend, blend_alpha):
  28. # 叠加
  29. if target.shape != blend.shape:
  30. return target
  31. mask1 = target <= 128
  32. mask2 = target > 128
  33. target, blend = arr_int2float(target, blend)
  34. blend_alpha = broaden_alpha(blend_alpha)
  35. tmp = np.zeros(target.shape, dtype=np.uint8)
  36. tmp[mask1] = target[mask1] * (blend[mask1] + (128 - blend[mask1]) * (1 - blend_alpha[mask1])) / 128
  37. tmp[mask2] = 255 - (255 - target[mask2]) * (128 + blend_alpha[mask2] * (127 - blend[mask2])) / 128
  38. return tmp
  39. def color_overlay(target_surface, color, package_surface):
  40. # 在半透明图层上叠加纯色
  41. # target_surface 半透明层Surface
  42. # color 叠加颜色
  43. # package_surface True返回pygame.Surface False返回三维数组
  44. target_arr = pygame.surfarray.array3d(target_surface)
  45. target_alpha = pygame.surfarray.array_alpha(target_surface)
  46. color = np.array([color])
  47. color = np.reshape(color, (1, 1, 3))
  48. color = np.tile(color, (target_arr.shape[0], target_arr.shape[1], 1))
  49. alpha = np.full((target_arr.shape[0], target_arr.shape[1]), 255)
  50. colored_target = overlay(target_arr, color, alpha)
  51. if not package_surface:
  52. return colored_target
  53. else:
  54. colored_surface = pygame.surfarray.make_surface(colored_target).convert_alpha()
  55. pygame.surfarray.pixels_alpha(colored_surface)[0::, 0::] = target_alpha
  56. return colored_surface
  57. def blit(source, source_alpha, dest, blend_at, blend_mode):
  58. # surfarray 不会自动遵守 Surface 的裁剪范围,必须在像素运算前裁剪。
  59. rb = source.get_rect(topleft=blend_at)
  60. ra_b = dest.get_clip().clip(rb)
  61. if ra_b.w == 0 or ra_b.h == 0:
  62. return
  63. fg_x, fg_y = ra_b.x - rb.x, ra_b.y - rb.y
  64. fg_dx, fg_dy = fg_x + ra_b.w, fg_y + ra_b.h
  65. a3d_item = pygame.surfarray.pixels3d(source)[fg_x: fg_dx, fg_y: fg_dy]
  66. source_alpha = source_alpha[fg_x: fg_dx, fg_y: fg_dy]
  67. bg_x, bg_dx, bg_y, bg_dy = ra_b.x, ra_b.x + ra_b.w, ra_b.y, ra_b.y + ra_b.h
  68. a3d_bg = pygame.surfarray.pixels3d(dest)[bg_x: bg_dx, bg_y: bg_dy]
  69. if blend_mode == BLEND_SCREEN:
  70. screen_color(a3d_bg, a3d_item, source_alpha)
  71. elif blend_mode == BLEND_MULTIPLY:
  72. multiply(a3d_bg, a3d_item, source_alpha)
  73. elif blend_mode == BLEND_OVERLAY:
  74. a3d_new = overlay(a3d_bg, a3d_item, source_alpha)
  75. p3d_bg = pygame.surfarray.pixels3d(dest)
  76. p3d_bg[bg_x: bg_dx, bg_y: bg_dy] = a3d_new
  77. del p3d_bg