Skip to content

Commit a4c1fdf

Browse files
authored
Merge pull request #29 from codewithkyle/webgl-rewrite
WebGL Rewrite
2 parents 15e20c5 + 66a9f21 commit a4c1fdf

File tree

19 files changed

+1111
-268
lines changed

19 files changed

+1111
-268
lines changed

client/src/components/window/windows/fog-brush/fog-brush.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ export default class FogBrush extends SuperComponent<IFogBrush> {
110110
line.setAttribute('stroke', 'yellow');
111111
line.setAttribute('stroke-width', "1");
112112
this.polyPreviewEl.appendChild(line);
113-
console.log("line", line);
114113
}
115114
}
116115

client/src/pages/tabletop-page/tabletop-component/doodle-canvas/doodle-canvas.scss

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
doodle-canvas{
22
position: absolute;
3-
top: 50%;
4-
left: 50%;
3+
top: 0;
4+
left: 0;
55
pointer-events: none;
6-
transform: translate(-50%, -50%);
6+
//transform: translate(-50%, -50%);
77

88
canvas{
99
display: inline-block;

client/src/pages/tabletop-page/tabletop-component/doodle-canvas/doodle-canvas.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,21 +126,21 @@ export default class DoodleCanvas extends SuperComponent<IDoodleCanvas>{
126126
}
127127

128128
// @ts-ignore
129-
override render(image: HTMLImageElement): void {
130-
if (!image) {
129+
override render(w:number, h:number): void {
130+
if (w == 0 || h == 0) {
131131
this.w = 0;
132132
this.h = 0;
133133
this.canvas.width = this.w;
134134
this.canvas.height = this.h;
135135
this.canvas.style.width = `0px`;
136136
this.canvas.style.height = `0px`;
137137
} else {
138-
this.w = image.width;
139-
this.h = image.height;
138+
this.w = w;
139+
this.h = h;
140140
this.canvas.width = this.w;
141141
this.canvas.height = this.h;
142-
this.canvas.style.width = `${image.width}px`;
143-
this.canvas.style.height = `${image.height}px`;
142+
this.canvas.style.width = `${w}px`;
143+
this.canvas.style.height = `${h}px`;
144144
}
145145
this.renderDoodles();
146146
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
export const fog_mask_vert_shader =
2+
`#version 300 es
3+
4+
in vec2 a_position;
5+
6+
void main() {
7+
gl_Position = vec4(a_position, 0.0, 1.0);
8+
}
9+
`;
10+
11+
export const fog_mask_frag_shader =
12+
`#version 300 es
13+
precision mediump float;
14+
15+
out vec4 outColor;
16+
17+
void main() {
18+
outColor = vec4(1.0, 1.0, 1.0, 1.0);
19+
}
20+
`;
21+
22+
export const fog_composite_vert_shader =
23+
`#version 300 es
24+
25+
in vec2 a_position;
26+
in vec2 a_texCoord;
27+
out vec2 v_texCoord;
28+
29+
uniform vec2 u_resolution;
30+
uniform vec2 u_translation;
31+
uniform vec2 u_scale;
32+
33+
void main() {
34+
// apply translation
35+
vec2 translatedPosition = a_position * u_scale + u_translation;
36+
37+
// convert pixel coord to normalized device coord
38+
vec2 zeroToOne = translatedPosition / u_resolution;
39+
vec2 zeroToTwo = zeroToOne * 2.0;
40+
vec2 clipSpace = zeroToTwo - 1.0;
41+
42+
gl_Position = vec4(clipSpace * vec2(1, -1), 0, 1); // flip y-axis
43+
v_texCoord = a_texCoord; // pass tex coords
44+
}
45+
`;
46+
47+
export const fog_composite_frag_shader =
48+
`#version 300 es
49+
precision mediump float;
50+
51+
// Varying texture coordinate from the vertex shader
52+
in vec2 v_texCoord;
53+
54+
// The two texture uniforms: one for the custom image and one for the mask.
55+
uniform sampler2D u_image;
56+
uniform sampler2D u_mask;
57+
uniform vec4 u_color;
58+
uniform int u_isGM;
59+
60+
// Output color of the fragment.
61+
out vec4 fragColor;
62+
63+
void main() {
64+
// Sample the custom image texture at the given UV coordinates.
65+
vec4 imageColor = texture(u_image, v_texCoord);
66+
67+
// Sample the mask texture at the same UV coordinates.
68+
// Assuming the mask is in grayscale, we only need one channel.
69+
float maskValue = texture(u_mask, v_texCoord).r;
70+
71+
// Use a threshold to decide if the mask is "white" (revealed).
72+
// You can adjust the threshold (here 0.5) as needed.
73+
if(maskValue > 0.5) {
74+
fragColor = imageColor; // Reveal the image where the mask is white.
75+
} else {
76+
if (u_isGM == 1) {
77+
fragColor = mix(imageColor, u_color, 0.5); // Otherwise, output black.
78+
} else {
79+
fragColor = u_color;
80+
}
81+
}
82+
}
83+
84+
`;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
export const grid_vert_shader =
2+
`#version 300 es
3+
4+
in vec2 a_position;
5+
out vec2 v_clipPos;
6+
7+
void main() {
8+
gl_Position = vec4(a_position, 0.0, 1.0);
9+
v_clipPos = a_position;
10+
}
11+
`;
12+
13+
export const grid_frag_shader =
14+
`#version 300 es
15+
precision highp float;
16+
17+
uniform vec2 u_origin; // The line's origin in top‐left coords
18+
uniform vec2 u_resolution; // (width, height) of the screen
19+
uniform float u_spacing; // grid spacing
20+
uniform vec2 u_offset;
21+
uniform float u_scale;
22+
uniform vec4 u_color;
23+
24+
out vec4 outColor;
25+
26+
void main() {
27+
// gl_FragCoord is the window coordinate:
28+
// gl_FragCoord.x in [0, W], with 0 at LEFT, W at RIGHT
29+
// gl_FragCoord.y in [0, H], with 0 at BOTTOM, H at TOP
30+
31+
// But your "top‐right origin" system wants:
32+
// (0,0) in the TOP‐RIGHT
33+
// x increasing leftward => x = W - gl_FragCoord.x
34+
// y increasing downward => y = H - gl_FragCoord.y
35+
36+
float worldX = gl_FragCoord.x;
37+
float worldY = u_resolution.y - gl_FragCoord.y;
38+
39+
float cellSize = u_spacing * u_scale;
40+
41+
// Compare the fragment's coordinate to the origin
42+
float distX = mod((worldX - u_origin.x - u_offset.x), cellSize);
43+
float distY = mod((worldY - u_origin.y - u_offset.y), cellSize);
44+
45+
bool onVertical = distX < 1.0;
46+
bool onHorizontal = distY < 1.0;
47+
48+
// If we're within the line width of either axis, draw it
49+
if (onVertical || onHorizontal) {
50+
outColor = u_color;
51+
} else {
52+
outColor = vec4(0.0, 0.0, 0.0, 0.0); // transparent background
53+
}
54+
}
55+
`;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
export const map_vert_shader =
2+
`#version 300 es
3+
in vec2 a_position;
4+
in vec2 a_texCoord;
5+
out vec2 v_texCoord;
6+
7+
uniform vec2 u_resolution;
8+
uniform vec2 u_translation;
9+
uniform vec2 u_scale;
10+
11+
void main() {
12+
// apply translation
13+
vec2 translatedPosition = a_position * u_scale + u_translation;
14+
15+
// convert pixel coord to normalized device coord
16+
vec2 zeroToOne = translatedPosition / u_resolution;
17+
vec2 zeroToTwo = zeroToOne * 2.0;
18+
vec2 clipSpace = zeroToTwo - 1.0;
19+
20+
gl_Position = vec4(clipSpace * vec2(1, -1), 0, 1); // flip y-axis
21+
v_texCoord = a_texCoord; // pass tex coords
22+
}
23+
`;
24+
export const map_frag_shader =
25+
`#version 300 es
26+
precision mediump float;
27+
28+
in vec2 v_texCoord;
29+
uniform sampler2D u_texture;
30+
out vec4 outColor;
31+
32+
void main() {
33+
outColor = texture(u_texture, v_texCoord);
34+
}
35+
`;

0 commit comments

Comments
 (0)