首页 > 解决方案 > 三.js Shader修改TextGeometry

问题描述

我对三个完全陌生,因此这个问题对其他人来说可能很明显。

所以我实现了加载我的字体并相应地构建了一个 TextGeometry。此外,我使用 EdgesGeometry 创建了一种边框。这工作正常。

但是我如何实现几何图形/网格的行为如下:https ://edoardosmerilli.com/overview/ - 换句话说:它从左到中心弯曲,反之亦然(就像旧的电子管电视的屏幕)到滚动位置。我怎样才能达到这样的效果?它是一个顶点着色器吗?是不是三个向量产生了这种效果?

我感谢每一个提示!

export default class Title {
    constructor($el, $scene) {
        this.scene = $scene;
        this.title = $el.firstElementChild;

        this.offset = new THREE.Vector2(0, 0);

        const loader = new THREE.FontLoader();
        loader.load('/RL-Unno.json', (font) => {

            const geometry = new THREE.TextGeometry(this.title.innerHTML, {
                font: font,
                size: 120,
                height: 1
            });
            geometry.center();
            this.init(geometry);
        })
    }

    init = (geometry) => {
        const material = new THREE.MeshBasicMaterial({opacity: 0});
        this.mesh = new THREE.Mesh(geometry, material);

        this.getPosition();
        this.mesh.position.set(this.offset.x, this.offset.y, 0);
        this.scene.add(this.mesh);

        const geo = new THREE.EdgesGeometry(geometry);
        const outline = new THREE.LineBasicMaterial({
            linewidth: 4
        });
        this.border = new THREE.LineSegments(geo, outline);
        this.border.position.set(this.mesh.position.x, this.mesh.position.y, 0);
        this.scene.add(this.border);

        this.createListeners();
    };

    getPosition = () => {
        const {width, height, top, left} = this.title.getBoundingClientRect();

        this.offset.set(left - window.innerWidth / 2 + width / 2, -top + window.innerHeight / 2 - height / 2);
    };

    createListeners = () => {
        this.title.addEventListener('mouseenter', () => {
            gsap.timeline().set(this.mesh.material, {
                opacity: 1
            })
        });

        this.title.addEventListener('mouseleave', () => {
            gsap.timeline().set(this.mesh.material, {
                opacity: 0
            })
        });
    };

    update = () => {
        if (!this.mesh) return;
        this.getPosition();

        gsap.timeline().set([this.mesh.position, this.border.position], {
            x: this.offset.x,
            y: this.offset.y,
        });
    }
}

标签: javascriptthree.jsfragment-shadervertex-shader

解决方案


推荐阅读