Documentation ¶
Index ¶
- Constants
- Variables
- func Destroy()
- func Flush() (num int)
- func Init(pixelRatio float32)
- func OverlapAB(a, b *AABB) bool
- func PackSortId(z int16, b uint16) (sid uint32)
- func UnpackSortId(sortId uint32) (z int16, b uint16)
- type AABB
- type Anchor
- type Atlas
- type Batch
- type BatchContext
- type BatchObject
- type BatchRender
- type BoundingBox
- type BoundingComp
- type BoundingTable
- type Camera
- func (c *Camera) Bounding() (left, top, right, bottom float32)
- func (c *Camera) Flow(entity engi.Entity)
- func (c *Camera) InView(xf *Transform, size, gravity f32.Vec2) bool
- func (c *Camera) MoveBy(dx, dy float32)
- func (c *Camera) MoveTo(x, y float32)
- func (c *Camera) P() (left, right, bottom, top float32)
- func (c *Camera) Position() (x, y float32)
- func (c *Camera) RotateBy(d float32)
- func (c *Camera) RotateTo(rt float32)
- func (c *Camera) Rotation() float32
- func (c *Camera) Scale() (sx, sy float32)
- func (c *Camera) ScaleBy(dsx, dsy float32)
- func (c *Camera) ScaleTo(sx, sy float32)
- func (c *Camera) Scene2Screen(x, y float32) (x1, y1 float32)
- func (c *Camera) Screen() (w, h float32)
- func (c *Camera) Screen2Scene(x, y float32) (x1, y1 float32)
- func (c *Camera) SetBound(left, top, right, bottom float32)
- func (c *Camera) SetDesiredViewport(w, h float32)
- func (c *Camera) SetViewPort(w, h float32)
- func (c *Camera) View() (x, y, w, h float32)
- type CameraMode
- type Color
- type CompRef
- type Mesh
- type MeshComp
- type MeshRender
- type MeshRenderFeature
- type MeshTable
- type PosTexColorVertex
- type Region
- type Render
- type RenderFeature
- type RenderMesh
- type RenderNodes
- type RenderObject
- type RenderSystem
- type RenderType
- type SRT
- type ShapeRender
- type Size
- type SortObject
- type Sprite
- type SpriteComp
- func (b *SpriteComp) BatchId() uint16
- func (sc *SpriteComp) Color() Color
- func (sc *SpriteComp) Flip(flipX, flipY bool)
- func (sc *SpriteComp) Gravity() (x, y float32)
- func (b *SpriteComp) SetBatchId(id uint16)
- func (sc *SpriteComp) SetColor(c Color)
- func (sc *SpriteComp) SetGravity(x, y float32)
- func (sc *SpriteComp) SetSize(w, h float32)
- func (sc *SpriteComp) SetSprite(spt Sprite)
- func (sc *SpriteComp) SetVisible(v bool)
- func (zo *SpriteComp) SetZOrder(z int16)
- func (sc *SpriteComp) Size() (w, h float32)
- func (sc *SpriteComp) Visible() bool
- func (zo *SpriteComp) Z() int16
- type SpriteRenderFeature
- func (f *SpriteRenderFeature) Draw(nodes RenderNodes)
- func (f *SpriteRenderFeature) Extract(v *View)
- func (f *SpriteRenderFeature) Flush()
- func (f *SpriteRenderFeature) Register(rs *RenderSystem)
- func (f *SpriteRenderFeature) SetRender(render *BatchRender)
- func (f *SpriteRenderFeature) SetTable(st *SpriteTable, xt *TransformTable)
- type SpriteTable
- func (st *SpriteTable) Alive(entity engi.Entity) bool
- func (st *SpriteTable) Comp(entity engi.Entity) (sc *SpriteComp)
- func (st *SpriteTable) Delete(entity engi.Entity)
- func (st *SpriteTable) Destroy()
- func (st *SpriteTable) NewComp(entity engi.Entity) (sc *SpriteComp)
- func (st *SpriteTable) NewCompX(entity engi.Entity, spt Tex2D) (sc *SpriteComp)
- func (st *SpriteTable) Size() (size, cap int)
- type StackAllocator
- type SubTex
- type Tex2D
- type TexManager
- type TextComp
- func (b *TextComp) BatchId() uint16
- func (tc *TextComp) Color() Color
- func (tc *TextComp) Font() font.Font
- func (tc *TextComp) FontSize() float32
- func (tc *TextComp) Gravity() (x, y float32)
- func (b *TextComp) SetBatchId(id uint16)
- func (tc *TextComp) SetColor(color Color)
- func (tc *TextComp) SetFont(fnt font.Font)
- func (tc *TextComp) SetFontSize(sz float32)
- func (tc *TextComp) SetGravity(x, y float32)
- func (tc *TextComp) SetText(text string)
- func (tc *TextComp) SetVisible(v bool)
- func (zo *TextComp) SetZOrder(z int16)
- func (tc *TextComp) Size() (w, h float32)
- func (tc *TextComp) Text() string
- func (tc *TextComp) Visible() bool
- func (zo *TextComp) Z() int16
- type TextQuad
- type TextRenderFeature
- type TextTable
- func (tt *TextTable) Alive(entity engi.Entity) bool
- func (tt *TextTable) Comp(entity engi.Entity) (tc *TextComp)
- func (tt *TextTable) Delete(entity engi.Entity) (tc *TextComp)
- func (tt *TextTable) Destroy()
- func (tt *TextTable) NewComp(entity engi.Entity) (tc *TextComp)
- func (tt *TextTable) Size() (size, cap int)
- type Transform
- func (xf *Transform) FirstChild() (c *Transform)
- func (xf *Transform) LinkChild(c *Transform)
- func (xf *Transform) LinkChildren(list ...*Transform)
- func (xf *Transform) Local() SRT
- func (xf *Transform) MoveBy(dx, dy float32)
- func (xf *Transform) Parent() (p *Transform)
- func (xf *Transform) Position() f32.Vec2
- func (xf *Transform) RemoveChild(c *Transform)
- func (xf *Transform) RotateBy(d float32)
- func (xf *Transform) Rotation() float32
- func (xf *Transform) Scale() f32.Vec2
- func (xf *Transform) ScaleBy(dx, dy float32)
- func (xf *Transform) SetPosition(position f32.Vec2)
- func (xf *Transform) SetRotation(rotation float32)
- func (xf *Transform) SetScale(scale f32.Vec2)
- func (xf *Transform) Sibling() (prev, next *Transform)
- func (xf *Transform) World() SRT
- type TransformTable
- func (tt *TransformTable) Alive(entity engi.Entity) bool
- func (tt *TransformTable) Comp(entity engi.Entity) (xf *Transform)
- func (tt *TransformTable) Delete(entity engi.Entity)
- func (tt *TransformTable) Destroy()
- func (tt *TransformTable) NewComp(entity engi.Entity) (xf *Transform)
- func (tt *TransformTable) Size() (size, cap int)
- type View
- type VisibilitySystem
Constants ¶
const ( ANCHOR_CENTER Anchor = 0x00 ANCHOR_LEFT = 0x01 ANCHOR_RIGHT = 0x02 ANCHOR_UP = 0x04 ANCHOR_DOWN = 0x08 )
const MAX_BATCH_QUAD_SIZE = uint32(8 << 10)
~ 640k per-batch, 32k vertex, 8k quad
const MAX_BATCH_VERTEX_SIZE = 4 * MAX_BATCH_QUAD_SIZE
const STEP = 64
Variables ¶
var ( White = Color{0xFF, 0xFF, 0xFF, 0xFF} Black = Color{0, 0, 0, 0xFF} LTGray = Color{0xCC, 0xCC, 0xCC, 0xFF} Gray = Color{0x88, 0x88, 0x88, 0xFF} DKGray = Color{0x44, 0x44, 0x44, 0xFF} Red = Color{0xFF, 0, 0, 0xFF} Green = Color{0, 0xFF, 0, 0xFF} Blue = Color{0, 0, 0xFF, 0xFF} Cyan = Color{0, 0xFF, 0xFF, 0xFF} Magenta = Color{0xFF, 00, 0xFF, 0xFF} Yellow = Color{0xFF, 0xFF, 0x00, 0xFF} Transparent = Color{} Opaque = Color{0xFF, 0xFF, 0xFF, 0xFF} )
var Context *context
global shared
var P4C4 = []bk.VertexComp{ {4, bk.AttrFloat, 0, 0}, {4, bk.AttrUInt8, 16, 1}, }
format <x,y,u,v rgba>
var PosTexColorVertexSize = unsafe.Sizeof(PosTexColorVertex{})
var UInt16Size = unsafe.Sizeof(uint16(0))
Functions ¶
func PackSortId ¶
func UnpackSortId ¶
Types ¶
type Atlas ¶
type Atlas struct {
// contains filtered or unexported fields
}
A small struct to describe a group of sub-texture
type Batch ¶
type Batch struct { TextureId uint16 VertexId uint16 IndexId uint16 // contains filtered or unexported fields }
* Batch 系统设计 对所有物体进行分类:静态和动态,静态物体使用稳定的batch系统,动态物体每次重新计算batch
静态: 使用稳定的batch面临的最大问题是,batch内存的物体的可见性问题。如果batch内的物体有的可见, 有的不可见(有可能是不再视野内,也有可能是主动隐藏). 采用 unity 方案,只减少状态切换,不减少 drawcall 的做法, 实现比较简单(否则要面临修改VBO缓存的问题)。
动态: 动态Batch每次都需要重新构建,只要实现正确的合并算法即可。
暗示:在 sortkey 中提供一个字段 batch=0 (默认情况使用动态batch),batch=1,2..N 的情况按batch值进行 分批处理。 优化:按照空间划分可以得到不同子空间的batch,可以在batch中再做一次筛选滤掉不可见的物体。
渲染: 静态系统,按照 batch-id 找到对应的batch数据,生成渲染命令 动态系统,直接合并数据并生成渲染命令
目前Batch实现只支持格式:pos_uv_color
type BatchContext ¶
type BatchContext struct { // batch-list BatchList [128]Batch // contains filtered or unexported fields }
管理一或多个Batch实例 最多可以生成 128 个 Batch 分组 最多可以使用 8 个 VBO 缓存
type BatchObject ¶
type BatchObject interface { Fill(vertex []PosTexColorVertex) Size() int }
type BatchRender ¶
type BatchRender struct { // batch context BatchContext // contains filtered or unexported fields }
/ A Tex2D Batch TypeRender
func NewBatchRender ¶
func NewBatchRender(vsh, fsh string) *BatchRender
func (*BatchRender) Begin ¶
func (br *BatchRender) Begin(tex uint16, depth int16)
func (*BatchRender) Draw ¶
func (br *BatchRender) Draw(b BatchObject)
func (*BatchRender) End ¶
func (br *BatchRender) End()
func (*BatchRender) Flush ¶
func (br *BatchRender) Flush() (num int)
func (*BatchRender) SetCamera ¶
func (br *BatchRender) SetCamera(camera *Camera)
type BoundingComp ¶
type BoundingComp struct {
BoundingBox
}
/ 每个Entity只有一个可见性对象,如果它包含多个 RenderComp,那么求出一个最大面积 / 作为该对象的可见面积
func (*BoundingComp) SetBounding ¶
func (bc *BoundingComp) SetBounding(bb *BoundingBox)
type BoundingTable ¶
type BoundingTable struct {
// contains filtered or unexported fields
}
func (*BoundingTable) Comp ¶
func (bt *BoundingTable) Comp(entity engi.Entity) (bc *BoundingComp)
func (*BoundingTable) NewComp ¶
func (bt *BoundingTable) NewComp(entity engi.Entity) (bc *BoundingComp)
type Camera ¶
func (*Camera) Scene2Screen ¶
Scene2Screen converts (x,y) in game's world coordinate to screen coordinate.
func (*Camera) Screen2Scene ¶
Screen2Scene converts (x,y) in screen coordinate to (x1,y1) in game's world coordinate.
func (*Camera) SetDesiredViewport ¶
type CompRef ¶
type CompRef struct { Type int32 *Transform *SpriteComp }
func (CompRef) SetBatchId ¶
func (b CompRef) SetBatchId(id uint16)
type Mesh ¶
type Mesh struct { IndexId uint16 VertexId uint16 FirstVertex uint16 NumVertex uint16 FirstIndex uint16 NumIndex uint16 // contains filtered or unexported fields }
func (*Mesh) SetTexture ¶
func (*Mesh) SetVertex ¶
func (m *Mesh) SetVertex(v []PosTexColorVertex)
type MeshComp ¶
func (*MeshComp) SetVisible ¶
type MeshRender ¶
type MeshRender struct {
// contains filtered or unexported fields
}
func NewMeshRender ¶
func NewMeshRender(vsh, fsh string) *MeshRender
func (*MeshRender) Extract ¶
func (mr *MeshRender) Extract(visibleObjects []uint32)
extract render object
func (*MeshRender) SetCamera ¶
func (mr *MeshRender) SetCamera(camera *Camera)
type MeshRenderFeature ¶
type MeshRenderFeature struct { Stack *StackAllocator R *MeshRender // contains filtered or unexported fields }
///
func (*MeshRenderFeature) Draw ¶
func (f *MeshRenderFeature) Draw(nodes RenderNodes)
func (*MeshRenderFeature) Extract ¶
func (f *MeshRenderFeature) Extract(v *View)
func (*MeshRenderFeature) Flush ¶
func (f *MeshRenderFeature) Flush()
func (*MeshRenderFeature) Register ¶
func (f *MeshRenderFeature) Register(rs *RenderSystem)
此处初始化所有的依赖
type MeshTable ¶
type MeshTable struct {
// contains filtered or unexported fields
}
func NewMeshTable ¶
type PosTexColorVertex ¶
vertex struct
type RenderFeature ¶
type RenderFeature interface { Extract(v *View) Draw(nodes RenderNodes) Flush() }
传入参数是经过可见性系统筛选后的 Entity,这是一个很小的数组,可以 直接传给各个 RenderFeature 来做可见性判断.
type RenderMesh ¶
type RenderNodes ¶
type RenderNodes []SortObject
func (RenderNodes) Len ¶
func (nodes RenderNodes) Len() int
func (RenderNodes) Less ¶
func (nodes RenderNodes) Less(i, j int) bool
func (RenderNodes) Swap ¶
func (nodes RenderNodes) Swap(i, j int)
type RenderObject ¶
Shared properties by most render object.
type RenderSystem ¶
type RenderSystem struct { MainCamera Camera View // visibility test V VisibilitySystem // render-data TableList []interface{} // render RenderList []Render // feature knows how to use render-data and render FeatureList []RenderFeature // contains filtered or unexported fields }
所有的Table和Render都在此管理 其它的 RenderFeature 在此提取依赖 这样的话, RenderSystem 就沦为一个管理 RenderFeature 和 Table 的地方 它们之间也会存在各种组合...
func NewRenderSystem ¶
func NewRenderSystem() (rs *RenderSystem)
func (*RenderSystem) Accept ¶
func (th *RenderSystem) Accept(rf RenderFeature) (index int)
func (*RenderSystem) Destroy ¶
func (th *RenderSystem) Destroy()
func (*RenderSystem) RegisterRender ¶
func (th *RenderSystem) RegisterRender(t RenderType, render Render)
register type-render
func (*RenderSystem) RequireTable ¶
func (th *RenderSystem) RequireTable(tables []interface{})
func (*RenderSystem) Update ¶
func (th *RenderSystem) Update(dt float32)
type RenderType ¶
type RenderType int32
type ShapeRender ¶
type ShapeRender struct { }
func (*ShapeRender) DrawCircle ¶
func (sr *ShapeRender) DrawCircle()
func (*ShapeRender) DrawLine ¶
func (sr *ShapeRender) DrawLine()
func (*ShapeRender) DrawPolygon ¶
func (sr *ShapeRender) DrawPolygon()
func (*ShapeRender) DrawRect ¶
func (sr *ShapeRender) DrawRect()
func (*ShapeRender) DrawTriangle ¶
func (sr *ShapeRender) DrawTriangle()
type SortObject ¶
type SpriteComp ¶
SpriteComp & SpriteTable Usually, sprite can be rendered with a BatchRenderer
func (*SpriteComp) Color ¶
func (sc *SpriteComp) Color() Color
func (*SpriteComp) Flip ¶
func (sc *SpriteComp) Flip(flipX, flipY bool)
func (*SpriteComp) Gravity ¶
func (sc *SpriteComp) Gravity() (x, y float32)
func (*SpriteComp) SetBatchId ¶
func (b *SpriteComp) SetBatchId(id uint16)
func (*SpriteComp) SetColor ¶
func (sc *SpriteComp) SetColor(c Color)
func (*SpriteComp) SetGravity ¶
func (sc *SpriteComp) SetGravity(x, y float32)
func (*SpriteComp) SetSize ¶
func (sc *SpriteComp) SetSize(w, h float32)
func (*SpriteComp) SetSprite ¶
func (sc *SpriteComp) SetSprite(spt Sprite)
func (*SpriteComp) SetVisible ¶
func (sc *SpriteComp) SetVisible(v bool)
func (*SpriteComp) Size ¶
func (sc *SpriteComp) Size() (w, h float32)
func (*SpriteComp) Visible ¶
func (sc *SpriteComp) Visible() bool
type SpriteRenderFeature ¶
type SpriteRenderFeature struct { Stack *StackAllocator R *BatchRender // contains filtered or unexported fields }
///
func (*SpriteRenderFeature) Draw ¶
func (f *SpriteRenderFeature) Draw(nodes RenderNodes)
func (*SpriteRenderFeature) Extract ¶
func (f *SpriteRenderFeature) Extract(v *View)
func (*SpriteRenderFeature) Flush ¶
func (f *SpriteRenderFeature) Flush()
func (*SpriteRenderFeature) Register ¶
func (f *SpriteRenderFeature) Register(rs *RenderSystem)
此处初始化所有的依赖
func (*SpriteRenderFeature) SetRender ¶
func (f *SpriteRenderFeature) SetRender(render *BatchRender)
func (*SpriteRenderFeature) SetTable ¶
func (f *SpriteRenderFeature) SetTable(st *SpriteTable, xt *TransformTable)
type SpriteTable ¶
type SpriteTable struct {
// contains filtered or unexported fields
}
func NewSpriteTable ¶
func NewSpriteTable(cap int) *SpriteTable
func (*SpriteTable) Comp ¶
func (st *SpriteTable) Comp(entity engi.Entity) (sc *SpriteComp)
func (*SpriteTable) Delete ¶
func (st *SpriteTable) Delete(entity engi.Entity)
func (*SpriteTable) Destroy ¶
func (st *SpriteTable) Destroy()
func (*SpriteTable) NewComp ¶
func (st *SpriteTable) NewComp(entity engi.Entity) (sc *SpriteComp)
func (*SpriteTable) NewCompX ¶
func (st *SpriteTable) NewCompX(entity engi.Entity, spt Tex2D) (sc *SpriteComp)
New SpriteComp with parameter
func (*SpriteTable) Size ¶
func (st *SpriteTable) Size() (size, cap int)
type StackAllocator ¶
type StackAllocator struct {
// contains filtered or unexported fields
}
// This is a stack allocator used for fast per step allocations.
func (*StackAllocator) Alloc ¶
func (stack *StackAllocator) Alloc(size int32) (ptr unsafe.Pointer)
Alloc alloc memory on the stack.
func (*StackAllocator) Free ¶
func (stack *StackAllocator) Free(size int32)
Free frees memory on the stack, it should pair with Alloc.
type SubTex ¶
type SubTex struct {
// contains filtered or unexported fields
}
SubTexture = (atlas-id << 16) + SubTexture-id
type Tex2D ¶
type Tex2D interface { // return texture id Tex() uint16 // uv Region() Region // size Size() Size }
Tex2D is a Texture or a SubTexture
type TexManager ¶
type TexManager struct {
// contains filtered or unexported fields
}
Texture Resource Manager
var R *TexManager
TextureManager as a global variable.
func (*TexManager) Atlas ¶
func (tm *TexManager) Atlas(name string) (at *Atlas)
func (*TexManager) Delete ¶
func (tm *TexManager) Delete(name string)
type TextComp ¶
TextSprite
func (*TextComp) SetBatchId ¶
func (b *TextComp) SetBatchId(id uint16)
func (*TextComp) SetFontSize ¶
func (*TextComp) SetGravity ¶
func (*TextComp) SetVisible ¶
type TextRenderFeature ¶
type TextRenderFeature struct { Stack *StackAllocator R *BatchRender // contains filtered or unexported fields }
func (*TextRenderFeature) Draw ¶
func (f *TextRenderFeature) Draw(nodes RenderNodes)
func (*TextRenderFeature) Extract ¶
func (f *TextRenderFeature) Extract(v *View)
func (*TextRenderFeature) Flush ¶
func (f *TextRenderFeature) Flush()
func (*TextRenderFeature) Register ¶
func (f *TextRenderFeature) Register(rs *RenderSystem)
此处初始化所有的依赖
type TextTable ¶
type TextTable struct {
// contains filtered or unexported fields
}
TextTable
func NewTextTable ¶
type Transform ¶
还可以做更细的拆分,把 Matrix 全部放到一个数组里面 position, position, position .... position rotation, rotation, rotation .... rotation 把这两个数组申请为一块,然后分两个小组使用
func (*Transform) FirstChild ¶
func (*Transform) LinkChildren ¶
func (*Transform) RemoveChild ¶
func (*Transform) SetPosition ¶
Set local position relative to parent
type TransformTable ¶
type TransformTable struct {
// contains filtered or unexported fields
}
func NewTransformTable ¶
func NewTransformTable(cap int) *TransformTable
func (*TransformTable) Comp ¶
func (tt *TransformTable) Comp(entity engi.Entity) (xf *Transform)
Return the TransformComp or nil
func (*TransformTable) Delete ¶
func (tt *TransformTable) Delete(entity engi.Entity)
Swap erase the TransformComp if exist Delete will unlink the parent-child relation
func (*TransformTable) Destroy ¶
func (tt *TransformTable) Destroy()
func (*TransformTable) NewComp ¶
func (tt *TransformTable) NewComp(entity engi.Entity) (xf *Transform)
Create a new TransformComp for the entity, Return the old one, if it already exist .
func (*TransformTable) Size ¶
func (tt *TransformTable) Size() (size, cap int)
type View ¶
type View struct { *Camera RenderNodes }
type VisibilitySystem ¶
type VisibilitySystem interface { UpdateBounding(id int32, bb BoundingBox) UpdateTransform() Collect(camera *Camera) []engi.Entity }