游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1807|回复: 1

[转]万用法线算法原形

[复制链接]

2万

主题

2万

帖子

6万

积分

论坛元老

Rank: 8Rank: 8

积分
66489
QQ
发表于 2010-10-1 14:35:00 | 显示全部楼层 |阅读模式
作者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;

2万

主题

2万

帖子

6万

积分

论坛元老

Rank: 8Rank: 8

积分
66489
QQ
 楼主| 发表于 2010-10-1 14:38:00 | 显示全部楼层

Re: [转]万用法线算法原形

载图如下

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-6-6 11:20

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表