Skip to content

Commit c90c348

Browse files
authored
Move normalization to shader (#600)
1 parent 7f9d267 commit c90c348

File tree

4 files changed

+28
-25
lines changed

4 files changed

+28
-25
lines changed

src/constants.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// some default charsets for loading bitmap fonts
22
export const ASCII_CHARS =
3-
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
3+
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
44
export const DEF_ANCHOR = "topleft";
55
export const BG_GRID_SIZE = 64;
66
export const DEF_FONT = "monospace";
@@ -19,9 +19,9 @@ export const DEF_FONT_FILTER = "linear";
1919
export const LOG_MAX = 8;
2020
export const LOG_TIME = 4;
2121
export const VERTEX_FORMAT = [
22-
{ name: "a_pos", size: 2 },
23-
{ name: "a_uv", size: 2 },
24-
{ name: "a_color", size: 4 },
22+
{ name: "a_pos", size: 2 },
23+
{ name: "a_uv", size: 2 },
24+
{ name: "a_color", size: 4 },
2525
];
2626
const STRIDE = VERTEX_FORMAT.reduce((sum, f) => sum + f.size, 0);
2727
const MAX_BATCHED_QUAD = 2048;
@@ -37,8 +37,11 @@ varying vec2 v_pos;
3737
varying vec2 v_uv;
3838
varying vec4 v_color;
3939
40+
uniform float width;
41+
uniform float height;
42+
4043
vec4 def_vert() {
41-
return vec4(a_pos, 0.0, 1.0);
44+
return vec4(a_pos.x / width * 2.0 - 1.0, a_pos.y / -height * 2.0 + 1.0, 0.0, 1.0);
4245
}
4346
4447
{{user}}
@@ -89,13 +92,13 @@ vec4 frag(vec2 pos, vec2 uv, vec4 color, sampler2D tex) {
8992
`;
9093
export const COMP_DESC = new Set(["id", "require"]);
9194
export const COMP_EVENTS = new Set([
92-
"add",
93-
"fixedUpdate",
94-
"update",
95-
"draw",
96-
"destroy",
97-
"inspect",
98-
"drawInspect",
95+
"add",
96+
"fixedUpdate",
97+
"update",
98+
"draw",
99+
"destroy",
100+
"inspect",
101+
"drawInspect",
99102
]);
100103
export const DEF_OFFSCREEN_DIS = 200;
101104
// maximum y velocity with body()

src/gfx/draw/drawRaw.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,11 @@ export function drawRaw(
3131
const vertLength = attributes.pos.length / 2;
3232
const vv: number[] = new Array(vertLength * 8);
3333

34-
const w = width();
35-
const h = height();
3634
let index = 0;
3735
for (let i = 0; i < vertLength; i++) {
3836
scratchPt.x = attributes.pos[i * 2];
3937
scratchPt.y = attributes.pos[i * 2 + 1];
40-
// normalized world space coordinate [-1.0 ~ 1.0]
41-
screen2ndc(
42-
transform.transformPoint(scratchPt, scratchPt),
43-
w,
44-
h,
45-
scratchPt,
46-
);
38+
transform.transformPoint(scratchPt, scratchPt)
4739

4840
vv[index++] = scratchPt.x;
4941
vv[index++] = scratchPt.y;
@@ -62,5 +54,7 @@ export function drawRaw(
6254
shader,
6355
parsedTex,
6456
uniform,
57+
width(),
58+
height()
6559
);
6660
}

src/gfx/gfx.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ export class BatchRenderer {
164164
shader: Shader,
165165
tex: Texture | null = null,
166166
uniform: Uniform | null = null,
167+
width: number,
168+
height: number
167169
) {
168170
if (
169171
primitive !== this.curPrimitive
@@ -172,10 +174,10 @@ export class BatchRenderer {
172174
|| ((this.curUniform != uniform)
173175
&& !deepEq(this.curUniform, uniform))
174176
|| this.vqueue.length + verts.length * this.stride
175-
> this.maxVertices
177+
> this.maxVertices
176178
|| this.iqueue.length + indices.length > this.maxIndices
177179
) {
178-
this.flush();
180+
this.flush(width, height);
179181
}
180182
const indexOffset = this.vqueue.length / this.stride;
181183
let l = verts.length;
@@ -192,7 +194,7 @@ export class BatchRenderer {
192194
this.curUniform = uniform;
193195
}
194196

195-
flush() {
197+
flush(width: number, height: number) {
196198
if (
197199
!this.curPrimitive
198200
|| !this.curShader
@@ -217,6 +219,10 @@ export class BatchRenderer {
217219
if (this.curUniform) {
218220
this.curShader.send(this.curUniform);
219221
}
222+
this.curShader.send({
223+
width,
224+
height
225+
});
220226
this.curTex?.bind();
221227
gl.drawElements(
222228
this.curPrimitive,

src/gfx/stack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export function popTransform() {
4848
}
4949

5050
export function flush() {
51-
_k.gfx.renderer.flush();
51+
_k.gfx.renderer.flush(width(), height());
5252
}
5353

5454
// get game width

0 commit comments

Comments
 (0)