update lights example code

This commit is contained in:
faiface 2017-04-15 14:09:06 +02:00
parent 0d95083651
commit 2669441ee5
1 changed files with 33 additions and 29 deletions

View File

@ -44,7 +44,7 @@ type colorlight struct {
imd *imdraw.IMDraw imd *imdraw.IMDraw
} }
func (cl *colorlight) apply(src, noise drawer, tmp, dst *pixelgl.Canvas) { func (cl *colorlight) apply(src, noise drawer, dst pixel.ComposeTarget) {
// create the light arc if not created already // create the light arc if not created already
if cl.imd == nil { if cl.imd == nil {
imd := imdraw.New(nil) imd := imdraw.New(nil)
@ -59,32 +59,26 @@ func (cl *colorlight) apply(src, noise drawer, tmp, dst *pixelgl.Canvas) {
} }
// draw the light arc // draw the light arc
tmp.Clear(pixel.Alpha(0)) dst.SetMatrix(pixel.IM.Scaled(0, cl.radius).Rotated(0, cl.angle).Moved(cl.point))
tmp.SetMatrix(pixel.IM.Scaled(0, cl.radius).Rotated(0, cl.angle).Moved(cl.point)) dst.SetColorMask(pixel.Alpha(1))
tmp.SetColorMask(pixel.Alpha(1)) dst.SetComposeMethod(pixel.ComposeCopy)
tmp.SetComposeMethod(pixel.ComposeCopy) cl.imd.Draw(dst)
cl.imd.Draw(tmp)
// draw the noise inside the light // draw the noise inside the light
tmp.SetMatrix(pixel.IM) dst.SetMatrix(pixel.IM)
tmp.SetComposeMethod(pixel.ComposeIn) dst.SetComposeMethod(pixel.ComposeIn)
noise.Draw(tmp) noise.Draw(dst)
// draw an image inside the noisy light // draw an image inside the noisy light
tmp.SetColorMask(cl.color) dst.SetColorMask(cl.color)
tmp.SetComposeMethod(pixel.ComposeIn) dst.SetComposeMethod(pixel.ComposeIn)
src.Draw(tmp) src.Draw(dst)
// draw the light reflected from the dust // draw the light reflected from the dust
tmp.SetMatrix(pixel.IM.Scaled(0, cl.radius).Rotated(0, cl.angle).Moved(cl.point)) dst.SetMatrix(pixel.IM.Scaled(0, cl.radius).Rotated(0, cl.angle).Moved(cl.point))
tmp.SetColorMask(cl.color.Mul(pixel.Alpha(cl.dust))) dst.SetColorMask(cl.color.Mul(pixel.Alpha(cl.dust)))
tmp.SetComposeMethod(pixel.ComposeOver) dst.SetComposeMethod(pixel.ComposeOver)
cl.imd.Draw(tmp) cl.imd.Draw(dst)
// draw the result to the dst
dst.SetColorMask(pixel.Alpha(1))
dst.SetComposeMethod(pixel.ComposePlus)
tmp.Draw(dst)
} }
func run() { func run() {
@ -147,8 +141,8 @@ func run() {
speed := []float64{11.0 / 23, 13.0 / 23, 17.0 / 23, 19.0 / 23} speed := []float64{11.0 / 23, 13.0 / 23, 17.0 / 23, 19.0 / 23}
tmp := pixelgl.NewCanvas(win.Bounds()) oneLight := pixelgl.NewCanvas(win.Bounds())
dst := pixelgl.NewCanvas(win.Bounds()) allLight := pixelgl.NewCanvas(win.Bounds())
var ( var (
frames = 0 frames = 0
@ -181,17 +175,27 @@ func run() {
} }
win.Clear(pixel.RGB(0, 0, 0)) win.Clear(pixel.RGB(0, 0, 0))
dst.Clear(pixel.Alpha(0))
dst.SetColorMask(pixel.Alpha(0.4)) // draw the panda visible outside the light
dst.SetComposeMethod(pixel.ComposeOver) win.SetColorMask(pixel.Alpha(0.4))
panda.Draw(dst) win.SetComposeMethod(pixel.ComposeOver)
panda.Draw(win)
allLight.Clear(pixel.Alpha(0))
allLight.SetComposeMethod(pixel.ComposePlus)
// accumulate all the lights
for i := range lights { for i := range lights {
lights[i].apply(panda, noise, tmp, dst) oneLight.Clear(pixel.Alpha(0))
lights[i].apply(panda, noise, oneLight)
oneLight.Draw(allLight)
} }
dst.Draw(win) // compose the final result
win.SetColorMask(pixel.Alpha(1))
win.SetComposeMethod(pixel.ComposePlus)
allLight.Draw(win)
win.Update() win.Update()
<-fps30 // maintain 30 fps, because my computer couldn't handle 60 here <-fps30 // maintain 30 fps, because my computer couldn't handle 60 here