jfa

package module
v0.0.0-...-6ae5079 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2024 License: MIT Imports: 8 Imported by: 0

README

JFA

An implementation of the jump flooding algorithm (JFA) using Ebitengine. It can be used to create signed distance field textures, with fonts for example.

Note that there are still a few edge cases that need to be fixed overall!

Usage

go get github.com/Zyko0/Ebiary/jfa

import "github.com/Zyko0/Ebiary/jfa"
// Create a destination image to store the computed distance info 
dstImg := ebiten.NewImage(width, height)
// Create a jfa instance with the desired image size
j := jfa.New(dstImg.Bounds())
// Compute the distance of the given "maskImg" ebiten image
j.Generate(dstImg, maskImg, &jfa.GenerateOptions{
    // Pixel colors with total luminance (grey) > 0 and alpha > 0 will
    // contribute to the plain shape
    PlainValueThresholds: map[jfa.ColorMask]float64{
        jfa.ColorMaskAlpha:     0,
        jfa.ColorMaskGreyscale: 0,
    },
    // The same distance value will be set in R,G,B channels
    Encoding:     jfa.EncodingDistanceGreyscale,
    // The exterior distance to the shape is calculated
    DistanceType: jfa.DistanceExterior,
    // The edges do not count as part of the plain shape
    EdgesPlain:   false,
    Steps:        1024,
    JumpDistance: 8,
})

Previews

Original Exterior
img image
Exterior (Edges plain) Interior
image image
UVs (Exterior) UVs (Interior)
image image

CLI (TBD)

Not done yet

go run ./cmd "test.png" / go run ./cmd (drag & drop)

Notes

  • This is an early version, a few things are not clearly defined yet
    • Different kinds of encoding methods for example
    • There's a bug with the default values around the edges that I don't know how to fix
  • Steps and JumpDistance must be played with in order to get satisfying results, for more information about the theory I recommend to look online!
  • Any PR contribution (fix/improvement/feature) is welcome btw!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EncodingShader

func EncodingShader() *ebiten.Shader

func JFAShader

func JFAShader(opts *GenerateOptions) *ebiten.Shader

Types

type ColorMask

type ColorMask byte
const (
	ColorMaskAlpha ColorMask = iota
	ColorMaskGreyscale
	ColorMaskR
	ColorMaskG
	ColorMaskB
)

type DistanceType

type DistanceType byte
const (
	DistanceExterior DistanceType = iota
	DistanceInterior
)

type Encoding

type Encoding byte
const (
	EncodingDistanceGreyscale Encoding = iota
	EncodingUV
)

type GenerateOptions

type GenerateOptions struct {
	// PlainValueOptions defines a list of constraints of minimum
	// value thresholds (in 0-1 range) to consider a pixel's color as a "plain" value
	// (as opposed to empty).
	// For example ColorMaskGreyScale with value: 0.1 means that
	// the pixel's greyscale value must be over 0.1 to contribute to a plain value.
	// Multiple options in the list act as an "AND" operation.
	// By default, it is considered that a pixel is a "plain" value if its
	// alpha channel > 0.
	PlainValueThresholds map[ColorMask]float64
	// DistanceType defines whether the resulting distance encoding should be a:
	// - Exterior distance to compute the exterior minimal distance to the shape.
	// - Interior distance to compute the interior minimal distance to the shape's edges
	// By default, the exterior distance is encoded.
	DistanceType DistanceType
	// EdgesPlain defines whether or not to consider the image's boundaries (or
	// edges of the image) as plain value for distance calculation.
	// By default, it is false and edges do not contribute to plain values.
	EdgesPlain bool
	// Encoding specifies the way to encode the final distance image.
	// By default, the distance will be encoded as greyscale with alpha = 255.
	Encoding Encoding
	// EncodingScale specifies a value to mutliply the resulting color by.
	// By default, the scale is 1 (it can exceed 1).
	EncodingScale float64
	// Steps defines the number of iterations to perform, a higher value means
	// an higher output quality.
	// Default is 255 steps.
	Steps int
	// JumpDistance defines the initial jump distance in the JFA.
	// Default is 8.
	JumpDistance int
}

type JFA

type JFA struct {
	// contains filtered or unexported fields
}

func New

func New(rect image.Rectangle) *JFA

func (*JFA) Bounds

func (jfa *JFA) Bounds() image.Rectangle

func (*JFA) Generate

func (jfa *JFA) Generate(dst, src *ebiten.Image, opts *GenerateOptions)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL