0

本质上,我想在以下示例中使用相机控件旋转平面中的框。每次我尝试添加相机控件时,我都会旋转飞机。最终我想scrollevents用控件移动飞机并旋转飞机上的场景。

function Box(props) {
    return (
        <mesh {...props}>
            <boxBufferGeometry args={[60, 60, 60]} />
            <meshNormalMaterial />
        </mesh>
    );
}

function Plane() {
    const cam = useRef();
    const [scene, target] = React.useMemo(() => {
        const scene = new THREE.Scene();
        scene.background = new THREE.Color('orange');
        const target = new THREE.WebGLMultisampleRenderTarget(
            window.innerWidth,
            window.innerHeight,
            {
                minFilter: THREE.LinearFilter,
                magFilter: THREE.LinearFilter,
                format: THREE.RGBFormat,
                stencilBuffer: false,
            },
        );
        target.samples = 8;
        return [scene, target];
    }, []);

    useFrame((state) => {
        cam.current.position.z = 200;
        state.gl.setRenderTarget(target);
        state.gl.render(scene, cam.current);
        state.gl.setRenderTarget(null);
    });

    return (
        <>
            <PerspectiveCamera ref={cam} />
            {createPortal(<Box />, scene)}
            <mesh>
                <planeBufferGeometry attach="geometry" args={[160, 90]} />
                <meshStandardMaterial attach="material" map={target.texture} />
            </mesh>
        </>
    );
}

function App() {
    return (
        <Canvas
            style={{ background: '#324466' }}
            orthographic={true}
            camera={{ position: [0, 0, 200] }}
        >
            <ambientLight intensity={0.5} />
            <Plane />
        </Canvas>
    );
}
4

1 回答 1

0

我能够弄清楚这一点。我不得不删除相机组件并手动创建相机。然后我可以正常地将轨道控制连接到相机,一切正常。

function Box(props) {
    return (
        <mesh {...props}>
            <boxBufferGeometry args={[60, 60, 60]} />
            <meshNormalMaterial />
        </mesh>
    );
}

function Plane() {
    const {
        gl: { domElement },
    } = useThree();

    const camera = new THREE.PerspectiveCamera(
        45,
        window.innerWidth / window.innerHeight,
        1,
       10000,
    );

    const controls = useRef<OrbitControls>();

    const [scene, target] = React.useMemo(() => {
        const scene = new THREE.Scene();
        scene.background = new THREE.Color('orange');
        const target = new THREE.WebGLMultisampleRenderTarget(
            window.innerWidth,
            window.innerHeight,
            {
                minFilter: THREE.LinearFilter,
                magFilter: THREE.LinearFilter,
                format: THREE.RGBFormat,
                stencilBuffer: false,
            },
        );
        target.samples = 8;
        return [scene, target];
    }, []);

    camera.position.set(0, 50, 100);

    useFrame((state) => {
        controls.current.update();
        state.gl.setRenderTarget(target);
        state.gl.render(scene, camera);
        state.gl.setRenderTarget(null);
    });

    return (
        <>
            <orbitControls
                ref={controls}
                args={[camera, domElement]}
                enableDamping={true}
            />
            {createPortal(<Box />, scene)}
            <mesh>
                <planeBufferGeometry attach="geometry" args={[window.innerWidth / 2, window.innerHeight / 2]} />
                <meshStandardMaterial attach="material" map={target.texture} />
            </mesh>
        </>
    );
}

function App() {
    return (
        <Canvas
            style={{ background: '#324466' }}
            orthographic={true}
            camera={{ position: [0, 0, 200] }}
        >
            <ambientLight intensity={0.5} />
            <Plane />
        </Canvas>
    );
}
于 2021-01-20T16:28:33.343 回答