How to use smoothstep function using renderscript in android

351 Views Asked by At

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. I tried the following code in other frameworks and they worked as expected.

iOS shader code:-

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:-

    float mask = btex.r;
    float maskValue = smoothstep(0.3, 0.5, mask);
    vec4 ress = vec4(maskValue,maskValue,maskValue,1.0);
1

There are 1 best solutions below

2
Perraco On

RenderScript doesn't have a buit-in smoothstep function, so the simplest thing is to implement it yourself. 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:

smoothstep

Enjoy