@@ -44,6 +44,7 @@ import (
44
44
45
45
type MeshDrawMode = int
46
46
type MeshCullMode = int
47
+ type QuadPivot = int32
47
48
48
49
const (
49
50
MeshDrawModePoints MeshDrawMode = iota
@@ -58,6 +59,18 @@ const (
58
59
MeshCullModeNone
59
60
)
60
61
62
+ const (
63
+ QuadPivotCenter = QuadPivot (iota )
64
+ QuadPivotLeft
65
+ QuadPivotTop
66
+ QuadPivotRight
67
+ QuadPivotBottom
68
+ QuadPivotBottomLeft
69
+ QuadPivotBottomRight
70
+ QuadPivotTopLeft
71
+ QuadPivotTopRight
72
+ )
73
+
61
74
type Mesh struct {
62
75
MeshId MeshId
63
76
key string
@@ -121,30 +134,61 @@ func (m *Mesh) DelayedCreate(renderer Renderer) {
121
134
func (m Mesh ) Key () string { return m .key }
122
135
func (m Mesh ) IsReady () bool { return m .MeshId .IsValid () }
123
136
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 {
126
152
if mesh , ok := cache .FindMesh (key ); ok {
127
153
return mesh
128
154
} 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 )
148
192
}
149
193
}
150
194
0 commit comments