【问题标题】:How normal maps work? [closed]法线贴图如何工作? [关闭]
【发布时间】:2014-06-01 20:54:20
【问题描述】:

我们如何从 2d 纹理中猜测纹理的 3d 表示?

我们知道的唯一数据是 RGB 颜色,我不明白如何通过颜色推断某物的高度。

【问题讨论】:

  • 你在说什么?
  • 我在问我们如何从纹理中获取法线贴图,就像 Photoshop、gimp 等一样。我想自己做
  • 您应该编辑问题以更符合您的实际问题。

标签: 3d textures normals


【解决方案1】:

法线贴图都是关于表示表面法线的方向,所以它是一个 3d 向量,我们使用纹理像素的 RGB 值作为向量的 XYZ 分量。 (R 代表 X,G 代表 Y,B 代表 Z)。当着色器试图确定表面如何响应灯光时,他们使用这个表面的法线和切线来计算来自表面的光反射。以下是数学的完成方式:

textureColor = shaderTextures[0].Sample(SampleType, input.tex);

// Sample the pixel in the normal map.
bumpMap = shaderTextures[1].Sample(SampleType, input.tex);

// Expand the range of the normal value from (0, +1) to (-1, +1).
bumpMap = (bumpMap * 2.0) - 1.0;

// Calculate the normal from the data in the normal map.
bumpNormal = (bumpMap.x * input.tangent) + (bumpMap.y * input.binormal) + (bumpMap.z * input.normal);

// Normalize the resulting bump normal.
bumpNormal = normalize(bumpNormal);

// Invert the light direction for calculations.
lightDir = -lightDirection;

// Calculate the amount of light on this pixel based on the normal map value.
lightIntensity = saturate(dot(bumpNormal, lightDir));

// Determine the final diffuse color based on the diffuse color and the amount of light intensity.
color = saturate(diffuseColor * lightIntensity);

// Combine the final bump light color with the texture color.
color = color * textureColor;

【讨论】:

  • 我知道法线贴图RGB代表向量XYZ。我的问题是我们如何通过算法从纹理中获取法线贴图,如下所示:img359.imageshack.us/img359/4721/develpierrecopiejk7.jpg
  • @user2591935 我们不会从纹理中“获取”法线贴图,纹理是可以表示法线贴图的光栅图像。我通过一些类似着色器的数学代码示例编辑了我的答案,该代码使用样本(光栅图像)作为法线贴图,以便在表面上创建凹凸效果。我想这会帮助你理解我的意思。
  • 你可以从一系列图像中或在特殊条件下获得法线贴图。但不是来自任何图像。
【解决方案2】:

虽然位置偏移在纹理中是 2D 的,但每个值都是 3D 值。与其将这三个值视为 RGB,不如将它们视为 XYZ?它表示指向 3D 空间中某个方向的法线向量的方向和大小。假设一个 10x10 大小的纹理将有 100 个这样的法线,每个偏移量 (i, j) 都有一个突出。我希望这能给你足够的可视化。

提示:不要练习将纹理视为图像,而应将它们视为 LUT(查找表),其中值仅存储为数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    相关资源
    最近更新 更多