shawn0326 / zen-3d

JavaScript 3D library.
MIT License
196 stars 24 forks source link

Curious very low performance trying to scroll image via shadermaterial #26

Closed VanderSP closed 1 year ago

VanderSP commented 1 year ago

Hello how have u been? im trying to use shadermaterial to scroll image, in threejs is totally blazing fast... and misteriously im getting very slow scrolling with zen3d even fps marking high, any light?? please thanks in advance!

export class Scroll {
    static name = 'Scroll'

    static uniforms = {
        texture1: null,
        texture2: null,
        direction: [0.0, 1.0],
        progress: 0
    }

    static vertexShader = `
    precision lowp float; 
    attribute vec3 a_Position;
    attribute vec2 a_Uv;
    varying vec2 v_Uv;
    uniform mat4 u_Projection;
    uniform mat4 u_View;
    uniform mat4 u_Model;

    void main() {
        gl_Position = u_Projection * u_View * u_Model * vec4( a_Position, 1.0 );
        v_Uv = a_Uv;
    }
    `

    static fragmentShader = `
    precision lowp float;
    varying vec2 v_Uv;
    uniform sampler2D texture1;
    uniform sampler2D texture2;
    uniform vec2 direction;
    uniform float progress;

    void main() {
        vec2 p = v_Uv + progress * sign(direction);
        vec2 f = fract(p);

        vec4 t1 = texture2D(texture1, f);
        vec4 t2 = texture2D(texture2, f);

        gl_FragColor = mix(t2, t1, step(0.0, p.y) * step(p.y, 1.0) * step(0.0, p.x) * step(p.x, 1.0));
    }
    `
}
shawn0326 commented 1 year ago

I'm not sure if it's the shader's problem, I suggest changing this shader to a simple solid shader to see if the performance improves.

void main() {
        gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
VanderSP commented 1 year ago

Okay, i made some tests... is really strange that looks the MIX line are causing a huge drop... but in threejs it doesn´t affect at all, any clue?? what can make MIX command being very slow? i suspect that mix command being heavier brings the performance issue upfront, but maybe in threejs without mix head to head, would be even faster... i feel confused, because i like the fastness of zen3d for skywater for example, is very faster than three to update uniforms (unless using they hacky pmrem that doesn´t reflect things lol)

VanderSP commented 1 year ago

ah there´s a mess in other part of my code... sorry the disturb...