maltadirk 发表于 2022-10-27 16:35

unity amplify shader editor custom expression 自定义贴图 ...

定义 hlsl 文件

#ifndef GAUSSIAN_BLUR_INCLUDE
#define GAUSSIAN_BLUR_INCLUDE

#define MAX_PARAMS_LENGHT 3

// 0.25, 0.5, 0.25
static const float gaussianCoreParams3 = {0.5, 0.25, 0};
static const int gaussianCoreParams3_Length = 2;

// 0.06542056, 0.2429907, 0.3831776, 0.2429907, 0.06542056
static const float gaussianCoreParams5 = {0.3831776, 0.2429907, 0.06542056};
static const int gaussianCoreParams5_Length = 3;

float4 GaussianSampleLine_Common(sampler2D tex, float2 uv, float2 uv_delta,
    const float params, const int params_length)
{
    float4 result = (float4)0;
    result += tex2D(tex, uv) * params;
    for (int i=1; i<params_length; i++)
    {
      result += tex2D(tex, uv + uv_delta*i) * params;
      result += tex2D(tex, uv - uv_delta*i) * params;
    }
    return result;
}

float4 GaussianBlur_Common(sampler2D tex, float2 uv, float2 uv_delta,
    const float params, const int params_length)
{
    float4 result = (float4)0;
    float2 delta_U = float2(uv_delta.x, 0);
    float2 delta_V = float2(0, uv_delta.y);
    result += GaussianSampleLine_Common(tex, uv, delta_U, params, params_length) * gaussianCoreParams3;
    for (int i=1; i<gaussianCoreParams3_Length; i++)
    {
      result += GaussianSampleLine_Common(tex, uv + delta_V*i, delta_U, params, params_length) * gaussianCoreParams3;
      result += GaussianSampleLine_Common(tex, uv - delta_V*i, delta_U, params, params_length) * gaussianCoreParams3;
    }
    return result;
}

float4 GaussianSampleLine_3(sampler2D tex, float2 uv, float2 uv_delta)
{
    return GaussianSampleLine_Common(tex, uv, uv_delta, gaussianCoreParams3, gaussianCoreParams3_Length);
}

float4 GaussianBlur_3x3(sampler2D tex, float2 uv, float2 uv_delta)
{
    return GaussianBlur_Common(tex, uv, uv_delta, gaussianCoreParams3, gaussianCoreParams3_Length);
}

float4 GaussianSampleLine_5(sampler2D tex, float2 uv, float2 uv_delta)
{
    return GaussianSampleLine_Common(tex, uv, uv_delta, gaussianCoreParams5, gaussianCoreParams5_Length);
}

float4 GaussianBlur_5x5(sampler2D tex, float2 uv, float2 uv_delta)
{
    return GaussianBlur_Common(tex, uv, uv_delta, gaussianCoreParams5, gaussianCoreParams5_Length);
}


#endif配置 ase custom expression 节点


[*]使用 File 模式
[*]引用 刚刚的 hlsl 文件
[*]节点改模板函数同名
[*]节点输入输出配置函数输入输出变量一致


关于编译后 GLES android 平台的shader


[*]循环展开了
[*]全局变量高斯模糊变成 12 次立即数,(每行3次,混合3行再3次,3*3+3 = 12)
[*]a 分量计算被编译器优化掉了

页: [1]
查看完整版本: unity amplify shader editor custom expression 自定义贴图 ...