Skip to content
This repository has been archived by the owner on Jul 21, 2024. It is now read-only.

Optimize calculate_disp_pixels for faster displacement calculation #329

Merged
merged 2 commits into from
Feb 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions materialengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,24 +155,22 @@ def calculate_disp_pixels(blender_image, age_factor, tone_factor, mass_factor):
logger.info('start: calculate_disp_pixels %s', blender_image.name)
tone_f = tone_factor if tone_factor > 0.0 else 0.0

ajustments = np.array([0.0, 0.5, 0.5, 0.5], dtype='float32')
factors = np.fmax(np.array([1, age_factor, tone_f, (1.0 - tone_f) * mass_factor], dtype='float32'), 0.0)
np_image = np.array(blender_image.pixels, dtype='float32').reshape(-1, 4)
adjustments = np.array([0.0, 0.5, 0.5, 0.5], dtype=np.float32)
factors = np.fmax(np.array([1, age_factor, tone_f, (1.0 - tone_f) * mass_factor], dtype=np.float32), 0.0)
np_image = np.empty(len(blender_image.pixels), dtype=np.float32)
blender_image.pixels.foreach_get(np_image)
np_image = np_image.reshape(-1, 4)
# add_result = r + age_f * (g - 0.5) + tone_f * (b - 0.5) + mass_f * (a - 0.5)
add_result = np.sum((np_image - ajustments) * factors, axis=1)
result_image = np.insert(np.repeat(np.fmin(add_result, 1.0), 3).reshape(-1, 3), 3, 1.0, axis=1)
np_image -= adjustments
np_image *= factors
add_result = np.sum(np_image, axis=1)
np.fmin(add_result, 1.0, add_result)
np_image[:,0] = add_result
np_image[:,1] = add_result
np_image[:,2] = add_result
np_image[:,3] = 1
logger.info('finish: calculate_disp_pixels %s', blender_image.name)
return result_image.flatten()

@staticmethod
def multiply_images(image1, image2, result_name, blending_factor=0.5):
logger.info('multiply_images %s', result_name)
if images_scale(image1, image2):
np_img1, np_img2 = np.array(image1.pixels, dtype='float32'), np.array(image2.pixels, dtype='float32')

result_img = new_image(result_name, image2.size)
result_img.pixels = np_img1 * np_img2 * blending_factor + (np_img1 * (1.0 - blending_factor))
logger.info('finish: multiply_images %s', result_name)
return np_image.reshape(-1)

# Link Textures to Nodes

Expand Down Expand Up @@ -309,7 +307,8 @@ def calculate_displacement_texture(self, age_factor, tone_factor, mass_factor):
return

if images_scale(disp_data_image, disp_img):
disp_img.pixels = self.calculate_disp_pixels(disp_data_image, age_factor, tone_factor, mass_factor)
new_pixels = self.calculate_disp_pixels(disp_data_image, age_factor, tone_factor, mass_factor)
disp_img.pixels.foreach_set(new_pixels)
disp_tex.image = disp_img
logger.info("Displacement calculated in %s seconds", time.time()-time1)
else:
Expand Down