【问题标题】:Disable effect via props react-three-fiber通过 props 禁用效果 react-three-fiber
【发布时间】:2021-02-22 22:49:21
【问题描述】:

我有一个使用react-three-fiber 创建的场景,它有一个EffectsComposer 和一个Glitch 效果使用react-postprocessing npm 包。

我想使用 html 输入复选框切换 Glitch 效果的 active 状态,但是切换 active 道具的状态不会禁用效果。禁用复选框输入效果的正确方法是什么?

我在这里设置了一个沙盒来解决这个问题 https://codesandbox.io/s/tender-star-lw07h?file=/src/App.tsx:0-1488

这是代码。

import React, {
  Suspense,
  useRef,
  useEffect,
  useState,
  createRef,
  Fragment
} from "react";
import Outline from "./components/Outline";
import { EffectComposer, Glitch, Noise } from "react-postprocessing";
import { Canvas } from "react-three-fiber";
import { OrbitControls } from "drei";
import { Mesh } from "three";
import { GlitchMode } from "postprocessing";

const Box = (props: any) => (
  <mesh {...props}>
    <meshBasicMaterial attach="material" color="red" />
    <boxGeometry args={[1, 1, 1]} attach="geometry" />
  </mesh>
);

const Composer = ({ active }: any) => {
  const ref = React.useRef();

  return (
    <EffectComposer>
      <Glitch ref={ref} active={active} mode={GlitchMode.CONSTANT_WILD} />
    </EffectComposer>
  );
};

const App = () => {
  const ref = useRef();

  const [selection, select] = useState<Mesh>();
  const [active, activate] = useState<boolean>(true);

  useEffect(() => {
    if (selection) {
      console.log(ref.current);
    }
  }, [selection]);

  return (
    <Fragment>
      <div style={{ background: "#fff" }}>
        <label htmlFor="">Active {JSON.stringify(active)}</label>
        <input
          type="checkbox"
          defaultChecked={active}
          onChange={($event) => {
            activate($event.target.checked);
          }}
        />
      </div>
      <Canvas>
        <Box onClick={(mesh) => select(mesh)} />
        <Composer active={active} />
      </Canvas>
    </Fragment>
  );
};

【问题讨论】:

    标签: reactjs three.js post-processing react-three-fiber


    【解决方案1】:

    做事

    const App = () => {
        ...
        <Canvas>
          <Box .../>
          {active && <Composer/>}
        </Canvas>
    

    const Composer = () => {
      return (
        <EffectComposer>
          <Glitch active={true} mode={GlitchMode.CONSTANT_WILD} />
        </EffectComposer>
      );
    };
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2021-07-13
    • 2021-08-20
    • 2020-10-31
    • 1970-01-01
    • 1970-01-01
    • 2019-10-27
    • 2022-01-25
    • 1970-01-01
    • 2021-05-13
    相关资源
    最近更新 更多