|
|
|
@ -3,6 +3,8 @@ const sdf_vs_src = `#version 300 es
@@ -3,6 +3,8 @@ const sdf_vs_src = `#version 300 es
|
|
|
|
|
in vec4 a_line; |
|
|
|
|
in vec3 a_color; |
|
|
|
|
|
|
|
|
|
in uint a_stroke_id; |
|
|
|
|
|
|
|
|
|
uniform vec2 u_scale; |
|
|
|
|
uniform vec2 u_res; |
|
|
|
|
uniform vec2 u_translation; |
|
|
|
@ -11,6 +13,7 @@ const sdf_vs_src = `#version 300 es
@@ -11,6 +13,7 @@ const sdf_vs_src = `#version 300 es
|
|
|
|
|
out vec2 v_texcoord; |
|
|
|
|
out vec3 v_color; |
|
|
|
|
|
|
|
|
|
flat out uint v_stroke_id; |
|
|
|
|
flat out float v_thickness; |
|
|
|
|
|
|
|
|
|
void main() { |
|
|
|
@ -48,6 +51,7 @@ const sdf_vs_src = `#version 300 es
@@ -48,6 +51,7 @@ const sdf_vs_src = `#version 300 es
|
|
|
|
|
v_line = a_line; |
|
|
|
|
v_color = a_color; |
|
|
|
|
v_thickness = a_pos.z; |
|
|
|
|
v_stroke_id = a_stroke_id; |
|
|
|
|
|
|
|
|
|
gl_Position = vec4(screen02 - 1.0, 0.0, 1); |
|
|
|
|
} |
|
|
|
@ -88,6 +92,28 @@ const sdf_fs_src = `#version 300 es
@@ -88,6 +92,28 @@ const sdf_fs_src = `#version 300 es
|
|
|
|
|
} |
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const tiles_fs_src = `#version 300 es
|
|
|
|
|
precision highp float; |
|
|
|
|
|
|
|
|
|
uniform int u_debug_mode; |
|
|
|
|
|
|
|
|
|
in vec4 v_line; |
|
|
|
|
in vec2 v_texcoord; |
|
|
|
|
in vec3 v_color; |
|
|
|
|
|
|
|
|
|
flat in uint v_stroke_id; |
|
|
|
|
flat in float v_thickness; |
|
|
|
|
|
|
|
|
|
//out uint TileId;
|
|
|
|
|
out vec4 FragColor; |
|
|
|
|
|
|
|
|
|
void main() { |
|
|
|
|
//TileId = uint(1);
|
|
|
|
|
vec3 color = vec3(float(v_stroke_id * 3245u % 255u) / 255.0, float(v_stroke_id * 7343u % 255u) / 255.0, float(v_stroke_id * 5528u % 255u) / 255.0); |
|
|
|
|
FragColor = vec4(color, 1); |
|
|
|
|
} |
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const tquad_vs_src = `#version 300 es
|
|
|
|
|
in vec2 a_pos; |
|
|
|
|
in vec2 a_texcoord; |
|
|
|
@ -148,8 +174,13 @@ function init_webgl(state, context) {
@@ -148,8 +174,13 @@ function init_webgl(state, context) {
|
|
|
|
|
const sdf_vs = create_shader(gl, gl.VERTEX_SHADER, sdf_vs_src); |
|
|
|
|
const sdf_fs = create_shader(gl, gl.FRAGMENT_SHADER, sdf_fs_src); |
|
|
|
|
|
|
|
|
|
const tiles_fs = create_shader(gl, gl.FRAGMENT_SHADER, tiles_fs_src); |
|
|
|
|
|
|
|
|
|
context.programs['image'] = create_program(gl, quad_vs, quad_fs); |
|
|
|
|
context.programs['sdf'] = create_program(gl, sdf_vs, sdf_fs); |
|
|
|
|
context.programs['sdf'] = { |
|
|
|
|
'main': create_program(gl, sdf_vs, sdf_fs), |
|
|
|
|
'tiles': create_program(gl, sdf_vs, tiles_fs), // same vertex shader
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
context.locations['image'] = { |
|
|
|
|
'a_pos': gl.getAttribLocation(context.programs['image'], 'a_pos'), |
|
|
|
@ -163,16 +194,33 @@ function init_webgl(state, context) {
@@ -163,16 +194,33 @@ function init_webgl(state, context) {
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
context.locations['sdf'] = { |
|
|
|
|
'a_pos': gl.getAttribLocation(context.programs['sdf'], 'a_pos'), |
|
|
|
|
'a_line': gl.getAttribLocation(context.programs['sdf'], 'a_line'), |
|
|
|
|
'a_color': gl.getAttribLocation(context.programs['sdf'], 'a_color'), |
|
|
|
|
|
|
|
|
|
'u_res': gl.getUniformLocation(context.programs['sdf'], 'u_res'), |
|
|
|
|
'u_scale': gl.getUniformLocation(context.programs['sdf'], 'u_scale'), |
|
|
|
|
'u_translation': gl.getUniformLocation(context.programs['sdf'], 'u_translation'), |
|
|
|
|
'u_texture_points': gl.getUniformLocation(context.programs['sdf'], 'u_texture_points'), |
|
|
|
|
'u_texture_indices': gl.getUniformLocation(context.programs['sdf'], 'u_texture_indices'), |
|
|
|
|
'u_debug_mode': gl.getUniformLocation(context.programs['sdf'], 'u_debug_mode') |
|
|
|
|
'main': { |
|
|
|
|
'a_pos': gl.getAttribLocation(context.programs['sdf'].main, 'a_pos'), |
|
|
|
|
'a_line': gl.getAttribLocation(context.programs['sdf'].main, 'a_line'), |
|
|
|
|
'a_color': gl.getAttribLocation(context.programs['sdf'].main, 'a_color'), |
|
|
|
|
'a_stroke_id': gl.getAttribLocation(context.programs['sdf'].main, 'a_stroke_id'), |
|
|
|
|
|
|
|
|
|
'u_res': gl.getUniformLocation(context.programs['sdf'].main, 'u_res'), |
|
|
|
|
'u_scale': gl.getUniformLocation(context.programs['sdf'].main, 'u_scale'), |
|
|
|
|
'u_translation': gl.getUniformLocation(context.programs['sdf'].main, 'u_translation'), |
|
|
|
|
'u_texture_points': gl.getUniformLocation(context.programs['sdf'].main, 'u_texture_points'), |
|
|
|
|
'u_texture_indices': gl.getUniformLocation(context.programs['sdf'].main, 'u_texture_indices'), |
|
|
|
|
'u_debug_mode': gl.getUniformLocation(context.programs['sdf'].main, 'u_debug_mode'), |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
'tiles': { |
|
|
|
|
'a_pos': gl.getAttribLocation(context.programs['sdf'].tiles, 'a_pos'), |
|
|
|
|
'a_line': gl.getAttribLocation(context.programs['sdf'].tiles, 'a_line'), |
|
|
|
|
'a_color': gl.getAttribLocation(context.programs['sdf'].tiles, 'a_color'), |
|
|
|
|
'a_stroke_id': gl.getAttribLocation(context.programs['sdf'].tiles, 'a_stroke_id'), |
|
|
|
|
|
|
|
|
|
'u_res': gl.getUniformLocation(context.programs['sdf'].tiles, 'u_res'), |
|
|
|
|
'u_scale': gl.getUniformLocation(context.programs['sdf'].tiles, 'u_scale'), |
|
|
|
|
'u_translation': gl.getUniformLocation(context.programs['sdf'].tiles, 'u_translation'), |
|
|
|
|
'u_texture_points': gl.getUniformLocation(context.programs['sdf'].tiles, 'u_texture_points'), |
|
|
|
|
'u_texture_indices': gl.getUniformLocation(context.programs['sdf'].tiles, 'u_texture_indices'), |
|
|
|
|
'u_debug_mode': gl.getUniformLocation(context.programs['sdf'].tiles, 'u_debug_mode'), |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
context.buffers['image'] = { |
|
|
|
@ -187,7 +235,25 @@ function init_webgl(state, context) {
@@ -187,7 +235,25 @@ function init_webgl(state, context) {
|
|
|
|
|
'b_packed_dynamic_index': gl.createBuffer(), |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
context.textures['sdf'] = {}; |
|
|
|
|
context.textures['sdf'] = { |
|
|
|
|
'tiles': gl.createTexture(), |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
context.framebuffers['sdf'] = { |
|
|
|
|
'tiles': gl.createFramebuffer(), |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
gl.bindTexture(gl.TEXTURE_2D, context.textures['sdf'].tiles); |
|
|
|
|
//gl.texImage2D(gl.TEXTURE_2D, 0, gl.R32UI, context.canvas.width, context.canvas.height, 0, gl.RED_INTEGER, gl.UNSIGNED_INT, null);
|
|
|
|
|
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, Math.ceil(context.canvas.width / config.tile_size), Math.ceil(context.canvas.height / config.tile_size), 0, gl.RGBA, gl.UNSIGNED_BYTE, null); |
|
|
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); |
|
|
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); |
|
|
|
|
|
|
|
|
|
gl.bindFramebuffer(gl.FRAMEBUFFER, context.framebuffers['sdf'].tiles); |
|
|
|
|
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, context.textures['sdf'].tiles, 0); |
|
|
|
|
|
|
|
|
|
// gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
|
|
|
|
|
|
|
|
context.textures['image'] = {}; |
|
|
|
|
|
|
|
|
|
const resize_canvas = (entries) => { |
|
|
|
@ -209,6 +275,9 @@ function init_webgl(state, context) {
@@ -209,6 +275,9 @@ function init_webgl(state, context) {
|
|
|
|
|
context.canvas.width = width; |
|
|
|
|
context.canvas.height = height; |
|
|
|
|
|
|
|
|
|
gl.bindTexture(gl.TEXTURE_2D, context.textures['sdf'].tiles); |
|
|
|
|
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, Math.ceil(context.canvas.width / config.tile_size), Math.ceil(context.canvas.height / config.tile_size), 0, gl.RGBA, gl.UNSIGNED_BYTE, null); |
|
|
|
|
|
|
|
|
|
schedule_draw(state, context); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|