【发布时间】:2014-06-01 20:54:20
【问题描述】:
我们如何从 2d 纹理中猜测纹理的 3d 表示?
我们知道的唯一数据是 RGB 颜色,我不明白如何通过颜色推断某物的高度。
【问题讨论】:
-
你在说什么?
-
我在问我们如何从纹理中获取法线贴图,就像 Photoshop、gimp 等一样。我想自己做
-
您应该编辑问题以更符合您的实际问题。
我们如何从 2d 纹理中猜测纹理的 3d 表示?
我们知道的唯一数据是 RGB 颜色,我不明白如何通过颜色推断某物的高度。
【问题讨论】:
法线贴图都是关于表示表面法线的方向,所以它是一个 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;
【讨论】:
虽然位置偏移在纹理中是 2D 的,但每个值都是 3D 值。与其将这三个值视为 RGB,不如将它们视为 XYZ?它表示指向 3D 空间中某个方向的法线向量的方向和大小。假设一个 10x10 大小的纹理将有 100 个这样的法线,每个偏移量 (i, j) 都有一个突出。我希望这能给你足够的可视化。
提示:不要练习将纹理视为图像,而应将它们视为 LUT(查找表),其中值仅存储为数组。
【讨论】: