滚动时重置三纤维动画

时间:2020-08-14 16:48:33

标签: three.js react-three-fiber

我使用three.js和react-three-fiber-纤维创建了一个低聚水动画。加载我的网页后,动画开始播放,但是当您开始向下滚动以查看网页上的其他内容时,我的动画将重置并重新开始。

PolyWater 只是我创建的用于使用顶点制作低聚水的组件。

将SeaScene导出到Home组件,该组件将其余组件合并在一起。

正在使用Router作为响应在App.js文件中渲染我的Home组件

SeaScene.js

import React, {useRef} from 'react'
import {Canvas, extend, useFrame, useThree} from "react-three-fiber"
import {OrbitControls} from "three/examples/jsm/controls/OrbitControls"
import PolyWater from "./PolyWater/PolyWater";
import './SeaScene.css'

extend({OrbitControls})

const Controls = () => {
    const orbitRef = useRef();
    const {camera, gl} = useThree();

    useFrame(() => {
        orbitRef.current.update()
        camera.position.set(25, 12.5, -20)
        camera.rotation.set(-1.5, 0, 0)
    })

    return (
        <orbitControls
            args={[camera, gl.domElement]}
            ref={orbitRef}
        />
    )
}

const SeaScene = () => {
    return (
        <section id="home" className="home-section">
            <Canvas>
                <ambientLight intensity={0.2}/>
                <directionalLight color={0xffffff} position={[0, 50, 30]}/>
                <Controls/>
                <PolyWater/>
            </Canvas>
        </section>
    )
}

Home.js

class Home extends Component {
    render() {
        return (
            <div>
                <SeaScene/>
                <About/>
                <Work/>
                <Footer/>
            </div>
        );
    }
}

App.js

class App extends Component {
    render() {
        return (
            <Router>
                <div>
                    <section>
                        <NavBar/>
                        <Switch>
                            <Route exact path='/' component={Home}/>
                        </Switch>
                    </section>
                </div>
            </Router>

        );
    };
}

2 个答案:

答案 0 :(得分:1)

虽然 @astronik 的答案对整体性能要好得多。我只用了很长时间就更换了所有几何形状和材料。

所以经过一些挖掘,我找到了一个快速修复的方法,感谢 this comment by drcmda。显然,在渲染的情况下,画布总是在监听滚动交互。您只需在 scroll: false 元素上设置 Canvas 即可禁用此行为。

   <Canvas resize={{ scroll: false }} >
<块引用>

⚠️ 使用此快速修复的唯一缺点是您不能再在画布中使用悬停/单击/滚动元素。

答案 1 :(得分:0)

我自己找到了解决方法。它是在react-three-fibers上,github只是不知道那是解决办法。我将材料和几何形状从您声明它们的常规方式更改为下面的方式。

export class refactor1597449093253 implements MigrationInterface {
    name = 'refactor1597449093253'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TABLE "departaments" DROP CONSTRAINT "FK_65bfca70b0ffbf5351061f2ea64"`);
        await queryRunner.query(`ALTER TABLE "departaments" DROP CONSTRAINT "UQ_65bfca70b0ffbf5351061f2ea64"`);
        await queryRunner.query(`ALTER TABLE "departaments" DROP COLUMN "manager_id"`);
        await queryRunner.query(`ALTER TABLE "departaments" ADD "manager_id" character varying`);
        await queryRunner.query(`ALTER TABLE "departaments" ADD CONSTRAINT "UQ_65bfca70b0ffbf5351061f2ea64" UNIQUE ("manager_id")`);
        await queryRunner.query(`ALTER TABLE "departaments" ADD CONSTRAINT "FK_65bfca70b0ffbf5351061f2ea64" FOREIGN KEY ("manager_id") REFERENCES "employees"("matricula") ON DELETE NO ACTION ON UPDATE NO ACTION`);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TABLE "departaments" DROP CONSTRAINT "FK_65bfca70b0ffbf5351061f2ea64"`);
        await queryRunner.query(`ALTER TABLE "departaments" DROP CONSTRAINT "UQ_65bfca70b0ffbf5351061f2ea64"`);
        await queryRunner.query(`ALTER TABLE "departaments" DROP COLUMN "manager_id"`);
        await queryRunner.query(`ALTER TABLE "departaments" ADD "manager_id" uuid`);
        await queryRunner.query(`ALTER TABLE "departaments" ADD CONSTRAINT "UQ_65bfca70b0ffbf5351061f2ea64" UNIQUE ("manager_id")`);
        await queryRunner.query(`ALTER TABLE "departaments" ADD CONSTRAINT "FK_65bfca70b0ffbf5351061f2ea64" FOREIGN KEY ("manager_id") REFERENCES "employees"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
    }

}

页面链接 https://github.com/react-spring/react-three-fiber/blob/master/pitfalls.md