# 【晶格噪声】
程序化生成晶格噪声,可以控制方格程度和模糊程度。
VoronoiseShader.shader
Shader "Unlit/VoronoiseShader"
{
Properties
{
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
float3 hash3( float2 p )
{
float3 q = float3( dot(p,float2(127.1,311.7)),
dot(p,float2(269.5,183.3)),
dot(p,float2(419.2,371.9)) );
return frac(sin(q)*43758.5453);
}
// u: 0为方块 1为晶格
// v: 0~1的模糊度
float voronoise(float2 p, float u, float v )
{
float k = 1.0+63.0*pow(1.0-v,6.0);
float2 i = floor(p);
float2 f = frac(p);
float2 a = float2(0.0,0.0);
for( int y=-2; y<=2; y++ )
for( int x=-2; x<=2; x++ )
{
float2 g = float2( x, y );
float3 o = hash3( i + g )*float3(u,u,1.0);
float2 d = g - f + o.xy;
float w = pow( 1.0-smoothstep(0.0,1.414,length(d)), k );
a += float2(o.z*w,w);
}
return a.x/a.y;
}
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float2 p = 0.5 - 0.5 * cos( _Time.y + float2(0.0,2.0) );
float f = voronoise( 24.0*i.uv, p.x, p.y );
return f;
}
ENDCG
}
}
}
← 【CG与WebGl差异】 【多边形填充】 →