Skip to content

Commit dad8886

Browse files
committed
added noise functionality
1 parent ec127e9 commit dad8886

File tree

10 files changed

+272
-6
lines changed

10 files changed

+272
-6
lines changed
File renamed without changes.
File renamed without changes.

examples/noise/config.json

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"SketchWidth": 1080,
3+
"SketchHeight": 1080,
4+
"ControlWidth": 240,
5+
"Controls": [
6+
{
7+
"Name": "cellSize",
8+
"MinVal": 1,
9+
"MaxVal": 100,
10+
"Val": 10,
11+
"Incr": 1
12+
},
13+
{
14+
"Name": "seed",
15+
"MinVal": -10000,
16+
"MaxVal": 10000,
17+
"Val": 0,
18+
"Incr": 1
19+
},
20+
{
21+
"Name": "octaves",
22+
"MinVal": 1,
23+
"MaxVal": 10,
24+
"Val": 1,
25+
"Incr": 1
26+
},
27+
{
28+
"Name": "persistence",
29+
"MinVal": 0,
30+
"MaxVal": 2,
31+
"Val": 0.9,
32+
"Incr": 0.01
33+
},
34+
{
35+
"Name": "lacunarity",
36+
"MinVal": 0,
37+
"MaxVal": 10,
38+
"Val": 2,
39+
"Incr": 0.1
40+
},
41+
{
42+
"Name": "xscale",
43+
"MinVal": 0,
44+
"MaxVal": 0.1,
45+
"Val": 0.005,
46+
"Incr": 0.001
47+
},
48+
{
49+
"Name": "yscale",
50+
"MinVal": 0,
51+
"MaxVal": 0.1,
52+
"Val": 0.005,
53+
"Incr": 0.001
54+
},
55+
{
56+
"Name": "xoffset",
57+
"MinVal": -1000,
58+
"MaxVal": 1000,
59+
"Val": 0,
60+
"Incr": 1
61+
},
62+
{
63+
"Name": "yoffset",
64+
"MinVal": -1000,
65+
"MaxVal": 1000,
66+
"Val": 0,
67+
"Incr": 1
68+
}
69+
]
70+
}

examples/noise/main.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
import (
4+
"log"
5+
6+
"github.com/aldernero/sketchy"
7+
"github.com/fogleman/gg"
8+
"github.com/hajimehoshi/ebiten/v2"
9+
"github.com/lucasb-eyer/go-colorful"
10+
)
11+
12+
func update(s *sketchy.Sketch) {
13+
s.Rand.SetSeed(int64(s.Var("seed")))
14+
s.Rand.SetNoiseOctaves(int(s.Var("octaves")))
15+
s.Rand.SetNoisePersistence(s.Var("persistence"))
16+
s.Rand.SetNoiseLacunarity(s.Var("lacunarity"))
17+
s.Rand.SetNoiseScaleX(s.Var("xscale"))
18+
s.Rand.SetNoiseScaleY(s.Var("yscale"))
19+
s.Rand.SetNoiseOffsetX(s.Var("xoffset"))
20+
s.Rand.SetNoiseOffsetY(s.Var("yoffset"))
21+
}
22+
23+
func draw(s *sketchy.Sketch, c *gg.Context) {
24+
cellSize := s.Var("cellSize")
25+
for x := 0.0; x < s.SketchWidth; x += cellSize {
26+
for y := 0.0; y < s.SketchHeight; y += cellSize {
27+
noise := s.Rand.Noise2D(x, y)
28+
hue := sketchy.Map(0, 1, 0, 360, noise)
29+
cellColor := colorful.Hsl(hue, 0.5, 0.5)
30+
c.SetColor(cellColor)
31+
c.DrawRectangle(x, y, cellSize, cellSize)
32+
c.Fill()
33+
}
34+
}
35+
}
36+
37+
func main() {
38+
s, err := sketchy.NewSketchFromFile("config.json")
39+
s.Updater = update
40+
s.Drawer = draw
41+
if err != nil {
42+
log.Fatal(err)
43+
}
44+
s.Init()
45+
ebiten.SetWindowSize(int(s.ControlWidth+s.SketchWidth), int(s.SketchHeight))
46+
ebiten.SetWindowTitle("Sketchy Noise Example")
47+
ebiten.SetWindowResizable(false)
48+
if err := ebiten.RunGame(s); err != nil {
49+
log.Fatal(err)
50+
}
51+
}
53.7 KB
Loading

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/fogleman/gg v1.3.0
77
github.com/hajimehoshi/ebiten/v2 v2.2.4
88
github.com/lucasb-eyer/go-colorful v1.2.0
9+
github.com/ojrac/opensimplex-go v1.0.2
910
)
1011

1112
require (

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ github.com/jfreymuth/oggvorbis v1.0.3/go.mod h1:1U4pqWmghcoVsCJJ4fRBKv9peUJMBHix
1919
github.com/jfreymuth/vorbis v1.0.2/go.mod h1:DoftRo4AznKnShRl1GxiTFCseHr4zR9BN3TWXyuzrqQ=
2020
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
2121
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
22+
github.com/ojrac/opensimplex-go v1.0.2 h1:l4vs0D+JCakcu5OV0kJ99oEaWJfggSc9jiLpxaWvSzs=
23+
github.com/ojrac/opensimplex-go v1.0.2/go.mod h1:NwbXFFbXcdGgIFdiA7/REME+7n/lOf1TuEbLiZYOWnM=
2224
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
2325
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
2426
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=

random.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package sketchy
2+
3+
import (
4+
"math/rand"
5+
6+
"github.com/ojrac/opensimplex-go"
7+
)
8+
9+
const (
10+
defaultScale = 0.001
11+
defaultOctaves = 1
12+
defaultPersistence = 0.9
13+
defaultLacunarity = 2.0
14+
)
15+
16+
type Rng struct {
17+
seed int64
18+
noise opensimplex.Noise
19+
octaves int
20+
persistence float64
21+
lacunarity float64
22+
xscale float64
23+
yscale float64
24+
zscale float64
25+
xoffset float64
26+
yoffset float64
27+
zoffset float64
28+
}
29+
30+
func NewRng(i int64) Rng {
31+
rand.Seed(i)
32+
return Rng{
33+
seed: i,
34+
noise: opensimplex.New(i),
35+
octaves: defaultOctaves,
36+
persistence: defaultPersistence,
37+
lacunarity: defaultLacunarity,
38+
xscale: defaultScale,
39+
yscale: defaultScale,
40+
zscale: defaultScale,
41+
xoffset: 0,
42+
yoffset: 0,
43+
zoffset: 0,
44+
}
45+
}
46+
47+
func (r *Rng) SetSeed(i int64) {
48+
rand.Seed(i)
49+
r.seed = i
50+
r.noise = opensimplex.New(i)
51+
}
52+
53+
func (r *Rng) SetNoiseScaleX(scale float64) {
54+
r.xscale = scale
55+
}
56+
57+
func (r *Rng) SetNoiseScaleY(scale float64) {
58+
r.yscale = scale
59+
}
60+
61+
func (r *Rng) SetNoiseScaleZ(scale float64) {
62+
r.zscale = scale
63+
}
64+
65+
func (r *Rng) SetNoiseOffsetX(offset float64) {
66+
r.xoffset = offset
67+
}
68+
69+
func (r *Rng) SetNoiseOffsetY(offset float64) {
70+
r.yoffset = offset
71+
}
72+
73+
func (r *Rng) SetNoiseOffsetZ(offset float64) {
74+
r.zoffset = offset
75+
}
76+
77+
func (r *Rng) SetNoiseOctaves(i int) {
78+
r.octaves = i
79+
}
80+
81+
func (r *Rng) SetNoisePersistence(p float64) {
82+
r.persistence = p
83+
}
84+
85+
func (r *Rng) SetNoiseLacunarity(l float64) {
86+
r.lacunarity = l
87+
}
88+
89+
func (r *Rng) Noise2D(x float64, y float64) float64 {
90+
return r.calcNoise(x, y, 0)
91+
}
92+
93+
func (r *Rng) Noise3D(x float64, y float64, z float64) float64 {
94+
return r.calcNoise(x, y, z)
95+
}
96+
97+
func (r *Rng) calcNoise(x, y, z float64) float64 {
98+
totalNoise := 0.0
99+
totalAmp := 0.0
100+
amp := 1.0
101+
freq := 1.0
102+
for i := 0; i < r.octaves; i++ {
103+
totalNoise += r.noise.Eval3(
104+
(x+r.xoffset)*r.xscale*freq,
105+
(y+r.yoffset)*r.yscale*freq,
106+
(z+r.zoffset)*r.zscale*freq,
107+
)
108+
totalAmp += amp
109+
amp *= r.persistence
110+
freq *= r.lacunarity
111+
}
112+
return totalNoise / totalAmp
113+
}

sketch.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package sketchy
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"image/color"
67
"log"
78
"os"
89

910
"github.com/fogleman/gg"
1011
"github.com/hajimehoshi/ebiten/v2"
12+
"github.com/hajimehoshi/ebiten/v2/inpututil"
1113
"github.com/lucasb-eyer/go-colorful"
1214
)
1315

@@ -35,6 +37,8 @@ type Sketch struct {
3537
Updater SketchUpdater
3638
Drawer SketchDrawer
3739
controlMap map[string]int
40+
Rand Rng
41+
isSavingPNG bool
3842
}
3943

4044
func NewSketchFromFile(fname string) (*Sketch, error) {
@@ -51,7 +55,8 @@ func NewSketchFromFile(fname string) (*Sketch, error) {
5155
}
5256

5357
func (s *Sketch) Init() {
54-
s.buildMap()
58+
s.buildMaps()
59+
s.Rand = NewRng(0)
5560
ctx := gg.NewContext(int(s.ControlWidth), int(s.SketchHeight))
5661
s.PlaceControls(s.ControlWidth, s.SketchHeight, ctx)
5762
for i := range s.Controls {
@@ -123,6 +128,12 @@ func (s *Sketch) Var(name string) float64 {
123128
}
124129

125130
func (s *Sketch) UpdateControls() {
131+
if inpututil.IsKeyJustReleased(ebiten.KeyS) {
132+
s.isSavingPNG = true
133+
}
134+
if inpututil.IsKeyJustReleased(ebiten.KeyC) {
135+
s.saveConfig()
136+
}
126137
for i := range s.Controls {
127138
s.Controls[i].CheckAndUpdate()
128139
}
@@ -167,7 +178,6 @@ func (s *Sketch) Update() error {
167178
func (s *Sketch) Draw(screen *ebiten.Image) {
168179
W := int(s.ControlWidth + s.SketchWidth)
169180
H := int(s.SketchHeight)
170-
//screen.Fill(color.White)
171181
cc := gg.NewContext(W, H)
172182
cc.DrawRectangle(0, 0, s.ControlWidth, s.SketchHeight)
173183
cc.SetColor(color.White)
@@ -180,17 +190,28 @@ func (s *Sketch) Draw(screen *ebiten.Image) {
180190
cc.DrawRectangle(s.ControlWidth+2.5, 2.5, s.SketchWidth-5, s.SketchHeight-5)
181191
cc.Stroke()
182192
screen.DrawImage(ebiten.NewImageFromImage(cc.Image()), nil)
183-
ctx := gg.NewContext(W, H)
193+
ctx := gg.NewContext(int(s.SketchWidth), H)
184194
ctx.Push()
185-
ctx.Translate(s.ControlWidth, 0)
186195
s.Drawer(s, ctx)
187196
ctx.Pop()
188-
screen.DrawImage(ebiten.NewImageFromImage(ctx.Image()), nil)
197+
if s.isSavingPNG {
198+
fmt.Println("saving image")
199+
fname := "test_" + GetTimestampString() + ".png"
200+
ctx.SavePNG(fname)
201+
s.isSavingPNG = false
202+
}
203+
op := &ebiten.DrawImageOptions{}
204+
op.GeoM.Translate(s.ControlWidth, 0)
205+
screen.DrawImage(ebiten.NewImageFromImage(ctx.Image()), op)
189206
}
190207

191-
func (s *Sketch) buildMap() {
208+
func (s *Sketch) buildMaps() {
192209
s.controlMap = make(map[string]int)
193210
for i := range s.Controls {
194211
s.controlMap[s.Controls[i].Name] = i
195212
}
196213
}
214+
215+
func (s *Sketch) saveConfig() {
216+
217+
}

util.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sketchy
22

33
import (
4+
"fmt"
45
"math"
56
"math/rand"
67
"time"
@@ -85,3 +86,10 @@ func Shuffle(p *[]Point) {
8586
(*p)[j], (*p)[k] = (*p)[k], (*p)[j]
8687
}
8788
}
89+
90+
func GetTimestampString() string {
91+
now := time.Now()
92+
return fmt.Sprintf("%d%02d%02d_%02d%02d%02d",
93+
now.Year(), now.Month(), now.Day(), now.Hour(),
94+
now.Minute(), now.Second())
95+
}

0 commit comments

Comments
 (0)