effect

package
v0.0.0-...-e1c21c9 Latest Latest
Warning

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

Go to latest
Published: May 10, 2019 License: Zlib Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Life      = ChanFiled{Type: ChanF32, Name: "Life"}
	Size      = ChanFiled{Type: ChanF32, Name: "ParticleSize"}
	SizeDelta = ChanFiled{Type: ChanF32, Name: "ParticleSize-delta"}

	Color      = ChanFiled{Type: ChanV4, Name: "Color"}
	ColorDelta = ChanFiled{Type: ChanV4, Name: "Color-delta"}

	Position      = ChanFiled{Type: ChanV2, Name: "position"}
	PositionStart = ChanFiled{Type: ChanV2, Name: "position-start"}
	Velocity      = ChanFiled{Type: ChanV2, Name: "velocity"}

	Speed         = ChanFiled{Type: ChanF32, Name: "speed"}
	Direction     = ChanFiled{Type: ChanV2, Name: "direction"}
	RadialAcc     = ChanFiled{Type: ChanF32, Name: "radial-acc"}
	TangentialAcc = ChanFiled{Type: ChanF32, Name: "tangent-acc"}

	Rotation      = ChanFiled{Type: ChanF32, Name: "rotation"}
	RotationDelta = ChanFiled{Type: ChanF32, Name: "rotation-delta"}

	Angle       = ChanFiled{Type: ChanF32, Name: "angle"}
	AngleDelta  = ChanFiled{Type: ChanF32, Name: "angle-delta"}
	Radius      = ChanFiled{Type: ChanF32, Name: "radius"}
	RadiusDelta = ChanFiled{Type: ChanF32, Name: "radius-delta"}
)

Functions

This section is empty.

Types

type BufferContext

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

目前所有的粒子都会使用一个VBO进行渲染 TODO 这么做同时可渲染的例子数量会受限于VBO的大小,需要一些经验数据支持

func (*BufferContext) AllocBuffer

func (ctx *BufferContext) AllocBuffer(vertexSize, indexSize int)

func (*BufferContext) Release

func (ctx *BufferContext) Release()

type ChanFiled

type ChanFiled struct {
	Type ChanType
	Name string
}

A description of channel used in pool

type ChanType

type ChanType uint8

Compute Channel: Types:float32/Vec2/Vec4 Methods: random/const/add/sub/mul/integrate

Compute channel is an array of values that can be computed in a for-loop. It's data structure is cache-friendly.

const (
	ChanF32 ChanType = iota
	ChanV2
	ChanV4
)

type Channel_f32

type Channel_f32 []float32

func (Channel_f32) Add

func (ch Channel_f32) Add(n int32, v float32)

func (Channel_f32) Integrate

func (ch Channel_f32) Integrate(n int32, ch1 Channel_f32, dt float32)

func (Channel_f32) Mul

func (ch Channel_f32) Mul(n int32, v float32)

func (Channel_f32) SetConst

func (ch Channel_f32) SetConst(n int32, v float32)

func (Channel_f32) SetRandom

func (ch Channel_f32) SetRandom(n int32, v Var)

func (Channel_f32) Sub

func (ch Channel_f32) Sub(n int32, v float32)

type Channel_v2

type Channel_v2 []f32.Vec2

func (Channel_v2) Add

func (ch Channel_v2) Add(n int32, x, y float32)

func (Channel_v2) Integrate

func (ch Channel_v2) Integrate(n int32, ch1 Channel_v2, dt float32)

func (Channel_v2) SetConst

func (ch Channel_v2) SetConst(n int32, x, y float32)

func (Channel_v2) SetRandom

func (ch Channel_v2) SetRandom(n int32, xlow, xhigh float32, ylow, yhigh float32)

type Channel_v4

type Channel_v4 []f32.Vec4

maybe only Color will use it

func (Channel_v4) Add

func (ch Channel_v4) Add(n int32, x, y, z, w float32)

func (Channel_v4) Integrate

func (ch Channel_v4) Integrate(n int32, d Channel_v4, dt float32)

func (Channel_v4) SetConst

func (ch Channel_v4) SetConst(n int32, x, y, z, w float32)

func (Channel_v4) SetRandom

func (ch Channel_v4) SetRandom(n int32, x, y, z, v [4]Var)

func (Channel_v4) Sub

func (ch Channel_v4) Sub(n int32, x, y, z, w float32)

type Config

type Config struct {
	Max int

	Duration float32
	Rate     float32 // Number of particles per-second

	Life Var
	X, Y Var
	Size Range
	Rot  Range

	R, G, B, A Range
	Additive   bool
}

type Controller

type Controller interface {
	Stop()
	Play()
}

ParticleSystem lifecycle controller.

type Emitter

type Emitter interface {
}

TODO: Emitter的概念可以提供一种可能: 在这里可以通过各种各样的Emitter实现,生成不同的初始粒子位置 这样可以实现更丰富的例子形状 之前要么认为粒子都是从一个点发射出来的,要么是全屏发射的,这只是hardcode了特殊情况 同时通过配置多个Emitter还可以实现交叉堆叠的形状

type ExplosionSimulator

type ExplosionSimulator struct {
	Pool

	RateController
	LifeController
	VisualController

	// Configuration.
	Config struct {
		Duration, Rate float32
		Life           Var
		Size           Var
		Color          TwoColor
		Position       [2]Var
		Angle          Var
		Speed          Var
		Additive       bool
	}
	// contains filtered or unexported fields
}

FireSimulator can simulate the fire effect.

func NewExplosionSimulator

func NewExplosionSimulator(cap int) *ExplosionSimulator

func (*ExplosionSimulator) Initialize

func (f *ExplosionSimulator) Initialize()

func (*ExplosionSimulator) Simulate

func (f *ExplosionSimulator) Simulate(dt float32)

func (*ExplosionSimulator) Size

func (f *ExplosionSimulator) Size() (live, cap int)

func (*ExplosionSimulator) Visualize

func (f *ExplosionSimulator) Visualize(buf []gfx.PosTexColorVertex, tex gfx.Tex2D)

type FireSimulator

type FireSimulator struct {
	Pool

	RateController
	LifeController
	VisualController

	// Configuration.
	Config struct {
		Duration, Rate float32
		Life           Var
		Size           Var
		Color          f32.Vec4
		Position       [2]Var
		Angle          Var
		Speed          Var
		Additive       bool
	}
	// contains filtered or unexported fields
}

FireSimulator can simulate the fire effect.

func NewFireSimulator

func NewFireSimulator(cap int) *FireSimulator

func (*FireSimulator) Initialize

func (f *FireSimulator) Initialize()

func (*FireSimulator) Simulate

func (f *FireSimulator) Simulate(dt float32)

func (*FireSimulator) Size

func (f *FireSimulator) Size() (live, cap int)

func (*FireSimulator) Visualize

func (f *FireSimulator) Visualize(buf []gfx.PosTexColorVertex, tex gfx.Tex2D)

type FountainSimulator

type FountainSimulator struct {
	Pool

	RateController
	LifeController
	VisualController

	// Configuration.
	Config struct {
		Duration, Rate float32
		Life           Var
		Size           Var
		Color          TwoColor
		Fading         bool
		Position       [2]Var
		Angle          Var
		Speed          Var
		Gravity        float32
		Rotation       Var
		Additive       bool
	}
	// contains filtered or unexported fields
}

FireSimulator can simulate the fire effect.

func NewFountainSimulator

func NewFountainSimulator(cap int) *FountainSimulator

func (*FountainSimulator) Initialize

func (f *FountainSimulator) Initialize()

func (*FountainSimulator) Simulate

func (f *FountainSimulator) Simulate(dt float32)

func (*FountainSimulator) Size

func (f *FountainSimulator) Size() (live, cap int)

func (*FountainSimulator) Visualize

func (f *FountainSimulator) Visualize(buf []gfx.PosTexColorVertex, tex gfx.Tex2D)

type GravityConfig

type GravityConfig struct {
	Config

	// gravity
	Gravity f32.Vec2

	// speed and direction
	Speed Var
	Angel Var

	// Radial acceleration
	RadialAcc Var

	// tangent acceleration
	TangentialAcc Var

	RotationIsDir bool
}

GravityConfig used to configure the GravitySimulator.

type GravitySimulator

type GravitySimulator struct {
	Pool

	RateController
	LifeController
	VisualController

	// config
	*GravityConfig
	// contains filtered or unexported fields
}

GravitySimulator works as the gravity mode of Cocos2D's particle-system.

func NewGravitySimulator

func NewGravitySimulator(cfg *GravityConfig) *GravitySimulator

func (*GravitySimulator) Initialize

func (g *GravitySimulator) Initialize()

alloc a big block

func (*GravitySimulator) Simulate

func (g *GravitySimulator) Simulate(dt float32)

func (*GravitySimulator) Size

func (r *GravitySimulator) Size() (live, cap int)

func (*GravitySimulator) Visualize

func (g *GravitySimulator) Visualize(buf []gfx.PosTexColorVertex, tex gfx.Tex2D)

type LifeController

type LifeController struct {
	// channel ref
	Life Channel_f32
	Live int
}

LifeController is a helper struct to manage the Life of particles.

func (*LifeController) GC

func (ctr *LifeController) GC(p *Pool) (dead int)

GC removes dead particles from the Pool.

type ParticleComp

type ParticleComp struct {
	engi.Entity
	// contains filtered or unexported fields
}

Particle Component

func (*ParticleComp) Play

func (pc *ParticleComp) Play()

func (*ParticleComp) Prewarm

func (pc *ParticleComp) Prewarm(t float32)

func (*ParticleComp) SetSimulator

func (pc *ParticleComp) SetSimulator(sim Simulator)

func (*ParticleComp) SetSize

func (pc *ParticleComp) SetSize(w, h float32)

The width and height of the particle system. We'll use it to make visibility test. The default value is {w:64, h:64}

func (*ParticleComp) SetTexture

func (pc *ParticleComp) SetTexture(tex gfx.Tex2D)

func (*ParticleComp) SetVisible

func (pc *ParticleComp) SetVisible(v bool)

func (*ParticleComp) SetZOrder

func (pc *ParticleComp) SetZOrder(z int16)

func (*ParticleComp) Simulator

func (pc *ParticleComp) Simulator() Simulator

func (*ParticleComp) Stop

func (pc *ParticleComp) Stop()

func (*ParticleComp) Texture

func (pc *ParticleComp) Texture() gfx.Tex2D

func (*ParticleComp) Visible

func (pc *ParticleComp) Visible() bool

func (*ParticleComp) Z

func (pc *ParticleComp) Z() int16

type ParticleRenderFeature

type ParticleRenderFeature struct {
	*gfx.MeshRender

	BufferContext
	// contains filtered or unexported fields
}

func (*ParticleRenderFeature) Draw

func (f *ParticleRenderFeature) Draw(nodes gfx.RenderNodes)

func (*ParticleRenderFeature) Extract

func (f *ParticleRenderFeature) Extract(v *gfx.View)

func (*ParticleRenderFeature) Flush

func (f *ParticleRenderFeature) Flush()

func (*ParticleRenderFeature) Register

func (f *ParticleRenderFeature) Register(rs *gfx.RenderSystem)

此处初始化所有的依赖

type ParticleSimulateSystem

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

ParticleSimulateSystem is the system that manage ParticleComp's simulation.

func NewSimulationSystem

func NewSimulationSystem() *ParticleSimulateSystem

func (*ParticleSimulateSystem) Initialize

func (pss *ParticleSimulateSystem) Initialize()

System 的生命周期中,应该安排一个 Initialize 的阶段

func (*ParticleSimulateSystem) RequireTable

func (pss *ParticleSimulateSystem) RequireTable(tables []interface{})

func (*ParticleSimulateSystem) Update

func (pss *ParticleSimulateSystem) Update(dt float32)

TODO: Need a better way to initialize each simulator

type ParticleSystemTable

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

component manager

func NewParticleSystemTable

func NewParticleSystemTable(cap int) *ParticleSystemTable

func (*ParticleSystemTable) Alive

func (et *ParticleSystemTable) Alive(entity engi.Entity) bool

func (*ParticleSystemTable) Comp

func (et *ParticleSystemTable) Comp(entity engi.Entity) (ec *ParticleComp)

func (*ParticleSystemTable) Delete

func (et *ParticleSystemTable) Delete(entity engi.Entity)

func (*ParticleSystemTable) NewComp

func (et *ParticleSystemTable) NewComp(entity engi.Entity) (ec *ParticleComp)

func (*ParticleSystemTable) Size

func (et *ParticleSystemTable) Size() (size, cap int)

type Pool

type Pool struct {
	Cap int
	// contains filtered or unexported fields
}

A Pool represent a particle-pool.

func (*Pool) AddChan

func (p *Pool) AddChan(fields ...ChanFiled)

AddChan adds new fields to the pool.

func (*Pool) Field

func (p *Pool) Field(t ChanFiled) (array interface{})

Field returns pointer of the filed in the pool.

func (*Pool) Initialize

func (p *Pool) Initialize()

Initialize the particle pool.

func (*Pool) Size

func (p *Pool) Size() (size int)

Size return the ParticleSize (in bytes) of the pool.

func (*Pool) Swap

func (p *Pool) Swap(dst, src int)

Swap swap all the field defined in the pool.

type RadiusConfig

type RadiusConfig struct {
	Config

	Radius            Range
	Angle, AngleDelta Var
}

RadiusConfig used to configure the RadiusSimulator.

type RadiusSimulator

type RadiusSimulator struct {
	Pool

	LifeController
	RateController
	VisualController

	*RadiusConfig
	// contains filtered or unexported fields
}

RadiusSimulator works as the radius mode of the Cocos2D's particle-system.

func NewRadiusSimulator

func NewRadiusSimulator(cfg *RadiusConfig) *RadiusSimulator

func (*RadiusSimulator) Initialize

func (r *RadiusSimulator) Initialize()

prepare data

func (*RadiusSimulator) Simulate

func (r *RadiusSimulator) Simulate(dt float32)

func (*RadiusSimulator) Size

func (r *RadiusSimulator) Size() (live, cap int)

func (*RadiusSimulator) Visualize

func (r *RadiusSimulator) Visualize(buf []gfx.PosTexColorVertex, tex gfx.Tex2D)

type Range

type Range struct {
	Start, End Var
}

Range define a range between [Start, End].

func (Range) HasRange

func (r Range) HasRange() bool

HasRange returns whether Start and End is the same.

func (*Range) RangeInit

func (r *Range) RangeInit(invLife float32) (start, d float32)

func (Range) Used

func (r Range) Used() bool

Used returns whether the value is empty.

type RateController

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

RateController is a helper struct to manage the EmitterRate.

func (*RateController) Initialize

func (ctr *RateController) Initialize(du, rate float32)

Initialize init RateController with duration and emitter-rate.

func (*RateController) Play

func (ctr *RateController) Play()

func (*RateController) Prewarm

func (ctr *RateController) Prewarm(t float32)

func (*RateController) Rate

func (ctr *RateController) Rate(dt float32) (n int)

Rate returns new particles that should spawn.

func (*RateController) Stop

func (ctr *RateController) Stop()

func (*RateController) WarmTime

func (ctr *RateController) WarmTime() float32

type Simulator

type Simulator interface {
	// Initialize the particle simulator.
	Initialize()

	// Run the simulator with delta time.
	Simulate(dt float32)

	// Write the result to vertex-buffer.
	Visualize(buf []gfx.PosTexColorVertex, tex gfx.Tex2D)

	// Return the ParticleSize of the simulator.
	Size() (live, cap int)
}

Simulator define how a particle-system works.

type SnowSimulator

type SnowSimulator struct {
	Pool

	RateController
	LifeController
	VisualController

	// Configuration.
	Config struct {
		Duration, Rate float32
		Life           Var
		Size           Var
		Color          f32.Vec4
		Position       [2]Var
		Velocity       [2]Var
		Rotation       Var
	}
	// contains filtered or unexported fields
}

SnowSimulator can simulate snow effect.

func NewSnowSimulator

func NewSnowSimulator(cap int, w, h float32) *SnowSimulator

func (*SnowSimulator) Initialize

func (sim *SnowSimulator) Initialize()

func (*SnowSimulator) NewParticle

func (sim *SnowSimulator) NewParticle(new int)

func (*SnowSimulator) Simulate

func (sim *SnowSimulator) Simulate(dt float32)

func (*SnowSimulator) Size

func (sim *SnowSimulator) Size() (live, cap int)

func (*SnowSimulator) Visualize

func (sim *SnowSimulator) Visualize(buf []gfx.PosTexColorVertex, tex gfx.Tex2D)

type TwoColor

type TwoColor struct {
	One, Other     f32.Vec4
	EnableGradient bool
}

func (TwoColor) Random

func (tc TwoColor) Random() (c f32.Vec4)

type Updater

type Updater interface {
}

TODO: 基于上面的想法,还可以设计出 Updater 的概念,不同的 Updater 对粒子执行不同的 行走路径,这会极大的增加粒子弹性

type Var

type Var struct {
	Base, Var float32
}

Var define a variable value between [Base-Var/2, Base+Var/2].

func (Var) Random

func (v Var) Random() float32

Random returns a value between [Base-Var/2, Base+Var/2].

func (Var) Used

func (v Var) Used() bool

Used returns whether the value is empty.

type VisualController

type VisualController struct {
	Position     Channel_v2
	Color        Channel_v4
	ParticleSize Channel_f32
	Rotation     Channel_f32
}

VisualController is a helper struct to write simulation result to vertex-buffer.

func (*VisualController) Visualize

func (ctr *VisualController) Visualize(buf []gfx.PosTexColorVertex, tex gfx.Tex2D, live int, additive bool)

Visualize write the Live particles to vertex-buffer.

type WarmupController

type WarmupController interface {
	Prewarm(t float32)
	WarmTime() float32
}

Prewarm particle system

Jump to

Keyboard shortcuts

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