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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
|
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build example
// +build example
//
// This build tag means that "go install golang.org/x/exp/shiny/..." doesn't
// install this example program. Use "go run main.go" to run it or "go install
// -tags=example" to install it.
// Fluid is a fluid dynamics simulator. It is based on Jos Stam, "Real-Time
// Fluid Dynamics for Games", Proceedings of the Game Developer Conference,
// March 2003. See
// http://www.dgp.toronto.edu/people/stam/reality/Research/pub.html
package main
import (
"image"
"image/color"
"image/draw"
"log"
"sync"
"time"
"golang.org/x/exp/shiny/driver"
"golang.org/x/exp/shiny/screen"
"golang.org/x/mobile/event/lifecycle"
"golang.org/x/mobile/event/mouse"
"golang.org/x/mobile/event/paint"
"golang.org/x/mobile/event/size"
)
const (
N = 128 // The grid of cells has size NxN.
tickDuration = time.Second / 60
// These remaining numbers have magic values, determined by trial and error
// to look good, rather than being derived from first principles.
iterations = 20
dt = 0.1
diff = 0
visc = 0
force = 5
source = 20
fade = 0.89
)
func main() {
driver.Main(func(s screen.Screen) {
w, err := s.NewWindow(&screen.NewWindowOptions{
Title: "Fluid Shiny Example",
})
if err != nil {
log.Fatal(err)
}
buf, tex := screen.Buffer(nil), screen.Texture(nil)
defer func() {
if buf != nil {
tex.Release()
buf.Release()
}
w.Release()
}()
go simulate(w)
var (
buttonDown bool
sz size.Event
)
for {
publish := false
switch e := w.NextEvent().(type) {
case lifecycle.Event:
if e.To == lifecycle.StageDead {
return
}
switch e.Crosses(lifecycle.StageVisible) {
case lifecycle.CrossOn:
pauseChan <- play
var err error
buf, err = s.NewBuffer(image.Point{N, N})
if err != nil {
log.Fatal(err)
}
tex, err = s.NewTexture(image.Point{N, N})
if err != nil {
log.Fatal(err)
}
tex.Fill(tex.Bounds(), color.White, draw.Src)
case lifecycle.CrossOff:
pauseChan <- pause
tex.Release()
tex = nil
buf.Release()
buf = nil
}
case mouse.Event:
if e.Button == mouse.ButtonLeft {
buttonDown = e.Direction == mouse.DirPress
}
if !buttonDown {
break
}
z := sz.Size()
x := int(e.X) * N / z.X
y := int(e.Y) * N / z.Y
if x < 0 || N <= x || y < 0 || N <= y {
break
}
shared.mu.Lock()
shared.mouseEvents = append(shared.mouseEvents, image.Point{x, y})
shared.mu.Unlock()
case paint.Event:
publish = buf != nil
case size.Event:
sz = e
case uploadEvent:
shared.mu.Lock()
if buf != nil {
copy(buf.RGBA().Pix, shared.pix)
publish = true
}
shared.uploadEventSent = false
shared.mu.Unlock()
if publish {
tex.Upload(image.Point{}, buf, buf.Bounds())
}
case error:
log.Print(e)
}
if publish {
w.Scale(sz.Bounds(), tex, tex.Bounds(), draw.Src, nil)
w.Publish()
}
}
})
}
const (
pause = false
play = true
)
// pauseChan lets the UI event goroutine pause and play the CPU-intensive
// simulation goroutine depending on whether the window is visible (e.g.
// minimized). 64 should be large enough, in typical use, so that the former
// doesn't ever block on the latter.
var pauseChan = make(chan bool, 64)
// uploadEvent signals that the shared pix slice should be uploaded to the
// screen.Texture via the screen.Buffer.
type uploadEvent struct{}
var shared = struct {
mu sync.Mutex
uploadEventSent bool
mouseEvents []image.Point
pix []byte
}{
pix: make([]byte, 4*N*N),
}
func simulate(q screen.EventDeque) {
var (
dens, densPrev array
u, uPrev array
v, vPrev array
xPrev, yPrev int
havePrevLoc bool
)
ticker := time.NewTicker(tickDuration)
var tickerC <-chan time.Time
for {
select {
case p := <-pauseChan:
if p == pause {
tickerC = nil
} else {
tickerC = ticker.C
}
continue
case <-tickerC:
}
shared.mu.Lock()
for _, p := range shared.mouseEvents {
dens[p.X+1][p.Y] = source
if havePrevLoc {
u[p.X+1][p.Y+1] = force * float32(p.X-xPrev)
v[p.X+1][p.Y+1] = force * float32(p.Y-yPrev)
}
xPrev, yPrev, havePrevLoc = p.X, p.Y, true
}
shared.mouseEvents = shared.mouseEvents[:0]
shared.mu.Unlock()
velStep(&u, &v, &uPrev, &vPrev)
densStep(&dens, &densPrev, &u, &v)
// This fade isn't part of Stam's GDC03 paper, but it looks nice.
for i := range dens {
for j := range dens[i] {
dens[i][j] *= fade
}
}
shared.mu.Lock()
for y := 0; y < N; y++ {
for x := 0; x < N; x++ {
d := int32(dens[x+1][y+1] * 0xff)
if d < 0 {
d = 0
} else if d > 0xff {
d = 0xff
}
v := 255 - uint8(d)
p := (N*y + x) * 4
shared.pix[p+0] = v
shared.pix[p+1] = v
shared.pix[p+2] = v
shared.pix[p+3] = 0xff
}
}
uploadEventSent := shared.uploadEventSent
shared.uploadEventSent = true
shared.mu.Unlock()
if !uploadEventSent {
q.Send(uploadEvent{})
}
}
}
// All of the remaining code more or less comes from Stam's GDC03 paper.
type array [N + 2][N + 2]float32
func addSource(x, s *array) {
for i := range x {
for j := range x[i] {
x[i][j] += dt * s[i][j]
}
}
}
func setBnd(b int, x *array) {
switch b {
case 0:
for i := 1; i <= N; i++ {
x[0+0][i] = +x[1][i]
x[N+1][i] = +x[N][i]
x[i][0+0] = +x[i][1]
x[i][N+1] = +x[i][N]
}
case 1:
for i := 1; i <= N; i++ {
x[0+0][i] = -x[1][i]
x[N+1][i] = -x[N][i]
x[i][0+0] = +x[i][1]
x[i][N+1] = +x[i][N]
}
case 2:
for i := 1; i <= N; i++ {
x[0+0][i] = +x[1][i]
x[N+1][i] = +x[N][i]
x[i][0+0] = -x[i][1]
x[i][N+1] = -x[i][N]
}
}
x[0+0][0+0] = 0.5 * (x[1][0+0] + x[0+0][1])
x[0+0][N+1] = 0.5 * (x[1][N+1] + x[0+0][N])
x[N+1][0+0] = 0.5 * (x[N][0+0] + x[N+1][1])
x[N+1][N+1] = 0.5 * (x[N][N+1] + x[N+1][N])
}
func linSolve(b int, x, x0 *array, a, c float32) {
// This if block isn't part of Stam's GDC03 paper, but it's a nice
// optimization when the diff diffusion parameter is zero.
if a == 0 && c == 1 {
for i := 1; i <= N; i++ {
for j := 1; j <= N; j++ {
x[i][j] = x0[i][j]
}
}
setBnd(b, x)
return
}
invC := 1 / c
for k := 0; k < iterations; k++ {
for i := 1; i <= N; i++ {
for j := 1; j <= N; j++ {
x[i][j] = (x0[i][j] + a*(x[i-1][j]+x[i+1][j]+x[i][j-1]+x[i][j+1])) * invC
}
}
setBnd(b, x)
}
}
func diffuse(b int, x, x0 *array, diff float32) {
a := dt * diff * N * N
linSolve(b, x, x0, a, 1+4*a)
}
func advect(b int, d, d0, u, v *array) {
const dt0 = dt * N
for i := 1; i <= N; i++ {
for j := 1; j <= N; j++ {
x := float32(i) - dt0*u[i][j]
if x < 0.5 {
x = 0.5
}
if x > N+0.5 {
x = N + 0.5
}
i0 := int(x)
i1 := i0 + 1
y := float32(j) - dt0*v[i][j]
if y < 0.5 {
y = 0.5
}
if y > N+0.5 {
y = N + 0.5
}
j0 := int(y)
j1 := j0 + 1
s1 := x - float32(i0)
s0 := 1 - s1
t1 := y - float32(j0)
t0 := 1 - t1
d[i][j] = s0*(t0*d0[i0][j0]+t1*d0[i0][j1]) + s1*(t0*d0[i1][j0]+t1*d0[i1][j1])
}
}
setBnd(b, d)
}
func project(u, v, p, div *array) {
for i := 1; i <= N; i++ {
for j := 1; j <= N; j++ {
div[i][j] = (u[i+1][j] - u[i-1][j] + v[i][j+1] - v[i][j-1]) / (-2 * N)
p[i][j] = 0
}
}
setBnd(0, div)
setBnd(0, p)
linSolve(0, p, div, 1, 4)
for i := 1; i <= N; i++ {
for j := 1; j <= N; j++ {
u[i][j] -= (N / 2) * (p[i+1][j+0] - p[i-1][j+0])
v[i][j] -= (N / 2) * (p[i+0][j+1] - p[i+0][j-1])
}
}
setBnd(1, u)
setBnd(2, v)
}
func velStep(u, v, u0, v0 *array) {
addSource(u, u0)
addSource(v, v0)
u0, u = u, u0
diffuse(1, u, u0, visc)
v0, v = v, v0
diffuse(2, v, v0, visc)
project(u, v, u0, v0)
u0, u = u, u0
v0, v = v, v0
advect(1, u, u0, u0, v0)
advect(2, v, v0, u0, v0)
project(u, v, u0, v0)
}
func densStep(x, x0, u, v *array) {
addSource(x, x0)
x0, x = x, x0
diffuse(0, x, x0, diff)
x0, x = x, x0
advect(0, x, x0, u, v)
}
|