【问题标题】:@react-three/fiber: How do I create a helper for a shadow camera?@react-three/fiber:如何为阴影相机创建助手?
【发布时间】:2021-11-04 20:19:45
【问题描述】:

所以我习惯于这样设置助手:

useHelper(sLightRef, THREE.SpotLightHelper);

return <spotLight
        castShadow
        ref={sLightRef}
        position={[0, 2, 2]}
        args={["#fff", 0.4, 10, Math.PI * 0.3]}
        ></spotLight>

这将在 SpotLight 对象上创建一个助手。

我不明白的是如何在 SpotLight 的阴影相机上创建一个。 (spotlight.shadow.camera) 我们不能为它分配一个 ref(),因为它不在我的 return 语句中。它是在 SpotLight 组件内部创建的。

在普通的 Three.js 中,这很简单:

const helper = new THREE.CameraHelper(spotlight.shadow.camera);
scene.add(helper);

我将如何在 react-three-fiber 中执行此操作?我有什么明显的遗漏吗?谢谢。

【问题讨论】:

    标签: three.js react-three-fiber


    【解决方案1】:

    我玩了一下,但找不到干净的解决方案。这就是为什么我自己制作了类似于 useHelper 钩子的钩子。

    // Components==============
    import { useFrame, useThree } from '@react-three/fiber';
    import React, { useRef } from 'react';
    import { CameraHelper, Light } from 'three';
    // =========================
    
    export default function useShadowHelper(
      ref: React.MutableRefObject<Light | undefined>
    ) {
      const helper = useRef<CameraHelper>();
      const scene = useThree((state) => state.scene);
    
      React.useEffect(() => {
        if (!ref.current) return;
    
        helper.current = new CameraHelper(ref.current?.shadow.camera);
        if (helper.current) {
          scene.add(helper.current);
        }
    
        return () => {
          if (helper.current) {
            scene.remove(helper.current);
          }
        };
      }, [helper.current?.uuid, ref.current]);
    
      useFrame(() => {
        if (helper.current?.update) {
          helper.current.update();
        }
      });
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-23
      • 2021-07-12
      • 1970-01-01
      • 1970-01-01
      • 2020-10-31
      • 2022-10-20
      • 2021-08-20
      • 2021-09-04
      • 1970-01-01
      相关资源
      最近更新 更多