Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance 2通道法线贴图的性能成本和缺点是什么?_Performance_Fragment Shader_Stage3d_Normals_Agal - Fatal编程技术网

Performance 2通道法线贴图的性能成本和缺点是什么?

Performance 2通道法线贴图的性能成本和缺点是什么?,performance,fragment-shader,stage3d,normals,agal,Performance,Fragment Shader,Stage3d,Normals,Agal,我正在做的是使用法线贴图来照亮2D精灵。最初,我打算在图像红色通道中使用凹凸/高度贴图,并使用其他两个通道存储其他信息。 我的想法是,因为我知道法线贴图上的任何法线的z值总是在[0..1]范围内,所以我可以从蓝色通道中排除此信息,并根据x和y值的长度计算z值。 所以我试过了,到目前为止似乎还可以用,看起来和原作几乎一样 我的问题是附加说明的费用是多少?有一些额外的乘法和加法,还有两个sqrt调用 以下是使用的片段着色器: // Input > // fc0 : Color to nor

我正在做的是使用法线贴图来照亮2D精灵。最初,我打算在图像红色通道中使用凹凸/高度贴图,并使用其他两个通道存储其他信息。 我的想法是,因为我知道法线贴图上的任何法线的z值总是在[0..1]范围内,所以我可以从蓝色通道中排除此信息,并根据x和y值的长度计算z值。 所以我试过了,到目前为止似乎还可以用,看起来和原作几乎一样

我的问题是附加说明的费用是多少?有一些额外的乘法和加法,还有两个sqrt调用

以下是使用的片段着色器:

// Input >
//   fc0 : Color to normal conversion factor - <2, 1, 0, 0>
//   fc1 : Light - <lightDirection.x, lightDirection.y, lightDirection.z>
//   v0  : texture coordinates - <u, v>
//   fs0 : color texture
//   fs1 : normal map

// Sample normal map (fs1) at coordinates (v0) into temp register (ft0)
tex ft0 v0 fs1 <2d,nearest>
// Convert color values [0, 1] to normal values [-1, 1]
mul ft0 ft0 fc0.xxx
sub ft0 ft0 fc0.yyy

//
// The code below is used to calculate the z value from the
// 2D x/y normal values
// 

// <Start normal z calculation>

// (ft0) should now hold a 2D vector - first find its length
// - length = sqrt(x * x + y * y)
mul ft1.x ft0.x ft0.x // x * x
mul ft1.y ft0.y ft0.y // y * y
add ft1.z ft1.x ft1.y // (x * x + y * y)
sqt ft1.z ft1.z // ft1.z = sqrt(x * x + y * y)
// Now using the length of the 2D normal find the z value
// - z = sqrt(1 - length * length)
mul ft1.z ft1.z ft1.z // length * length
sub ft1.z fc0.y ft1.z // 1 - length * length
sqt ft1.z ft1.z // ft1.z = sqrt(1 - length * length)
// Now move the z value into temp register (ft0) along with the normal x and y value
mov ft0.z ft1.z

// <End normal z calculations>

// The rest of the shader left out
//输入>
//fc0:颜色到法线的转换因子-
//fc1:灯光-
//v0:纹理坐标-
//fs0:颜色纹理
//fs1:法线贴图
//将坐标(v0)处的法线图(fs1)采样到温度寄存器(ft0)中
特克斯ft0 v0 fs1
//将颜色值[0,1]转换为正常值[-1,1]
mul ft0 ft0 fc0.xxx
子ft0 ft0 fc0.yyy
//
//下面的代码用于根据
//二维x/y法线值
// 
// 
//(ft0)现在应保持2D向量-首先查找其长度
//-长度=sqrt(x*x+y*y)
多ft1.x ft0.x ft0.x//x*x
多ft1.y ft0.y ft0.y//y*y
添加ft1.z ft1.x ft1.y/(x*x+y*y)
sqt ft1.z ft1.z//ft1.z=sqrt(x*x+y*y)
//现在,使用二维法线的长度,找到z值
//-z=sqrt(1-长度*长度)
mul ft1.z ft1.z ft1.z//length*length
子ft1.z fc0.y ft1.z//1-长度*长度
sqt ft1.z ft1.z//ft1.z=sqrt(1-长度*长度)
//现在将z值与正常x和y值一起移动到临时寄存器(ft0)中
mov ft0.z ft1.z
// 
//着色器的其余部分被忽略

sqrt的运行速度非常慢。所以你应该尽可能避免它。实际上,法线贴图之前是凹凸贴图,只有灰度图像表示高度。在这种情况下,法线是基于neightbor像素计算的。但当硬件内存增长时,就不值得节约了。但在移动设备上

只有针对实际硬件的测试才能显示哪种方法更好。如果你想节省GPU内存,那么你可能是在瞄准移动硬件。但是,一般来说,在移动设备上,mul和sqt等操作的成本要高得多(这取决于设备)。