使用Shader文件
在项目根目录新建一个文件夹Shaders,新建shader文件,必须以.usf 或者.ush 结尾。
通过添加一个custom node,在code中添加代码,在Gaussian Blur中添加
1 2
   | #include "C:/UGit/Shader/CustomShaders/CustomShadersStarter/Shaders/Gaussian.usf" return 1;
   | 
 
然后在Gaussian.usf中填充shader
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
   | static const int SceneTextureId = 14; float2 TexelSize = View.ViewSizeAndInvSize.zw;             //将偏移量转换到UV空间 float2 UV = GetDefaultSceneTextureUV(Parameters, SceneTextureId);   //当前像素的UV float3 PixelSum = float3(0, 0, 0);                                  //将内核(kernel)中每一个像素的颜色累加(sum)起来。 float WeightSum = 0;                                                //将内核中每一个像素的权重累加起来。
  for (int x = -Radius; x <= Radius; x++)           //水平方向 {     for (int y = -Radius; y <= Radius; y++)       //垂直方向     {         float2 Offset = UV + float2(x, y) * TexelSize;           //计算采样像素的相对偏移量并将其转换为UV空间         float3 PixelColor = SceneTextureLookup(Offset, SceneTextureId, 0).rgb;    //经过偏移量对场景纹理进行采样         float Weight = Calculate1DGaussian(x / Radius) * Calculate1DGaussian(y / Radius);    //计算采样像素的加权。为了计算二维的高斯模糊,你须要将两个一维的高斯模糊相乘(multiply),除以(divide)radius是由于简化的高斯模糊公式的值域是[-1,1],因此须要将它们的值归一化。         PixelSum += PixelColor * Weight;          //将加权的颜色添加到PixelSum         WeightSum += Weight;                      //将权重添加给WeightSum
      } }
  return PixelSum / WeightSum;
   | 
 
