简体   繁体   中英

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. 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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM