Skip to content

Commit 40a4016

Browse files
committed
Adding image pivot, ppu, and max size entries
1 parent 84266ba commit 40a4016

File tree

5 files changed

+136
-37
lines changed

5 files changed

+136
-37
lines changed

src/editor/content/content_opener/image_opener.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,28 @@ func (o ImageOpener) Open(adi asset_info.AssetDatabaseInfo, ed editor_interface.
6464
console.For(ed.Host()).Write("Opening an image")
6565
host := ed.Host()
6666
meta := adi.Metadata.(*asset_importer.ImageMetadata)
67-
texture, err := host.TextureCache().Texture(adi.Path, meta.TextureFilter())
67+
texture, err := host.TextureCache().Texture(adi.Path, meta.ImageFilterMeta())
6868
if err != nil {
6969
return errors.New("failed to load the texture " + adi.Path)
7070
}
71+
texture.MipLevels = int(meta.Mipmaps)
7172
// TODO: Swap this to sprite and remove the visuals2d sprite stuff and make it 3D
7273
mat, err := host.MaterialCache().Material(assets.MaterialDefinitionBasic)
7374
if err != nil {
7475
return errors.New("failed to find the sprite material")
7576
}
76-
mesh := rendering.NewMeshQuad(host.MeshCache())
77+
mesh := rendering.NewMeshQuadAnchored(meta.ImagePivotMeta(), host.MeshCache())
78+
ppu := matrix.Float(max(1, meta.PixelsPerUnit))
79+
scaled := matrix.Vec2{
80+
matrix.Float(texture.Width) / ppu,
81+
matrix.Float(texture.Height) / ppu,
82+
}
7783
e := engine.NewEntity(ed.Host().WorkGroup())
7884
e.GenerateId()
7985
p := ed.Camera().LookAtPoint()
8086
p.SetZ(0)
8187
e.Transform.SetPosition(p)
88+
e.Transform.SetScale(matrix.NewVec3(scaled.X(), scaled.Y(), 1))
8289
host.AddEntity(e)
8390
e.SetName(strings.TrimSuffix(filepath.Base(adi.Path), filepath.Ext(adi.Path)))
8491
// TODO: Swap this to a sprite basic that has control over UVs

src/engine/assets/asset_importer/image_importer.go

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,70 @@ import (
55
"log/slog"
66
)
77

8+
var (
9+
// Registered in meta_options_export.go
10+
imageFilterOptions = map[string]rendering.TextureFilter{
11+
"Linear": rendering.TextureFilterLinear,
12+
"Nearest": rendering.TextureFilterNearest,
13+
}
14+
imagePivot = map[string]rendering.QuadPivot{
15+
"Center": rendering.QuadPivotCenter,
16+
"Left": rendering.QuadPivotLeft,
17+
"Top": rendering.QuadPivotTop,
18+
"Right": rendering.QuadPivotRight,
19+
"Bottom": rendering.QuadPivotBottom,
20+
"Bottom left": rendering.QuadPivotBottomLeft,
21+
"Bottom right": rendering.QuadPivotBottomRight,
22+
"Top left": rendering.QuadPivotTopLeft,
23+
"Top right": rendering.QuadPivotTopRight,
24+
}
25+
imageMaxSize = map[string]int32{
26+
"16": 16,
27+
"32": 32,
28+
"64": 64,
29+
"128": 128,
30+
"256": 256,
31+
"512": 512,
32+
"1024": 1024,
33+
"2048": 2048,
34+
"4096": 4096,
35+
"8192": 8192,
36+
}
37+
)
38+
839
type ImageMetadata struct {
9-
Filter string `options:"textureFilterOptions"`
10-
Mipmaps int32
40+
Filter string `options:"imageFilterOptions" default:"Linear"`
41+
Pivot string `options:"imagePivot" default:"Center"`
42+
PixelsPerUnit int32 `default:"100"`
43+
Mipmaps int32 `default:"1"`
44+
45+
// TODO: This needs to be used for packaging the content
46+
MaxSize string `options:"imageMaxSize" default:"8192"`
1147
}
1248

13-
func (m *ImageMetadata) TextureFilter() rendering.TextureFilter {
14-
if f, ok := textureFilterOptions[m.Filter]; ok {
49+
func (m *ImageMetadata) ImageFilterMeta() rendering.TextureFilter {
50+
if f, ok := imageFilterOptions[m.Filter]; ok {
1551
return f
1652
}
17-
slog.Warn("tried to read image metadata filter but has invalid key",
53+
slog.Warn("tried to read image filter metadata but has invalid key",
1854
"key", m.Filter)
1955
return rendering.TextureFilterLinear
2056
}
57+
58+
func (m *ImageMetadata) ImagePivotMeta() rendering.QuadPivot {
59+
if f, ok := imagePivot[m.Pivot]; ok {
60+
return f
61+
}
62+
slog.Warn("tried to read image pivot metadata but has invalid key",
63+
"key", m.Pivot)
64+
return rendering.QuadPivotCenter
65+
}
66+
67+
func (m *ImageMetadata) MaxSizeMeta() int32 {
68+
if f, ok := imageMaxSize[m.MaxSize]; ok {
69+
return f
70+
}
71+
slog.Warn("tried to read image max size metadata but has invalid key",
72+
"key", m.MaxSize)
73+
return 8192
74+
}

src/engine/assets/asset_importer/meta_options_export.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package asset_importer
22

33
var (
44
MetaOptions = map[string]any{
5-
"textureFilterOptions": textureFilterOptions,
5+
"imageFilterOptions": imageFilterOptions,
6+
"imagePivot": imagePivot,
7+
"imageMaxSize": imageMaxSize,
68
}
79
)

src/engine/assets/asset_importer/png_importer.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,9 @@ package asset_importer
4040
import (
4141
"kaiju/editor/editor_config"
4242
"kaiju/engine/assets/asset_info"
43-
"kaiju/rendering"
4443
"path/filepath"
4544
)
4645

47-
var (
48-
textureFilterOptions = map[string]rendering.TextureFilter{
49-
"Linear": rendering.TextureFilterLinear,
50-
"Nearest": rendering.TextureFilterNearest,
51-
}
52-
)
53-
5446
type PngImporter struct{}
5547

5648
func (m PngImporter) MetadataStructure() any {

src/rendering/mesh.go

Lines changed: 65 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import (
4444

4545
type MeshDrawMode = int
4646
type MeshCullMode = int
47+
type QuadPivot = int32
4748

4849
const (
4950
MeshDrawModePoints MeshDrawMode = iota
@@ -58,6 +59,18 @@ const (
5859
MeshCullModeNone
5960
)
6061

62+
const (
63+
QuadPivotCenter = QuadPivot(iota)
64+
QuadPivotLeft
65+
QuadPivotTop
66+
QuadPivotRight
67+
QuadPivotBottom
68+
QuadPivotBottomLeft
69+
QuadPivotBottomRight
70+
QuadPivotTopLeft
71+
QuadPivotTopRight
72+
)
73+
6174
type Mesh struct {
6275
MeshId MeshId
6376
key string
@@ -121,30 +134,61 @@ func (m *Mesh) DelayedCreate(renderer Renderer) {
121134
func (m Mesh) Key() string { return m.key }
122135
func (m Mesh) IsReady() bool { return m.MeshId.IsValid() }
123136

124-
func NewMeshQuad(cache *MeshCache) *Mesh {
125-
const key = "quad"
137+
var (
138+
meshQuadUvs = [4]matrix.Vec2{{0, 1}, {0, 0}, {1, 0}, {1, 1}}
139+
meshQuadIndexes = [6]uint32{0, 2, 1, 0, 3, 2}
140+
meshQuadCenter = [4]matrix.Vec3{{-0.5, -0.5, 0}, {-0.5, 0.5, 0}, {0.5, 0.5, 0}, {0.5, -0.5, 0}}
141+
meshQuadLeft = [4]matrix.Vec3{{0, -0.5, 0}, {0, 0.5, 0}, {1, 0.5, 0}, {1, -0.5, 0}}
142+
meshQuadTop = [4]matrix.Vec3{{-0.5, -1, 0}, {-0.5, 0, 0}, {0.5, 0, 0}, {0.5, -1, 0}}
143+
meshQuadRight = [4]matrix.Vec3{{-1, -0.5, 0}, {-1, 0.5, 0}, {0, 0.5, 0}, {0, -0.5, 0}}
144+
meshQuadBottom = [4]matrix.Vec3{{-0.5, 0, 0}, {-0.5, 1, 0}, {0.5, 1, 0}, {0.5, 0, 0}}
145+
meshQuadBottomLeft = [4]matrix.Vec3{{0, 0, 0}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0}}
146+
meshQuadBottomRight = [4]matrix.Vec3{{-1, 0, 0}, {-1, 1, 0}, {0, 1, 0}, {0, 0, 0}}
147+
meshQuadTopLeft = [4]matrix.Vec3{{0, -1, 0}, {0, 0, 0}, {1, 0, 0}, {1, -1, 0}}
148+
meshQuadTopRight = [4]matrix.Vec3{{-1, -1, 0}, {-1, 0, 0}, {0, 0, 0}, {0, -1, 0}}
149+
)
150+
151+
func newMeshQuad(key string, points [4]matrix.Vec3, cache *MeshCache) *Mesh {
126152
if mesh, ok := cache.FindMesh(key); ok {
127153
return mesh
128154
} else {
129-
verts := make([]Vertex, 4)
130-
verts[0].Position = matrix.Vec3{-0.5, -0.5, 0.0}
131-
verts[0].Normal = matrix.Vec3{0.0, 0.0, 1.0}
132-
verts[0].UV0 = matrix.Vec2{0.0, 1.0}
133-
verts[0].Color = matrix.ColorWhite()
134-
verts[1].Position = matrix.Vec3{-0.5, 0.5, 0.0}
135-
verts[1].Normal = matrix.Vec3{0.0, 0.0, 1.0}
136-
verts[1].UV0 = matrix.Vec2{0.0, 0.0}
137-
verts[1].Color = matrix.ColorWhite()
138-
verts[2].Position = matrix.Vec3{0.5, 0.5, 0.0}
139-
verts[2].Normal = matrix.Vec3{0.0, 0.0, 1.0}
140-
verts[2].UV0 = matrix.Vec2{1.0, 0.0}
141-
verts[2].Color = matrix.ColorWhite()
142-
verts[3].Position = matrix.Vec3{0.5, -0.5, 0.0}
143-
verts[3].Normal = matrix.Vec3{0.0, 0.0, 1.0}
144-
verts[3].UV0 = matrix.Vec2{1.0, 1.0}
145-
verts[3].Color = matrix.ColorWhite()
146-
indexes := []uint32{0, 2, 1, 0, 3, 2}
147-
return cache.Mesh(key, verts, indexes)
155+
verts := make([]Vertex, len(points))
156+
for i := range points {
157+
verts[i].Position = points[i]
158+
verts[i].Normal = matrix.Vec3{0.0, 0.0, 1.0}
159+
verts[i].UV0 = meshQuadUvs[i]
160+
verts[i].Color = matrix.ColorWhite()
161+
}
162+
return cache.Mesh(key, verts, meshQuadIndexes[:])
163+
}
164+
}
165+
166+
func NewMeshQuad(cache *MeshCache) *Mesh {
167+
return NewMeshQuadAnchored(QuadPivotCenter, cache)
168+
}
169+
170+
func NewMeshQuadAnchored(anchor QuadPivot, cache *MeshCache) *Mesh {
171+
switch anchor {
172+
case QuadPivotLeft:
173+
return newMeshQuad("quad_left", meshQuadLeft, cache)
174+
case QuadPivotTop:
175+
return newMeshQuad("quad_top", meshQuadTop, cache)
176+
case QuadPivotRight:
177+
return newMeshQuad("quad_right", meshQuadRight, cache)
178+
case QuadPivotBottom:
179+
return newMeshQuad("quad_bottom", meshQuadBottom, cache)
180+
case QuadPivotBottomLeft:
181+
return newMeshQuad("quad_bottom_left", meshQuadBottomLeft, cache)
182+
case QuadPivotBottomRight:
183+
return newMeshQuad("quad_bottom_right", meshQuadBottomRight, cache)
184+
case QuadPivotTopLeft:
185+
return newMeshQuad("quad_top_left", meshQuadTopLeft, cache)
186+
case QuadPivotTopRight:
187+
return newMeshQuad("quad_top_right", meshQuadTopRight, cache)
188+
case QuadPivotCenter:
189+
fallthrough
190+
default:
191+
return newMeshQuad("quad", meshQuadCenter, cache)
148192
}
149193
}
150194

0 commit comments

Comments
 (0)