看啥推荐读物
专栏名称: 暴走的TA
会扫地的TA
目录
相关文章推荐
今天看啥  ›  专栏  ›  暴走的TA

UnitTips 之 皮肤预积分图生成py代码

暴走的TA  · 简书  ·  · 2021-04-03 08:37

简介: 近期在做皮肤效果,正好网上找到了一段 生成与积分图的代码,留作备份
版本: 和引擎无关,使用python 如果是python3 PIL 库安装时需要使用 pip install pillow

from PIL import Image,ImageDraw,ImageFont,ImageFilter
from numpy import *
import time
import random
import math

time_start = time.time()
width = 100
height = 100

A = 0.15
B = 0.50
C = 0.10
D = 0.20
E = 0.02
F = 0.30
W = 11.2

def Tonemap(x):
    x = array(x)
    return ((x * ( A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E/F

def G1(Neg_r_2, v):
    return math.exp(Neg_r_2)

def G2(Neg_r_2, v):
    v2 = 2.0 * v
    return 1.0/(v2 * math.pi) * math.exp(Neg_r_2/v2)

def Cal(distance,G):
    Neg_r_2 = -distance*distance
    rgb = array([0.233,0.455,0.649]) * G(Neg_r_2 , 0.0064)+\
          array([0.100,0.336,0.344]) * G(Neg_r_2 , 0.0484)+\
          array([0.118,0.198,0.000]) * G(Neg_r_2 , 0.1870)+\
          array([0.113,0.007,0.007]) * G(Neg_r_2 , 0.5670)+\
          array([0.358,0.004,0.000]) * G(Neg_r_2 , 1.9900)+\
          array([0.078,0.000,0.000]) * G(Neg_r_2 , 7.4100)
    return rgb

def IntegrateDiffuseScatteringOnRing(uvx,Radius):
    #theta = math.acos(uvx)
    theta = math.pi * (1 - uvx)
    x = -math.pi
    totalWeights = array([0.0,0.0,0.0])
    totalLight = array([0.0,0.0,0.0])
    while x <= math.pi:
        sampleAngle = theta + x
        sampleDist = abs(2.0 * Radius * math.sin(x*0.5))
        diffuse = max(math.cos(theta + x),0.0)
        weight = Cal(sampleDist,G2)
        totalLight += weight * diffuse
        totalWeights += weight
        x += 0.1
    rgb = totalLight/totalWeights
##ToneMapping部分
    rgb *= 32.0
    rgb = Tonemap(rgb)
    whiteScale = 1.0/Tonemap(array([W,W,W]))
    rgb *= whiteScale
    rgb = pow(rgb, 1/2.2)
    rgb = multiply(rgb,array([255,255,255]))
    return (int(rgb[0]),int(rgb[1]),int(rgb[2]))

image = Image.new('RGB',(width,height),(255,255,255))
draw = ImageDraw.Draw(image)
for y in range(height):
    uvy = float(y)/float(height)
    if(uvy < 0.01):
        Radius = 100;
    else:
        Radius = 1.0/uvy
    for x in range(width):
        uvx = float(x)/float(width)
        RGB = IntegrateDiffuseScatteringOnRing(uvx,Radius)
        draw.point((x,height - y -1),fill = RGB)
image.filter = ImageFilter.BLUR
image.save('color.png','PNG')
time_end=time.time()
print('totally cost',time_end-time_start)
  • 由于是 python 注意空格问题,空行不对齐 代码可能无法正常运行
  • 生成的和正常使用的图多少有些差异,不过逻辑应该差不多,这代码就留个备忘



原文地址:访问原文地址
快照地址: 访问文章快照