因自己需要将图片转贴图功能,用VB6写了个工具,从图片生成法线、粗糙度、置换贴图,反复测试后,基本达到需求,法线图生成的代码如下:imgpixel为灰度图像素点集合,VOL为强度,Bump为光滑度(灰度图转换那些很简单就不再提供源码了)
'转法线
Public Function Gray2Normal(imgPixel As image_Pixel, Optional VOL As Single = 0.15, Optional Bump As Single = 0) As image_Pixel
Dim w As Long, h As Long
Dim IDx As Long, IDy As Long
Dim TLx As Long, TLy As Long
Dim TX As Long, Ty As Long
Dim TRx As Long, TRy As Long
Dim Rx As Long, Ry As Long
Dim BRx As Long, BRy As Long
Dim Bx As Long, By As Long
Dim BLx As Long, BLy As Long
Dim Lx As Long, Ly As Long
Dim PTL As Integer, PT As Integer, Ptr As Integer, PR As Integer, PBR As Integer, PB As Integer, PBL As Integer, PL As Integer
Dim dRED As Integer, dGREEN As Integer
Gray2Normal = imgPixel
w = imgPixel.Width - 1
h = imgPixel.Height - 1
For IDx = 0 To w
For IDy = 0 To h
'左上
TLx = IDx - 1: TLy = h - IDy + 1
If TLx < 0 Then TLx = 0
If TLy > h Then TLy = h
'上
TX = IDx: Ty = h - IDy + 1
If Ty > h Then Ty = h
'右上
TRx = IDx + 1: TRy = h - IDy + 1
If TRx > w Then TRx = w
If TRy > h Then TRy = h
'右
Rx = IDx + 1: Ry = h - IDy
If Rx > w Then Rx = w
'右下
BRx = IDx + 1: BRy = h - IDy - 1
If BRx > w Then BRx = w
If BRy < 0 Then BRy = 0
'下
Bx = IDx: By = h - IDy - 1
If By < 0 Then By = 0
'左下
BLx = IDx - 1: BLy = h - IDy - 1
If BLx < 0 Then BLx = 0
If BLy < 0 Then BLy = 0
'左
Lx = IDx - 1: Ly = h - IDy
If Lx < 0 Then Lx = 0
PTL = imgPixel.pixel(2, TLx, TLy) '* 0.00390625
PT = imgPixel.pixel(2, TX, Ty) '* 0.00390625
Ptr = imgPixel.pixel(2, TRx, TRy) ' * 0.00390625
PR = imgPixel.pixel(2, Rx, Ry) '* 0.00390625
PBR = imgPixel.pixel(2, BRx, BRy) ' * 0.00390625
PB = imgPixel.pixel(2, Bx, By) '* 0.00390625
PBL = imgPixel.pixel(2, BLx, BLy) '* 0.00390625
PL = imgPixel.pixel(2, Lx, Ly) '* 0.00390625
PTL = PTL - PTL * Bump
PT = PT - PT * Bump
Ptr = Ptr - Ptr * Bump
PR = PR - PR * Bump
PBR = PBR - PBR * Bump
PB = PB - PB * Bump
PBL = PBL - PBL * Bump
PL = PL - PL * Bump
'soble
dRED = (-(Ptr + 2 * PR + PBR - PTL - 2 * PL - PBL)) * VOL + 128
dGREEN = ((PBL + 2 * PB + PBR - PTL - 2 * PT - Ptr)) * VOL + 128
If dRED < 0 Then dRED = 0
If dRED > 255 Then dRED = 255
If dGREEN < 0 Then dGREEN = 0
If dGREEN > 255 Then dGREEN = 255
Gray2Normal.pixel(2, IDx, h - IDy) = dRED
Gray2Normal.pixel(1, IDx, h - IDy) = dGREEN
Gray2Normal.pixel(0, IDx, h - IDy) = 255
Next
Next
End Function