simplify a bit
This commit is contained in:
parent
1b14970313
commit
9e68b2d9d3
|
|
@ -13,6 +13,7 @@
|
||||||
min-height: 100%;
|
min-height: 100%;
|
||||||
position: relative;
|
position: relative;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
|
overscroll-behavior: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
fc-geometry {
|
fc-geometry {
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,8 @@ export class DistanceField extends HTMLElement {
|
||||||
|
|
||||||
// Start the JFA process
|
// Start the JFA process
|
||||||
this.runJFA();
|
this.runJFA();
|
||||||
|
|
||||||
|
window.addEventListener('resize', this.handleResize);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lifecycle hooks
|
// Lifecycle hooks
|
||||||
|
|
@ -62,11 +64,12 @@ export class DistanceField extends HTMLElement {
|
||||||
geometry.removeEventListener('move', this.handleGeometryUpdate);
|
geometry.removeEventListener('move', this.handleGeometryUpdate);
|
||||||
geometry.removeEventListener('resize', this.handleGeometryUpdate);
|
geometry.removeEventListener('resize', this.handleGeometryUpdate);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
window.removeEventListener('resize', this.handleResize);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle updates from geometries
|
// Handle updates from geometries
|
||||||
private handleGeometryUpdate = () => {
|
private handleGeometryUpdate = () => {
|
||||||
console.log('handleGeometryUpdate');
|
|
||||||
// Re-render seed points and rerun JFA
|
// Re-render seed points and rerun JFA
|
||||||
this.initSeedPointRendering();
|
this.initSeedPointRendering();
|
||||||
this.runJFA();
|
this.runJFA();
|
||||||
|
|
@ -98,8 +101,6 @@ export class DistanceField extends HTMLElement {
|
||||||
}
|
}
|
||||||
|
|
||||||
private initShaders() {
|
private initShaders() {
|
||||||
const gl = this.gl;
|
|
||||||
|
|
||||||
// Shader sources
|
// Shader sources
|
||||||
const vertexShaderSource = vert`#version 300 es
|
const vertexShaderSource = vert`#version 300 es
|
||||||
precision highp float;
|
precision highp float;
|
||||||
|
|
@ -198,15 +199,18 @@ export class DistanceField extends HTMLElement {
|
||||||
|
|
||||||
this.offsets = new Float32Array(offsets);
|
this.offsets = new Float32Array(offsets);
|
||||||
|
|
||||||
// Compile JFA shaders
|
// Compile JFA shaders using the utility function
|
||||||
const vertexShader = this.createShader(gl.VERTEX_SHADER, vertexShaderSource);
|
this.program = this.compileShaderProgram(vertexShaderSource, fragmentShaderSource);
|
||||||
const fragmentShader = this.createShader(gl.FRAGMENT_SHADER, fragmentShaderSource);
|
|
||||||
this.program = this.createProgram(vertexShader, fragmentShader);
|
|
||||||
|
|
||||||
// Compile display shaders
|
// Compile display shaders using the utility function
|
||||||
const displayVertexShader = this.createShader(gl.VERTEX_SHADER, displayVertexShaderSource);
|
this.displayProgram = this.compileShaderProgram(displayVertexShaderSource, displayFragmentShaderSource);
|
||||||
const displayFragmentShader = this.createShader(gl.FRAGMENT_SHADER, displayFragmentShaderSource);
|
}
|
||||||
this.displayProgram = this.createProgram(displayVertexShader, displayFragmentShader);
|
|
||||||
|
private compileShaderProgram(vertexSource: string, fragmentSource: string): WebGLProgram {
|
||||||
|
const gl = this.gl;
|
||||||
|
const vertexShader = this.createShader(gl.VERTEX_SHADER, vertexSource);
|
||||||
|
const fragmentShader = this.createShader(gl.FRAGMENT_SHADER, fragmentSource);
|
||||||
|
return this.createProgram(vertexShader, fragmentShader);
|
||||||
}
|
}
|
||||||
|
|
||||||
private createShader(type: GLenum, source: string): WebGLShader {
|
private createShader(type: GLenum, source: string): WebGLShader {
|
||||||
|
|
@ -306,7 +310,7 @@ export class DistanceField extends HTMLElement {
|
||||||
out vec4 outColor;
|
out vec4 outColor;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vec2 seedCoord = gl_FragCoord.xy / u_resolution;
|
vec2 seedCoord = gl_FragCoord.xy / vec2(${this.canvas.width.toFixed(1)}, ${this.canvas.height.toFixed(1)});
|
||||||
outColor = vec4(seedCoord, v_shapeID, 0.0); // Seed coords, shape ID, initial distance 0
|
outColor = vec4(seedCoord, v_shapeID, 0.0); // Seed coords, shape ID, initial distance 0
|
||||||
}`;
|
}`;
|
||||||
|
|
||||||
|
|
@ -423,29 +427,13 @@ export class DistanceField extends HTMLElement {
|
||||||
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer);
|
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer);
|
||||||
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, outputTexture, 0);
|
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, outputTexture, 0);
|
||||||
|
|
||||||
// Check framebuffer status
|
|
||||||
const status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
|
|
||||||
if (status !== gl.FRAMEBUFFER_COMPLETE) {
|
|
||||||
console.error('Framebuffer is incomplete:', status.toString(16));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
gl.viewport(0, 0, this.canvas.width, this.canvas.height);
|
|
||||||
|
|
||||||
// Use shader program
|
// Use shader program
|
||||||
gl.useProgram(this.program);
|
gl.useProgram(this.program);
|
||||||
|
|
||||||
// Adjust offsets based on step size and resolution
|
// Compute and set the offsets uniform
|
||||||
const adjustedOffsets = [];
|
const offsets = this.computeOffsets(stepSize);
|
||||||
for (let i = 0; i < this.offsets.length; i += 2) {
|
|
||||||
const offsetX = (this.offsets[i] * stepSize) / this.canvas.width;
|
|
||||||
const offsetY = (this.offsets[i + 1] * stepSize) / this.canvas.height;
|
|
||||||
adjustedOffsets.push(offsetX, offsetY);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the offsets uniform
|
|
||||||
const offsetsLocation = gl.getUniformLocation(this.program, 'u_offsets');
|
const offsetsLocation = gl.getUniformLocation(this.program, 'u_offsets');
|
||||||
gl.uniform2fv(offsetsLocation, new Float32Array(adjustedOffsets));
|
gl.uniform2fv(offsetsLocation, offsets);
|
||||||
|
|
||||||
// Bind input texture
|
// Bind input texture
|
||||||
gl.activeTexture(gl.TEXTURE0);
|
gl.activeTexture(gl.TEXTURE0);
|
||||||
|
|
@ -517,4 +505,26 @@ export class DistanceField extends HTMLElement {
|
||||||
|
|
||||||
gl.bindVertexArray(null);
|
gl.bindVertexArray(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle window resize
|
||||||
|
private handleResize = () => {
|
||||||
|
// Update canvas size
|
||||||
|
this.canvas.width = window.innerWidth;
|
||||||
|
this.canvas.height = window.innerHeight;
|
||||||
|
|
||||||
|
// Re-initialize WebGL resources
|
||||||
|
this.initPingPongTextures();
|
||||||
|
this.renderSeedPoints();
|
||||||
|
this.runJFA();
|
||||||
|
};
|
||||||
|
|
||||||
|
private computeOffsets(stepSize: number): Float32Array {
|
||||||
|
const offsets = [];
|
||||||
|
for (let y = -1; y <= 1; y++) {
|
||||||
|
for (let x = -1; x <= 1; x++) {
|
||||||
|
offsets.push((x * stepSize) / this.canvas.width, (y * stepSize) / this.canvas.height);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new Float32Array(offsets);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,29 @@
|
||||||
export function glsl(strings: TemplateStringsArray) {
|
export function glsl(strings: TemplateStringsArray, ...values: any[]) {
|
||||||
return strings[0];
|
return strings.reduce((result, str, i) => {
|
||||||
|
return result + str + (values[i] || '');
|
||||||
|
}, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
export function vert(strings: TemplateStringsArray) {
|
export function vert(strings: TemplateStringsArray, ...values: any[]) {
|
||||||
return strings[0];
|
return strings.reduce((result, str, i) => {
|
||||||
|
return result + str + (values[i] || '');
|
||||||
|
}, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
export function frag(strings: TemplateStringsArray) {
|
export function frag(strings: TemplateStringsArray, ...values: any[]) {
|
||||||
return strings[0];
|
return strings.reduce((result, str, i) => {
|
||||||
|
return result + str + (values[i] || '');
|
||||||
|
}, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
export function css(strings: TemplateStringsArray) {
|
export function css(strings: TemplateStringsArray, ...values: any[]) {
|
||||||
return strings[0];
|
return strings.reduce((result, str, i) => {
|
||||||
|
return result + str + (values[i] || '');
|
||||||
|
}, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
export function html(strings: TemplateStringsArray) {
|
export function html(strings: TemplateStringsArray, ...values: any[]) {
|
||||||
return strings[0];
|
return strings.reduce((result, str, i) => {
|
||||||
|
return result + str + (values[i] || '');
|
||||||
|
}, '');
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue