yel*_*low 1 shader hlsl bump-mapping
我正在尝试创建带有凹凸贴图的着色器。
我具有对象的真实法线,并且具有凹凸贴图采样法线。我想做的是旋转采样法线,以使采样法线在实际法线方向上“向上”旋转。
我一直在从事数学工作,但似乎做得不好……Y在这个世界上崭露头角。
// Vertex shader sends this matrix to the pixel shader
OUT.normal[0] = mul((float3x3)world, normal);
float3 temptan = cross(normal, float3(0, 0, 1));
temptan = normalize(temptan);
OUT.normal[2] = mul((float3x3)world, temptan);
OUT.normal[1] = cross(OUT.normal[0], OUT.normal[2]); calculating binormal (in world space)
// Pixel Shader:
// Get normal from bump texture
float3 normal = tex2D(normalmap, texCoord);
normal = normal * 2 - 1;
// Swap Z and Y, since Z is up on the texture, but Y is up in this world.
float temp = normal[1];
normal[1] = -normal[2];
normal[2] = temp;
// Multiply the sampled normal and the normal-basis matrix together.
normal = mul(normalMat, normal);
Run Code Online (Sandbox Code Playgroud)
当您要使用法线贴图(这实际上是“凹凸贴图”的意思)时,您需要使用两个额外的向量来进行打算:切线和双法线。切线和双法线是根据“凹凸贴图”的几何法线和纹理空间计算的。
我在我对StackOverflow问题“如何计算切线和Binormal?”的回答中做了解释。
法线,切线和双法线形成一个坐标系统,可表示为3×3矩阵。该矩阵实际上是将法线贴图转换为模型局部空间所需的旋转矩阵,因此您可以将其用于照明计算。简而言之,您可以乘以由这三个向量的三脚架形成的矩阵。