automated snapshot

This commit is contained in:
sumi
2025-12-17 23:54:25 -06:00
parent 66df6a29ff
commit 57ad5d0d5e
3 changed files with 141 additions and 48 deletions

33
field.go Normal file
View File

@@ -0,0 +1,33 @@
package main
import (
"github.com/ojrac/opensimplex-go"
)
type Field interface {
/**
* return a value on the range 0,1
*/
Get(x float32, y float32) float32
}
// TRANSFORM FIELDS
type ScaleField struct {
Scale float32
Field Field
}
func (f *ScaleField) Get(x, y float32) float32 {
return f.Field.Get(x / f.Scale, y / f.Scale)
}
// NOISE FIELDS
type SimplexNoiseField struct {
Noise opensimplex.Noise32
}
func (f *SimplexNoiseField) Get(x, y float32) float32 {
return f.Noise.Eval2(x, y)
}

132
main.go
View File

@@ -12,22 +12,6 @@ import (
"github.com/ojrac/opensimplex-go"
)
func clamp01(v float64) float64 {
if v < 0 {
return 0
}
if v > 1 {
return 1
}
return v
}
func GrayCurve(v, k float64) rl.Color {
v = math.Pow(clamp01(v), k) // k < 1 boosts highlights, k > 1 boosts shadows
c := uint8(v * 255)
return rl.Color{R: c, G: c, B: c, A: 255}
}
const (
screenWidth = 1400
screenHeight = 700
@@ -39,7 +23,7 @@ func main() {
os.MkdirAll(snapshotsDir, 0755)
rng := rand.New(rand.NewSource(0))
//rng := rand.New(rand.NewSource(0))
log := log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile)
@@ -57,32 +41,9 @@ func main() {
rl.GetRenderWidth(), rl.GetRenderHeight(),
)
w := rl.GetRenderWidth()
h := rl.GetRenderHeight()
sketches := make([]Sketch, 1000)
for i := range 1000 {
angles := make([]float32, 100)
noise := opensimplex.NewNormalized(int64(i))
r := 1.0
dtheta := 360.0/float64(len(angles))
for i := range len(angles) {
rad := float64(i) * dtheta * math.Pi / 180.0
x := r * math.Cos(rad)
y := r * math.Sin(rad)
angles[i] = float32(noise.Eval2(x, y) * 360.0)
}
sketches[i] =
&Worm{
position: RandRadialVec(rng, 0, 100, -180, 180),
angles: angles,
angleIndex: 0,
stepSize: 1,
renderPct: 0.80,
}
}
var camera = rl.Camera2D{
Target: rl.Vector2{X: 0, Y: 0},
Offset: rl.Vector2{X: float32(w) / 2, Y: float32(h) / 2},
@@ -90,18 +51,28 @@ func main() {
Zoom: 1.0,
}
field :=
ScaleField {
Scale: 100.0,
Field: &SimplexNoiseField {
Noise: opensimplex.NewNormalized32(0),
},
}
rng := rand.New(rand.NewSource(0))
contourSketch := NewContourSketch(rng, &field)
sketches := []Sketch {
//&FieldSketch { Field: &field },
&contourSketch,
}
rl.SetTargetFPS(60)
t0 := time.Now()
ports := MakePorts()
ports["sierpinskiArrowLength"] = Const{
V: 1200,
}
ports["sierpinskiArrowDepth"] = Const{
V: 7,
}
ports["sierpinskiArrowAngle"] = Sine{
Amp: 120,
@@ -114,7 +85,7 @@ func main() {
// begin drawing
rl.BeginDrawing()
rl.ClearBackground(rl.Black)
//rl.ClearBackground(rl.Black)
rl.BeginMode2D(camera)
t := time.Since(t0).Seconds()
@@ -125,6 +96,7 @@ func main() {
Height: int32(h),
Time: t,
Ports: ports.Eval(t),
Cam: camera,
}
/**
@@ -153,6 +125,70 @@ func main() {
rl.CloseWindow()
}
type FieldSketch struct {
Field Field
}
func (s *FieldSketch) Draw(ctx *RenderCtx) {
fmt.Printf("drawing field")
for x := range ctx.Width {
for y := range ctx.Height {
//screen := rl.Vector2 { X: float32(x) - float32(ctx.Width) / 2.0, Y: float32(y) - float32(ctx.Height) / 2.0 }
screen := rl.Vector2 { X: float32(x), Y: float32(y) }
world := rl.GetScreenToWorld2D(screen, ctx.Cam)
//fmt.Printf("screen -> %v, world -> %v\n", screen, world)
v := s.Field.Get(world.X, world.Y)
//fmt.Printf("%.3f\n", v)
clr := GrayCurve(v, 1.0)
rl.DrawPixelV(world, clr)
}
}
}
type ContourSketch struct {
field Field
actors []*Actor
}
func NewContourSketch(rng *rand.Rand, field Field) ContourSketch {
actors := make([]*Actor, 10000)
for i := range len(actors) {
actors[i] =
&Actor {
position: RandRadialVec(rng, 0, 500, 0, 360),
field: field,
}
}
return ContourSketch {
actors: actors,
}
}
func (s *ContourSketch) Draw(ctx *RenderCtx) {
for _, actor := range s.actors {
actor.Draw()
}
}
type Actor struct {
position rl.Vector2
field Field
}
func (a *Actor) Draw() {
v := a.field.Get(a.position.X, a.position.Y)
rad := rl.Remap(v, 0, 1, 0, 2 * math.Pi)
stepSize := float32(2.0)
nextPosition := rl.Vector2 { X: a.position.X + stepSize*float32(math.Cos(float64(rad))), Y: a.position.Y + stepSize*float32(math.Sin(float64(rad))) }
rl.DrawLineV(a.position, nextPosition, rl.NewColor(255, 255, 255, 25))
//fmt.Printf("position %v -> nextPosition %v \n", a.position, nextPosition)
a.position = nextPosition
}
func RandRadialVec(rng *rand.Rand, minRadius float32, maxRadius float32, loAngle float32, hiAngle float32) rl.Vector2 {
r := float64(rl.Remap(rng.Float32(), 0, 1, minRadius, maxRadius))
deg := float64(rl.Remap(rng.Float32(), 0, 1, loAngle, hiAngle))

24
utils.go Normal file
View File

@@ -0,0 +1,24 @@
package main
import (
"math"
"github.com/gen2brain/raylib-go/raylib"
)
func clamp01(v float32) float32 {
if v < 0 {
return 0
}
if v > 1 {
return 1
}
return v
}
func GrayCurve(v, k float32) rl.Color {
v = float32(math.Pow(float64(clamp01(v)), float64(k))) // k < 1 boosts highlights, k > 1 boosts shadows
c := uint8(v * 255)
return rl.Color{R: c, G: c, B: c, A: 255}
}