gpt4 book ai didi

javascript - 如何在react-three-fiber中创建水

转载 作者:行者123 更新时间:2023-12-05 03:41:47 26 4
gpt4 key购买 nike

我想像本例中那样用水创建 3D 场景 three.js shader oceanwater但我必须在 react-three-fiber 库中创建它。我已经在互联网上搜索了一些适合这种情况的有效示例,但没有结果。

我可以寻求帮助来弄清楚如何将上述示例实现为 react 三纤维方法吗?

到目前为止,这是我提到的水组件:

import React from "react";
import waterNormal from "./waternormals.jpg";
//import { useLoader } from "react-three-fiber";
import * as THREE from "three";

import { Water } from "three/examples/jsm/objects/Water.js";

const WaterObject = () => {
//const mapNormalWater = useLoader(TextureLoader, waterNormal);

return (
<mesh>
<planeBufferGeometry attach="geometry" args={[100, 100]} />
<Water
options={{
textureWidth: 512,
textureHeight: 512,
waterNormals: new THREE.TextureLoader().load(
waterNormal,
function (texture) {
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
}
),
sunDirection: new THREE.Vector3(),
sunColor: 0xffffff,
waterColor: 0x001e0f,
distortionScale: 3.7,
// fog: scene.fog !== undefined
}}
></Water>
</mesh>
);
};

export default WaterObject ;

最佳答案

感谢 hpalu 解决了。

下面是可以在 Canvas 组件中导入和使用的组件的代码。

import React, { useRef, useMemo } from "react";
import { extend, useThree, useLoader, useFrame } from "@react-three/fiber";
import * as THREE from "three";

import { Water } from "three/examples/jsm/objects/Water.js";

extend({ Water });

function Ocean() {
const ref = useRef();
const gl = useThree((state) => state.gl);
const waterNormals = useLoader(
THREE.TextureLoader, "https://raw.githubusercontent.com/mrdoob/three.js/master/examples/textures/waternormals.jpg"
);


waterNormals.wrapS = waterNormals.wrapT = THREE.RepeatWrapping;
const geom = useMemo(() => new THREE.PlaneGeometry(30000, 30000), []);
const config = useMemo(
() => ({
textureWidth: 512,
textureHeight: 512,
waterNormals,
sunDirection: new THREE.Vector3(),
sunColor: 0xeb8934,
waterColor: 0x0064b5,
distortionScale: 40,
fog: false,
format: gl.encoding,
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
[waterNormals]
);
useFrame(
(state, delta) => (ref.current.material.uniforms.time.value += delta)
);
return (
<water
ref={ref}
args={[geom, config]}
rotation-x={-Math.PI / 2}
position={[0, 0, 0]}
/>
);
}

export default Ocean;

关于javascript - 如何在react-three-fiber中创建水,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67611934/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com