API 레퍼런스는 소스 코드의 Go 문서 주석에서 추출되며 영어로 작성되어 있습니다.
package effects
import "github.com/nsgo-dev/game/pkg/effects"개요
Package effects draws a game's frame through a chain of shader passes, in order, with uniforms animated on the platform's frame clock.
An Effect is a fragment program in Kage, Go's syntax for shaders (core.CompileShader), that defines
func Effect(pos vec2) vec4
the premultiplied color of the frame's point pos, in the frame's pixels from its top left corner, and reads the pass before it with Input(pos) and the frame's size in pixels with InputSize(). The first effect's Input reads the frame itself: the pixel under pos, transparent outside the frame. An effect that reads an extra texture, such as a palette, calls Texture(pos) and TextureSize(), bound to image1 through image3 in the chain's order (at most 3 textured effects per chain). Exported variables at the package level are the effect's uniforms.
Compile fuses a chain's effects into one program, in which each effect's Input calls the effect before it, so the passes run in one draw with no texture between them: a pass that reads its input at several points runs the passes before it at each of them. View draws a frame, usually a pixel buffer the game writes (core.ImageSource.Pixels), through a chain.
색인
- 상수
- func Clock(cx *core.Scope, period time.Duration) *core.AnimatedValue
- func Float(u core.Uniforms, name string) float64
- func PaletteImage(rows ...[]core.Color) *image.RGBA
- func ShakeOffset(amplitude, seconds, frequency float64, phase [4]float64) (x, y float64)
- type Chain
- func Compile(effects ...Effect) (*Chain, error)
- func MustCompile(effects ...Effect) *Chain
- func (c *Chain) HasTexture(i int) bool
- func (c *Chain) HasUniform(i int, name string) bool
- func (c *Chain) Images(frame core.ImageSource, perEffect []core.ImageSource) []core.ImageSource
- func (c *Chain) Len() int
- func (c *Chain) Program() *core.ShaderProgram
- func (c *Chain) Reference(frame *image.RGBA, width, height int, perEffect []core.Uniforms) (*image.RGBA, error)
- func (c *Chain) ReferenceWithTextures(frame *image.RGBA, width, height int, perEffect []core.Uniforms, textures []core.ImageSource) (*image.RGBA, error)
- func (c *Chain) Source() string
- func (c *Chain) Uniforms(perEffect []core.Uniforms) core.Uniforms
- type Effect
- type Flash
- type FlashConfig
- type Input
- type Shake
- type ShakeConfig
- type View
상수
const DefaultShakeFrequency = 12DefaultShakeFrequency is a Shake's Frequency when its config leaves it 0.
함수
func Clock(cx *core.Scope, period time.Duration) *core.AnimatedValueClock returns a value that counts seconds on the platform's frame clock, from 0 up to period and from 0 again, for as long as cx's owner lasts: a uniform set to it animates an effect with no work per frame. A period of 0 or less is an hour. Choose a period after which the effect looks as it started, such as a whole number of its cycles, as the value jumps back to 0.
func Float(u core.Uniforms, name string) float64Float returns the number uniform u sets name to, for an effect's Reference: any of Go's integer or float kinds, a bool as 0 or 1, or a core.Constant. It is 0 when u leaves name out or sets it to anything else, such as a vector, which a Reference reads from u itself.
func PaletteImage(rows ...[]core.Color) *image.RGBAPaletteImage builds a premultiplied RGBA palette texture from rows of colors, one row per palette. Shorter rows are padded on the right with transparent pixels. Returns an empty image when rows has no colors.
func ShakeOffset(amplitude, seconds, frequency float64, phase [4]float64) (x, y float64)ShakeOffset returns how far ShakeEffect moves the frame, in its pixels, at the uniforms given: (0, 0) when the offset is not finite or is 1e30 pixels or more along either direction, as on the GPU. It reads plain numbers, so it serves a ShakeEffect a game drives by hand; a Shake's uniforms follow animated nodes whose values only the platform knows.
타입
type Chain struct {
// contains filtered or unexported fields
}Chain is effects compiled into one program, which View draws a frame through.
func Compile(effects ...Effect) (*Chain, error)Compile compiles effects into a chain that draws them in order, the first over the frame and each next over the one before it. No effects make a chain that draws the frame as it is.
Each effect's source is checked and compiled on its own first, so an error names the effect and gives the line and column in its source.
func MustCompile(effects ...Effect) *ChainMustCompile is Compile for effects that are part of the program: it panics when they do not compile.
func (c *Chain) HasTexture(i int) boolHasTexture reports whether effect i of the chain reads an extra texture through Texture or TextureSize.
func (c *Chain) HasUniform(i int, name string) boolHasUniform reports whether effect i of the chain declares a uniform named name.
func (c *Chain) Images(frame core.ImageSource, perEffect []core.ImageSource) []core.ImageSourceImages returns the shader's Images slice for frame (image0) and each textured effect's image (image1 through image3 in chain order). A non-zero entry in perEffect[i] replaces effect i's own Texture. It panics when perEffect has more entries than the chain has effects, or when a non-zero entry targets an effect that reads no texture.
func (c *Chain) Program() *core.ShaderProgramProgram returns the chain's compiled program, which reads the frame as image0, any textured effects' images as image1 through image3, and the effects' uniforms under the names Uniforms gives them.
func (c *Chain) Reference(frame *image.RGBA, width, height int, perEffect []core.Uniforms) (*image.RGBA, error)Reference draws frame through the chain on the CPU into a picture of width by height pixels, with each effect's Reference, as View draws it on the GPU into a box of that many pixels with the same uniforms: each pixel's center is mapped onto the frame and drawn by the last effect. perEffect[i] is effect i's uniforms, as Chain.Uniforms and View.Uniforms take them. It is the picture a test compares a chain's output with, and a screenshot of it where no GPU draws. Colors are clamped to 0 to 1 and rounded to 8 bits.
It returns an error when an effect has no Reference, when perEffect names a uniform an effect does not declare or has more entries than the chain has effects, when a textured effect's Texture has neither Pixels nor Data, and for a uniform set to an AnimatedNode other than a core.Constant, whose value only the platform knows.
func (c *Chain) ReferenceWithTextures(frame *image.RGBA, width, height int, perEffect []core.Uniforms, textures []core.ImageSource) (*image.RGBA, error)ReferenceWithTextures is Reference with per-effect texture overrides, as View.Textures and Chain.Images take them: a non-zero entry in textures[i] replaces effect i's own Texture.
func (c *Chain) Source() stringSource returns the Kage program the chain's effects compile into, which is the same for the same effects.
func (c *Chain) Uniforms(perEffect []core.Uniforms) core.UniformsUniforms returns the program's uniforms from each effect's own, in the chain's order: perEffect[i] sets effect i's uniforms by the names its source declares. A value is what core.Uniforms takes, an AnimatedNode among them, which follows the platform's frame clock. It panics when an effect has no uniform of a name, or when perEffect has more entries than the chain has effects.
type Effect struct {
// Name names the effect in errors; the effect's index names it when empty.
Name string
// Source is the effect's Kage program, as the package documentation describes it.
Source string
// Reference draws the effect on the CPU for Chain.Reference: the premultiplied color, red, green, blue and alpha from 0 to 1, of the frame's point (x, y) given the pass before it and the effect's uniforms, by the names its source declares, as Chain.Reference is given them (Float reads a number). A uniform left out is 0, as on the GPU. It is nil for an effect that has none, which a chain draws on the GPU only.
Reference func(in Input, u core.Uniforms, x, y float64) [4]float64
// Texture is the effect's extra texture, read in its source with Texture(pos) and TextureSize(), and in its Reference with Input.TextureAt and Input.TextureSize. View binds it automatically and may replace it via View.Textures or View.TexturesFunc.
Texture core.ImageSource
}Effect is one pass of a chain.
func Bloom() EffectBloom returns an effect that makes the frame's highlights glow in one pass: it adds, over each point, the bright part of the input at the point and at 16 points around it, an inner ring of 8 at half the radius and an outer ring of 8 at the radius turned half a step, weighted 4 at the center, 2 on the inner ring and 1 on the outer. It approximates a blurred bright pass without a texture between passes, so its glow is a soft star rather than a smooth disc at large radii.
Its uniforms, by name:
- Threshold, from 0 to 1: the luminance (Rec. 709) above which a pixel glows, as much as its luminance rises from Threshold toward 1. It is the luminance of the premultiplied color, so a translucent pixel glows in proportion to its alpha: white at half alpha has a luminance of 0.5. Values outside 0 to 1 are clamped, and NaN acts as 0.
- Intensity: how much of the glow is added; 0 or less, the default being 0, or NaN returns the input unchanged, 1 to 2 is a strong bloom, and above 1000 it acts as 1000.
- Radius: how far, in the frame's pixels, the glow reaches.
The glow adds light: a translucent pixel near a highlight becomes more opaque, and colors stay premultiplied. Each output point reads its input 17 times, and a chain runs the effects before it at each of those points, so put Bloom first in a chain or after effects that read their input once.
func CRT() EffectCRT returns an effect that bends the frame onto a curved cathode-ray tube and draws horizontal scanlines along the curve in one pass.
Its uniforms, by name:
- Curvature: barrel distortion strength. 0 or less, the default being 0, or NaN leaves the frame flat; 0.1 to 0.3 is a typical CRT curve; values above 4 act as 4. The four edge midpoints stay pinned to the frame border while the corners pull inward onto an opaque black bezel.
- Scanlines, from 0 to 1: how dark the gaps between horizontal scanlines turn, 0 leaving the brightness unchanged and 1 turning the troughs black. Values outside 0 to 1 are clamped, and NaN acts as 0.
- Lines: how many scanlines span the frame's height. 0 or less, the default being 0, or NaN draws one scanline per pixel row of the frame, which shows as alternating bright and dark rows when the view scales the frame up (at 2x or higher); set Lines to half the frame's height or less when drawing a frame at 1:1. Values above 16384 act as 16384.
When both Curvature and Scanlines are 0 or less, CRT passes its input through unchanged. Inside the screen, scanlines darken RGB while keeping each pixel's alpha, so colors stay premultiplied; outside the curved tube, the bezel is opaque black. Each output point reads its input at most once, so CRT can follow multi-tap passes such as Bloom without multiplying their taps, and chains with VignetteEffect when darkened corners are also wanted. The effect has a Reference, so a chain of it draws a reference picture.
func FlashEffect() EffectFlashEffect returns an effect that turns the whole frame toward an opaque color, as a hit, a bolt of lightning or a camera's flash does. Its uniforms are Amount, from 0, which draws the frame as it is, to 1, which draws only the color, and Color, a core.Color, black when left out. A Flash animates them on the frame clock. The effect has a Reference, so a chain of it draws a reference picture.
func PaletteRamp(palette core.ImageSource) EffectPaletteRamp returns an effect that maps each pixel's straight Rec. 709 luminance through a palette texture: each row is a dark-to-light palette of any width, with column 0 for black and column width-1 for white, preserving the pixel's alpha (multiplied by the palette entry's alpha).
Its uniforms, by name:
- Amount, from 0 to 1: how far the frame turns to the palette; 0 or less, the default, or NaN returns the input unchanged.
- Row, from 0 to height-1: which palette row to map into; clamped to the texture's height, with NaN as 0. Fractional values blend linearly between adjacent rows.
palette is bound as the effect's Texture and may be left zero to supply or replace it per view via View.Textures or View.TexturesFunc. The effect has a Reference, so a chain of it draws a reference picture when the palette has Pixels or Data.
func PaletteSwap(palette core.ImageSource) EffectPaletteSwap returns an effect that swaps colors through a palette texture: row 0 holds up to 256 source (key) colors (transparent pixels are skipped), and each row from 0 to height-1 holds a target palette. A pixel whose straight RGB matches a key in row 0 within Tolerance is replaced by the color at that column in Row, premultiplied by the pixel's alpha; unmatched colors and transparent pixels pass through unchanged.
Its uniforms, by name:
- Amount, from 0 to 1: how far matched colors turn to the target row; 0 or less, the default, or NaN returns the input unchanged.
- Row, from 0 to height-1: which palette row to swap to; clamped to the texture's height, with NaN as 0. Fractional values blend linearly between adjacent rows, so animating Row transitions smoothly between palettes on the frame clock.
- Tolerance, from 0 to 1: the largest difference in any straight RGB channel that matches a key in row 0. Values at or below 0.002/alpha (including 0, the default, and NaN) match exact 8-bit colors, accounting for premultiplied alpha rounding; larger values pick the nearest matching key in Euclidean RGB distance, and 1 maps every non-transparent color to its nearest key.
palette is bound as the effect's Texture and may be left zero to supply or replace it per view via View.Textures or View.TexturesFunc. The effect has a Reference, so a chain of it draws a reference picture when the palette has Pixels or Data.
func ShakeEffect() EffectShakeEffect returns an effect that moves the whole frame about, as an explosion or a heavy landing does, reading the frame's edge where it moves off it. Its uniforms are:
- Amplitude, how far the frame moves at most along each direction, in the frame's pixels;
- Time, the shake's clock in seconds;
- Frequency, how many times a second the frame sways;
- Phase, four numbers or nodes in radians that start the sway's sines, which tell one shake from another.
The frame moves by Amplitude times 0.6·sin(w+Phase[0]) + 0.4·sin(2.17w+Phase[1]) across and 0.6·sin(1.13w+Phase[2]) + 0.4·sin(2.71w+Phase[3]) down, with w = 2π·Frequency·Time. An offset that is not finite, or 1e30 pixels or more along either direction, as a NaN or infinite uniform gives, leaves the frame where it is. A Shake animates them on the frame clock. The effect has a Reference, so a chain of it draws a reference picture.
func VignetteEffect() EffectVignetteEffect returns an effect that turns the frame's edges toward a color, as an old lens or a hurt player's view does. Its uniforms are:
- Strength, how far the corners turn, from 0, which draws the frame as it is, to 1;
- Radius, the distance from the center at which the edges start to turn, 0 at the center and 1 at the corners, round in the frame's pixels;
- Softness, the distance over which they turn all the way, eased; 0 draws a hard edge;
- Color, a core.Color the edges turn to, black when left out; it keeps each pixel's alpha.
Each is a number or an AnimatedNode, so a game can close the vignette in on the frame clock. The effect has a Reference, so a chain of it draws a reference picture.
type Flash struct {
// contains filtered or unexported fields
}Flash is a full-screen flash a game triggers: FlashEffect's Amount jumps to a peak and fades to 0 on the platform's frame clock, with no commit per frame.
func NewFlash(cx *core.Scope, cfg FlashConfig) *FlashNewFlash returns a flash for FlashEffect, at rest, that lasts as long as cx's owner.
func (f *Flash) Amount() core.AnimatedNodeAmount returns the node FlashEffect's Amount follows, from 0 at rest up to a Trigger's peak.
func (f *Flash) Trigger(peak float64, fade time.Duration) boolTrigger sets Amount to peak, clamped to 0 to 1, and fades it to 0 over fade, easing out, in place of a flash still fading. It reports whether the flash runs: it does not under reduced motion, nor for a peak of 0 or less or a fade of 0 or less, which leave a running flash alone: Stop, not Trigger(0, fade), ends a running flash.
func (f *Flash) Uniforms() core.UniformsUniforms returns FlashEffect's uniforms, for its place in a View's Uniforms: Amount follows the flash.
type FlashConfig struct {
// Color is the flash's color; the zero Color is black.
Color core.Color
// ReduceMotion, when set, is asked at each Trigger, and the flash does not run while it reports true: pass an a11y.Assist's ReduceMotion, which follows the player's choice and else the platform's. When nil, the platform's reduced motion setting is asked.
ReduceMotion func() bool
}FlashConfig configures a Flash.
type Input struct {
// contains filtered or unexported fields
}Input is what an effect's Reference reads: the pass before it, or the frame for the first, as Input and InputSize read them in its source, plus the effect's extra texture as Texture and TextureSize read it.
func (in Input) At(x, y float64) [4]float64At returns the premultiplied color of the input at (x, y), in the frame's pixels: for the frame, the pixel under the point, transparent outside it.
func (in Input) TextureAt(x, y float64) [4]float64TextureAt returns the premultiplied color of the effect's extra texture at (x, y), in its pixels: the pixel under the point, transparent outside it, as Texture(pos) reads it in its source.
func (in Input) TextureSize() (w, h float64)TextureSize returns the size of the effect's extra texture in pixels, as TextureSize() reads it in its source.
type Shake struct {
// contains filtered or unexported fields
}Shake is a screen shake a game triggers: ShakeEffect's Amplitude jumps to a peak and decays to 0 while its Time runs, on the platform's frame clock, with no commit per frame.
func NewShake(cx *core.Scope, cfg ShakeConfig) *ShakeNewShake returns a shake for ShakeEffect, at rest, that lasts as long as cx's owner.
func (s *Shake) Amplitude() core.AnimatedNodeAmplitude returns the node ShakeEffect's Amplitude follows, 0 at rest.
func (s *Shake) Stop()Stop ends a running shake at once, with the frame back in place.
func (s *Shake) Trigger(amplitude float64, d time.Duration) boolTrigger shakes the frame by amplitude pixels at first, decaying to 0 over d, easing out, in place of a shake still running, with the next phases the seed gives. It reports whether the shake runs: it does not under reduced motion, nor for an amplitude or a duration of 0 or less, which leave a running shake alone and draw no phases. Stop, not Trigger(0, d), ends a running shake: unlike tilemap.Camera.Shake, where an intensity or a duration of 0 or less cancels.
func (s *Shake) Uniforms() core.UniformsUniforms returns ShakeEffect's uniforms, for its place in a View's Uniforms: Amplitude, Time and Phase follow the shake.
type ShakeConfig struct {
// Seed picks each trigger's phases in turn, so the same seed shakes the same way in a replay; 0 is a seed of its own like any other, where tilemap.Camera's ShakeSeed takes 0 as 1.
Seed uint64
// Frequency is how many times a second the frame sways, DefaultShakeFrequency when 0 or less.
Frequency float64
// ReduceMotion, when set, is asked at each Trigger, and the shake does not run while it reports true: pass an a11y.Assist's ReduceMotion, which follows the player's choice and else the platform's. When nil, the platform's reduced motion setting is asked.
ReduceMotion func() bool
}ShakeConfig configures a Shake.
type View struct {
// Chain is the effects the frame is drawn through; a nil Chain draws the frame as it is.
Chain *Chain
// Frame is the picture the chain reads, usually a pixel buffer the game writes each frame, with Version telling its frames apart; FrameFunc, when set, follows the graph instead.
Frame core.ImageSource
FrameFunc func() core.ImageSource
// Uniforms are each effect's uniforms, in the chain's order, as Chain.Uniforms takes them: an AnimatedNode among them follows the platform's frame clock with no commit per frame. UniformsFunc, when set, follows the graph, its values over Uniforms'.
Uniforms []core.Uniforms
UniformsFunc func() []core.Uniforms
// Textures overrides each textured effect's Texture, in the chain's order, as Chain.Images takes them; a zero entry keeps the effect's own Texture. TexturesFunc, when set, follows the graph instead.
Textures []core.ImageSource
TexturesFunc func() []core.ImageSource
// Class is Tailwind CSS classes, as core.View's.
Class string
Style core.Style
StyleFunc func() core.Style
// Children draw over the chain's picture, such as a HUD.
Children []core.Node
OnPress func(core.PressEvent)
OnLayout func(core.Bounds)
Ref *core.Ref
// Label names what the view shows for assistive technology, which skips an unlabeled view as a decoration.
Label string
LabelFunc func() string
TestID string
}View draws a frame through a chain, stretched over its box: a core.Shader whose image0 is the frame. Give the box the frame's aspect ratio, as a screen.Viewport places a virtual canvas, for square pixels.