def supportedFormat(format): if len(format) == 1: return TextureFormat.fromString(format) if len(format) == 2 and format.find("L") != -1 and format.find("A") != -1: return TextureFormat.fromString("LA") if len(format) == 3: return TextureFormat.fromString("RGB") return TextureFormat.fromString("RGBA")
def computeNormalMap(self, whiteHeightInPixels = -1.0, lowPassBump = False, scaleHeightByNz = False): if whiteHeightInPixels == -1.0: whiteHeightInPixels = max(self.width, self.height) w = self.width h = self.height self.convert(TextureFormat.fromString("RGB")) stride = len(self.format) bump = self.data normal = [w*h*[0], w*h*[0], w*h*[0], w*h*[0]] for y in range(h): for x in range(w): i = x + y*w j = stride*i def height(dx, dy): return bump[(((dx + x + w) % w) + ((dy + y + h) % h) * w) * stride] deltay = -(height(-1, -1) * 1 + height( 0, -1) * 2 + height( 1, -1) * 1 + \ height(-1, 1) * -1 + height( 0, 1) * -2 + height( 1, 1) * -1) deltax = -(height(-1, -1) * -1 + height( 1, -1) * 1 + \ height(-1, 0) * -2 + height( 1, 0) * 2 + \ height(-1, 1) * -1 + height( 1, 1) * 1) deltaz = 4 * 2 * (whiteHeightInPixels / 255.0) r = 1/sqrt(deltax*deltax + deltay*deltay + deltaz*deltaz) deltax *= r deltay *= r deltaz *= r H = bump[j] / 255.0 if lowPassBump: H = (height(-1, -1) + height( 0, -1) + height(1, -1) + \ height(-1, 0) + height( 0, 0) + height(1, 0) + \ height(-1, 1) + height( 0, 1) + height(1, 1)) / (255.0 * 9.0) if scaleHeightByNz: H *= deltaz def clamp(val, low, high): if val > high: return high elif val < low: return low else: return val normal[3][i] = int(H * 255.0) normal[0][i] = clamp(int(deltax*127.5 + 127.5),0, 255) normal[1][i] = clamp(int(deltay*127.5 + 127.5),0, 255) normal[2][i] = clamp(int(deltaz*127.5 + 127.5),0, 255) self.colors = normal self.format = "RGBA" self.convert(self.textureFormat)