[英]How to use smoothstep function using renderscript in android
How can we use the smoothstep function in renderscript to smoothen a mask image (already blurred using gaussian blur with kernel size 3 or 5) and make its edges smoother. 我们如何在渲染脚本中使用smoothstep函数来平滑蒙版图像(已经使用内核大小为3或5的高斯模糊来模糊)并使其边缘更平滑。 I tried the following code in other frameworks and they worked as expected. 我在其他框架中尝试了以下代码,它们按预期工作。
iOS shader code:- iOS着色器代码:-
let kernelStr = """
kernel vec4 myColor(__sample source) {
float maskValue = smoothstep(0.3, 0.5, source.r);
return vec4(maskValue,maskValue,maskValue,1.0);
}
"""
In opengl glsl fragment shader:- 在opengl glsl片段着色器中:-
float mask = btex.r;
float maskValue = smoothstep(0.3, 0.5, mask);
vec4 ress = vec4(maskValue,maskValue,maskValue,1.0);
RenderScript doesn't have a buit-in smoothstep function, so the simplest thing is to implement it yourself. RenderScript没有内置的smoothstep函数,因此最简单的事情是自己实现。 Next the source ready to be used in your scripts: 接下来,准备在脚本中使用的源代码:
static inline float smoothstep(float edge0, float edge1, float x)
{
float value = clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
return value * value * (3.0f - 2.0f * value);
}
Example of usage: 用法示例:
static inline float smoothstep(float edge0, float edge1, float x)
{
float value = clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
return value * value * (3.0f - 2.0f * value);
}
uchar4 RS_KERNEL root(uint32_t x, uint32_t y)
{
....
float mask = btex.r;
float maskValue = smoothstep(0.3f, 0.5f, mask);
float4 ress = (float4){maskValue, maskValue, maskValue, 1.0f};
....
}
And next a link about how smoothstep internally works, in case you have any other doubts: 接下来是有关平滑步骤内部工作方式的链接,以防您有任何其他疑问:
Enjoy 请享用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.