Line data Source code
1 : package overworld
2 :
3 : import (
4 : "github.com/kijimaD/ruins/internal/consts"
5 : )
6 :
7 : // 草の茂みの濃淡を決める定数
8 : const (
9 : // grassPatchCell は茂みの大きさを決める格子の間隔。ノイズの値をこの間隔の格子点に置き、間を補間する
10 : grassPatchCell consts.Tile = 8
11 : // grassNoiseChannel は茂みのノイズを他の抽選と無相関にするハッシュチャネル
12 : grassNoiseChannel uint64 = 0x67726173735f6e7a // "grass_nz"
13 : )
14 :
15 : // grassPatchFactor は絶対タイル座標 p の草の密度倍率を返す。値ノイズ n の 3n² で、平均はおよそ0.9
16 : func grassPatchFactor(seed uint64, p consts.Coord[consts.Tile]) float64 {
17 1 : cx, cy := consts.FloorDiv(p.X, grassPatchCell), consts.FloorDiv(p.Y, grassPatchCell)
18 1 : fx := smoothstep(float64(p.X-cx*grassPatchCell) / float64(grassPatchCell))
19 1 : fy := smoothstep(float64(p.Y-cy*grassPatchCell) / float64(grassPatchCell))
20 1 : corner := func(dx, dy consts.Tile) float64 {
21 1 : return consts.HashRoll(hashTileCoord(seed, consts.Coord[consts.Tile]{X: cx + dx, Y: cy + dy})).Float64()
22 1 : }
23 1 : top := lerp(corner(0, 0), corner(1, 0), fx)
24 1 : bottom := lerp(corner(0, 1), corner(1, 1), fx)
25 1 : n := lerp(top, bottom, fy)
26 1 : return 3 * n * n
27 : }
28 :
29 : // placesGrass は絶対タイル座標 p に草を置くかを、密度 density と茂みの倍率から決める
30 : func placesGrass(seed uint64, density float64, p consts.Coord[consts.Tile]) bool {
31 1 : return consts.HashRoll(hashTileCoord(seed, p)).Float64() < density*grassPatchFactor(seed^grassNoiseChannel, p)
32 1 : }
33 :
34 : func lerp(a, b, t float64) float64 {
35 1 : return a + (b-a)*t
36 1 : }
37 :
38 : func smoothstep(t float64) float64 {
39 1 : return t * t * (3 - 2*t)
40 1 : }
|