|
作者QQ:一九七零九零
type
GeneratorNormalmapColorSampler = (csRed, csGreen, csBlue, csAlpha);
procedure GeneratorNormalmap(source, output: TBitmap32; SamplerMode: GeneratorNormalmapColorSampler = csRed);
var
x, y: Integer;
dY, dX: single;
nX, nY, nZ: Single;
oolen: Single;
destColor: TColor32;
function GetColorStyle(x, y: Integer): Single;
begin
case SamplerMode of
csRed: Result := (source.Pixel[x, y] and $00FF0000) shr 16;
csGreen: Result := (source.Pixel[x, y] and $0000FF00) shr 8;
csBlue: Result := source.Pixel[x, y] and $000000FF;
else Result := source.Pixel[x, y] shr 24;
end;
end;
begin
output.Clear;
output.SetSizeFrom(source);
for y := 0 to source.Height - 1 do
for x := 0 to source.Width - 1 do
begin
dY := GetColorStyle((x - 1 + source.Width) mod source.Width, (y + 1) mod source.Height) / 255.0 * -1.0;
dY := dY + GetColorStyle(x mod source.Width, (y + 1) mod source.Height) / 255.0 * -2.0;
dY := dY + GetColorStyle((x + 1) mod source.Width, (y + 1) mod source.Height) / 255.0 * -1.0;
dY := dY + GetColorStyle((x - 1 + source.Width) mod source.Width, (y - 1 + source.Height) mod source.Height) / 255.0 * 1.0;
dY := dY + GetColorStyle(x mod source.Width, (y - 1 + source.Height) mod source.Height) / 255.0 * 2.0;
dY := dY + GetColorStyle((x + 1) mod source.Width, (y - 1 + source.Height) mod source.Height) / 255.0 * 1.0;
dX := GetColorStyle((x - 1 + source.Width) mod source.Width, (y - 1 + source.Height) mod source.Height) / 255.0 * -1.0;
dX := dX + GetColorStyle((x - 1 + source.Width) mod source.Width, y mod source.Height) / 255.0 * -2.0;
dX := dX + GetColorStyle((x - 1 + source.Width) mod source.Width, (y + 1) mod source.Height) / 255.0 * -1.0;
dX := dX + GetColorStyle((x + 1) mod source.Width, (y - 1 + source.Height) mod source.Height) / 255.0 * 1.0;
dX := dX + GetColorStyle((x + 1) mod source.Width, y mod source.Height) / 255.0 * 2.0;
dX := dX + GetColorStyle((x + 1) mod source.Width, (y + 1) mod source.Height) / 255.0 * 1.0;
nX := -dX;
nY := -dY;
nZ := 1;
oolen := 1.0 / (sqrt(nX * nX + nY * nY + nZ * nZ));
nX := nX * oolen;
nY := nY * oolen;
nZ := nZ * oolen;
destColor := Color32(Trunc((nX + 1.0) / 2.0 * 255.0), Trunc((nY + 1.0) / 2.0 * 255.0), Trunc((nZ + 1.0) / 2.0 * 255.0));
output.Pixel[x, y] := destColor;
end;
end; |
|