shader progress

This commit is contained in:
Josh Deprez 2021-09-09 12:13:24 +10:00
parent 74f72d06a6
commit 9d16134368

View file

@ -1,6 +1,7 @@
package engine package engine
import ( import (
"image"
"log" "log"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
@ -9,6 +10,21 @@ import (
const screenShaderSrc = `package main const screenShaderSrc = `package main
func Fragment(position vec4, texCoord vec2, color vec4) vec4 { func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
// Image 0 is the image being drawn.
// Image 1 is the depth map of the image being drawn.
// Image 2 is the depth buffer.
// Convert the colours from the depth map and depth buffer into scalars.
stairs := vec4(1, 256, 65536, 16777216)
c1 := imageSrc1UnsafeAt(texCoord)
c2 := imageSrc2UnsafeAt(texCoord)
d1 := dot(c1, stairs)
d2 := dot(c2, stairs)
// If the depth buffer value is higher, return transparent.
if d2 > d1 {
return vec4(0)
}
return imageSrc0UnsafeAt(texCoord) return imageSrc0UnsafeAt(texCoord)
} }
` `
@ -16,7 +32,21 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
const depthShaderSrc = `package main const depthShaderSrc = `package main
func Fragment(position vec4, texCoord vec2, color vec4) vec4 { func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
return imageSrc0UnsafeAt(texCoord) // Image 0 is the depth buffer being updated.
// Image 1 is the depth map being drawn from.
// Convert the colour vectors into scalars.
stairs := vec4(1, 256, 65536, 16777216)
c0 := imageSrc0UnsafeAt(texCoord)
c1 := imageSrc1UnsafeAt(texCoord)
d0 := dot(c0, stairs)
d1 := dot(c1, stairs)
// Write back the larger of the two.
if d0 > d1 {
return c0
}
return c1
} }
` `
@ -37,5 +67,21 @@ func init() {
} }
type DepthBuffer struct { type DepthBuffer struct {
Buffer *ebiten.Image buffer *ebiten.Image
}
func NewDepthBuffer(size image.Point) *DepthBuffer {
return &DepthBuffer{
buffer: ebiten.NewImage(size.X, size.Y),
}
}
func (b *DepthBuffer) Draw(dst, src, srcDepth *ebiten.Image) {
w, h := 0, 0
dst.DrawRectShader(w, h, screenShader, &ebiten.DrawRectShaderOptions{
Images: [4]*ebiten.Image{src, srcDepth, b.buffer},
})
dst.DrawRectShader(w, h, depthShader, &ebiten.DrawRectShaderOptions{
Images: [4]*ebiten.Image{b.buffer, srcDepth},
})
} }