0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Unity】stepからsmoothstepに置き換えができると思ったらできなかった

Posted at

はじめに

Shaderでstepを使用している処理で単純な0、1ではなくフェードをさせたくなり、stepならsmoothstepへの置き換えができると思い実装を行いました。

しかし、stepの第二引数とsmoothstepの第三引数を0にした場合の挙動が違っておりバグりました。
どうしてこうなったかの分析になります。

Step

step(0.0, 0.0);

image.png
全部0の場合は1.0が返ってきます。

step(0.0, 1.0);

image.png
一応第二引数が1の場合は1.0が来ます。(0.1でも0.5でも1.0が返却されます)

float step(float a, float x)
{
  return x >= a;
}

これはstepがa以上かを判断してるためこうなります。

Smoothstep

smoothstep(0.0, 0.0, 0.0);

image.png
この場合は未定義となり表示されません。(多分0が返ってきます。)

smoothstep(0.0, 0.0, 1.0);

image.png

一応第三引数が1の場合は1.0が来ます。(0.1でも0.5でも1.0が返却されます)

これはsmoothstepでは内部で除算をしており、0 / 0が発生し、未定義となりおそらく0が返ってくるからです。

float smoothstep(float a, float b, float x)
{
    float t = saturate((x - a)/(b - a));
    return t*t*(3.0 - (2.0*t));
}

さいごに

stepの処理をsmoothstepに置き換える際、step(0.0, 0.0)smoothstep(0.0, 0.0, 0.0)にできないのでifなどを使用して分岐が必要そうです

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?