Skip to content

Instantly share code, notes, and snippets.

@mao-test-h
Created April 19, 2018 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mao-test-h/b6974cf60255cf5ef1044e4807a502b1 to your computer and use it in GitHub Desktop.
Save mao-test-h/b6974cf60255cf5ef1044e4807a502b1 to your computer and use it in GitHub Desktop.
ShaderLabでのWorleyNoiseの実装
// 参考サイト
// ・セルラーノイズ
// https://thebookofshaders.com/12/?lan=jp
Shader "Custom/WorleyNoise"
{
CGINCLUDE
#include "UnityCG.cginc"
float2 random2(fixed2 st)
{
st = fixed2(dot(st, fixed2(127.1, 311.7)),
dot(st, fixed2(269.5, 183.3)));
return -1.0 + 2.0 * frac(sin(st) * 43758.5453123);
}
float4 worley_noise(float2 st)
{
// セルのサイズ
const float Size = 16;
st *= Size;
// 出力色
float3 color = float3(0, 0, 0);
// セルの間隔
float2 i_st = floor(st);
float2 f_st = frac(st);
// 最小距離の記録用
float dist = 1.0;
for(int y = -1; y <= 1; y++)
{
for(int x = -1; x <= 1; x++)
{
// 隣接するセル
float2 neighbor = float2(x, y);
// 隣接するセルの中のランダムな位置を取得(位置はアニメーションさせる)
float2 p = 0.5 + 0.5 * sin(_Time.w + 6.2831 * random2(i_st + neighbor));
// 点までの距離を計算して、最も近いものまでの距離をdistに記録
dist = min(dist, length(neighbor + p - f_st));
}
}
// 最小距離を描画
color += dist;
// セルの中央を描画
color += 1.0 - step(0.02, dist);
// グリッドを描画
//color.x += step(0.98, f_st.x) + step(0.98, f_st.y);
return float4(color, 1.0);
}
float4 frag(v2f_img i) : SV_Target
{
return worley_noise(i.uv);
}
ENDCG
SubShader
{
Cull Off
ZWrite Off
Pass
{
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment