Roi*_*lia 5 shader ios swift metal
mix
OpenGL
我试图了解金属中的功能相当于什么。这是OpenGL
我试图转换的代码:
float udRoundBox( vec2 p, vec2 b, float r )
{
return length(max(abs(p)-b+r,0.0))-r;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// setup
float t = 0.2 + 0.2 * sin(mod(iTime, 2.0 * PI) - 0.5 * PI);
float iRadius = min(iResolution.x, iResolution.y) * (0.05 + t);
vec2 halfRes = 0.5 * iResolution.xy;
// compute box
float b = udRoundBox( fragCoord.xy - halfRes, halfRes, iRadius );
// colorize (red / black )
vec3 c = mix( vec3(1.0,0.0,0.0), vec3(0.0,0.0,0.0), smoothstep(0.0,1.0,b) );
fragColor = vec4( c, 1.0 );
}
Run Code Online (Sandbox Code Playgroud)
到目前为止我已经能够转换其中的一部分:
float udRoundBox( float2 p, float2 b, float r )
{
return length(max(abs(p)-b+r,0.0))-r;
}
float4 cornerRadius(sampler_h src) {
float2 greenCoord = src.coord(); // this is alreay in relative coords; no need to devide by image size
float t = 0.5;
float iRadius = min(greenCoord.x, greenCoord.y) * (t);
float2 halfRes = float2(greenCoord.x * 0.5, greenCoord.y * 0.5);
float b = udRoundBox( float2(greenCoord.x - halfRes.x, greenCoord.y - halfRes.y), halfRes, iRadius );
float3 c = mix(float3(1.0,0.0,0.0), float3(0.0,0.0,0.0), smoothstep(0.0,1.0,b) );
return float4(c, 1.0);
}
Run Code Online (Sandbox Code Playgroud)
但它正在产生绿屏。我正在尝试在视频上实现角半径,如下所示:
mix 函数是线性插值的实现,更常称为 Lerp 函数。
当您有一个值(例如 t)并且您想知道该值在特定范围内如何映射时,您可以使用线性插值。
例如,如果我有三个值:
a = 0 b = 1 且 t = 0.5
我可以调用 mix(a,b,t),结果将是 0.5。这是因为 mix 函数需要一个起始范围值、一个结束范围值和一个插值因子,因此我得到 0.5,它介于 0 和 1 之间。
查看文档 Metal 有一个进行线性插值的 mix 实现。
问题是,greenCoord
(顺便说一句,这只是您提出的另一个问题的一个很好的变量名称)是当前像素的相对坐标,与绝对输入分辨率无关。
如果您想要替换您的iResolution
,请改用src.size()
。
看来您需要以绝对(像素)单位输入坐标。destination
您可以通过向内核的输入添加参数来实现这一点,如下所示:
float4 cornerRadius(sampler src, destination dest) {
const float2 destCoord = dest.coord(); // pixel position in the output buffer in absolute coordinates
const float2 srcSize = src.size();
const float t = 0.5;
const float radius = min(srcSize.x, srcSize.y) * t;
const float2 halfRes = 0.5 * srcSize;
const float b = udRoundBox(destCoord - halfRes, halfRes, radius);
const float3 c = mix(float3(1.0,0.0,0.0), float3(0.0,0.0,0.0), smoothstep(0.0,1.0,b) );
return float4(c, 1.0);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2341 次 |
最近记录: |