1

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);
hippietrail
  • 15,848
  • 18
  • 99
  • 158
anilsathyan7
  • 1,423
  • 17
  • 25

1 Answers1

3

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

PerracoLabs
  • 16,449
  • 15
  • 74
  • 127
  • However ,in the following links they do mention about smoothstep functions.. May be they are not currently implemented in rs – anilsathyan7 Mar 27 '19 at 19:44
  • The link in the answer is not related to RenderScript, is just a link about information about how the general smoothstep function works, in case you want to write your own function. And as stated in the answer, RenderScript doesn't have a smoothstep function, so you can use the one I put in the answer, which is a smoothstep function written 100% in RenderScript. – PerracoLabs Mar 27 '19 at 21:06