【问题标题】:I want to implement the horizontal image slider with three.js我想用three.js实现水平图像滑块
【发布时间】:2020-08-30 16:43:45
【问题描述】:

我想在three.js中实现一个水平图像滑块。

这是垂直滑块的示例。 我想实现下图。 (水平滑块)。 This is the example of a horizontal slider.

vertexShader() {

    return `
        varying vec2 vUv;
        varying vec3 vPosition;

        void main() {
            vUv = uv;
            vPosition = position;
            gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
        }   
    `

}

fragmentShader() {

    return `
        varying vec2 vUv;
        varying vec3 vPosition;

        uniform sampler2D tex0;
        uniform sampler2D tex1;
        uniform float divider;
        uniform float zoomFactor;
        uniform bool hidden;

        void main() {
            float dividerWidth;
            if (hidden) {
                dividerWidth = 0.0;
            } else {
                dividerWidth = 0.03 / zoomFactor;
            }

            if (vPosition.x > divider + dividerWidth) {
                gl_FragColor = texture2D(tex1, vUv);
            } else if (vPosition.x < divider - dividerWidth) {
                gl_FragColor = texture2D(tex0, vUv);
            } else {
                gl_FragColor = vec4(0.5, 0.5, 1.0, 1.0);
            }

        }
    `
}

【问题讨论】:

    标签: image three.js slider glsl shader


    【解决方案1】:

    您必须在纹理坐标的y 组件上输入文字,而不是在顶点坐标的x 组件上输入文字。纹理坐标的分量在 [0.0, 1.0] 范围内。因此divider 的值也必须在 [0.0, 1.0] 范围内:

    vec4 texColor0 = texture2D(tex0, vUv);
    vec4 texColor1 = texture2D(tex1, vUv);
    vec4 sliderColor = vec4(0.5, 0.5, 1.0, 1.0);
    
    float limit0 = divider - dividerWidth;
    float limit1 = divider + dividerWidth;
    
    gl_FragColor = vUv.y > limit1 ? texColor1 : (vUv.y < limit0 ? texColor0 : sliderColor);
    

    【讨论】:

    • 嗨,Rabbid76。谢谢你的好意回答。你能解释一下如何在两张图片中间显示水平滑块吗?
    • @happybear0320 divider = 0.5时滑块在图片中间
    • 哇,非常感谢。我期待您继续提供帮助。
    猜你喜欢
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多