1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| ... // 随机数 float random (in half2 uv) { return frac(sin(dot(uv.xy, float2(12.9898,78.233)))*43758.5453123); } float2 random2( in half2 uv ) { return frac(sin(float2(dot(uv.xy, float2(127.1,311.7)),dot(uv.xy,float2(269.5,183.3))))*43758.5453); } // 梯度化噪声 float gradientNoise (in half2 uv) { half2 iuv = floor(uv); half2 fuv = frac(uv); float a = random(iuv); float b = random(iuv + half2(1.0, 0.0)); float c = random(iuv + half2(0.0, 1.0)); float d = random(iuv + half2(1.0, 1.0)); half2 u = fuv*fuv*(3.0-2.0*fuv); return lerp(a, b, u.x) + (c - a)* u.y * (1.0 - u.x) + (d - b) * u.x * u.y; } // voronoi噪声,计算最小距离 float voronoiNoise(in half2 uv) { half2 iuv = floor(uv); half2 fuv = frac(uv); float minDist = 1.; //最小距离
for (int y= -1; y <= 1; y++) { for (int x= -1; x <= 1; x++) { // 邻居网格节点 float2 neighbor = float2(float(x),float(y)); // 在网格内从current+neighor位置来随机位置 float2 pt = random2(iuv + neighbor); // 移动特征点 pt = 0.5 + 0.5*sin(time + 6.2831*pt); // 像素到特征点的矢量 float2 diff = neighbor + pt - fuv; // 计算到特征点的距离 float dist = length(diff); // 计算最小距离 minDist = min(minDist, dist); } } // 返回最小距离 return minDist; } ...
|