Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move normalization to shader #600

Merged
merged 1 commit into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// some default charsets for loading bitmap fonts
export const ASCII_CHARS =
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
export const DEF_ANCHOR = "topleft";
export const BG_GRID_SIZE = 64;
export const DEF_FONT = "monospace";
Expand All @@ -19,9 +19,9 @@ export const DEF_FONT_FILTER = "linear";
export const LOG_MAX = 8;
export const LOG_TIME = 4;
export const VERTEX_FORMAT = [
{ name: "a_pos", size: 2 },
{ name: "a_uv", size: 2 },
{ name: "a_color", size: 4 },
{ name: "a_pos", size: 2 },
{ name: "a_uv", size: 2 },
{ name: "a_color", size: 4 },
];
const STRIDE = VERTEX_FORMAT.reduce((sum, f) => sum + f.size, 0);
const MAX_BATCHED_QUAD = 2048;
Expand All @@ -37,8 +37,11 @@ varying vec2 v_pos;
varying vec2 v_uv;
varying vec4 v_color;

uniform float width;
uniform float height;

vec4 def_vert() {
return vec4(a_pos, 0.0, 1.0);
return vec4(a_pos.x / width * 2.0 - 1.0, a_pos.y / -height * 2.0 + 1.0, 0.0, 1.0);
}

{{user}}
Expand Down Expand Up @@ -89,13 +92,13 @@ vec4 frag(vec2 pos, vec2 uv, vec4 color, sampler2D tex) {
`;
export const COMP_DESC = new Set(["id", "require"]);
export const COMP_EVENTS = new Set([
"add",
"fixedUpdate",
"update",
"draw",
"destroy",
"inspect",
"drawInspect",
"add",
"fixedUpdate",
"update",
"draw",
"destroy",
"inspect",
"drawInspect",
]);
export const DEF_OFFSCREEN_DIS = 200;
// maximum y velocity with body()
Expand Down
12 changes: 3 additions & 9 deletions src/gfx/draw/drawRaw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,11 @@ export function drawRaw(
const vertLength = attributes.pos.length / 2;
const vv: number[] = new Array(vertLength * 8);

const w = width();
const h = height();
let index = 0;
for (let i = 0; i < vertLength; i++) {
scratchPt.x = attributes.pos[i * 2];
scratchPt.y = attributes.pos[i * 2 + 1];
// normalized world space coordinate [-1.0 ~ 1.0]
screen2ndc(
transform.transformPoint(scratchPt, scratchPt),
w,
h,
scratchPt,
);
transform.transformPoint(scratchPt, scratchPt)

vv[index++] = scratchPt.x;
vv[index++] = scratchPt.y;
Expand All @@ -62,5 +54,7 @@ export function drawRaw(
shader,
parsedTex,
uniform,
width(),
height()
);
}
12 changes: 9 additions & 3 deletions src/gfx/gfx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ export class BatchRenderer {
shader: Shader,
tex: Texture | null = null,
uniform: Uniform | null = null,
width: number,
height: number
) {
if (
primitive !== this.curPrimitive
Expand All @@ -172,10 +174,10 @@ export class BatchRenderer {
|| ((this.curUniform != uniform)
&& !deepEq(this.curUniform, uniform))
|| this.vqueue.length + verts.length * this.stride
> this.maxVertices
> this.maxVertices
|| this.iqueue.length + indices.length > this.maxIndices
) {
this.flush();
this.flush(width, height);
}
const indexOffset = this.vqueue.length / this.stride;
let l = verts.length;
Expand All @@ -192,7 +194,7 @@ export class BatchRenderer {
this.curUniform = uniform;
}

flush() {
flush(width: number, height: number) {
if (
!this.curPrimitive
|| !this.curShader
Expand All @@ -217,6 +219,10 @@ export class BatchRenderer {
if (this.curUniform) {
this.curShader.send(this.curUniform);
}
this.curShader.send({
width,
height
});
this.curTex?.bind();
gl.drawElements(
this.curPrimitive,
Expand Down
2 changes: 1 addition & 1 deletion src/gfx/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function popTransform() {
}

export function flush() {
_k.gfx.renderer.flush();
_k.gfx.renderer.flush(width(), height());
}

// get game width
Expand Down
Loading