Commit 21a4c3f8 authored by Laureline Nevin's avatar Laureline Nevin ⌨️
Browse files

Better export window preview

parent 6230d39e
Loading
Loading
Loading
Loading
+4 −4
Changes for index.html: 4 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -11,10 +11,10 @@
    </script>
</head>
<body v-scope>
<div class="body-content" v-cloak>
<div class="body-content">
    <h1 class="title" title="Image Viewer with Online Interactive Relighting">IVOIRe</h1>

    <header class="navbar">
    <header class="navbar" v-cloak>
        <div>
            <label for="select-dataset" v-t="controls.selected"></label>
            <span v-t="colon"></span>
@@ -45,7 +45,7 @@
        </div>
    </header>

    <div class="controls">
    <div class="controls" v-cloak>
        <label v-show="phase === 'render'" for="select-rendermode"><span v-t="controls.rendermode"></span>
            <span v-t="colon"></span></label>
        <select v-show="phase === 'render'" name="rendermode" id="select-rendermode">
@@ -114,7 +114,7 @@
        </div>
    </div>

    <main id="app">
    <main id="app" v-cloak>
        <div class="view-controls">
            <button class="btn-dark btn-icon" :title="t('controls.fitview')" @click="fn.zoomFit()"
                    v-icon="zoom-fit"></button>
+9 −7
Changes for src/app.ts: 9 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -411,6 +411,7 @@ export class App {
            const exportedState = exportValues(this.state);
            let data: RenderData = {
                ...exportedState,
                renderArea: this.source.totalSize,
                lightSource: getLightSourceVec(exportedState),
                exportParams: this.rx.phase === 'export' ? this.rx.raw().exportParams : null
            };
@@ -678,20 +679,21 @@ export class App {

    private exportImage() {
        const dataset = this.currentDataset?.data;
        if (!dataset) return;
        this.source = null;
        DownloadManager.withProgressBar<Blob>(
            async (prog) =>
                new Promise((res, rej) => {
                    const worker = new Worker(exporterUrl, { type: 'module' });

        if (!dataset || !this.source) return;
        const exportedState = exportValues(this.state);
        const data: ExportWorkerData = {
            ...exportedState,
            lightSource: getLightSourceVec(exportedState),
            renderArea: this.source.totalSize,
            exportParams: null,
            dataset,
        };
        this.source = null;
        DownloadManager.withProgressBar<Blob>(
            async (prog) =>
                new Promise((res, rej) => {
                    const worker = new Worker(exporterUrl, { type: 'module' });

                    worker.onmessage = (m: MessageEvent<ExportWorkerResult>) => {
                        console.debug('Worker returned:', m.data);
                        const type = m.data.type;
+17 −4
Changes for src/gl-program.ts: 17 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ import { ExportParams } from './app.ts';

export type RenderData = Exported<State> & {
    readonly lightSource: Readonly<vec3>;
    readonly renderArea: [number, number];
    readonly exportParams: ExportParams | null;
};

@@ -143,8 +144,17 @@ abstract class GLProgramBase<Attribs extends string[], Uniforms extends string[]
        // See if it compiled successfully

        if (!this.gl.getShaderParameter(shader, GL.COMPILE_STATUS)) {
            const err = this.gl.getShaderInfoLog(shader);
            let err = this.gl.getShaderInfoLog(shader);
            this.gl.deleteShader(shader);
            const line = err?.match(/ERROR: \d+:(\d+):/);
            if (line) {
                const l = parseInt(line[1]) - 1;
                const lines = source.split('\n')
                    .map((s, i) => Math.abs(i - l) <= 3 ? (i === l ? '>>' : '| ') + s : null)
                    .filter((s) => s)
                    .join('\n');
                err += '\n' + lines;
            }
            throw new Error(
                `An error occurred compiling the ${type === GL.VERTEX_SHADER ? 'vertex' : 'fragment'} shader: ${err}`,
            );
@@ -366,7 +376,7 @@ export class ExportRectProgram extends GLProgramBase<
    public static readonly PARAMS = {
        cornerRadius: 25,
        lineSize: 4,
        color: [1., 1., 0., .75],
        color: [1., 1., 0., .5],
    } as const;
    static attributes = {
        vertexPosition: GLProgramBase.newAttribute('aVertexPosition', 2, WebGLRenderingContext.FLOAT),
@@ -374,6 +384,7 @@ export class ExportRectProgram extends GLProgramBase<
    static uniformNames = {
        projectionMatrix: 'uProjectionMatrix',
        exportRect: 'uExportRect',
        renderRect: 'uRenderRect',
        parameters: 'uParameters',
        color: 'uColor',
    };
@@ -408,9 +419,11 @@ export class ExportRectProgram extends GLProgramBase<
        gl.uniform2f(this.uniformLocations.parameters, cornerRadius, lineSize);
        gl.uniform4f(this.uniformLocations.exportRect,
            ...data.exportParams.offset, ...data.exportParams.size);
        gl.uniform4f(this.uniformLocations.renderRect,
            0, 0, ...data.renderArea);

        const area = new Rect(...data.exportParams.offset, ...data.exportParams.size)
            .grow(cornerRadius + lineSize + 1);
        const area = new Rect(0, 0, ...data.renderArea)
            .grow(cornerRadius + lineSize * 2 + 1);
        this.setAttribute("vertexPosition");
        this.gl.bufferData(GL.ARRAY_BUFFER, area.vertexPositions(), GL.DYNAMIC_DRAW);
        gl.drawArrays(GL.TRIANGLE_STRIP, 0, 4);
+5 −0
Changes for src/main.ts: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ import { DownloadManager } from './download.ts';
import en from './lang/en.yaml'; // Import translation YAMLs here to avoid transitively inclucing them in Worker code
import fr from './lang/fr.yaml';
import { createApp } from 'petite-vue';
import { catchError } from './utils.ts';

DownloadManager.setBar(document.querySelector('progress#load-progress') as HTMLProgressElement);

@@ -21,6 +22,7 @@ for (const el of document.querySelectorAll('*[data-icon]')) {
    }
}

try {
    const app = new App({ en, fr });

    const vApp = createApp(app.rx);
@@ -48,3 +50,6 @@ vApp.directive('t', ({ el, exp, modifiers, effect }) => {
        });
    });
    vApp.mount();
} catch (e) {
    catchError(true, e as Error);
}
+29 −8
Changes for src/shaders/exportRect.frag: 29 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -2,11 +2,16 @@ precision mediump float;

uniform mediump vec4 uColor;
uniform mediump vec2 uParameters;
uniform mediump vec4 uRenderRect;
uniform mediump vec4 uExportRect;

varying mediump vec2 vPosition;
varying mediump vec2 vVertexCoord;

bool inRect(vec2 pos, vec4 rect) {
    return pos.x > rect.x && pos.y > rect.y && pos.x < rect.x + rect.z && pos.y < rect.y + rect.w;
}

void main(void) {
    float cornerRadius = uParameters.x, lineWidth = uParameters.y;
    vec2 pos = vPosition; // Shorthand for readability
@@ -14,14 +19,30 @@ void main(void) {
    vec2 recS = uExportRect.zw;
    float left = recP.x - pos.x, right = pos.x - recP.x - recS.x,
    top = recP.y - pos.y, bottom = pos.y - recP.y - recS.y;
    if ((left > 0. && left < lineWidth) ||
    (right > 0. && right < lineWidth) ||
    (top > 0. && top < lineWidth) ||
    (bottom > 0. && bottom < lineWidth)) {
        gl_FragColor = uColor;
    } else if (pos.x > recP.x && pos.y > recP.y && pos.x < recP.x + recS.x && pos.y < recP.y + recS.y) {
        discard;
    } else {
    bool onBorder = bool(// Redundant constructor because of an IDE issue
    left >= 0. && left < lineWidth ||
    right >= 0. && right < lineWidth ||
    top >= 0. && top < lineWidth ||
    bottom >= 0. && bottom < lineWidth);
    float cornerDistance = min(
        min(distance(pos + vec2(.5, .5) * lineWidth, uExportRect.xy),
            distance(pos + vec2(-.5, .5) * lineWidth, uExportRect.xy + vec2(uExportRect.z, 0.))),
        min(distance(pos + vec2(.5, -.5) * lineWidth, uExportRect.xy + vec2(0., uExportRect.w)),
            distance(pos + vec2(-.5, -.5) * lineWidth, uExportRect.xy + uExportRect.zw)));
    bool onCorner = bool(cornerDistance <= cornerRadius && cornerRadius - cornerDistance <= lineWidth);
    if (inRect(pos, uExportRect)) {
        discard; // Inside export rectangle
    } else if (inRect(pos, uExportRect + cornerRadius * vec4(-1, -1, 2, 2)) && onBorder || cornerDistance <= cornerRadius) {
        vec4 col = uColor;
        if (!onBorder && cornerRadius - cornerDistance > lineWidth) {
            col.a *= .5;
        }
        gl_FragColor = col;
    } else if (inRect(pos, uRenderRect)) {
        // In render area, outside of exporrt rectangle
        gl_FragColor = vec4(0., 0., 0., mod((pos.x + pos.y) / lineWidth / 2., 2.) > 1. ? .2 : .1);
    } else {
        // Outside of render area
        discard;
    }
}
 No newline at end of file
Loading